30 lines
909 B
TypeScript
30 lines
909 B
TypeScript
import { FastifyInstance } from 'fastify';
|
|
import { z } from 'zod';
|
|
import { TenantsService } from './tenants.service.js';
|
|
import { AuditService } from '../audit/audit.service.js';
|
|
|
|
const tenantSchema = z.object({
|
|
name: z.string().min(2),
|
|
plan: z.string().optional(),
|
|
status: z.enum(['active', 'suspended']).optional(),
|
|
});
|
|
|
|
export const registerTenantsController = (
|
|
app: FastifyInstance,
|
|
tenantsService: TenantsService,
|
|
auditService: AuditService,
|
|
) => {
|
|
app.get('/tenants', async () => tenantsService.listTenants());
|
|
|
|
app.post('/tenants', async (request, reply) => {
|
|
const payload = tenantSchema.parse(request.body);
|
|
const tenant = await tenantsService.createTenant(payload);
|
|
await auditService.record({
|
|
tenantId: tenant.id,
|
|
action: 'tenant.created',
|
|
metadata: { name: tenant.name, plan: tenant.plan },
|
|
});
|
|
reply.code(201);
|
|
return tenant;
|
|
});
|
|
};
|