Presupuestador_web/server/src/contexts/auth/infrastructure/mappers/user.mapper.ts
2024-05-20 00:04:23 +02:00

66 lines
1.6 KiB
TypeScript

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<AuthUser_Model, TCreationUser_Attributes, AuthUser>
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<string, any> | 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,
});