Skip to content

Commit 0095a14

Browse files
authored
Improve error handling for user lookup by email (#1766)
Part of OPS-3009.
1 parent ce03f39 commit 0095a14

4 files changed

Lines changed: 19 additions & 9 deletions

File tree

packages/server/api/src/app/authentication/new-user/organization-assignment.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ export async function assignDefaultOrganization(
1717
): Promise<UserWithOrganization> {
1818
let organization = await organizationService.getOldestOrganization();
1919

20-
const adminUser = await userService.getUserByEmailOrFail({
21-
email: system.getOrThrow(AppSystemProp.OPENOPS_ADMIN_EMAIL),
22-
});
20+
const adminUser = await userService.getUserByEmailOrThrow(
21+
system.getOrThrow(AppSystemProp.OPENOPS_ADMIN_EMAIL),
22+
);
2323

2424
organization = !isNil(adminUser.organizationId)
2525
? await organizationService.getOne(adminUser.organizationId)

packages/server/api/src/app/openops-tables/auth-admin-tables.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export async function authenticateAdminUserInOpenOpsTables(
2323

2424
if (!tokens) {
2525
const email = system.getOrThrow(AppSystemProp.OPENOPS_ADMIN_EMAIL);
26-
const user = await userService.getUserByEmailOrFail({ email });
26+
const user = await userService.getUserByEmailOrThrow(email);
2727

2828
tokens = await authenticateUserInOpenOpsTables(
2929
email,

packages/server/api/src/app/user/user-service.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,18 @@ export const userService = {
160160
.execute();
161161
},
162162

163-
async getUserByEmailOrFail({ email }: { email: string }): Promise<User> {
164-
return userRepo().findOneByOrFail({ email });
163+
async getUserByEmailOrThrow(email: string): Promise<User> {
164+
const user = await userRepo().findOneBy({ email });
165+
if (isNil(user)) {
166+
throw new ApplicationError({
167+
code: ErrorCode.ENTITY_NOT_FOUND,
168+
params: {
169+
entityType: 'user',
170+
entityId: email,
171+
},
172+
});
173+
}
174+
return user;
165175
},
166176

167177
async getUsersByEmail({ email }: { email: string }): Promise<User[]> {

packages/server/api/test/unit/openops-tables/auth-admin-tables.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ jest.mock('@openops/server-shared', () => ({
1818
},
1919
}));
2020

21-
const getUserByEmailOrFailMock = jest.fn().mockResolvedValue({});
21+
const getUserByEmailOrThrowMock = jest.fn().mockResolvedValue({});
2222
jest.mock('../../../src/app/user/user-service', () => ({
2323
userService: {
24-
getUserByEmailOrFail: getUserByEmailOrFailMock,
24+
getUserByEmailOrThrow: getUserByEmailOrThrowMock,
2525
},
2626
}));
2727

@@ -41,7 +41,7 @@ describe('Authenticate admin user in OpenOps Tables', () => {
4141
password: 'secret',
4242
};
4343

44-
getUserByEmailOrFailMock.mockResolvedValue(user);
44+
getUserByEmailOrThrowMock.mockResolvedValue(user);
4545
});
4646

4747
it('returns cached tokens when available', async () => {

0 commit comments

Comments
 (0)