From 182991af7fcdab4075142a4a237427d1ffa98b98 Mon Sep 17 00:00:00 2001 From: Darcy Ye Date: Wed, 12 Jun 2024 15:07:05 +0800 Subject: [PATCH] refactor: rebase master branch --- packages/core/src/libraries/user.ts | 18 +++++++++--------- .../admin-user/mfa-verifications.test.ts | 12 ++++++++---- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/packages/core/src/libraries/user.ts b/packages/core/src/libraries/user.ts index 781ef6af6d28..816aa8592c28 100644 --- a/packages/core/src/libraries/user.ts +++ b/packages/core/src/libraries/user.ts @@ -1,3 +1,4 @@ +/* eslint-disable max-lines */ import type { BindMfa, CreateUser, MfaVerification, Scope, User } from '@logto/schemas'; import { MfaFactor, RoleType, Users, UsersPasswordEncryptionMethod } from '@logto/schemas'; import { generateStandardShortId, generateStandardId } from '@logto/shared'; @@ -122,15 +123,14 @@ export const createUserLibrary = (queries: Queries) => { set: Partial>, jsonbMode?: 'replace' | 'merge' ) => { + const { primaryPhone, ...rest } = set; const validPhoneNumber = conditional( - 'primaryPhone' in set && - typeof set.primaryPhone === 'string' && - getValidPhoneNumber(set.primaryPhone) + typeof primaryPhone === 'string' && getValidPhoneNumber(primaryPhone) ); return updateUserByIdQuery( id, - { ...set, ...conditional(validPhoneNumber && { primaryPhone: validPhoneNumber }) }, + { ...rest, ...conditional(validPhoneNumber && { primaryPhone: validPhoneNumber }) }, jsonbMode ); }; @@ -147,10 +147,9 @@ export const createUserLibrary = (queries: Queries) => { assertThat(parameterRoles.length === roleNames.length, 'role.default_role_missing'); + const { primaryPhone, ...rest } = data; const validPhoneNumber = conditional( - 'primaryPhone' in data && - typeof data.primaryPhone === 'string' && - getValidPhoneNumber(data.primaryPhone) + typeof primaryPhone === 'string' && getValidPhoneNumber(primaryPhone) ); return pool.transaction(async (connection) => { @@ -159,7 +158,7 @@ export const createUserLibrary = (queries: Queries) => { }); const user = await insertUserQuery({ - ...data, + ...rest, ...conditional(validPhoneNumber && { primaryPhone: validPhoneNumber }), }); const roles = deduplicateByKey([...parameterRoles, ...defaultRoles], 'id'); @@ -173,7 +172,7 @@ export const createUserLibrary = (queries: Queries) => { const provisionOrganizations = async (): Promise => { // Just-in-time organization provisioning - const userEmailDomain = data.primaryEmail?.split('@')[1]; + const userEmailDomain = rest.primaryEmail?.split('@')[1]; // TODO: Remove this check when launching if (EnvSet.values.isDevFeaturesEnabled && userEmailDomain) { const organizationQueries = new OrganizationQueries(connection); @@ -368,3 +367,4 @@ export const createUserLibrary = (queries: Queries) => { updateUserById, }; }; +/* eslint-enable max-lines */ diff --git a/packages/core/src/routes/admin-user/mfa-verifications.test.ts b/packages/core/src/routes/admin-user/mfa-verifications.test.ts index 3439504dddaf..5e1dc4a4d490 100644 --- a/packages/core/src/routes/admin-user/mfa-verifications.test.ts +++ b/packages/core/src/routes/admin-user/mfa-verifications.test.ts @@ -8,6 +8,7 @@ import { mockUserTotpMfaVerification, mockUserWithMfaVerifications, } from '#src/__mocks__/index.js'; +import { type InsertUserResult } from '#src/libraries/user.js'; import type Libraries from '#src/tenants/Libraries.js'; import type Queries from '#src/tenants/Queries.js'; import { MockTenant, type Partial2 } from '#src/test-utils/tenant.js'; @@ -44,10 +45,13 @@ const mockLibraries = { users: { generateUserId: jest.fn(async () => 'fooId'), insertUser: jest.fn( - async (user: CreateUser): Promise => ({ - ...mockUser, - ...removeUndefinedKeys(user), // No undefined values will be returned from database - }) + async (user: CreateUser): Promise => [ + { + ...mockUser, + ...removeUndefinedKeys(user), // No undefined values will be returned from database + }, + { organizationIds: [] }, + ] ), updateUserById: jest.fn( async (_, data: Partial): Promise => ({