47 lines
1.5 KiB
TypeScript
47 lines
1.5 KiB
TypeScript
import { Result, UniqueID } from "@common/domain";
|
|
import { EmailAddress, PasswordHash, TabContext, Username } from "@contexts/auth/domain";
|
|
import { ITabContextMapper } from "./tab-context-mapper.interface";
|
|
|
|
export class TabContextMapper implements ITabContextMapper {
|
|
toDomain(entity: any): Result<TabContext, Error> {
|
|
if (!entity) {
|
|
return Result.fail(new Error("Entity not found"));
|
|
}
|
|
|
|
// Crear Value Objects asegurando que sean válidos
|
|
const uniqueIdResult = UniqueID.create(entity.id);
|
|
const userIdResult = Username.create(entity.user_id);
|
|
const companyIdResult = PasswordHash.create(entity.passwordHash);
|
|
const brachIdResult = EmailAddress.create(entity.email);
|
|
|
|
// Validar que no haya errores en la creación de los Value Objects
|
|
const okOrError = Result.combine([
|
|
uniqueIdResult,
|
|
usernameResult,
|
|
companyIdResult,
|
|
emailResult,
|
|
]);
|
|
if (okOrError.isFailure) {
|
|
return Result.fail(okOrError.error.message);
|
|
}
|
|
|
|
// Crear el agregado de dominio
|
|
return TabContext.create(
|
|
{
|
|
username: usernameResult.data!,
|
|
email: emailResult.data!,
|
|
passwordHash: companyIdResult.data!,
|
|
roles: entity.roles || [],
|
|
token: entity.token,
|
|
},
|
|
uniqueIdResult.data!
|
|
);
|
|
}
|
|
|
|
toPersistence(tabContext: TabContext): any {
|
|
return tabContext.toPersistenceData();
|
|
}
|
|
}
|
|
|
|
export const createTabContextMapper = (): ITabContextMapper => new TabContextMapper();
|