Skip to content

Commit

Permalink
do not update the same field twice with updater
Browse files Browse the repository at this point in the history
  • Loading branch information
matheusbsilva137 committed Dec 27, 2024
1 parent 27a6657 commit 064312f
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 8 deletions.
4 changes: 2 additions & 2 deletions apps/meteor/app/lib/server/functions/saveUser/saveUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export const saveUser = async function (userId: IUser['_id'], userData: SaveUser

if (userData.email) {
const shouldSendVerificationEmailToUser = userData.verified !== true;
await setEmail(userData._id, userData.email, shouldSendVerificationEmailToUser, updater);
await setEmail(userData._id, userData.email, shouldSendVerificationEmailToUser, userData.verified === true, updater);
}

if (
Expand Down Expand Up @@ -138,7 +138,7 @@ export const saveUser = async function (userId: IUser['_id'], userData: SaveUser
}
}

if (typeof userData.verified === 'boolean') {
if (typeof userData.verified === 'boolean' && !userData.email) {
updater.set('emails.0.verified', userData.verified);
}

Expand Down
12 changes: 9 additions & 3 deletions apps/meteor/app/lib/server/functions/setEmail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,13 @@ const _sendEmailChangeNotification = async function (to: string, newEmail: strin
}
};

const _setEmail = async function (userId: string, email: string, shouldSendVerificationEmail = true, updater?: Updater<IUser>) {
const _setEmail = async function (
userId: string,
email: string,
shouldSendVerificationEmail = true,
verified = false,
updater?: Updater<IUser>,
) {
email = email.trim();
if (!userId) {
throw new Meteor.Error('error-invalid-user', 'Invalid user', { function: '_setEmail' });
Expand Down Expand Up @@ -77,9 +83,9 @@ const _setEmail = async function (userId: string, email: string, shouldSendVerif

// Set new email
if (updater) {
updater.set('emails', [{ address: email, verified: false }]);
updater.set('emails', [{ address: email, verified }]);
} else {
await Users.setEmail(user?._id, email);
await Users.setEmail(user?._id, email, verified);
}

const result = {
Expand Down
2 changes: 1 addition & 1 deletion packages/model-typings/src/models/IUsersModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ export interface IUsersModel extends IBaseModel<IUser> {
addPasswordToHistory(userId: string, password: string, passwordHistoryAmount: number): Promise<UpdateResult>;
setServiceId(userId: string, serviceName: string, serviceId: string): Promise<UpdateResult>;
setUsername(userId: string, username: string): Promise<UpdateResult>;
setEmail(userId: string, email: string): Promise<UpdateResult>;
setEmail(userId: string, email: string, verified?: boolean): Promise<UpdateResult>;
setEmailVerified(userId: string, email: string): Promise<UpdateResult>;
setName(userId: string, name: string): Promise<UpdateResult>;
unsetName(userId: string): Promise<UpdateResult>;
Expand Down
4 changes: 2 additions & 2 deletions packages/models/src/models/Users.js
Original file line number Diff line number Diff line change
Expand Up @@ -2577,13 +2577,13 @@ export class UsersRaw extends BaseRaw {
return this.updateOne({ _id }, update);
}

setEmail(_id, email) {
setEmail(_id, email, verified = false) {
const update = {
$set: {
emails: [
{
address: email,
verified: false,
verified,
},
],
},
Expand Down

0 comments on commit 064312f

Please sign in to comment.