Uecko_ERP/modules/customers/src/api/infrastructure/express/controllers/create-customer.controller.ts

34 lines
1.0 KiB
TypeScript
Raw Normal View History

2025-08-21 08:14:12 +00:00
import {
ExpressController,
authGuard,
errorMapper,
forbidQueryFieldGuard,
tenantGuard,
} from "@erp/core/api";
import { CreateCustomerRequestDTO } from "../../../../common/dto";
import { CreateCustomerUseCase } from "../../../application";
2025-08-11 17:49:52 +00:00
export class CreateCustomerController extends ExpressController {
2025-08-21 08:14:12 +00:00
public constructor(private readonly useCase: CreateCustomerUseCase) {
2025-08-11 17:49:52 +00:00
super();
// 🔐 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() {
2025-08-21 08:14:12 +00:00
const tenantId = this.getTenantId()!; // garantizado por tenantGuard
const dto = this.req.body as CreateCustomerRequestDTO;
2025-08-11 17:49:52 +00:00
/*
// Inyectar empresa del usuario autenticado (ownership)
dto.customerCompanyId = user.companyId;
*/
2025-08-25 17:42:56 +00:00
const result = await this.useCase.execute({ tenantId, dto });
2025-08-11 17:49:52 +00:00
2025-08-21 08:14:12 +00:00
return result.match(
(data) => this.created(data),
(err) => this.handleApiError(errorMapper.toApiError(err))
);
2025-08-11 17:49:52 +00:00
}
}