Presupuestador_web/server/src/contexts/sales/infrastructure/mappers/dealer.mapper.ts
2024-05-21 18:48:40 +02:00

56 lines
1.6 KiB
TypeScript

import {
ISequelizeMapper,
MapperParamsType,
SequelizeMapper,
} from "@/contexts/common/infrastructure";
import { Name, UniqueID } from "@shared/contexts";
import { Dealer, IDealerProps } from "../../domain/entities";
import { ISalesContext } from "../Sales.context";
import { DealerCreationAttributes, DealerStatus, Dealer_Model } from "../sequelize";
export interface IDealerMapper
extends ISequelizeMapper<Dealer_Model, DealerCreationAttributes, Dealer> {}
class DealerMapper
extends SequelizeMapper<Dealer_Model, DealerCreationAttributes, Dealer>
implements IDealerMapper
{
public constructor(props: { context: ISalesContext }) {
super(props);
}
protected toDomainMappingImpl(source: Dealer_Model, params: any): Dealer {
const props: IDealerProps = {
name: this.mapsValue(source, "name", Name.create),
};
const id = this.mapsValue(source, "id", UniqueID.create);
const userOrError = Dealer.create(props, id);
if (userOrError.isFailure) {
throw userOrError.error;
}
return userOrError.object;
}
protected toPersistenceMappingImpl(source: Dealer, params?: MapperParamsType | undefined) {
return {
id: source.id.toPrimitive(),
id_contact: undefined,
name: source.name.toPrimitive(),
contact_information: "",
default_payment_method: "",
default_notes: "",
default_legal_terms: "",
default_quote_validity: "",
status: DealerStatus.ACTIVE,
};
}
}
export const createDealerMapper = (context: ISalesContext): IDealerMapper =>
new DealerMapper({
context,
});