| 1 | import { and, count, eq, like, or } from "drizzle-orm"; |
| 2 | import { z } |
| 3 | import { db } |
| 4 | import { user, session |
| 5 | import { recordActivity } |
| 6 | import { adminProcedure, protectedProcedure |
| 7 | |
| 8 | export const usersRouter = |
| 9 | getSelf: protectedProcedure.handler |
| 10 | return db.query |
| 11 | where: eq(user |
| 12 | columns: { |
| 13 | id: true, |
| 14 | name: true, |
| 15 | email: true, |
| 16 | image: true, |
| 17 | role: true, |
| 18 | createdAt: true, |
| 19 | twoFactorEnabled: true, |
| 20 | billingName: true, |
| 21 | billingAddressLine1: true, |
| 22 | billingAddressLine2: true, |
| 23 | billingCity: true, |
| 24 | billingState: true, |
| 25 | billingPostalCode: true, |
| 26 | billingCountry: true, |
| 27 | vatNumber: true, |
| 28 | vatCountry: true, |
| 29 | locale: true, |
| 30 | }, |
| 31 | }); |
| 32 | }), |
| 33 | |
| 34 | updateProfile: protectedProcedure |
| 35 | .input(z.object |
| 36 | .handler(async ( |
| 37 | await db.update |
| 38 | }), |
| 39 | |
| 40 | updateBilling: protectedProcedure |
| 41 | .input( |
| 42 | z.object({ |
| 43 | billingName: z.string |
| 44 | billingAddressLine1: z.string |
| 45 | billingAddressLine2: z.string |
| 46 | billingCity: z.string |
| 47 | billingState: z.string |
| 48 | billingPostalCode: z.string |
| 49 | billingCountry: z.string |
| 50 | vatNumber: z.string |
| 51 | vatCountry: z.string |
| 52 | }), |
| 53 | ) |
| 54 | .handler(async ( |
| 55 | await db |
| 56 | .update(user) |
| 57 | .set({ |
| 58 | billingName: input.billingName |
| 59 | billingAddressLine1: input.billingAddressLine1 |
| 60 | billingAddressLine2: input.billingAddressLine2 |
| 61 | billingCity: input.billingCity |
| 62 | billingState: input.billingState |
| 63 | billingPostalCode: input.billingPostalCode |
| 64 | billingCountry: input.billingCountry |
| 65 | vatNumber: input.vatNumber |
| 66 | vatCountry: input.vatCountry |
| 67 | }) |
| 68 | .where(eq(user |
| 69 | }), |
| 70 | |
| 71 | updateLocale: protectedProcedure |
| 72 | .input(z.object |
| 73 | .handler(async ( |
| 74 | await db.update |
| 75 | }), |
| 76 | |
| 77 | listSessions: protectedProcedure.handler |
| 78 | return db.query |
| 79 | where: eq(session |
| 80 | columns: { |
| 81 | id: true, |
| 82 | token: true, |
| 83 | createdAt: true, |
| 84 | expiresAt: true, |
| 85 | ipAddress: true, |
| 86 | userAgent: true, |
| 87 | }, |
| 88 | orderBy: (s, |
| 89 | }); |
| 90 | }), |
| 91 | |
| 92 | revokeSession: protectedProcedure |
| 93 | .input(z.object |
| 94 | .handler(async ( |
| 95 | await db |
| 96 | .delete(session) |
| 97 | .where( |
| 98 | and( |
| 99 | eq(session.token |
| 100 | eq(session.userId |
| 101 | ), |
| 102 | ); |
| 103 | }), |
| 104 | |
| 105 | search: adminProcedure |
| 106 | .input(z.object |
| 107 | .handler(async ( |
| 108 | const q = |
| 109 | return db.query |
| 110 | where: or(like |
| 111 | columns: { id: true |
| 112 | limit: 10, |
| 113 | }); |
| 114 | }), |
| 115 | |
| 116 | list: adminProcedure |
| 117 | .input( |
| 118 | z.object({ |
| 119 | page: z.number |
| 120 | search: z.string |
| 121 | }), |
| 122 | ) |
| 123 | .handler(async ( |
| 124 | const pageSize = |
| 125 | const offset = |
| 126 | |
| 127 | const whereClause = |
| 128 | ? or(like( |
| 129 | : undefined; |
| 130 | |
| 131 | const [users, |
| 132 | db.query.user |
| 133 | where: whereClause, |
| 134 | columns: { |
| 135 | id: true, |
| 136 | name: true, |
| 137 | email: true, |
| 138 | image: true, |
| 139 | role: true, |
| 140 | banned: true, |
| 141 | banReason: true, |
| 142 | banExpires: true, |
| 143 | createdAt: true, |
| 144 | }, |
| 145 | limit: pageSize, |
| 146 | offset, |
| 147 | orderBy: (u, |
| 148 | }), |
| 149 | db.select({ |
| 150 | ]); |
| 151 | |
| 152 | return { users, |
| 153 | }), |
| 154 | |
| 155 | adminGet: adminProcedure |
| 156 | .input(z.object |
| 157 | .handler(async ( |
| 158 | const [userData, |
| 159 | db.query.user |
| 160 | where: eq(user |
| 161 | columns: { |
| 162 | id: true, |
| 163 | name: true, |
| 164 | email: true, |
| 165 | image: true, |
| 166 | role: true, |
| 167 | banned: true, |
| 168 | banReason: true, |
| 169 | banExpires: true, |
| 170 | createdAt: true, |
| 171 | twoFactorEnabled: true, |
| 172 | billingName: true, |
| 173 | billingAddressLine1: true, |
| 174 | billingAddressLine2: true, |
| 175 | billingCity: true, |
| 176 | billingState: true, |
| 177 | billingPostalCode: true, |
| 178 | billingCountry: true, |
| 179 | vatNumber: true, |
| 180 | vatCountry: true, |
| 181 | }, |
| 182 | }), |
| 183 | db.select({ |
| 184 | ]); |
| 185 | |
| 186 | if (!userData) |
| 187 | return { ...userData |
| 188 | }), |
| 189 | |
| 190 | adminGetSessions: adminProcedure |
| 191 | .input(z.object |
| 192 | .handler(async ( |
| 193 | return db.query |
| 194 | where: eq(session |
| 195 | columns: { |
| 196 | id: true, |
| 197 | token: true, |
| 198 | createdAt: true, |
| 199 | expiresAt: true, |
| 200 | ipAddress: true, |
| 201 | userAgent: true, |
| 202 | }, |
| 203 | orderBy: (s, |
| 204 | }); |
| 205 | }), |
| 206 | |
| 207 | adminGetServers: adminProcedure |
| 208 | .input(z.object |
| 209 | .handler(async ( |
| 210 | const pageSize = |
| 211 | const offset = |
| 212 | |
| 213 | const [userServers, |
| 214 | db |
| 215 | .select({ |
| 216 | id: servers.id |
| 217 | uuid: servers.uuid |
| 218 | name: servers.name |
| 219 | suspended: servers.suspended |
| 220 | createdAt: servers.createdAt |
| 221 | nodeName: nodes.name |
| 222 | allocationIp: nodeAllocations.ip |
| 223 | allocationPort: nodeAllocations.port |
| 224 | }) |
| 225 | .from(servers) |
| 226 | .leftJoin(nodes, |
| 227 | .leftJoin(nodeAllocations, |
| 228 | .where(eq(servers |
| 229 | .limit(pageSize) |
| 230 | .offset(offset) |
| 231 | .orderBy(servers.createdAt |
| 232 | db.select({ |
| 233 | ]); |
| 234 | |
| 235 | return { servers: |
| 236 | }), |
| 237 | |
| 238 | setRole: adminProcedure |
| 239 | .input(z.object |
| 240 | .handler(async ( |
| 241 | if (input.userId |
| 242 | throw new Error( |
| 243 | } |
| 244 | await db.update |
| 245 | recordActivity({ eventType: |
| 246 | }), |
| 247 | |
| 248 | ban: adminProcedure |
| 249 | .input( |
| 250 | z.object({ |
| 251 | userId: z.string |
| 252 | reason: z.string |
| 253 | expiresAt: z.string |
| 254 | }), |
| 255 | ) |
| 256 | .handler(async ( |
| 257 | if (input.userId |
| 258 | throw new Error( |
| 259 | } |
| 260 | await db |
| 261 | .update(user) |
| 262 | .set({ |
| 263 | banned: true, |
| 264 | banReason: input.reason |
| 265 | banExpires: input.expiresAt |
| 266 | }) |
| 267 | .where(eq(user |
| 268 | recordActivity({ eventType: |
| 269 | }), |
| 270 | |
| 271 | unban: adminProcedure |
| 272 | .input(z.object |
| 273 | .handler(async ( |
| 274 | await db |
| 275 | .update(user) |
| 276 | .set({ banned: |
| 277 | .where(eq(user |
| 278 | recordActivity({ eventType: |
| 279 | }), |
| 280 | |
| 281 | delete: adminProcedure |
| 282 | .input(z.object |
| 283 | .handler(async ( |
| 284 | if (input.userId |
| 285 | throw new Error( |
| 286 | } |
| 287 | await db.delete |
| 288 | recordActivity({ eventType: |
| 289 | }), |
| 290 | }; |