59 lines
1.6 KiB
TypeScript
59 lines
1.6 KiB
TypeScript
import {
|
|
ISequelizeMapper,
|
|
SequelizeMapper,
|
|
} from "@/contexts/common/infrastructure";
|
|
import { Email, Name, Password, UniqueID } from "@shared/contexts";
|
|
import { IUserProps, User } from "../../domain";
|
|
import { IUserContext } from "../User.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: IUserContext }) {
|
|
super(props);
|
|
}
|
|
|
|
protected toDomainMappingImpl(source: User_Model, params: any): User {
|
|
const props: IUserProps = {
|
|
name: this.mapsValue(source, "name", Name.create),
|
|
email: this.mapsValue(source, "email", Email.create),
|
|
password: this.mapsValue(
|
|
source,
|
|
"password",
|
|
Password.createFromHashedText,
|
|
),
|
|
};
|
|
|
|
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(),
|
|
name: source.name.toPrimitive(),
|
|
email: source.email.toPrimitive(),
|
|
password: source.password.toPrimitive(),
|
|
};
|
|
}
|
|
}
|
|
|
|
export const createUserMapper = (context: IUserContext): IUserMapper =>
|
|
new UserMapper({
|
|
context,
|
|
});
|