From d1075d3f3b0f7c7e1fb9fcbcc5c3e2bace8e353b Mon Sep 17 00:00:00 2001 From: dev4 Date: Wed, 17 Sep 2025 15:48:08 +0000 Subject: [PATCH] =?UTF-8?q?feat(idnot):=20statuts=20pr=C3=A9cis=20auth=20?= =?UTF-8?q?=E2=80=94=20401=20email=20manquant,=20403=20non-rattachement=20?= =?UTF-8?q?office?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controllers/idnot.controller.ts | 46 ++++++++++++++--------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/src/controllers/idnot.controller.ts b/src/controllers/idnot.controller.ts index a6611e5..ff84d8b 100644 --- a/src/controllers/idnot.controller.ts +++ b/src/controllers/idnot.controller.ts @@ -4,14 +4,14 @@ import { IdNotService } from '../services/idnot'; import { authTokens } from '../utils/auth-tokens'; import { IdNotUser, AuthToken } from '../types'; import { Logger } from '../utils/logger'; -import { NotFoundError, ExternalServiceError, BusinessRuleError } from '../types/errors'; +import { NotFoundError, ExternalServiceError, BusinessRuleError, UnauthorizedError, ForbiddenError } from '../types/errors'; /** * Pure controller methods that handle business logic * without depending on Express Request/Response objects */ export class IdNotController { - + /** * Get user rattachements by idNot */ @@ -51,9 +51,9 @@ export class IdNotController { } })); - Logger.info('Successfully retrieved user rattachements', { + Logger.info('Successfully retrieved user rattachements', { idNot, - count: officeData.length + count: officeData.length }); return officeData; @@ -67,12 +67,12 @@ export class IdNotController { try { const result = await IdNotService.getOfficeRattachements(idNot); - - Logger.info('Successfully retrieved office rattachements', { - idNot, - count: result.result?.length || 0 + + Logger.info('Successfully retrieved office rattachements', { + idNot, + count: result.result?.length || 0 }); - + return result; } catch (error) { Logger.error('Failed to get office rattachements', { @@ -103,14 +103,14 @@ export class IdNotController { // Get user data const userData = await IdNotService.getUserData(payload.profile_idn); - + if (!userData || !userData.statutDuRattachement || userData.entite.typeEntite.name !== 'office') { - throw new BusinessRuleError('User not attached to an office'); + throw new ForbiddenError('User not attached to an office'); } // Get office location data const officeLocationData = await IdNotService.getOfficeLocationData(userData.entite.locationsUrl); - + if (!officeLocationData || !officeLocationData.result || officeLocationData.result.length === 0) { throw new BusinessRuleError('Office location data not found'); } @@ -143,7 +143,7 @@ export class IdNotController { }; if (!idNotUser.contact.email) { - throw new BusinessRuleError('User professional email is empty'); + throw new UnauthorizedError('Email not found'); } // Generate auth token @@ -157,10 +157,10 @@ export class IdNotController { createdAt: Date.now(), expiresAt: Date.now() + (24 * 60 * 60 * 1000) // 24 hours }; - + authTokens.push(tokenData); - Logger.info('IdNot authentication successful', { + Logger.info('IdNot authentication successful', { idNot: idNotUser.idNot, office: idNotUser.office.name }); @@ -172,11 +172,11 @@ export class IdNotController { codePrefix: code.substring(0, 8) + '...', error: error instanceof Error ? error.message : 'Unknown error' }); - + if (error instanceof BusinessRuleError || error instanceof ExternalServiceError) { throw error; } - + throw new ExternalServiceError('IdNot', `Authentication failed: ${error instanceof Error ? error.message : 'Unknown error'}`); } } @@ -189,12 +189,12 @@ export class IdNotController { // Find the full token data const userAuth = authTokens.find(auth => auth.authToken === authToken); - + if (!userAuth || !userAuth.idNotUser) { throw new NotFoundError('User data not found. Please log in again.'); } - Logger.info('Current user data retrieved', { + Logger.info('Current user data retrieved', { idNot, office: userAuth.idNotUser.office.name }); @@ -213,16 +213,16 @@ export class IdNotController { // Remove the auth token from the array const tokenIndex = authTokens.findIndex(auth => auth.authToken === authToken); - + if (tokenIndex > -1) { const removedToken = authTokens.splice(tokenIndex, 1)[0]; - Logger.info('User logout successful', { - idNot: removedToken.idNot + Logger.info('User logout successful', { + idNot: removedToken.idNot }); } else { Logger.warn('Logout attempted with invalid token'); } - + return { success: true, message: 'Déconnexion réussie'