register.ts (1338B)
1 import { EntityLocked, Repository } from "../repository_error.ts"; 2 import { Auth } from "#core/domain/auth.ts"; 3 import { Email, InvalidEmail } from "#core/domain/email.ts"; 4 import { Password, PasswordMismatch } from "#core/domain/password.ts"; 5 6 export type AuthRegisterRequest = { 7 email: string; 8 password: string; 9 passwordConfirmation: string; 10 }; 11 12 export type AuthRegisterResponse = { 13 status: "registered" | "invalid" | "conflict"; 14 uuid: string | null; 15 }; 16 17 export class AuthRegisterUseCase { 18 constructor(private readonly repo: Repository<Auth>) { 19 } 20 21 async execute(request: AuthRegisterRequest): Promise<AuthRegisterResponse> { 22 try { 23 const email = new Email(request.email); 24 const password = Password.hash(request.password); 25 const auth = Auth.register( 26 email, 27 password, 28 request.passwordConfirmation, 29 ); 30 await this.repo.store(auth); 31 return { 32 status: "registered", 33 uuid: auth.id.toString(), 34 }; 35 } catch (error) { 36 if ( 37 error instanceof InvalidEmail || 38 error instanceof PasswordMismatch 39 ) { 40 return { 41 status: "invalid", 42 uuid: null, 43 }; 44 } 45 if (error instanceof EntityLocked) { 46 return { status: "conflict", uuid: null }; 47 } 48 throw error; 49 } 50 } 51 }