refactor use cases

This commit is contained in:
2026-01-08 15:34:51 +01:00
parent d984ab24a8
commit 52e9a2f6a7
362 changed files with 5192 additions and 8409 deletions

View File

@@ -1,5 +1,6 @@
import { Provider } from '@nestjs/common';
import type { Logger } from '@core/shared/application';
import { CookieIdentitySessionAdapter } from '@adapters/identity/session/CookieIdentitySessionAdapter';
import { LoginUseCase } from '@core/identity/application/use-cases/LoginUseCase';
import { LogoutUseCase } from '@core/identity/application/use-cases/LogoutUseCase';
@@ -13,13 +14,6 @@ import type { ICompanyRepository } from '@core/identity/domain/repositories/ICom
import type { IMagicLinkRepository } from '@core/identity/domain/repositories/IMagicLinkRepository';
import type { IPasswordHashingService } from '@core/identity/domain/services/PasswordHashingService';
import type { IMagicLinkNotificationPort } from '@core/identity/domain/ports/IMagicLinkNotificationPort';
import type { LoginResult } from '@core/identity/application/use-cases/LoginUseCase';
import type { LogoutResult } from '@core/identity/application/use-cases/LogoutUseCase';
import type { SignupResult } from '@core/identity/application/use-cases/SignupUseCase';
import type { SignupSponsorResult } from '@core/identity/application/use-cases/SignupSponsorUseCase';
import type { ForgotPasswordResult } from '@core/identity/application/use-cases/ForgotPasswordUseCase';
import type { ResetPasswordResult } from '@core/identity/application/use-cases/ResetPasswordUseCase';
import type { Logger, UseCaseOutputPort } from '@core/shared/application';
import {
AUTH_REPOSITORY_TOKEN,
@@ -75,9 +69,8 @@ export const AuthProviders: Provider[] = [
authRepo: IAuthRepository,
passwordHashing: IPasswordHashingService,
logger: Logger,
output: UseCaseOutputPort<LoginResult>,
) => new LoginUseCase(authRepo, passwordHashing, logger, output),
inject: [AUTH_REPOSITORY_TOKEN, PASSWORD_HASHING_SERVICE_TOKEN, LOGGER_TOKEN, AUTH_SESSION_OUTPUT_PORT_TOKEN],
) => new LoginUseCase(authRepo, passwordHashing, logger),
inject: [AUTH_REPOSITORY_TOKEN, PASSWORD_HASHING_SERVICE_TOKEN, LOGGER_TOKEN],
},
{
provide: SIGNUP_USE_CASE_TOKEN,
@@ -85,9 +78,8 @@ export const AuthProviders: Provider[] = [
authRepo: IAuthRepository,
passwordHashing: IPasswordHashingService,
logger: Logger,
output: UseCaseOutputPort<SignupResult>,
) => new SignupUseCase(authRepo, passwordHashing, logger, output),
inject: [AUTH_REPOSITORY_TOKEN, PASSWORD_HASHING_SERVICE_TOKEN, LOGGER_TOKEN, AUTH_SESSION_OUTPUT_PORT_TOKEN],
) => new SignupUseCase(authRepo, passwordHashing, logger),
inject: [AUTH_REPOSITORY_TOKEN, PASSWORD_HASHING_SERVICE_TOKEN, LOGGER_TOKEN],
},
{
provide: SIGNUP_SPONSOR_USE_CASE_TOKEN,
@@ -96,15 +88,14 @@ export const AuthProviders: Provider[] = [
companyRepo: ICompanyRepository,
passwordHashing: IPasswordHashingService,
logger: Logger,
output: UseCaseOutputPort<SignupSponsorResult>,
) => new SignupSponsorUseCase(authRepo, companyRepo, passwordHashing, logger, output),
inject: [AUTH_REPOSITORY_TOKEN, COMPANY_REPOSITORY_TOKEN, PASSWORD_HASHING_SERVICE_TOKEN, LOGGER_TOKEN, SIGNUP_SPONSOR_OUTPUT_PORT_TOKEN],
) => new SignupSponsorUseCase(authRepo, companyRepo, passwordHashing, logger),
inject: [AUTH_REPOSITORY_TOKEN, COMPANY_REPOSITORY_TOKEN, PASSWORD_HASHING_SERVICE_TOKEN, LOGGER_TOKEN],
},
{
provide: LOGOUT_USE_CASE_TOKEN,
useFactory: (sessionPort: IdentitySessionPort, logger: Logger, output: UseCaseOutputPort<LogoutResult>) =>
new LogoutUseCase(sessionPort, logger, output),
inject: [IDENTITY_SESSION_PORT_TOKEN, LOGGER_TOKEN, COMMAND_RESULT_OUTPUT_PORT_TOKEN],
useFactory: (sessionPort: IdentitySessionPort, logger: Logger) =>
new LogoutUseCase(sessionPort, logger),
inject: [IDENTITY_SESSION_PORT_TOKEN, LOGGER_TOKEN],
},
ForgotPasswordPresenter,
ResetPasswordPresenter,
@@ -132,9 +123,8 @@ export const AuthProviders: Provider[] = [
magicLinkRepo: IMagicLinkRepository,
notificationPort: IMagicLinkNotificationPort,
logger: Logger,
output: UseCaseOutputPort<ForgotPasswordResult>,
) => new ForgotPasswordUseCase(authRepo, magicLinkRepo, notificationPort, logger, output),
inject: [AUTH_REPOSITORY_TOKEN, MAGIC_LINK_REPOSITORY_TOKEN, MAGIC_LINK_NOTIFICATION_PORT_TOKEN, LOGGER_TOKEN, FORGOT_PASSWORD_OUTPUT_PORT_TOKEN],
) => new ForgotPasswordUseCase(authRepo, magicLinkRepo, notificationPort, logger),
inject: [AUTH_REPOSITORY_TOKEN, MAGIC_LINK_REPOSITORY_TOKEN, MAGIC_LINK_NOTIFICATION_PORT_TOKEN, LOGGER_TOKEN],
},
{
provide: RESET_PASSWORD_USE_CASE_TOKEN,
@@ -143,8 +133,7 @@ export const AuthProviders: Provider[] = [
magicLinkRepo: IMagicLinkRepository,
passwordHashing: IPasswordHashingService,
logger: Logger,
output: UseCaseOutputPort<ResetPasswordResult>,
) => new ResetPasswordUseCase(authRepo, magicLinkRepo, passwordHashing, logger, output),
inject: [AUTH_REPOSITORY_TOKEN, MAGIC_LINK_REPOSITORY_TOKEN, PASSWORD_HASHING_SERVICE_TOKEN, LOGGER_TOKEN, RESET_PASSWORD_OUTPUT_PORT_TOKEN],
) => new ResetPasswordUseCase(authRepo, magicLinkRepo, passwordHashing, logger),
inject: [AUTH_REPOSITORY_TOKEN, MAGIC_LINK_REPOSITORY_TOKEN, PASSWORD_HASHING_SERVICE_TOKEN, LOGGER_TOKEN],
},
];

View File

@@ -87,8 +87,7 @@ describe('AuthService', () => {
const signupUseCase = {
execute: vi.fn(async () => {
authSessionPresenter.present({ userId: 'u2', email: 'e2', displayName: 'Jane Smith' });
return Result.ok(undefined);
return Result.ok({ userId: 'u2', email: 'e2', displayName: 'Jane Smith' });
}),
};
@@ -156,8 +155,7 @@ describe('AuthService', () => {
const loginUseCase = {
execute: vi.fn(async () => {
authSessionPresenter.present({ userId: 'u3', email: 'e3', displayName: 'Bob Wilson' });
return Result.ok(undefined);
return Result.ok({ userId: 'u3', email: 'e3', displayName: 'Bob Wilson' });
}),
};
@@ -234,8 +232,7 @@ describe('AuthService', () => {
const commandResultPresenter = new FakeCommandResultPresenter();
const logoutUseCase = {
execute: vi.fn(async () => {
commandResultPresenter.present({ success: true });
return Result.ok(undefined);
return Result.ok({ success: true });
}),
};

View File

@@ -116,8 +116,6 @@ export class AuthService {
async signupWithEmail(params: SignupParamsDTO): Promise<AuthSessionDTO> {
this.logger.debug(`[AuthService] Attempting signup for email: ${params.email}`);
this.authSessionPresenter.reset();
const input: SignupInput = {
email: params.email,
password: params.password,
@@ -131,6 +129,9 @@ export class AuthService {
throw new Error(mapApplicationErrorToMessage(error, 'Signup failed'));
}
const signupResult = result.unwrap();
this.authSessionPresenter.present(signupResult);
const userDTO = this.authSessionPresenter.responseModel;
const inferredRole = inferDemoRoleFromEmail(userDTO.email);
const session = await this.identitySessionPort.createSession({
@@ -149,8 +150,6 @@ export class AuthService {
async signupSponsor(params: SignupSponsorParamsDTO): Promise<AuthSessionDTO> {
this.logger.debug(`[AuthService] Attempting sponsor signup for email: ${params.email}`);
this.authSessionPresenter.reset();
const input: SignupSponsorInput = {
email: params.email,
password: params.password,
@@ -165,6 +164,9 @@ export class AuthService {
throw new Error(mapApplicationErrorToMessage(error, 'Sponsor signup failed'));
}
const signupResult = result.unwrap();
this.authSessionPresenter.present(signupResult);
const userDTO = this.authSessionPresenter.responseModel;
const inferredRole = inferDemoRoleFromEmail(userDTO.email);
const session = await this.identitySessionPort.createSession({
@@ -183,8 +185,6 @@ export class AuthService {
async loginWithEmail(params: LoginParamsDTO): Promise<AuthSessionDTO> {
this.logger.debug(`[AuthService] Attempting login for email: ${params.email}`);
this.authSessionPresenter.reset();
const input: LoginInput = {
email: params.email,
password: params.password,
@@ -197,6 +197,9 @@ export class AuthService {
throw new Error(mapApplicationErrorToMessage(error, 'Login failed'));
}
const loginResult = result.unwrap();
this.authSessionPresenter.present(loginResult);
const userDTO = this.authSessionPresenter.responseModel;
const sessionOptions = params.rememberMe !== undefined
? { rememberMe: params.rememberMe }
@@ -223,8 +226,6 @@ export class AuthService {
async logout(): Promise<CommandResultDTO> {
this.logger.debug('[AuthService] Attempting logout.');
this.commandResultPresenter.reset();
const result = await this.logoutUseCase.execute();
if (result.isErr()) {
@@ -232,6 +233,9 @@ export class AuthService {
throw new Error(mapApplicationErrorToMessage(error, 'Logout failed'));
}
const logoutResult = result.unwrap();
this.commandResultPresenter.present(logoutResult);
return this.commandResultPresenter.responseModel;
}
@@ -285,8 +289,6 @@ export class AuthService {
async forgotPassword(params: { email: string }): Promise<{ message: string; magicLink?: string }> {
this.logger.debug(`[AuthService] Attempting forgot password for email: ${params.email}`);
this.forgotPasswordPresenter.reset();
const input: ForgotPasswordInput = {
email: params.email,
};
@@ -298,6 +300,9 @@ export class AuthService {
throw new Error(mapApplicationErrorToMessage(error, 'Forgot password failed'));
}
const forgotPasswordResult = executeResult.unwrap();
this.forgotPasswordPresenter.present(forgotPasswordResult);
const response = this.forgotPasswordPresenter.responseModel;
const result: { message: string; magicLink?: string } = {
message: response.message,
@@ -311,8 +316,6 @@ export class AuthService {
async resetPassword(params: { token: string; newPassword: string }): Promise<{ message: string }> {
this.logger.debug('[AuthService] Attempting reset password');
this.resetPasswordPresenter.reset();
const input: ResetPasswordInput = {
token: params.token,
newPassword: params.newPassword,
@@ -325,6 +328,9 @@ export class AuthService {
throw new Error(mapApplicationErrorToMessage(error, 'Reset password failed'));
}
const resetResult = result.unwrap();
this.resetPasswordPresenter.present(resetResult);
return this.resetPasswordPresenter.responseModel;
}
}