Skip to content

Commit

Permalink
10007: Fix create users local script
Browse files Browse the repository at this point in the history
  • Loading branch information
rachelschneiderman committed Feb 19, 2024
1 parent 3f579b3 commit 0692d2c
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 47 deletions.
3 changes: 2 additions & 1 deletion web-api/src/persistence/dynamo/users/createOrUpdateUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export const createUserRecords = async ({
applicationContext: ServerApplicationContext;
user: any;
userId: string;
}): Promise<{ userId: string }> => {
}) => {
delete user.password;

if (user.barNumber === '') {
Expand Down Expand Up @@ -154,6 +154,7 @@ export const createUserRecords = async ({
}

return {
...user,
userId,
};
};
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import * as client from '../../dynamodbClientService';
import { ServerApplicationContext } from '@web-api/applicationContext';

export const createPetitionerUserRecords = async ({
applicationContext,
user,
userId,
}: {
applicationContext: IApplicationContext;
applicationContext: ServerApplicationContext;
user: any;
userId: string;
}) => {
Expand Down
85 changes: 40 additions & 45 deletions web-api/storage/scripts/createUsers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { ROLES } from '../../../shared/src/business/entities/EntityConstants';
import {
ROLES,
Role,
} from '../../../shared/src/business/entities/EntityConstants';
import { createApplicationContext } from '../../src/applicationContext';
import { createPetitionerUserRecords } from '../../../web-api/src/persistence/dynamo/users/createPetitionerUserRecords';
import { createUserRecords as createPractitionerUserRecords } from '../../../web-api/src/persistence/dynamo/users/createOrUpdatePractitionerUser';
Expand All @@ -13,71 +16,63 @@ const EXCLUDE_PROPS = ['pk', 'sk', 'userId'];
export const createUsers = async () => {
usersByEmail = {};

const user = {
const applicationContext = createApplicationContext({
role: ROLES.admin,
};

const applicationContext = createApplicationContext(user);
});

await Promise.all(
users.map(userRecord => {
users.map(async userRecord => {
if (!userRecord.userId) {
throw new Error('User has no uuid');
}

const { userId } = userRecord;

if (
[
ROLES.irsPractitioner,
ROLES.privatePractitioner,
ROLES.inactivePractitioner,
].includes(userRecord.role)
) {
return createPractitionerUserRecords({
const practitionerRoles: Role[] = [
ROLES.irsPractitioner,
ROLES.privatePractitioner,
ROLES.inactivePractitioner,
];
if (practitionerRoles.includes(userRecord.role as Role)) {
const userCreated = await createPractitionerUserRecords({
applicationContext,
user: omit(userRecord, EXCLUDE_PROPS),
userId,
}).then(userCreated => {
if (usersByEmail[userCreated.email]) {
throw new Error('User already exists');
}
usersByEmail[userCreated.email] = userCreated;
userId: userRecord.userId,
});

if (usersByEmail[userCreated.email]) {
throw new Error(`User ${JSON.stringify(userRecord)} already exists`);
}

usersByEmail[userCreated.email] = userCreated;
return;
}

if (userRecord.role === ROLES.petitioner) {
return createPetitionerUserRecords({
const userCreated = await createPetitionerUserRecords({
applicationContext,
user: omit(userRecord, EXCLUDE_PROPS),
userId,
}).then(userCreated => {
if (usersByEmail[userCreated.email]) {
throw new Error('User already exists');
}
usersByEmail[userCreated.email] = userCreated;
userId: userRecord.userId,
});
}

return createUserRecords({
applicationContext,
user: omit(userRecord, EXCLUDE_PROPS),
userId,
}).then(userCreated => {
if (usersByEmail[userCreated.email]) {
throw new Error('User already exists');
throw new Error(`User ${JSON.stringify(userRecord)} already exists`);
}

usersByEmail[userCreated.email] = userCreated;
return;
}

const userCreated = await createUserRecords({
applicationContext,
user: omit(userRecord, EXCLUDE_PROPS),
userId: userRecord.userId,
});

if (usersByEmail[userCreated.email]) {
throw new Error(`User ${JSON.stringify(userRecord)} already exists`);
}

usersByEmail[userCreated.email] = userCreated;
return;
}),
);
};

export const asUserFromEmail = async (email, callback) => {
const asUser = usersByEmail[email];
if (!asUser) {
throw new Error('User not found');
}
const applicationContext = createApplicationContext(asUser);
return await callback(applicationContext);
};

0 comments on commit 0692d2c

Please sign in to comment.