GitFOSS
.ts
TypeScript
(application/typescript)
// 1st-party
import type { ReqHandler } from "@ethicdevs/react-monolith";
// app
import { AppRoute, AppRoutesParams } from "../../routes";
// app services
import { makeOrganizationService } from "../../services/organization";
import { makePullRequestService } from "../../services/pullRequest";
import { makeRepositoryService } from "../../services/repository";
// app views
import RepositoryPullRequestsView, {
  RepositoryPullRequestsViewProps,
} from "../../views/repository/RepositoryPullRequestsView";

const getRepositoryPullRequestsView: ReqHandler = async (request, reply) => {
  const { orgSlug, repoSlug } =
    request.params as AppRoutesParams[AppRoute.REPOSITORY_PULL_REQUESTS]["params"];

  const orgService = makeOrganizationService({ request });
  const prService = makePullRequestService({ request });
  const repoService = makeRepositoryService({ request });

  const parentOrg = await orgService.getOrganizationBySlug(orgSlug);

  if (parentOrg == null) {
    return reply.status(404).callNotFound();
  }

  const repo = await repoService.getRepository(orgSlug, repoSlug);

  if (repo == null) {
    return reply.status(404).callNotFound();
  }

  const pullRequests = await prService.getPullRequestsInRepository(repo);

  const reqHandler = reply.makeRequestHandler(request, reply);
  return reqHandler<RepositoryPullRequestsViewProps>(
    RepositoryPullRequestsView.name,
    {
      parentOrg,
      pullRequests,
      repo,
    }
  );
};

export default getRepositoryPullRequestsView;