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

@ -1,25 +1,16 @@
import { SubscriptionStatus } from '@prisma/client';
import { IS_BILLING_ENABLED } from '@documenso/lib/constants/app';
import { AppError } from '@documenso/lib/errors/app-error';
import type { SetAvatarImageOptions } from '@documenso/lib/server-only/profile/set-avatar-image';
import { setAvatarImage } from '@documenso/lib/server-only/profile/set-avatar-image';
import { getSubscriptionsByUserId } from '@documenso/lib/server-only/subscription/get-subscriptions-by-user-id';
import { createBillingPortal } from '@documenso/lib/server-only/user/create-billing-portal';
import { createCheckoutSession } from '@documenso/lib/server-only/user/create-checkout-session';
import { deleteUser } from '@documenso/lib/server-only/user/delete-user';
import { findUserSecurityAuditLogs } from '@documenso/lib/server-only/user/find-user-security-audit-logs';
import { getUserById } from '@documenso/lib/server-only/user/get-user-by-id';
import { updateProfile } from '@documenso/lib/server-only/user/update-profile';
import { updatePublicProfile } from '@documenso/lib/server-only/user/update-public-profile';
import { adminProcedure, authenticatedProcedure, router } from '../trpc';
import {
ZCreateCheckoutSessionRequestSchema,
ZFindUserSecurityAuditLogsSchema,
ZRetrieveUserByIdQuerySchema,
ZSetProfileImageMutationSchema,
ZUpdateProfileMutationSchema,
ZUpdatePublicProfileMutationSchema,
} from './schema';
export const profileRouter = router({
@ -38,31 +29,6 @@ export const profileRouter = router({
return await getUserById({ id });
}),
createBillingPortal: authenticatedProcedure.mutation(async ({ ctx }) => {
return await createBillingPortal({
user: {
id: ctx.user.id,
customerId: ctx.user.customerId,
email: ctx.user.email,
name: ctx.user.name,
},
});
}),
createCheckoutSession: authenticatedProcedure
.input(ZCreateCheckoutSessionRequestSchema)
.mutation(async ({ ctx, input }) => {
return await createCheckoutSession({
user: {
id: ctx.user.id,
customerId: ctx.user.customerId,
email: ctx.user.email,
name: ctx.user.name,
},
priceId: input.priceId,
});
}),
updateProfile: authenticatedProcedure
.input(ZUpdateProfileMutationSchema)
.mutation(async ({ input, ctx }) => {
@ -76,37 +42,6 @@ export const profileRouter = router({
});
}),
updatePublicProfile: authenticatedProcedure
.input(ZUpdatePublicProfileMutationSchema)
.mutation(async ({ input, ctx }) => {
const { url, bio, enabled } = input;
if (IS_BILLING_ENABLED() && url !== undefined && url.length < 6) {
const subscriptions = await getSubscriptionsByUserId({
userId: ctx.user.id,
}).then((subscriptions) =>
subscriptions.filter((s) => s.status === SubscriptionStatus.ACTIVE),
);
if (subscriptions.length === 0) {
throw new AppError('PREMIUM_PROFILE_URL', {
message: 'Only subscribers can have a username shorter than 6 characters',
});
}
}
const user = await updatePublicProfile({
userId: ctx.user.id,
data: {
url,
bio,
enabled,
},
});
return { success: true, url: user.url };
}),
deleteAccount: authenticatedProcedure.mutation(async ({ ctx }) => {
return await deleteUser({
id: ctx.user.id,
@ -116,11 +51,29 @@ export const profileRouter = router({
setProfileImage: authenticatedProcedure
.input(ZSetProfileImageMutationSchema)
.mutation(async ({ input, ctx }) => {
const { bytes, teamId } = input;
const { bytes, teamId, organisationId } = input;
let target: SetAvatarImageOptions['target'] = {
type: 'user',
};
if (teamId) {
target = {
type: 'team',
teamId,
};
}
if (organisationId) {
target = {
type: 'organisation',
organisationId,
};
}
return await setAvatarImage({
userId: ctx.user.id,
teamId,
target,
bytes,
requestMetadata: ctx.metadata,
});

View File

@ -1,7 +1,5 @@
import { z } from 'zod';
export const MAX_PROFILE_BIO_LENGTH = 256;
export const ZFindUserSecurityAuditLogsSchema = z.object({
page: z.number().optional(),
perPage: z.number().optional(),
@ -15,10 +13,6 @@ export const ZRetrieveUserByIdQuerySchema = z.object({
export type TRetrieveUserByIdQuerySchema = z.infer<typeof ZRetrieveUserByIdQuerySchema>;
export const ZCreateCheckoutSessionRequestSchema = z.object({
priceId: z.string().min(1),
});
export const ZUpdateProfileMutationSchema = z.object({
name: z.string().min(1),
signature: z.string(),
@ -26,30 +20,10 @@ export const ZUpdateProfileMutationSchema = z.object({
export type TUpdateProfileMutationSchema = z.infer<typeof ZUpdateProfileMutationSchema>;
export const ZUpdatePublicProfileMutationSchema = z.object({
bio: z
.string()
.max(MAX_PROFILE_BIO_LENGTH, {
message: `Bio must be shorter than ${MAX_PROFILE_BIO_LENGTH + 1} characters`,
})
.optional(),
enabled: z.boolean().optional(),
url: z
.string()
.trim()
.toLowerCase()
.min(1, { message: 'Please enter a valid username.' })
.regex(/^[a-z0-9-]+$/, {
message: 'Username can only container alphanumeric characters and dashes.',
})
.optional(),
});
export type TUpdatePublicProfileMutationSchema = z.infer<typeof ZUpdatePublicProfileMutationSchema>;
export const ZSetProfileImageMutationSchema = z.object({
bytes: z.string().nullish(),
teamId: z.number().min(1).nullish(),
teamId: z.number().min(1).nullable(),
organisationId: z.string().nullable(),
});
export type TSetProfileImageMutationSchema = z.infer<typeof ZSetProfileImageMutationSchema>;