From 3b4c5bf9ae1b2dd894040d187ca9f1d55f163ecc Mon Sep 17 00:00:00 2001 From: Benjamin Blanchard Date: Mon, 12 Feb 2024 17:02:54 -0500 Subject: [PATCH] use upsert() on user creation --- .../controllers/ExperimentClientController.ts | 20 +++++++++++-------- .../ExperimentClientController.v1.ts | 20 +++++++++++-------- .../ExperimentClientController.v4.ts | 20 +++++++++++-------- .../ExperimentClientController.v5.ts | 20 +++++++++++-------- .../controllers/ExperimentUserController.ts | 3 ++- .../src/api/services/ExperimentUserService.ts | 10 +++++----- 6 files changed, 55 insertions(+), 38 deletions(-) diff --git a/backend/packages/Upgrade/src/api/controllers/ExperimentClientController.ts b/backend/packages/Upgrade/src/api/controllers/ExperimentClientController.ts index ad21f4b816..0ffd73e75c 100644 --- a/backend/packages/Upgrade/src/api/controllers/ExperimentClientController.ts +++ b/backend/packages/Upgrade/src/api/controllers/ExperimentClientController.ts @@ -175,27 +175,31 @@ export class ExperimentClientController { request.logger.info({ message: 'Starting the init call for user' }); // getOriginalUserDoc call for alias const experimentUserDoc = await this.experimentUserService.getUserDoc(experimentUser.id, request.logger); + + // if reinit call is made with any of the below fields not included in the call, + // then we will fetch the stored values of the field and return them in the response + // for consistent init response with 3 fields ['userId', 'group', 'workingGroup'] + const { id, group, workingGroup } = { ...experimentUserDoc, ...experimentUser }; + if (experimentUserDoc) { // append userDoc in logger request.logger.child({ userDoc: experimentUserDoc }); request.logger.info({ message: 'Got the original user doc' }); } - const userDocument = await this.experimentUserService.upsertOnChange( + const upsertResult = await this.experimentUserService.upsertOnChange( experimentUserDoc, experimentUser, request.logger ); - if (!userDocument || !userDocument[0]) { + + if (!upsertResult) { request.logger.error({ - details: 'user document not present', + details: 'user upsert failed', }); - throw new InternalServerError('user document not present'); + throw new InternalServerError('user init failed'); } - // if reinit call is made with any of the below fields not included in the call, - // then we will fetch the stored values of the field and return them in the response - // for consistent init response with 3 fields ['userId', 'group', 'workingGroup'] - const { id, group, workingGroup } = userDocument[0]; + return { id, group, workingGroup }; } diff --git a/backend/packages/Upgrade/src/api/controllers/ExperimentClientController.v1.ts b/backend/packages/Upgrade/src/api/controllers/ExperimentClientController.v1.ts index f323719c8e..ec2fd99b36 100644 --- a/backend/packages/Upgrade/src/api/controllers/ExperimentClientController.v1.ts +++ b/backend/packages/Upgrade/src/api/controllers/ExperimentClientController.v1.ts @@ -181,26 +181,30 @@ export class ExperimentClientController { request.logger.info({ message: 'Starting the init call for user' }); // getOriginalUserDoc call for alias const experimentUserDoc = await this.experimentUserService.getUserDoc(experimentUser.id, request.logger); + // if reinit call is made with any of the below fields not included in the call, + // then we will fetch the stored values of the field and return them in the response + // for consistent init response with 3 fields ['userId', 'group', 'workingGroup'] + const { id, group, workingGroup } = { ...experimentUserDoc, ...experimentUser }; + if (experimentUserDoc) { // append userDoc in logger request.logger.child({ userDoc: experimentUserDoc }); request.logger.info({ message: 'Got the original user doc' }); } - const userDocument = await this.experimentUserService.upsertOnChange( + + const upsertResult = await this.experimentUserService.upsertOnChange( experimentUserDoc, experimentUser, request.logger ); - if (!userDocument || !userDocument[0]) { + + if (!upsertResult) { request.logger.error({ - details: 'user document not present', + details: 'user upsert failed', }); - throw new InternalServerError('user document not present'); + throw new InternalServerError('user init failed'); } - // if reinit call is made with any of the below fields not included in the call, - // then we will fetch the stored values of the field and return them in the response - // for consistent init response with 3 fields ['userId', 'group', 'workingGroup'] - const { id, group, workingGroup } = userDocument[0]; + return { id, group, workingGroup }; } diff --git a/backend/packages/Upgrade/src/api/controllers/ExperimentClientController.v4.ts b/backend/packages/Upgrade/src/api/controllers/ExperimentClientController.v4.ts index c35b135a93..9adcc25247 100644 --- a/backend/packages/Upgrade/src/api/controllers/ExperimentClientController.v4.ts +++ b/backend/packages/Upgrade/src/api/controllers/ExperimentClientController.v4.ts @@ -180,26 +180,30 @@ export class ExperimentClientController { request.logger.info({ message: 'Starting the init call for user' }); // getOriginalUserDoc call for alias const experimentUserDoc = await this.experimentUserService.getUserDoc(experimentUser.id, request.logger); + // if reinit call is made with any of the below fields not included in the call, + // then we will fetch the stored values of the field and return them in the response + // for consistent init response with 3 fields ['userId', 'group', 'workingGroup'] + const { id, group, workingGroup } = { ...experimentUserDoc, ...experimentUser }; + if (experimentUserDoc) { // append userDoc in logger request.logger.child({ userDoc: experimentUserDoc }); request.logger.info({ message: 'Got the original user doc' }); } - const userDocument = await this.experimentUserService.upsertOnChange( + + const upsertResult = await this.experimentUserService.upsertOnChange( experimentUserDoc, experimentUser, request.logger ); - if (!userDocument || !userDocument[0]) { + + if (!upsertResult) { request.logger.error({ - details: 'user document not present', + details: 'user upsert failed', }); - throw new InternalServerError('user document not present'); + throw new InternalServerError('user init failed'); } - // if reinit call is made with any of the below fields not included in the call, - // then we will fetch the stored values of the field and return them in the response - // for consistent init response with 3 fields ['userId', 'group', 'workingGroup'] - const { id, group, workingGroup } = userDocument[0]; + return { id, group, workingGroup }; } diff --git a/backend/packages/Upgrade/src/api/controllers/ExperimentClientController.v5.ts b/backend/packages/Upgrade/src/api/controllers/ExperimentClientController.v5.ts index c30ea0a937..0f4faabb8b 100644 --- a/backend/packages/Upgrade/src/api/controllers/ExperimentClientController.v5.ts +++ b/backend/packages/Upgrade/src/api/controllers/ExperimentClientController.v5.ts @@ -178,26 +178,30 @@ export class ExperimentClientController { request.logger.info({ message: 'Starting the init call for user' }); // getOriginalUserDoc call for alias const experimentUserDoc = await this.experimentUserService.getUserDoc(experimentUser.id, request.logger); + // if reinit call is made with any of the below fields not included in the call, + // then we will fetch the stored values of the field and return them in the response + // for consistent init response with 3 fields ['userId', 'group', 'workingGroup'] + const { id, group, workingGroup } = { ...experimentUserDoc, ...experimentUser }; + if (experimentUserDoc) { // append userDoc in logger request.logger.child({ userDoc: experimentUserDoc }); request.logger.info({ message: 'Got the original user doc' }); } - const userDocument = await this.experimentUserService.upsertOnChange( + + const upsertResult = await this.experimentUserService.upsertOnChange( experimentUserDoc, experimentUser, request.logger ); - if (!userDocument || !userDocument[0]) { + + if (!upsertResult) { request.logger.error({ - details: 'user document not present', + details: 'user upsert failed', }); - throw new InternalServerError('user document not present'); + throw new InternalServerError('user init failed'); } - // if reinit call is made with any of the below fields not included in the call, - // then we will fetch the stored values of the field and return them in the response - // for consistent init response with 3 fields ['userId', 'group', 'workingGroup'] - const { id, group, workingGroup } = userDocument[0]; + return { id, group, workingGroup }; } diff --git a/backend/packages/Upgrade/src/api/controllers/ExperimentUserController.ts b/backend/packages/Upgrade/src/api/controllers/ExperimentUserController.ts index fe85002d18..1a2ccf8d01 100644 --- a/backend/packages/Upgrade/src/api/controllers/ExperimentUserController.ts +++ b/backend/packages/Upgrade/src/api/controllers/ExperimentUserController.ts @@ -6,6 +6,7 @@ import { SERVER_ERROR } from 'upgrade_types'; import { isUUID } from 'class-validator'; import { AppRequest } from '../../types'; import { ExperimentUserArrayValidator, ExperimentUserValidator } from './validators/ExperimentUserValidator'; +import { InsertResult } from 'typeorm'; // TODO delete this from experiment system /** @@ -110,7 +111,7 @@ export class UserController { @Body({ validate: true }) users: ExperimentUserArrayValidator, @Req() request: AppRequest - ): Promise { + ): Promise { return this.userService.create(users.users, request.logger); } diff --git a/backend/packages/Upgrade/src/api/services/ExperimentUserService.ts b/backend/packages/Upgrade/src/api/services/ExperimentUserService.ts index 15be4cce88..3db7240c61 100644 --- a/backend/packages/Upgrade/src/api/services/ExperimentUserService.ts +++ b/backend/packages/Upgrade/src/api/services/ExperimentUserService.ts @@ -6,7 +6,7 @@ import { ExperimentUserRepository } from '../repositories/ExperimentUserReposito import { ExperimentUser } from '../models/ExperimentUser'; import { ExperimentRepository } from '../repositories/ExperimentRepository'; import { ASSIGNMENT_UNIT, CONSISTENCY_RULE, EXPERIMENT_STATE, IUserAliases, SERVER_ERROR } from 'upgrade_types'; -import { getConnection, In, Not } from 'typeorm'; +import { getConnection, In, InsertResult, Not } from 'typeorm'; import { IndividualExclusionRepository } from '../repositories/IndividualExclusionRepository'; import { GroupExclusionRepository } from '../repositories/GroupExclusionRepository'; import { Experiment } from '../models/Experiment'; @@ -39,7 +39,7 @@ export class ExperimentUserService { oldExperimentUser: RequestedExperimentUser, newExperimentUser: Partial, logger: UpgradeLogger - ): Promise { + ): Promise { if (!oldExperimentUser) { return this.create([newExperimentUser], logger); } @@ -65,7 +65,7 @@ export class ExperimentUserService { return this.create([newExperimentUser], logger); } - return [oldExperimentUser]; + return true; } private isGroupsEqual(oldUserData: RequestedExperimentUser, newUserData: Partial): boolean { @@ -88,10 +88,10 @@ export class ExperimentUserService { } } - public async create(users: Array>, logger: UpgradeLogger): Promise { + public async create(users: Array>, logger: UpgradeLogger): Promise { logger.info({ message: 'Create a new User. Metadata of the user =>', details: users }); // insert or update in the database - return this.userRepository.save(users); + return this.userRepository.upsert(users, ['id']); } public async setAliasesForUser(