import { ISequelizeMapper, MapperParamsType, SequelizeMapper, } from "@/contexts/common/infrastructure"; import { UniqueID } from "@shared/contexts"; import { IProfileProps, Profile } from "../../domain"; import { IProfileContext } from "../Profile.context"; import { ProfileCreationAttributes, Profile_Model } from "../sequelize"; export interface IProfileMapper extends ISequelizeMapper {} class ProfileMapper extends SequelizeMapper implements IProfileMapper { public constructor(props: { context: IProfileContext }) { super(props); } protected toDomainMappingImpl(source: Profile_Model, params: any): Profile { const props: IProfileProps = { contactInformation: source.contact_information, defaultPaymentMethod: source.default_payment_method, defaultNotes: source.default_notes, defaultLegalTerms: source.default_legal_terms, defaultQuoteValidity: source.default_quote_validity, }; const id = this.mapsValue(source, "id", UniqueID.create); const userOrError = Profile.create(props, id); if (userOrError.isFailure) { throw userOrError.error; } return userOrError.object; } protected toPersistenceMappingImpl(source: Profile, params?: MapperParamsType | undefined) { return { id: source.id.toPrimitive(), contact_information: source.contactInformation, default_payment_method: source.defaultPaymentMethod, default_notes: source.defaultNotes, default_legal_terms: source.defaultLegalTerms, default_quote_validity: source.defaultQuoteValidity, }; } } export const createProfileMapper = (context: IProfileContext): IProfileMapper => new ProfileMapper({ context, });