Presupuestador_web/server/src/contexts/sales/infrastructure/mappers/dealer.mapper.ts

56 lines
1.6 KiB
TypeScript
Raw Normal View History

2024-05-21 16:48:40 +00:00
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";
2024-05-27 09:55:04 +00:00
import { DEALER_STATUS, DealerCreationAttributes, Dealer_Model } from "../sequelize";
2024-05-21 16:48:40 +00:00
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: "",
2024-05-27 09:55:04 +00:00
status: DEALER_STATUS.STATUS_ACTIVE,
2024-05-21 16:48:40 +00:00
};
}
}
export const createDealerMapper = (context: ISalesContext): IDealerMapper =>
new DealerMapper({
context,
});