-
Notifications
You must be signed in to change notification settings - Fork 4
feat(AUTH, USER): ajouter la fonctionnalité de suppression de compte utilisateur #71
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -55,3 +55,52 @@ export async function createUserProfile(payload: CreateProfileDTO): Promise<User | |
| throw new ServiceError(APP_ERRORS.SERVICE_UNAVAILABLE, { originalError: error }); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Supprime un profil utilisateur via le service users | ||
| */ | ||
| export async function deleteUserProfile(userId: number): Promise<void> { | ||
| try { | ||
| logger.info({ msg: `calling DELETE ${UM_SERVICE_URL}/users/${userId}` }); | ||
|
|
||
| // Configuration de la requête avec l'agent mTLS | ||
| const init: MTLSRequestInit = { | ||
| method: 'DELETE', | ||
| headers: { | ||
| 'x-user-id': String(userId), | ||
| }, | ||
| dispatcher: mtlsAgent, | ||
| }; | ||
|
|
||
| const response = await fetch(`${UM_SERVICE_URL}/users/${userId}`, init); | ||
rom98759 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| if (!response.ok) { | ||
| const errorText = await response.text(); | ||
|
|
||
| let parsedMessage = 'Failed to delete user profile'; | ||
| try { | ||
rom98759 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const parsed = JSON.parse(errorText); | ||
| parsedMessage = parsed?.message || parsedMessage; | ||
| } catch { | ||
| // keep fallback | ||
| } | ||
|
|
||
| throw new ServiceError( | ||
| { | ||
| code: ERROR_CODES.INTERNAL_ERROR, | ||
| event: EVENTS.DEPENDENCY.FAIL, | ||
| statusCode: response.status, | ||
| reason: REASONS.NETWORK.UPSTREAM_ERROR, | ||
| message: parsedMessage, | ||
| }, | ||
| { originalError: { status: response.status, body: errorText } }, | ||
| ); | ||
| } | ||
|
|
||
| logger.info({ msg: `user profile deleted successfully`, userId }); | ||
| } catch (error) { | ||
| logger.error({ msg: `error DELETE ${UM_SERVICE_URL}/users/${userId}`, error: error }); | ||
| if (error instanceof ServiceError) throw error; | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. on peut utiliser AppError : c'était trop de distinguer les DataError et ServiceError |
||
| throw new ServiceError(APP_ERRORS.SERVICE_UNAVAILABLE, { originalError: error }); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -244,3 +244,32 @@ export async function closeRedis(): Promise<void> { | |
| logger.info({ event: 'redis_connection_closed' }); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Supprime toutes les données Redis d'un utilisateur | ||
| */ | ||
| export async function removeUserFromRedis(userId: number): Promise<void> { | ||
| try { | ||
| const client = getRedisClient(); | ||
| const userKey = `${ONLINE_KEY_PREFIX}${userId}`; | ||
|
|
||
| // Supp user online key | ||
| await client.del(userKey); | ||
|
|
||
| // Supp user du set des utilisateurs en ligne | ||
| await client.srem(ONLINE_USERS_SET, userId.toString()); | ||
|
|
||
| logger.info({ | ||
| event: 'user_redis_cleanup', | ||
| userId, | ||
| message: 'User data removed from Redis', | ||
| }); | ||
| } catch (error) { | ||
| logger.error({ | ||
| event: 'user_redis_cleanup_error', | ||
| userId, | ||
| error: (error as Error)?.message, | ||
| }); | ||
| throw error; | ||
| } | ||
|
Comment on lines
+251
to
+274
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -73,6 +73,20 @@ const deleteProfileSchema = { | |
| }, | ||
| } as const; | ||
|
|
||
| const deleteProfileByIdSchema = { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
| tags: ['users'], | ||
| summary: 'Delete a profile by user ID', | ||
| description: 'Delete a profile by user ID (internal service use)', | ||
| params: z.object({ | ||
| userId: z.coerce.number(), | ||
| }), | ||
| response: { | ||
| 204: z.void(), | ||
| 400: ValidationErrorSchema, | ||
| 404: DetailedErrorSchema, | ||
| }, | ||
| } as const; | ||
|
|
||
| export const umRoutes: FastifyPluginAsyncZod = async (app) => { | ||
| app.get( | ||
| '/health', | ||
|
|
@@ -104,4 +118,10 @@ export const umRoutes: FastifyPluginAsyncZod = async (app) => { | |
| { schema: deleteProfileSchema }, | ||
| profileController.deleteProfile, | ||
| ); | ||
|
|
||
| app.delete( | ||
| '/users/:userId', | ||
| { schema: deleteProfileByIdSchema }, | ||
| profileController.deleteProfileById, | ||
| ); | ||
rom98759 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }; | ||
Uh oh!
There was an error while loading. Please reload this page.