Skip to content

Commit

Permalink
feat: add endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
lamdiki-sherpa committed Jan 2, 2025
1 parent 796ca83 commit bc514ce
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
86 changes: 86 additions & 0 deletions packages/user/src/model/users/resolver.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import mercurius from "mercurius";
import { EmailVerificationClaim } from "supertokens-node/recipe/emailverification";
import { createNewSession } from "supertokens-node/recipe/session";
import { emailPasswordSignUp } from "supertokens-node/recipe/thirdpartyemailpassword";
import UserRoles from "supertokens-node/recipe/userroles";
Expand Down Expand Up @@ -264,6 +265,91 @@ const Mutation = {
);
mercuriusError.statusCode = 500;

return mercuriusError;
}
},
changeEmail: async (
parent: unknown,
arguments_: {
email: string;
},
context: MercuriusContext,
) => {
const { app, config, database, dbSchema, user } = context;

try {
if (user) {
if (config.user.features?.updateEmail?.enabled === false) {
return {
status: "EMAIL_FEATURE_DISABLED_ERROR",
message: "Update email feature is currently disabled.",
};
}

const request = context.reply.request;

if (config.user.features?.profileValidation?.enabled) {
await request.session?.fetchAndSetClaim(
new ProfileValidationClaim(),
createUserContext(undefined, request),
);
}

if (config.user.features?.signUp?.emailVerification) {
await request.session?.fetchAndSetClaim(
EmailVerificationClaim,
createUserContext(undefined, request),
);
}

const emailValidationResult = validateEmail(arguments_.email, config);

if (!emailValidationResult.success) {
return {
status: "EMAIL_INVALID_ERROR",
message: emailValidationResult.message,
};
}

if (user.email === arguments_.email) {
return {
status: "EMAIL_SAME_AS_CURRENT_ERROR",
message: "Email is same as the current one.",
};
}

const service = getUserService(config, database, dbSchema);

const response = await service.changeEmail(user.id, arguments_.email);

request.user = response;

return {
status: "OK",
message: "Email updated successfully.",
};
} else {
return {
status: "NOT_FOUND",
message: "User not found",
};
}
/*eslint-disable-next-line @typescript-eslint/no-explicit-any */
} catch (error: any) {
// FIXME [OP 28 SEP 2022]
app.log.error(error);

if (error.message === "EMAIL_ALREADY_EXISTS_ERROR") {
return {
status: error.message,
};
}

const mercuriusError = new mercurius.ErrorWithProps(
"Oops, Something went wrong",
);
mercuriusError.statusCode = 500;

return mercuriusError;
}
},
Expand Down
6 changes: 6 additions & 0 deletions packages/user/src/model/users/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ const user = gql`
password: String!
}
type ChangeEmailResponse {
status: String!
message: String
}
type Mutation {
adminSignUp(data: SingUpFieldInput!): AuthResponse
disableUser(id: String!): UpdateUserResponse @auth
Expand All @@ -54,6 +59,7 @@ const user = gql`
newPassword: String
): ChangePasswordResponse @auth
updateMe(data: UserUpdateInput): User @auth(profileValidation: false)
changeEmail(email: String!): ChangeEmailResponse
}
type Query {
Expand Down

0 comments on commit bc514ce

Please sign in to comment.