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

const makeGetSshCloneUrl: ServiceMethodFactory<
  RepositoryServiceDeps,
  [Repository],
  Promise<string>
> = ({ request }) => {
  return async (repo) => {
    const parentOrg = await request.prisma.organization.findUnique({
      where: {
        id: repo.organizationId,
      },
    });

    if (parentOrg == null) {
      throw new Error(
        `Could not find the parent organization for project "${repo.slug}".`
      );
    }

    const baseUrl = `ssh://git@${Env.DEPLOYMENT_DOMAIN}`;
    return `${baseUrl}/${parentOrg.slug}/${repo.slug}.git`;
  };
};

export default makeGetSshCloneUrl;