2025-08-11 17:49:52 +00:00
|
|
|
import { ExpressController, errorMapper } from "@erp/core/api";
|
2025-08-14 14:58:13 +00:00
|
|
|
import { CreateCustomerCommandDTO } from "../../../../../common/dto";
|
|
|
|
|
import { CreateCustomerUseCase } from "../../../../application";
|
2025-08-11 17:49:52 +00:00
|
|
|
|
|
|
|
|
export class CreateCustomerController extends ExpressController {
|
|
|
|
|
public constructor(private readonly createCustomer: CreateCustomerUseCase) {
|
|
|
|
|
super();
|
2025-08-14 14:58:13 +00:00
|
|
|
// 🔐 Reutiliza guards de auth/tenant y prohíbe 'companyId' en query
|
|
|
|
|
this.useGuards(authGuard(), tenantGuard(), forbidQueryFieldGuard("companyId"));
|
2025-08-11 17:49:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected async executeImpl() {
|
|
|
|
|
const dto = this.req.body as CreateCustomerCommandDTO;
|
|
|
|
|
/*
|
|
|
|
|
const user = this.req.user; // asumimos middleware authenticateJWT inyecta user
|
|
|
|
|
|
|
|
|
|
if (!user || !user.companyId) {
|
|
|
|
|
this.unauthorized(res, "Unauthorized: user or company not found");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Inyectar empresa del usuario autenticado (ownership)
|
|
|
|
|
dto.customerCompanyId = user.companyId;
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
const result = await this.createCustomer.execute(dto);
|
|
|
|
|
|
|
|
|
|
if (result.isFailure) {
|
|
|
|
|
console.log(result.error);
|
|
|
|
|
const apiError = errorMapper.toApiError(result.error);
|
|
|
|
|
return this.handleApiError(apiError);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return this.created(result.data);
|
|
|
|
|
}
|
|
|
|
|
}
|