mirror of
https://github.com/documenso/documenso.git
synced 2025-11-14 00:32:43 +10:00
feat: add organisations (#1820)
This commit is contained in:
79
packages/trpc/server/organisation-router/get-organisation.ts
Normal file
79
packages/trpc/server/organisation-router/get-organisation.ts
Normal 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,
|
||||
};
|
||||
};
|
||||
Reference in New Issue
Block a user