Browse Source

fix(core): Fix updating customer email with no NativeAuth configured

Fixes #1092
Michael Bromley 4 years ago
parent
commit
f6d3a52502

+ 27 - 24
packages/core/src/service/services/customer.service.ts

@@ -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);
             }
         }
 

+ 8 - 1
packages/core/src/service/services/user.service.ts

@@ -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;