35 lines
1.0 KiB
TypeScript
35 lines
1.0 KiB
TypeScript
|
|
import { ExpressController, errorMapper } from "@erp/core/api";
|
||
|
|
import { CreateCustomerCommandDTO } from "../../../common/dto";
|
||
|
|
import { CreateCustomerUseCase } from "../../application";
|
||
|
|
|
||
|
|
export class CreateCustomerController extends ExpressController {
|
||
|
|
public constructor(private readonly createCustomer: CreateCustomerUseCase) {
|
||
|
|
super();
|
||
|
|
}
|
||
|
|
|
||
|
|
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);
|
||
|
|
}
|
||
|
|
}
|