Skip to content

Commit

Permalink
Merge pull request #2932 from Akarshit/bugfix-userNameUpdate
Browse files Browse the repository at this point in the history
Username only updated on first address and when there is 1 address
  • Loading branch information
mikemurray authored Oct 3, 2017
2 parents c1f0550 + ff2fcd8 commit ad581db
Showing 1 changed file with 34 additions and 18 deletions.
52 changes: 34 additions & 18 deletions server/methods/accounts/accounts.js
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,9 @@ export function addressBookAdd(address, accountUserId) {
this.unblock();

const userId = accountUserId || Meteor.userId();
const account = Accounts.findOne({
userId: userId
});
// required default id
if (!address._id) {
address._id = Random.id();
Expand Down Expand Up @@ -356,24 +359,30 @@ export function addressBookAdd(address, accountUserId) {
}
}

Meteor.users.update(Meteor.userId(), {
const userUpdateQuery = {
$set: {
"name": address.fullName,
"profile.addressBook": address
}
});

return Accounts.upsert({
userId: userId
}, {
};
const accountsUpdateQuery = {
$set: {
name: address.fullName,
userId: userId
},
$addToSet: {
"profile.addressBook": address
}
});
};

if (!account.name || _.get(account, "profile.addressBook.length", 0) === 0) {
userUpdateQuery.$set.name = address.fullName;
accountsUpdateQuery.$set.name = address.fullName;
}

Meteor.users.update(Meteor.userId(), userUpdateQuery);

return Accounts.upsert({
userId: userId
}, accountsUpdateQuery);
}

/**
Expand Down Expand Up @@ -469,22 +478,29 @@ export function addressBookUpdate(address, accountUserId, type) {
}
}

Meteor.users.update(Meteor.userId(), {
const userUpdateQuery = {
$set: {
"name": address.fullName,
"profile.addressBook": address
}
});
};

return Accounts.update({
"userId": userId,
"profile.addressBook._id": address._id
}, {
const accountsUpdateQuery = {
$set: {
"name": address.fullName,
"profile.addressBook.$": address
}
});
};
// update the name when there is no name or the user updated his only shipping address
if (!account.name || _.get(account, "profile.addressBook.length", 0) <= 1) {
userUpdateQuery.$set.name = address.fullName;
accountsUpdateQuery.$set.name = address.fullName;
}

Meteor.users.update(Meteor.userId(), userUpdateQuery);

return Accounts.update({
"userId": userId,
"profile.addressBook._id": address._id
}, accountsUpdateQuery);
}

/**
Expand Down

0 comments on commit ad581db

Please sign in to comment.