feat: add organisations (#1820)

This commit is contained in:
David Nguyen
2025-06-10 11:49:52 +10:00
committed by GitHub
parent 0b37f19641
commit e6dc237ad2
631 changed files with 37616 additions and 25695 deletions

View File

@ -0,0 +1,79 @@
import { AppError, AppErrorCode } from '@documenso/lib/errors/app-error';
import { prisma } from '@documenso/prisma';
import { authenticatedProcedure } from '../trpc';
import {
ZGetOrganisationRequestSchema,
ZGetOrganisationResponseSchema,
} from './get-organisation.types';
export const getOrganisationRoute = authenticatedProcedure
// .meta(getOrganisationMeta)
.input(ZGetOrganisationRequestSchema)
.output(ZGetOrganisationResponseSchema)
.query(async ({ input, ctx }) => {
const { organisationReference } = input;
return await getOrganisation({
userId: ctx.user.id,
organisationReference,
});
});
type GetOrganisationOptions = {
userId: number;
/**
* The ID or URL of the organisation.
*/
organisationReference: string;
};
export const getOrganisation = async ({
userId,
organisationReference,
}: GetOrganisationOptions) => {
const organisation = await prisma.organisation.findFirst({
where: {
OR: [{ id: organisationReference }, { url: organisationReference }],
members: {
some: {
userId,
},
},
},
include: {
organisationGlobalSettings: true,
subscription: true,
organisationClaim: true,
teams: {
where: {
teamGroups: {
some: {
organisationGroup: {
organisationGroupMembers: {
some: {
organisationMember: {
userId,
},
},
},
},
},
},
},
},
},
});
if (!organisation) {
throw new AppError(AppErrorCode.NOT_FOUND, {
message: 'Organisation not found',
});
}
return {
...organisation,
teams: organisation.teams,
};
};