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

const getOrganizationRepositories: ServiceMethodFactory<
  OrganizationServiceDeps,
  [Organization],
  Promise<(Repository & { parentOrg: Organization })[]>
> = ({ request }) => {
  return async (org) => {
    const orgRepos = await request.prisma.repository.findMany({
      include: {
        organization: true,
      },
      where: {
        organization: {
          id: org.id,
        },
      },
      orderBy: {
        lastPushedAt: "desc",
      },
    });

    return orgRepos.map(({ organization: parentOrg, ...repo }) => ({
      ...repo,
      parentOrg,
    }));
  };
};

export default getOrganizationRepositories;