Skip to content

Commit

Permalink
Merge branch 'main' into feat/update-income-type
Browse files Browse the repository at this point in the history
  • Loading branch information
veronikasif authored Sep 13, 2024
2 parents cde800a + 2c7c9c0 commit ec97dd2
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -816,21 +816,23 @@ describe('User profile API', () => {
)
})

it('POST /userProfile/{nationalId}/device-tokens duplicate token should return 400 bad request', async () => {
it('POST /userProfile/{nationalId}/device-tokens duplicate token should return the existing token', async () => {
// create it
await request(app.getHttpServer())
const res1 = await request(app.getHttpServer())
.post(`/userProfile/${mockProfile.nationalId}/device-tokens`)
.send({
deviceToken: mockDeviceToken.deviceToken,
})
.expect(201)
// try to create same again
await request(app.getHttpServer())
const res2 = await request(app.getHttpServer())
.post(`/userProfile/${mockProfile.nationalId}/device-tokens`)
.send({
deviceToken: mockDeviceToken.deviceToken,
})
.expect(400)
.expect(201)

expect(res1.body).toEqual(res2.body)
})

it('POST /userProfile/{nationalId}/device-tokens with missing payload should 400 bad request', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,10 @@ export class UserProfileController {
} else {
// findOrCreateUserProfile for edge cases - fragmented onboarding
await this.findOrCreateUserProfile(nationalId, user)
return await this.userProfileService.addDeviceToken(body, user)
// The behaviour of returning the token if it already exists is not following API Design Guide
// It should respond with 303 See Other and a Location header to the existing resource
// But as the v1 of the user profile is not following this, we will keep the same behaviour.
return this.userProfileService.addDeviceToken(body, user)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ export class UserProfileService {

async addDeviceToken(body: DeviceTokenDto, user: User) {
try {
const token = await this.userDeviceTokensModel.findOne({
where: { nationalId: user.nationalId, deviceToken: body.deviceToken },
})

if (token) {
return token
}

return await this.userDeviceTokensModel.create({
...body,
nationalId: user.nationalId,
Expand Down

0 comments on commit ec97dd2

Please sign in to comment.