Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use upsert() on user creation #1304

Merged
merged 2 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
/**
Expand Down Expand Up @@ -110,7 +111,7 @@ export class UserController {
@Body({ validate: true })
users: ExperimentUserArrayValidator,
@Req() request: AppRequest
): Promise<ExperimentUser[]> {
): Promise<InsertResult> {
return this.userService.create(users.users, request.logger);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -39,7 +39,7 @@ export class ExperimentUserService {
oldExperimentUser: RequestedExperimentUser,
newExperimentUser: Partial<ExperimentUser>,
logger: UpgradeLogger
): Promise<ExperimentUser[]> {
): Promise<InsertResult | boolean> {
if (!oldExperimentUser) {
return this.create([newExperimentUser], logger);
}
Expand All @@ -65,7 +65,7 @@ export class ExperimentUserService {
return this.create([newExperimentUser], logger);
}

return [oldExperimentUser];
return true;
}

private isGroupsEqual(oldUserData: RequestedExperimentUser, newUserData: Partial<ExperimentUser>): boolean {
Expand All @@ -88,10 +88,10 @@ export class ExperimentUserService {
}
}

public async create(users: Array<Partial<ExperimentUser>>, logger: UpgradeLogger): Promise<ExperimentUser[]> {
public async create(users: Array<Partial<ExperimentUser>>, logger: UpgradeLogger): Promise<InsertResult> {
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']);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both the syntax save and upsert are the same in this case. Right?

}

public async setAliasesForUser(
Expand Down
Loading