.ts
TypeScript
(application/typescript)
// 1st-party
import type { ServiceMethodFactory } from "@ethicdevs/react-monolith";
// generated via script[generate:prisma]
import type { PullRequest } from "@prisma/client";
// service
import type { CreatePullRequestDTO, PullRequestServiceDeps } from "./types";

const createPullRequest: ServiceMethodFactory<
  PullRequestServiceDeps,
  [CreatePullRequestDTO],
  Promise<PullRequest>
> = ({ request }) => {
  return async ({ author, source, summary, target, textMd }) => {
    const pullRequest = await request.prisma.pullRequest.create({
      data: {
        summary,
        textMd,
        sourceBranch: source.fromBranch,
        targetBranch: target.destBranch,
        author: {
          connect: {
            id: author.id,
          },
        },
        sourceRepository: {
          connect: {
            id: source.repository.id,
          },
        },
        targetRepository: {
          connect: {
            id: target.repository.id,
          },
        },
      },
    });

    return pullRequest;
  };
};

export default createPullRequest;