Presupuestador_web/server/src/contexts/profile/infrastructure/mappers/profile.mapper.ts
2024-06-17 18:54:30 +02:00

58 lines
1.8 KiB
TypeScript

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<Profile_Model, ProfileCreationAttributes, Profile> {}
class ProfileMapper
extends SequelizeMapper<Profile_Model, ProfileCreationAttributes, Profile>
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,
});