Skip to content

Commit 04fc731

Browse files
committed
modify code based on code review
1 parent e397812 commit 04fc731

File tree

8 files changed

+17
-39
lines changed

8 files changed

+17
-39
lines changed

packages/backend/.env-example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ M365_SG_GOVT_ALLOWED_SENSITIVITY_LABEL_GUIDS_CSV=11111111-1111-1111-1111-1111111
3232
LAUNCH_DARKLY_SDK_KEY=...
3333
MAX_JOB_ATTEMPTS=3
3434
POSTMAN_SMS_QPS_LIMIT_PER_CAMPAIGN=3
35+
ONBOARDING_EMAIL_WEBHOOK_URL=test-webhook-url
3536

3637
# LOCAL DEV
3738
SERVE_WEB_APP_SEPARATELY=true

packages/backend/src/graphql/__tests__/mutations/login-with-selected-sgid.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ describe('Login with selected SGID', () => {
8383
expect(mocks.getOrCreateUser).toHaveBeenCalledWith(
8484
8585
)
86-
expect(mocks.sendOnboardingEmail).toHaveBeenCalledWith('abc-def')
86+
expect(mocks.sendOnboardingEmail).toHaveBeenCalledWith({ id: 'abc-def' })
8787
expect(mocks.updateLastLogin).toHaveBeenCalledWith('abc-def')
8888
expect(mocks.setAuthCookie).toHaveBeenCalledWith(expect.anything(), {
8989
userId: 'abc-def',

packages/backend/src/graphql/__tests__/mutations/login-with-sgid.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ describe('Login with SGID', () => {
9393
expect(mocks.getOrCreateUser).toHaveBeenCalledWith(
9494
9595
)
96-
expect(mocks.sendOnboardingEmail).toHaveBeenCalledWith('abc-def')
96+
expect(mocks.sendOnboardingEmail).toHaveBeenCalledWith({ id: 'abc-def' })
9797
expect(mocks.updateLastLogin).toHaveBeenCalledWith('abc-def')
9898
expect(mocks.setAuthCookie).toHaveBeenCalledWith(expect.anything(), {
9999
userId: 'abc-def',
@@ -184,7 +184,7 @@ describe('Login with SGID', () => {
184184
const result = await loginWithSgid(null, STUB_PARAMS, STUB_CONTEXT)
185185

186186
expect(mocks.getOrCreateUser).toHaveBeenCalledWith('[email protected]')
187-
expect(mocks.sendOnboardingEmail).toHaveBeenCalledWith('abc-def')
187+
expect(mocks.sendOnboardingEmail).toHaveBeenCalledWith({ id: 'abc-def' })
188188
expect(mocks.updateLastLogin).toHaveBeenCalledWith('abc-def')
189189
expect(mocks.setAuthCookie).toHaveBeenCalledWith(expect.anything(), {
190190
userId: 'abc-def',

packages/backend/src/graphql/mutations/login-with-selected-sgid.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ const loginWithSelectedSgid: MutationResolvers['loginWithSelectedSgid'] =
4949
}
5050

5151
const user = await getOrCreateUser(workEmail)
52-
await sendOnboardingEmail(user.id)
52+
await sendOnboardingEmail(user)
5353
await updateLastLogin(user.id)
5454
setAuthCookie(context.res, { userId: user.id })
5555

packages/backend/src/graphql/mutations/login-with-sgid.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ const loginWithSgid: MutationResolvers['loginWithSgid'] = async (
134134
// Log user in directly if there is only 1 employment.
135135
if (publicOfficerEmployments.length === 1) {
136136
const user = await getOrCreateUser(publicOfficerEmployments[0].workEmail)
137-
await sendOnboardingEmail(user.id)
137+
await sendOnboardingEmail(user)
138138
await updateLastLogin(user.id)
139139
setAuthCookie(context.res, { userId: user.id })
140140

packages/backend/src/graphql/mutations/verify-otp.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ const verifyOtp: MutationResolvers['verifyOtp'] = async (
5151
) {
5252
throw new BaseError('Invalid OTP')
5353
}
54-
await sendOnboardingEmail(user.id)
54+
await sendOnboardingEmail(user)
5555

5656
// reset otp columns
5757
await user.$query().patch({

packages/backend/src/helpers/__tests__/auth.test.ts

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -229,31 +229,17 @@ describe('Auth helpers', () => {
229229
afterEach(() => {
230230
vi.restoreAllMocks()
231231
})
232-
233-
it('throws error with no user id', async () => {
234-
await expect(sendOnboardingEmail('')).rejects.toThrowError(
235-
'User id required',
236-
)
237-
})
238-
239-
it('throws error with non-existent user id', async () => {
240-
mocks.findById.mockResolvedValueOnce(null)
241-
await expect(sendOnboardingEmail('non-existent-id')).rejects.toThrowError(
242-
'User not found',
243-
)
244-
})
245-
246232
it('does not send email if user has logged in before', async () => {
247233
const mockUser = {
248234
id: 'test-id',
249235
250236
lastLoginAt: new Date(),
251237
createdAt: new Date(),
252-
}
238+
} as unknown as User
253239

254240
mocks.findById.mockResolvedValueOnce(mockUser)
255241

256-
await sendOnboardingEmail(mockUser.id)
242+
await sendOnboardingEmail(mockUser)
257243
expect(axios.post).not.toHaveBeenCalled()
258244
})
259245

@@ -262,11 +248,11 @@ describe('Auth helpers', () => {
262248
id: 'test-id',
263249
264250
createdAt: new Date('2024-01-01'), // Before release date
265-
}
251+
} as unknown as User
266252

267253
mocks.findById.mockResolvedValueOnce(mockUser)
268254

269-
await sendOnboardingEmail(mockUser.id)
255+
await sendOnboardingEmail(mockUser)
270256
expect(axios.post).not.toHaveBeenCalled()
271257
})
272258

@@ -275,11 +261,11 @@ describe('Auth helpers', () => {
275261
id: 'test-id',
276262
277263
createdAt: new Date('2025-03-01'), // After release date
278-
}
264+
} as unknown as User
279265

280266
mocks.findById.mockResolvedValueOnce(mockUser)
281267

282-
await sendOnboardingEmail(mockUser.id)
268+
await sendOnboardingEmail(mockUser)
283269
expect(axios.post).not.toHaveBeenCalled()
284270
})
285271

@@ -296,7 +282,7 @@ describe('Auth helpers', () => {
296282
mocks.findById.mockResolvedValueOnce(mockUser)
297283
vi.mocked(axios.post).mockResolvedValueOnce({ data: {} })
298284

299-
await sendOnboardingEmail(mockUser.id)
285+
await sendOnboardingEmail(mockUser)
300286

301287
expect(axios.post).toHaveBeenCalledWith(
302288
appConfig.onboardingEmailWebhookUrl,
@@ -319,7 +305,7 @@ describe('Auth helpers', () => {
319305

320306
mocks.findById.mockResolvedValueOnce(mockUser)
321307

322-
await sendOnboardingEmail(mockUser.id)
308+
await sendOnboardingEmail(mockUser)
323309
expect(axios.post).not.toHaveBeenCalled()
324310
})
325311
})

packages/backend/src/helpers/auth.ts

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -63,16 +63,7 @@ export async function getOrCreateUser(email: string): Promise<User> {
6363
return user
6464
}
6565

66-
export async function sendOnboardingEmail(id: string) {
67-
if (!id) {
68-
throw new Error('User id required!')
69-
}
70-
71-
const user = await User.query().findById(id)
72-
if (!user) {
73-
throw new Error('User not found!')
74-
}
75-
66+
export async function sendOnboardingEmail(user: User) {
7667
// check if user has logged in before and has been created
7768
// after the specified date for the release of onboarding email
7869
if (
@@ -82,7 +73,7 @@ export async function sendOnboardingEmail(id: string) {
8273
return
8374
}
8475
// call plumber webhook to send onboarding email only in prod
85-
if (appConfig.isProd && appConfig.onboardingEmailWebhookUrl) {
76+
if (!appConfig.isDev && appConfig.onboardingEmailWebhookUrl) {
8677
await axios.post(appConfig.onboardingEmailWebhookUrl, {
8778
email: user.email,
8879
})

0 commit comments

Comments
 (0)