import { ISequelizeMapper, SequelizeMapper, } from "@/contexts/common/infrastructure"; import { Email, Name, Password, UniqueID } from "@shared/contexts"; import { AuthUser, IAuthUserProps } from "../../domain/entities"; import { IAuthContext } from "../Auth.context"; import { AuthUser_Model, TCreationUser_Attributes, } from "../sequelize/authUser.model"; export interface IUserMapper extends ISequelizeMapper< AuthUser_Model, TCreationUser_Attributes, AuthUser > {} class UserMapper extends SequelizeMapper implements IUserMapper { public constructor(props: { context: IAuthContext }) { super(props); } protected toDomainMappingImpl(source: AuthUser_Model, params: any): AuthUser { const props: IAuthUserProps = { name: this.mapsValue(source, "name", Name.create), email: this.mapsValue(source, "email", Email.create), hashed_password: this.mapsValue( source, "password", Password.createFromHashedText, ), }; const id = this.mapsValue(source, "id", UniqueID.create); const userOrError = AuthUser.create(props, id); if (userOrError.isFailure) { throw userOrError.error; } return userOrError.object; } protected toPersistenceMappingImpl( source: AuthUser, params?: Record | undefined, ) { return { id: source.id.toPrimitive(), name: source.name.toPrimitive(), email: source.email.toPrimitive(), password: source.hashed_password, }; } } export const createUserMapper = (context: IAuthContext): IUserMapper => new UserMapper({ context, });