52 lines
1.9 KiB
TypeScript
52 lines
1.9 KiB
TypeScript
|
|
import { EmailAddress, Name, UniqueID } from "@repo/rdx-ddd";
|
||
|
|
import { Maybe, Result } from "@repo/rdx-utils";
|
||
|
|
|
||
|
|
import type { CompanyRequestDTO, ContactRequestDTO, SiteRequestDTO } from "../../common";
|
||
|
|
import { CompanyError } from "../domain/company-error";
|
||
|
|
import { PhoneNumber } from "../domain/value-objects/phone-number";
|
||
|
|
import { SiteAddress } from "../domain/value-objects/site-address";
|
||
|
|
|
||
|
|
export class CompanyInputMapper {
|
||
|
|
id(value: string): Result<UniqueID, CompanyError> {
|
||
|
|
const result = UniqueID.create(value);
|
||
|
|
return result.isSuccess
|
||
|
|
? Result.ok(result.data)
|
||
|
|
: Result.fail(new CompanyError("INVALID_REQUEST"));
|
||
|
|
}
|
||
|
|
company(dto: CompanyRequestDTO) {
|
||
|
|
const name = Name.create(dto.name, 100);
|
||
|
|
return name.isSuccess
|
||
|
|
? Result.ok({ name: name.data, active: dto.active })
|
||
|
|
: Result.fail(new CompanyError("INVALID_REQUEST"));
|
||
|
|
}
|
||
|
|
contact(dto: ContactRequestDTO) {
|
||
|
|
const name = Name.create(dto.name, 100);
|
||
|
|
const position = Name.create(dto.position, 100);
|
||
|
|
const email = EmailAddress.create(dto.email);
|
||
|
|
const phone = PhoneNumber.create(dto.phone);
|
||
|
|
if (!(name.isSuccess && position.isSuccess && email.isSuccess && phone.isSuccess)) {
|
||
|
|
return Result.fail(new CompanyError("INVALID_REQUEST"));
|
||
|
|
}
|
||
|
|
return Result.ok({
|
||
|
|
name: name.data,
|
||
|
|
position: position.data,
|
||
|
|
email: email.data,
|
||
|
|
phone: phone.data,
|
||
|
|
active: dto.active,
|
||
|
|
});
|
||
|
|
}
|
||
|
|
site(dto: SiteRequestDTO) {
|
||
|
|
const name = Name.create(dto.name, 100);
|
||
|
|
if (!name.isSuccess) {
|
||
|
|
return Result.fail(new CompanyError("INVALID_REQUEST"));
|
||
|
|
}
|
||
|
|
if (dto.address === null || dto.address === undefined || !dto.address.trim()) {
|
||
|
|
return Result.ok({ name: name.data, address: Maybe.none<SiteAddress>() });
|
||
|
|
}
|
||
|
|
const address = SiteAddress.create(dto.address);
|
||
|
|
return address.isSuccess
|
||
|
|
? Result.ok({ name: name.data, address: Maybe.some(address.data) })
|
||
|
|
: Result.fail(new CompanyError("INVALID_REQUEST"));
|
||
|
|
}
|
||
|
|
}
|