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

66 lines
1.6 KiB
TypeScript
Raw Normal View History

2024-05-15 19:56:22 +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 { AuthUser, IAuthUserProps } from "../../domain/entities";
2024-05-15 19:56:22 +00:00
import { IAuthContext } from "../Auth.context";
2024-05-16 18:16:00 +00:00
import {
AuthUser_Model,
TCreationUser_Attributes,
} from "../sequelize/authUser.model";
2024-05-15 19:56:22 +00:00
export interface IUserMapper
2024-05-16 18:16:00 +00:00
extends ISequelizeMapper<
AuthUser_Model,
TCreationUser_Attributes,
AuthUser
> {}
2024-05-15 19:56:22 +00:00
class UserMapper
2024-05-16 18:16:00 +00:00
extends SequelizeMapper<AuthUser_Model, TCreationUser_Attributes, AuthUser>
2024-05-15 19:56:22 +00:00
implements IUserMapper
{
public constructor(props: { context: IAuthContext }) {
super(props);
}
2024-05-16 18:16:00 +00:00
protected toDomainMappingImpl(source: AuthUser_Model, params: any): AuthUser {
const props: IAuthUserProps = {
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),
2024-05-19 22:04:23 +00:00
hashed_password: this.mapsValue(
source,
"password",
Password.createFromHashedText,
),
2024-05-15 19:56:22 +00:00
};
const id = this.mapsValue(source, "id", UniqueID.create);
2024-05-16 18:16:00 +00:00
const userOrError = AuthUser.create(props, id);
2024-05-15 19:56:22 +00:00
if (userOrError.isFailure) {
throw userOrError.error;
}
return userOrError.object;
}
protected toPersistenceMappingImpl(
2024-05-16 18:16:00 +00:00
source: AuthUser,
2024-05-15 19:56:22 +00:00
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,
});