| 240 | |
| 241 | // Clients CRUD |
| 242 | export async function listClients( |
| 243 | request: FastifyRequest<{ Querystring: { projectId?: string } }>, |
| 244 | reply: FastifyReply |
| 245 | ) { |
| 246 | const where: any = { |
| 247 | organizationId: request.client!.organizationId, |
| 248 | }; |
| 249 | |
| 250 | if (request.query.projectId) { |
| 251 | // Verify project belongs to organization |
| 252 | const project = await db.project.findFirst({ |
| 253 | where: { |
| 254 | id: request.query.projectId, |
| 255 | organizationId: request.client!.organizationId, |
| 256 | }, |
| 257 | }); |
| 258 | |
| 259 | if (!project) { |
| 260 | throw new HttpError('Project not found', { status: 404 }); |
| 261 | } |
| 262 | |
| 263 | where.projectId = request.query.projectId; |
| 264 | } |
| 265 | |
| 266 | const clients = await db.client.findMany({ |
| 267 | where, |
| 268 | orderBy: { |
| 269 | createdAt: 'desc', |
| 270 | }, |
| 271 | }); |
| 272 | |
| 273 | reply.send({ data: clients }); |
| 274 | } |
| 275 | |
| 276 | export async function getClient( |
| 277 | request: FastifyRequest<{ Params: { id: string } }>, |