({
ctx,
input,
}: InferProcedureOpts<typeof baseProcedure>)
| 24 | export const createProcedure = once(() => baseProcedure.mutation(create)); |
| 25 | |
| 26 | export async function create({ |
| 27 | ctx, |
| 28 | input, |
| 29 | }: InferProcedureOpts<typeof baseProcedure>) { |
| 30 | return await ctx.usingLocks( |
| 31 | [[`user-lock:${ctx.userId}`], [`group-lock:${input.groupId}`]], |
| 32 | async (signals) => { |
| 33 | return await ctx.dataAbstraction.transaction(async (dtrx) => { |
| 34 | // Check sufficient permissions |
| 35 | |
| 36 | if (input.groupCreation == null) { |
| 37 | await ctx.assertSufficientGroupPermissions({ |
| 38 | userId: ctx.userId, |
| 39 | groupId: input.groupId, |
| 40 | permission: 'editGroupPages', |
| 41 | }); |
| 42 | } |
| 43 | |
| 44 | // Get some necessary user data |
| 45 | |
| 46 | const [userPlan, personalGroupId] = await ctx.dataAbstraction.hmget( |
| 47 | 'user', |
| 48 | ctx.userId, |
| 49 | ['plan', 'personal-group-id'], |
| 50 | ); |
| 51 | |
| 52 | // Assert agent is subscribed |
| 53 | |
| 54 | if (input.groupId !== personalGroupId || input.groupCreation != null) { |
| 55 | await ctx.assertUserSubscribed({ userId: ctx.userId }); |
| 56 | } |
| 57 | |
| 58 | // Check if can create page |
| 59 | |
| 60 | let numFreePages; |
| 61 | |
| 62 | if (userPlan !== 'pro') { |
| 63 | numFreePages = |
| 64 | (await ctx.dataAbstraction.hget( |
| 65 | 'user', |
| 66 | ctx.userId, |
| 67 | 'num-free-pages', |
| 68 | )) + 1; |
| 69 | |
| 70 | if (numFreePages > 50) { |
| 71 | throw new TRPCError({ |
| 72 | code: 'FORBIDDEN', |
| 73 | message: 'You have reached your limit of 50 free pages.', |
| 74 | }); |
| 75 | } |
| 76 | |
| 77 | await ctx.dataAbstraction.patch( |
| 78 | 'user', |
| 79 | ctx.userId, |
| 80 | { num_free_pages: numFreePages }, |
| 81 | { dtrx }, |
| 82 | ); |
| 83 | } |
nothing calls this directly
no test coverage detected