Presupuestador_web/server/src/contexts/users/infrastructure/mappers/user.mapper.ts

59 lines
1.6 KiB
TypeScript
Raw Normal View History

2024-05-16 18:16:00 +00:00
import {
ISequelizeMapper,
SequelizeMapper,
} from "@/contexts/common/infrastructure";
2024-05-19 22:04:23 +00:00
import { Email, Name, Password, UniqueID } from "@shared/contexts";
2024-05-16 18:16:00 +00:00
import { IUserProps, User } from "../../domain";
import { IUserContext } from "../User.context";
import { TCreationUser_Attributes, User_Model } from "../sequelize/user.model";
export interface IUserMapper
extends ISequelizeMapper<User_Model, TCreationUser_Attributes, User> {}
class UserMapper
extends SequelizeMapper<User_Model, TCreationUser_Attributes, User>
implements IUserMapper
{
public constructor(props: { context: IUserContext }) {
super(props);
}
protected toDomainMappingImpl(source: User_Model, params: any): User {
const props: IUserProps = {
name: this.mapsValue(source, "name", Name.create),
email: this.mapsValue(source, "email", Email.create),
2024-05-19 22:04:23 +00:00
password: this.mapsValue(
source,
"password",
Password.createFromHashedText,
),
2024-05-16 18:16:00 +00:00
};
const id = this.mapsValue(source, "id", UniqueID.create);
const userOrError = User.create(props, id);
if (userOrError.isFailure) {
throw userOrError.error;
}
return userOrError.object;
}
protected toPersistenceMappingImpl(
source: User,
params?: Record<string, any> | undefined,
) {
return {
id: source.id.toPrimitive(),
name: source.name.toPrimitive(),
email: source.email.toPrimitive(),
2024-05-20 07:36:28 +00:00
password: source.password.toPrimitive(),
2024-05-16 18:16:00 +00:00
};
}
}
export const createUserMapper = (context: IUserContext): IUserMapper =>
new UserMapper({
context,
});