diff --git a/packages/user/src/model/users/graphql/resolver.ts b/packages/user/src/model/users/graphql/resolver.ts index 7e0af7d03..51243a13e 100644 --- a/packages/user/src/model/users/graphql/resolver.ts +++ b/packages/user/src/model/users/graphql/resolver.ts @@ -306,6 +306,57 @@ const Mutation = { return mercuriusError; } }, + removePhoto: async ( + parent: unknown, + arguments_: undefined, + context: MercuriusContext, + ) => { + const { app, config, database, dbSchema, reply, user } = context; + + const service = getUserService(config, database, dbSchema); + + if (!user) { + return new mercurius.ErrorWithProps("unauthorized", {}, 401); + } + + try { + // eslint-disable-next-line unicorn/no-null + const updatedUser = await service.update(user.id, { photoId: null }); + + if (user.photoId) { + await service.fileService.delete(user.photoId); + } + + const request = reply.request; + + request.user = updatedUser; + + if (request.config.user.features?.profileValidation?.enabled) { + await request.session?.fetchAndSetClaim( + new ProfileValidationClaim(), + createUserContext(undefined, request), + ); + } + + if (request.config.user.features?.signUp?.emailVerification) { + await request.session?.fetchAndSetClaim( + EmailVerificationClaim, + createUserContext(undefined, request), + ); + } + + return updatedUser; + } catch (error) { + app.log.error(error); + + const mercuriusError = new mercurius.ErrorWithProps( + "Oops, Something went wrong", + ); + mercuriusError.statusCode = 500; + + return mercuriusError; + } + }, changeEmail: async ( parent: unknown, arguments_: { diff --git a/packages/user/src/model/users/graphql/schema.ts b/packages/user/src/model/users/graphql/schema.ts index a8068efb2..53ce4d5ff 100644 --- a/packages/user/src/model/users/graphql/schema.ts +++ b/packages/user/src/model/users/graphql/schema.ts @@ -75,6 +75,7 @@ const user = gql` @auth(profileValidation: false, emailVerification: false) changeEmail(email: String!): ChangeEmailResponse @auth(profileValidation: false, emailVerification: false) + removePhoto: User @auth(profileValidation: false, emailVerification: false) } type Query {