(
request: FastifyRequest<{ Body: z.infer<typeof zCreateClient> }>,
reply: FastifyReply
)
| 292 | } |
| 293 | |
| 294 | export async function createClient( |
| 295 | request: FastifyRequest<{ Body: z.infer<typeof zCreateClient> }>, |
| 296 | reply: FastifyReply |
| 297 | ) { |
| 298 | const { name, projectId, type } = request.body; |
| 299 | |
| 300 | // If projectId is provided, verify it belongs to organization |
| 301 | if (projectId) { |
| 302 | const project = await db.project.findFirst({ |
| 303 | where: { |
| 304 | id: projectId, |
| 305 | organizationId: request.client!.organizationId, |
| 306 | }, |
| 307 | }); |
| 308 | |
| 309 | if (!project) { |
| 310 | throw new HttpError('Project not found', { status: 404 }); |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | // Generate secret |
| 315 | const secret = `sec_${crypto.randomBytes(10).toString('hex')}`; |
| 316 | |
| 317 | const client = await db.client.create({ |
| 318 | data: { |
| 319 | organizationId: request.client!.organizationId, |
| 320 | projectId: projectId || null, |
| 321 | name, |
| 322 | type: type || 'write', |
| 323 | secret: await hashPassword(secret), |
| 324 | }, |
| 325 | }); |
| 326 | |
| 327 | await getClientByIdCached.clear(client.id); |
| 328 | |
| 329 | reply.send({ |
| 330 | data: { |
| 331 | ...client, |
| 332 | secret, // Return plain secret only once |
| 333 | }, |
| 334 | }); |
| 335 | } |
| 336 | |
| 337 | export async function updateClient( |
| 338 | request: FastifyRequest<{ |
no test coverage detected