| 1 | import { randomUUID } from "crypto"; |
| 2 | import { and, eq |
| 3 | import { z } |
| 4 | import { ORPCError } |
| 5 | import { db } |
| 6 | import { scheduleTasks, schedules |
| 7 | import { computeNextRun } |
| 8 | import { recordActivity } |
| 9 | import { protectedProcedure } |
| 10 | |
| 11 | async function requireServerAccess( |
| 12 | const server = |
| 13 | if (!server) |
| 14 | if (role !== |
| 15 | const sub = |
| 16 | where: and(eq |
| 17 | }); |
| 18 | if (!sub) |
| 19 | } |
| 20 | return server; |
| 21 | } |
| 22 | |
| 23 | const CronScheduleSchema = z |
| 24 | cronSecond: z.string |
| 25 | cronMinute: z.string |
| 26 | cronHour: z.string |
| 27 | cronDayOfWeek: z.string |
| 28 | cronDayOfMonth: z.string |
| 29 | }); |
| 30 | |
| 31 | export const schedulesRouter = |
| 32 | list: protectedProcedure |
| 33 | .input(z.object |
| 34 | .handler(async ( |
| 35 | await requireServerAccess(context |
| 36 | return db.query |
| 37 | where: eq(schedules |
| 38 | with: { tasks: true |
| 39 | }); |
| 40 | }), |
| 41 | |
| 42 | create: protectedProcedure |
| 43 | .input( |
| 44 | z.object({ |
| 45 | serverId: z.string |
| 46 | name: z.string |
| 47 | isActive: z.boolean |
| 48 | ...CronScheduleSchema.shape |
| 49 | }), |
| 50 | ) |
| 51 | .handler(async ( |
| 52 | await requireServerAccess(context |
| 53 | |
| 54 | const nextRunAt = |
| 55 | input.cronSecond, |
| 56 | input.cronMinute, |
| 57 | input.cronHour, |
| 58 | input.cronDayOfMonth, |
| 59 | input.cronDayOfWeek, |
| 60 | ); |
| 61 | |
| 62 | const id = |
| 63 | await db.insert |
| 64 | |
| 65 | recordActivity({ |
| 66 | eventType: "server:schedule.create", |
| 67 | userId: context.session |
| 68 | serverId: input.serverId |
| 69 | ip: context.ip |
| 70 | properties: { name: input |
| 71 | }); |
| 72 | |
| 73 | return db.query |
| 74 | }), |
| 75 | |
| 76 | update: protectedProcedure |
| 77 | .input( |
| 78 | z.object({ |
| 79 | serverId: z.string |
| 80 | scheduleId: z.string |
| 81 | name: z.string |
| 82 | isActive: z.boolean |
| 83 | cronSecond: z.string |
| 84 | cronMinute: z.string |
| 85 | cronHour: z.string |
| 86 | cronDayOfWeek: z.string |
| 87 | cronDayOfMonth: z.string |
| 88 | }), |
| 89 | ) |
| 90 | .handler(async ( |
| 91 | await requireServerAccess(context |
| 92 | |
| 93 | const schedule = |
| 94 | where: and(eq |
| 95 | }); |
| 96 | if (!schedule) |
| 97 | |
| 98 | const { serverId, |
| 99 | const merged = |
| 100 | const nextRunAt = |
| 101 | merged.cronSecond, |
| 102 | merged.cronMinute, |
| 103 | merged.cronHour, |
| 104 | merged.cronDayOfMonth, |
| 105 | merged.cronDayOfWeek, |
| 106 | ); |
| 107 | |
| 108 | await db.update |
| 109 | |
| 110 | recordActivity({ |
| 111 | eventType: "server:schedule.update", |
| 112 | userId: context.session |
| 113 | serverId: input.serverId |
| 114 | ip: context.ip |
| 115 | properties: { name: merged |
| 116 | }); |
| 117 | |
| 118 | return db.query |
| 119 | }), |
| 120 | |
| 121 | delete: protectedProcedure |
| 122 | .input(z.object |
| 123 | .handler(async ( |
| 124 | await requireServerAccess(context |
| 125 | |
| 126 | const schedule = |
| 127 | where: and(eq |
| 128 | }); |
| 129 | |
| 130 | await db.delete |
| 131 | and(eq(schedules |
| 132 | ); |
| 133 | |
| 134 | recordActivity({ |
| 135 | eventType: "server:schedule.delete", |
| 136 | userId: context.session |
| 137 | serverId: input.serverId |
| 138 | ip: context.ip |
| 139 | properties: { name: schedule |
| 140 | }); |
| 141 | }), |
| 142 | |
| 143 | createTask: protectedProcedure |
| 144 | .input( |
| 145 | z.object({ |
| 146 | serverId: z.string |
| 147 | scheduleId: z.string |
| 148 | sequenceId: z.number |
| 149 | action: z.enum |
| 150 | payload: z.string |
| 151 | timeOffset: z.number |
| 152 | continueOnFailure: z.boolean |
| 153 | }), |
| 154 | ) |
| 155 | .handler(async ( |
| 156 | await requireServerAccess(context |
| 157 | const id = |
| 158 | const { serverId, |
| 159 | await db.insert |
| 160 | const task = |
| 161 | if (!task) |
| 162 | return task; |
| 163 | }), |
| 164 | |
| 165 | updateTask: protectedProcedure |
| 166 | .input( |
| 167 | z.object({ |
| 168 | serverId: z.string |
| 169 | taskId: z.string |
| 170 | sequenceId: z.number |
| 171 | action: z.enum |
| 172 | payload: z.string |
| 173 | timeOffset: z.number |
| 174 | continueOnFailure: z.boolean |
| 175 | }), |
| 176 | ) |
| 177 | .handler(async ( |
| 178 | await requireServerAccess(context |
| 179 | const { serverId, |
| 180 | await db.update |
| 181 | return db.query |
| 182 | }), |
| 183 | |
| 184 | deleteTask: protectedProcedure |
| 185 | .input(z.object |
| 186 | .handler(async ( |
| 187 | await requireServerAccess(context |
| 188 | await db.delete |
| 189 | }), |
| 190 | }; |