66 lines
2.0 KiB
TypeScript
66 lines
2.0 KiB
TypeScript
|
|
import type { ITransactionManager } from "@erp/core/api";
|
||
|
|
import type { UniqueID } from "@repo/rdx-ddd";
|
||
|
|
import { Result } from "@repo/rdx-utils";
|
||
|
|
|
||
|
|
import type { CreateSupplierRequestDTO } from "../../../common";
|
||
|
|
import type { ISupplierCreator } from "../services";
|
||
|
|
import type {
|
||
|
|
ICreateSupplierInputMapper,
|
||
|
|
ISupplierFullSnapshotBuilder,
|
||
|
|
} from "../snapshot-builders";
|
||
|
|
|
||
|
|
type CreateSupplierUseCaseInput = {
|
||
|
|
companyId: UniqueID;
|
||
|
|
dto: CreateSupplierRequestDTO;
|
||
|
|
};
|
||
|
|
|
||
|
|
type CreateSupplierUseCaseDeps = {
|
||
|
|
dtoMapper: ICreateSupplierInputMapper;
|
||
|
|
creator: ISupplierCreator;
|
||
|
|
fullSnapshotBuilder: ISupplierFullSnapshotBuilder;
|
||
|
|
transactionManager: ITransactionManager;
|
||
|
|
};
|
||
|
|
|
||
|
|
export class CreateSupplierUseCase {
|
||
|
|
private readonly dtoMapper: ICreateSupplierInputMapper;
|
||
|
|
private readonly creator: ISupplierCreator;
|
||
|
|
private readonly fullSnapshotBuilder: ISupplierFullSnapshotBuilder;
|
||
|
|
private readonly transactionManager: ITransactionManager;
|
||
|
|
|
||
|
|
constructor(deps: CreateSupplierUseCaseDeps) {
|
||
|
|
this.dtoMapper = deps.dtoMapper;
|
||
|
|
this.creator = deps.creator;
|
||
|
|
this.fullSnapshotBuilder = deps.fullSnapshotBuilder;
|
||
|
|
this.transactionManager = deps.transactionManager;
|
||
|
|
}
|
||
|
|
|
||
|
|
public execute(params: CreateSupplierUseCaseInput) {
|
||
|
|
const { dto, companyId } = params;
|
||
|
|
|
||
|
|
const mappedPropsResult = this.dtoMapper.map(dto, { companyId });
|
||
|
|
if (mappedPropsResult.isFailure) {
|
||
|
|
return mappedPropsResult;
|
||
|
|
}
|
||
|
|
|
||
|
|
const { props, id } = mappedPropsResult.data;
|
||
|
|
|
||
|
|
return this.transactionManager.complete(async (transaction: unknown) => {
|
||
|
|
try {
|
||
|
|
const createResult = await this.creator.create({ companyId, id, props, transaction });
|
||
|
|
|
||
|
|
if (createResult.isFailure) {
|
||
|
|
return createResult;
|
||
|
|
}
|
||
|
|
|
||
|
|
const newSupplier = createResult.data;
|
||
|
|
|
||
|
|
const snapshot = this.fullSnapshotBuilder.toOutput(newSupplier);
|
||
|
|
|
||
|
|
return Result.ok(snapshot);
|
||
|
|
} catch (error: unknown) {
|
||
|
|
return Result.fail(error as Error);
|
||
|
|
}
|
||
|
|
});
|
||
|
|
}
|
||
|
|
}
|