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

67 lines
1.7 KiB
TypeScript
Raw Normal View History

2024-05-20 07:47:44 +00:00
import { Password } from "@/contexts/common/domain";
2024-05-15 19:56:22 +00:00
import {
ISequelizeMapper,
SequelizeMapper,
} from "@/contexts/common/infrastructure";
2024-05-20 07:47:44 +00:00
import { Email, Name, 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 {
2024-05-21 16:48:40 +00:00
AuthUserCreationAttributes,
2024-05-16 18:16:00 +00:00
AuthUser_Model,
} 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,
2024-05-21 16:48:40 +00:00
AuthUserCreationAttributes,
2024-05-16 18:16:00 +00:00
AuthUser
> {}
2024-05-15 19:56:22 +00:00
2024-05-20 07:36:28 +00:00
class AuthUserMapper
2024-05-21 16:48:40 +00:00
extends SequelizeMapper<AuthUser_Model, AuthUserCreationAttributes, 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-20 07:36:28 +00:00
password: this.mapsValue(
2024-05-19 22:04:23 +00:00
source,
"password",
2024-05-20 07:47:44 +00:00
Password.createFromHashedTextPassword,
2024-05-19 22:04:23 +00:00
),
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(),
2024-05-20 07:36:28 +00:00
password: source.password.toPrimitive(),
2024-05-15 19:56:22 +00:00
};
}
}
export const createUserMapper = (context: IAuthContext): IUserMapper =>
2024-05-20 07:36:28 +00:00
new AuthUserMapper({
2024-05-15 19:56:22 +00:00
context,
});