FactuGES_ERP/modules/document-series/src/api/application/mappers/create-document-series-command.mapper.ts

111 lines
3.5 KiB
TypeScript
Raw Normal View History

import {
Name,
TextValue,
UniqueID,
UtcDate,
ValidationErrorCollection,
type ValidationErrorDetail,
extractOrPushError,
maybeFromNullableResult,
} from "@repo/rdx-ddd";
import { Maybe, Result } from "@repo/rdx-utils";
import type { CreateDocumentSeriesRequestDTO } from "../../../common";
import {
DocumentSeries,
DocumentSeriesCode,
DocumentSeriesNumber,
DocumentType,
} from "../../domain";
import type { CreateDocumentSeriesCommand } from "../commands";
export class CreateDocumentSeriesCommandMapper {
public map(dto: CreateDocumentSeriesRequestDTO, companyId: UniqueID) {
const command: CreateDocumentSeriesCommand = {
id: dto.id,
companyId: companyId.toString(),
branchId: dto.branch_id ?? null,
documentType: dto.document_type,
code: dto.code,
name: dto.name,
description: dto.description ?? null,
prefix: dto.prefix ?? null,
suffix: dto.suffix ?? null,
nextNumber: dto.next_number,
padding: dto.padding,
validFrom: dto.valid_from ?? null,
validTo: dto.valid_to ?? null,
isDefault: dto.is_default ?? false,
isActive: dto.is_active ?? true,
};
return this.toAggregate(command);
}
public toAggregate(command: CreateDocumentSeriesCommand) {
const errors: ValidationErrorDetail[] = [];
const id = extractOrPushError(UniqueID.create(command.id, true), "id", errors);
const companyId = extractOrPushError(UniqueID.create(command.companyId), "companyId", errors);
const branchId = extractOrPushError(
maybeFromNullableResult(command.branchId, (value) => UniqueID.create(value)),
"branchId",
errors
);
const documentType = extractOrPushError(
DocumentType.create(command.documentType),
"documentType",
errors
);
const code = extractOrPushError(DocumentSeriesCode.create(command.code), "code", errors);
const name = extractOrPushError(Name.create(command.name), "name", errors);
const description = extractOrPushError(
maybeFromNullableResult(command.description, (value) => TextValue.create(value)),
"description",
errors
);
const nextNumber = extractOrPushError(
DocumentSeriesNumber.create(command.nextNumber),
"nextNumber",
errors
);
const validFrom = extractOrPushError(
maybeFromNullableResult(command.validFrom, (value) => UtcDate.createFromISO(value)),
"validFrom",
errors
);
const validTo = extractOrPushError(
maybeFromNullableResult(command.validTo, (value) => UtcDate.createFromISO(value)),
"validTo",
errors
);
if (errors.length > 0) {
return Result.fail(new ValidationErrorCollection("Document series mapping failed", errors));
}
return DocumentSeries.create(
{
companyId: companyId!,
branchId: branchId ?? Maybe.none(),
documentType: documentType!,
code: code!,
name: name!,
description: description ?? Maybe.none(),
prefix: Maybe.fromNullable(command.prefix ?? undefined),
suffix: Maybe.fromNullable(command.suffix ?? undefined),
nextNumber: nextNumber!,
padding: command.padding,
validFrom: validFrom ?? Maybe.none(),
validTo: validTo ?? Maybe.none(),
isDefault: command.isDefault,
isActive: command.isActive,
hasAssignedNumbers: false,
createdAt: new Date(),
updatedAt: new Date(),
deletedAt: Maybe.none(),
},
id
);
}
}