import type { ServiceMethodFactory } from "@ethicdevs/react-monolith";
import { Organization, Repository, ResourceVisibility } from "@prisma/client";
import type { RepositoryServiceDeps } from "./types";
import { default as makeGetRepositoryHTTPCloneUrl } from "./getRepositoryHTTPCloneUrl";
import { default as makeGetRepositorySSHCloneUrl } from "./getRepositorySSHCloneUrl";
const makeGetRepositoryExploreCollection: ServiceMethodFactory<
RepositoryServiceDeps,
void[],
Promise<(Repository & { parentOrg: Organization })[]>
> = (deps) => {
const { request } = deps;
const getRepositoryHTTPCloneUrl = makeGetRepositoryHTTPCloneUrl(deps);
const getRepositorySSHCloneUrl = makeGetRepositorySSHCloneUrl(deps);
return async () => {
const repositories = await request.prisma.repository.findMany({
include: {
organization: true,
},
where: {
visibility: ResourceVisibility.PUBLIC,
},
orderBy: {
lastPushedAt: {
sort: "desc",
nulls: "last",
},
},
});
const repositoriesWithMetas = await Promise.all(
repositories.map(async ({ organization: parentOrg, ...repo }) => ({
...repo,
parentOrg,
httpCloneUrl: await getRepositoryHTTPCloneUrl(repo),
sshCloneUrl: await getRepositorySSHCloneUrl(repo),
}))
);
return repositoriesWithMetas;
};
};
export default makeGetRepositoryExploreCollection;