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

55 lines
1.5 KiB
TypeScript
Raw Normal View History

2024-05-15 19:56:22 +00:00
import {
ISequelizeMapper,
SequelizeMapper,
} from "@/contexts/common/infrastructure";
2024-05-16 11:56:46 +00:00
import { Email, Name, UniqueID } from "@shared/contexts";
2024-05-15 19:56:22 +00:00
import { IUserProps, User } from "../../domain/entities";
import { IAuthContext } from "../Auth.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: IAuthContext }) {
super(props);
}
protected toDomainMappingImpl(source: User_Model, params: any): User {
const props: IUserProps = {
2024-05-16 11:56:46 +00:00
name: this.mapsValue(source, "name", Name.create),
2024-05-15 19:56:22 +00:00
email: this.mapsValue(source, "email", Email.create),
hashed_password: source.password,
};
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(),
2024-05-16 11:56:46 +00:00
name: source.name.toPrimitive(),
2024-05-15 19:56:22 +00:00
email: source.email.toPrimitive(),
password: source.hashed_password,
};
}
}
export const createUserMapper = (context: IAuthContext): IUserMapper =>
new UserMapper({
context,
});