| 1 | import { randomUUID } from "crypto"; |
| 2 | import { eq, isNull |
| 3 | import { z } |
| 4 | import { db } |
| 5 | import { backupDestinations } |
| 6 | import { recordActivity } |
| 7 | import { encrypt } |
| 8 | import { |
| 9 | backupDestinationInputSchema, |
| 10 | getDestinationForNode, |
| 11 | } from "../lib/backup-destinations"; |
| 12 | import { adminProcedure } |
| 13 | |
| 14 | function serializeConfig(input: |
| 15 | const { type: |
| 16 | return encrypt(JSON |
| 17 | } |
| 18 | |
| 19 | export const backupDestinationsRouter = |
| 20 | getGlobal: adminProcedure.handler |
| 21 | return getDestinationForNode(null) |
| 22 | }), |
| 23 | |
| 24 | upsertGlobal: adminProcedure |
| 25 | .input(backupDestinationInputSchema) |
| 26 | .handler(async ( |
| 27 | await db.delete |
| 28 | |
| 29 | await db.insert |
| 30 | id: randomUUID(), |
| 31 | nodeId: null, |
| 32 | type: input.type |
| 33 | config: serializeConfig(input |
| 34 | }); |
| 35 | |
| 36 | recordActivity({ |
| 37 | eventType: "admin:backup-destination.update", |
| 38 | userId: context.session |
| 39 | ip: context.ip |
| 40 | properties: { type: input |
| 41 | }); |
| 42 | |
| 43 | return getDestinationForNode(null) |
| 44 | }), |
| 45 | |
| 46 | clearGlobal: adminProcedure.handler |
| 47 | await db.delete |
| 48 | |
| 49 | recordActivity({ |
| 50 | eventType: "admin:backup-destination.clear", |
| 51 | userId: context.session |
| 52 | ip: context.ip |
| 53 | properties: { scope: " |
| 54 | }); |
| 55 | }), |
| 56 | |
| 57 | getForNode: adminProcedure |
| 58 | .input(z.object |
| 59 | .handler(async ( |
| 60 | return getDestinationForNode(input |
| 61 | }), |
| 62 | |
| 63 | upsertForNode: adminProcedure |
| 64 | .input( |
| 65 | z.object({ |
| 66 | ) |
| 67 | .handler(async ( |
| 68 | const { nodeId, |
| 69 | const config = |
| 70 | |
| 71 | await db |
| 72 | .insert(backupDestinations) |
| 73 | .values({ |
| 74 | id: randomUUID(), |
| 75 | nodeId, |
| 76 | type: destination.type |
| 77 | config, |
| 78 | updatedAt: new Date |
| 79 | }) |
| 80 | .onDuplicateKeyUpdate({ |
| 81 | set: { |
| 82 | type: destination.type |
| 83 | config, |
| 84 | updatedAt: new Date |
| 85 | }, |
| 86 | }); |
| 87 | |
| 88 | recordActivity({ |
| 89 | eventType: "admin:backup-destination.update", |
| 90 | userId: context.session |
| 91 | nodeId, |
| 92 | ip: context.ip |
| 93 | properties: { type: destination |
| 94 | }); |
| 95 | |
| 96 | return getDestinationForNode(nodeId |
| 97 | }), |
| 98 | |
| 99 | clearForNode: adminProcedure |
| 100 | .input(z.object |
| 101 | .handler(async ( |
| 102 | await db |
| 103 | .delete(backupDestinations) |
| 104 | .where(eq(backupDestinations |
| 105 | |
| 106 | recordActivity({ |
| 107 | eventType: "admin:backup-destination.clear", |
| 108 | userId: context.session |
| 109 | nodeId: input.nodeId |
| 110 | ip: context.ip |
| 111 | properties: { scope: " |
| 112 | }); |
| 113 | }), |
| 114 | }; |