|
| 1 | +import { isNanoID } from '@stdlib/misc'; |
| 2 | +import { checkRedlockSignalAborted } from '@stdlib/redlock'; |
| 3 | +import { once } from 'lodash'; |
| 4 | +import type { InferProcedureOpts } from 'src/trpc/helpers'; |
| 5 | +import { authProcedure } from 'src/trpc/helpers'; |
| 6 | +import { z } from 'zod'; |
| 7 | + |
| 8 | +const baseProcedure = authProcedure.input( |
| 9 | + z.object({ |
| 10 | + groupId: z.string().refine(isNanoID), |
| 11 | + |
| 12 | + areJoinRequestsAllowed: z.boolean(), |
| 13 | + }), |
| 14 | +); |
| 15 | + |
| 16 | +export const setJoinRequestsAllowedProcedure = once(() => |
| 17 | + baseProcedure.mutation(setJoinRequestsAllowed), |
| 18 | +); |
| 19 | + |
| 20 | +export async function setJoinRequestsAllowed({ |
| 21 | + ctx, |
| 22 | + input, |
| 23 | +}: InferProcedureOpts<typeof baseProcedure>) { |
| 24 | + return await ctx.usingLocks( |
| 25 | + [[`user-lock:${ctx.userId}`], [`group-lock:${input.groupId}`]], |
| 26 | + async (signals) => { |
| 27 | + return await ctx.dataAbstraction.transaction(async (dtrx) => { |
| 28 | + // Assert agent is subscribed |
| 29 | + |
| 30 | + await ctx.assertUserSubscribed({ userId: ctx.userId }); |
| 31 | + |
| 32 | + // Check if user has sufficient permissions |
| 33 | + |
| 34 | + await ctx.assertSufficientGroupPermissions({ |
| 35 | + userId: ctx.userId, |
| 36 | + groupId: input.groupId, |
| 37 | + permission: 'editGroupSettings', |
| 38 | + }); |
| 39 | + |
| 40 | + await ctx.dataAbstraction.hmset( |
| 41 | + 'group', |
| 42 | + input.groupId, |
| 43 | + { 'are-join-requests-allowed': input.areJoinRequestsAllowed }, |
| 44 | + { dtrx }, |
| 45 | + ); |
| 46 | + |
| 47 | + checkRedlockSignalAborted(signals); |
| 48 | + }); |
| 49 | + }, |
| 50 | + ); |
| 51 | +} |
0 commit comments