Skip to content

Commit

Permalink
fix(core): Fix updating customer email with no NativeAuth configured
Browse files Browse the repository at this point in the history
Fixes #1092
  • Loading branch information
michaelbromley committed Sep 23, 2021
1 parent 36c15b2 commit f6d3a52
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 25 deletions.
51 changes: 27 additions & 24 deletions packages/core/src/service/services/customer.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,33 +251,36 @@ export class CustomerService {
});

if (hasEmailAddress(input)) {
const existingCustomerInChannel = await this.connection
.getRepository(ctx, Customer)
.createQueryBuilder('customer')
.leftJoin('customer.channels', 'channel')
.where('channel.id = :channelId', { channelId: ctx.channelId })
.andWhere('customer.emailAddress = :emailAddress', { emailAddress: input.emailAddress })
.andWhere('customer.id != :customerId', { customerId: input.id })
.andWhere('customer.deletedAt is null')
.getOne();

if (existingCustomerInChannel) {
return new EmailAddressConflictAdminError();
}
if (input.emailAddress !== customer.emailAddress) {
const existingCustomerInChannel = await this.connection
.getRepository(ctx, Customer)
.createQueryBuilder('customer')
.leftJoin('customer.channels', 'channel')
.where('channel.id = :channelId', { channelId: ctx.channelId })
.andWhere('customer.emailAddress = :emailAddress', { emailAddress: input.emailAddress })
.andWhere('customer.id != :customerId', { customerId: input.id })
.andWhere('customer.deletedAt is null')
.getOne();

if (existingCustomerInChannel) {
return new EmailAddressConflictAdminError();
}

if (customer.user) {
const existingUserWithEmailAddress = await this.userService.getUserByEmailAddress(
ctx,
input.emailAddress,
);
if (customer.user) {
const existingUserWithEmailAddress = await this.userService.getUserByEmailAddress(
ctx,
input.emailAddress,
);

if (
existingUserWithEmailAddress &&
!idsAreEqual(customer.user.id, existingUserWithEmailAddress.id)
) {
return new EmailAddressConflictAdminError();
if (
existingUserWithEmailAddress &&
!idsAreEqual(customer.user.id, existingUserWithEmailAddress.id)
) {
return new EmailAddressConflictAdminError();
}

await this.userService.changeNativeIdentifier(ctx, customer.user.id, input.emailAddress);
}
await this.userService.changeNativeIdentifier(ctx, customer.user.id, input.emailAddress);
}
}

Expand Down
9 changes: 8 additions & 1 deletion packages/core/src/service/services/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,14 @@ export class UserService {
if (!user) {
return;
}
const nativeAuthMethod = user.getNativeAuthenticationMethod();
const nativeAuthMethod = user.authenticationMethods.find(
(m): m is NativeAuthenticationMethod => m instanceof NativeAuthenticationMethod,
);
if (!nativeAuthMethod) {
// If the NativeAuthenticationMethod is not configured, then
// there is nothing to do.
return;
}
user.identifier = newIdentifier;
nativeAuthMethod.identifier = newIdentifier;
nativeAuthMethod.identifierChangeToken = null;
Expand Down

0 comments on commit f6d3a52

Please sign in to comment.