2025-02-20 18:55:24 +00:00
|
|
|
import { EmailAddress, PhoneNumber, PostalAddress, TINNumber, UniqueID } from "@common/domain";
|
|
|
|
|
import { Maybe, Result } from "@common/helpers";
|
|
|
|
|
import {
|
|
|
|
|
ISequelizeMapper,
|
|
|
|
|
MapperParamsType,
|
|
|
|
|
SequelizeMapper,
|
|
|
|
|
} from "@common/infrastructure/sequelize/sequelize-mapper";
|
|
|
|
|
import { Company } from "@contexts/companies/domain/aggregates/company";
|
|
|
|
|
import { CompanyCreationAttributes, CompanyModel } from "../sequelize/company.model";
|
|
|
|
|
|
|
|
|
|
export interface ICompanyMapper
|
|
|
|
|
extends ISequelizeMapper<CompanyModel, CompanyCreationAttributes, Company> {}
|
|
|
|
|
|
|
|
|
|
export class CompanyMapper
|
|
|
|
|
extends SequelizeMapper<CompanyModel, CompanyCreationAttributes, Company>
|
|
|
|
|
implements ICompanyMapper
|
|
|
|
|
{
|
|
|
|
|
public mapToDomain(source: CompanyModel, params?: MapperParamsType): Result<Company, Error> {
|
|
|
|
|
const idOrError = UniqueID.create(source.id);
|
|
|
|
|
const tinOrError = TINNumber.create(source.tin);
|
|
|
|
|
const emailOrError = EmailAddress.create(source.email);
|
|
|
|
|
const phoneOrError = PhoneNumber.create(source.phone);
|
|
|
|
|
const faxOrError = PhoneNumber.createNullable(source.fax);
|
|
|
|
|
const postalAddressOrError = PostalAddress.create({
|
|
|
|
|
street: source.street,
|
|
|
|
|
city: source.city,
|
|
|
|
|
state: source.state,
|
|
|
|
|
postalCode: source.postal_code,
|
|
|
|
|
country: source.country,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const result = Result.combine([
|
|
|
|
|
idOrError,
|
|
|
|
|
tinOrError,
|
|
|
|
|
emailOrError,
|
|
|
|
|
phoneOrError,
|
|
|
|
|
faxOrError,
|
|
|
|
|
postalAddressOrError,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
if (result.isFailure) {
|
|
|
|
|
return Result.fail(result.error);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Company.create(
|
|
|
|
|
{
|
|
|
|
|
isFreelancer: source.is_freelancer,
|
|
|
|
|
name: source.name,
|
|
|
|
|
tradeName: source.trade_name ? Maybe.Some(source.trade_name) : Maybe.None(),
|
|
|
|
|
tin: tinOrError.data,
|
|
|
|
|
address: postalAddressOrError.data,
|
|
|
|
|
email: emailOrError.data,
|
|
|
|
|
phone: phoneOrError.data,
|
|
|
|
|
fax: faxOrError.data,
|
|
|
|
|
website: source.website ? Maybe.Some(source.website) : Maybe.None(),
|
|
|
|
|
legalRecord: source.legal_record,
|
|
|
|
|
defaultTax: source.default_tax,
|
|
|
|
|
status: source.status,
|
|
|
|
|
langCode: source.lang_code,
|
|
|
|
|
currencyCode: source.currency_code,
|
|
|
|
|
logo: source.logo ? Maybe.Some(source.logo) : Maybe.None(),
|
|
|
|
|
},
|
|
|
|
|
idOrError.data
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public mapToPersistence(
|
|
|
|
|
source: Company,
|
|
|
|
|
params?: MapperParamsType
|
|
|
|
|
): Result<CompanyCreationAttributes, Error> {
|
|
|
|
|
return Result.ok({
|
|
|
|
|
id: source.id.toString(),
|
|
|
|
|
is_freelancer: source.isFreelancer,
|
|
|
|
|
name: source.name,
|
2025-02-25 15:25:30 +00:00
|
|
|
trade_name: source.tradeName.getOrUndefined(),
|
2025-02-20 18:55:24 +00:00
|
|
|
tin: source.tin.toString(),
|
|
|
|
|
|
|
|
|
|
street: source.address.street,
|
|
|
|
|
city: source.address.city,
|
|
|
|
|
state: source.address.state,
|
|
|
|
|
postal_code: source.address.postalCode,
|
|
|
|
|
country: source.address.country,
|
|
|
|
|
|
|
|
|
|
email: source.email.toString(),
|
|
|
|
|
phone: source.phone.toString(),
|
2025-02-25 15:25:30 +00:00
|
|
|
fax: source.fax.isSome() ? source.fax.getOrUndefined()?.toString() : undefined,
|
|
|
|
|
website: source.website.getOrUndefined(),
|
2025-02-20 18:55:24 +00:00
|
|
|
|
|
|
|
|
legal_record: source.legalRecord,
|
|
|
|
|
default_tax: source.defaultTax,
|
|
|
|
|
status: source.isActive ? "active" : "inactive",
|
|
|
|
|
lang_code: source.langCode,
|
|
|
|
|
currency_code: source.currencyCode,
|
2025-02-25 15:25:30 +00:00
|
|
|
logo: source.logo.getOrUndefined(),
|
2025-02-20 18:55:24 +00:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const companyMapper: CompanyMapper = new CompanyMapper();
|
|
|
|
|
export { companyMapper };
|