({
canUpdate,
channel,
updateData,
ctx,
}: {
canUpdate: (input: {
oldSettings: ChannelWithFlags;
doSettingsExistAlready: boolean;
}) => PermissionsChecks;
channel: z.infer<typeof zChannelWithServerCreate>;
updateData: Parameters<typeof upsertChannel>[0]['update'];
ctx: Context;
})
| 40 | ); |
| 41 | |
| 42 | async function mutateChannel({ |
| 43 | canUpdate, |
| 44 | channel, |
| 45 | updateData, |
| 46 | ctx, |
| 47 | }: { |
| 48 | canUpdate: (input: { |
| 49 | oldSettings: ChannelWithFlags; |
| 50 | doSettingsExistAlready: boolean; |
| 51 | }) => PermissionsChecks; |
| 52 | channel: z.infer<typeof zChannelWithServerCreate>; |
| 53 | updateData: Parameters<typeof upsertChannel>[0]['update']; |
| 54 | ctx: Context; |
| 55 | }) { |
| 56 | return protectedMutation({ |
| 57 | permissions: () => assertCanEditServerBotOnly(ctx, channel.server.id), |
| 58 | operation: async () => { |
| 59 | const channelWithServerId = { |
| 60 | ...channel, |
| 61 | serverId: channel.server.id, |
| 62 | }; |
| 63 | let oldSettings = await findChannelById(channel.id); |
| 64 | let doSettingsExistAlready = true; |
| 65 | if (!oldSettings) { |
| 66 | oldSettings = getDefaultChannelWithFlags(channelWithServerId); |
| 67 | doSettingsExistAlready = false; |
| 68 | } else { |
| 69 | doSettingsExistAlready = true; |
| 70 | } |
| 71 | // We only want to create the server |
| 72 | await upsertServer({ |
| 73 | create: channel.server, |
| 74 | update: {}, |
| 75 | }); |
| 76 | return protectedMutation({ |
| 77 | permissions: canUpdate({ oldSettings, doSettingsExistAlready }), |
| 78 | operation: async () => |
| 79 | upsertChannel({ |
| 80 | create: { |
| 81 | ...channelWithServerId, |
| 82 | ...updateData, |
| 83 | }, |
| 84 | update: updateData, |
| 85 | }), |
| 86 | }); |
| 87 | }, |
| 88 | }); |
| 89 | } |
| 90 | |
| 91 | export const channelRouter = router({ |
| 92 | byId: publicProcedure.input(z.string()).query(async ({ ctx, input }) => { |
nothing calls this directly
no test coverage detected