Skip to content

Commit

Permalink
Throw 429 on application creation retry failure
Browse files Browse the repository at this point in the history
  • Loading branch information
pbn4 committed Jun 21, 2021
1 parent 38ff6b8 commit 46a4556
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,21 +83,24 @@ export class ApplicationFlaggedSetsService {
const appsToBeResolved = afs.applications.filter((afsApp) =>
dto.applications.map((appIdDto) => appIdDto.id).includes(afsApp.id)
)

const appsNotToBeResolved = afs.applications.filter(
(afsApp) => !dto.applications.map((appIdDto) => appIdDto.id).includes(afsApp.id)
)

for (const appToBeResolved of appsToBeResolved) {
appToBeResolved.markedAsDuplicate = true
await transApplicationsRepository.save(appToBeResolved)
}

for (const appNotToBeResolved of appsNotToBeResolved) {
appNotToBeResolved.markedAsDuplicate = false
await transApplicationsRepository.save(appNotToBeResolved)
}

await transApplicationsRepository.save([...appsToBeResolved, ...appsNotToBeResolved])

appsToBeResolved.forEach((app) => (app.markedAsDuplicate = true))
await transAfsRepository.save(afs)

return afs
})
}
Expand Down
50 changes: 33 additions & 17 deletions backend/core/src/applications/applications.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Inject, Injectable, Scope } from "@nestjs/common"
import { HttpException, HttpStatus, Inject, Injectable, Scope } from "@nestjs/common"
import { Application } from "./entities/application.entity"
import { ApplicationCreateDto, ApplicationUpdateDto } from "./dto/application.dto"
import { InjectRepository } from "@nestjs/typeorm"
Expand Down Expand Up @@ -158,23 +158,39 @@ export class ApplicationsService {

private async _create(applicationCreateDto: ApplicationUpdateDto) {
let application: Application
await retry(
async (bail) => {
try {
application = await this._createApplication(applicationCreateDto)
} catch (e) {
console.error(e.message)
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
if (!(e instanceof QueryFailedError && e.code === "40001")) {
// 40001: could not serialize access due to read/write dependencies among transactions
bail(e)

try {
await retry(
async (bail) => {
try {
application = await this._createApplication(applicationCreateDto)
} catch (e) {
console.error(e.message)
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
if (!(e instanceof QueryFailedError && e.code === "40001")) {
// 40001: could not serialize access due to read/write dependencies among transactions
bail(e)
}
throw e
}
throw e
}
},
{ retries: 5, minTimeout: 200 }
)
},
{ retries: 6, minTimeout: 200 }
)
} catch (e) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
if (e instanceof QueryFailedError && e.code === "40001") {
throw new HttpException(
{
statusCode: HttpStatus.TOO_MANY_REQUESTS,
error: "Too Many Requests",
message: "Please try again later.",
},
429
)
}
}

const listing = await this.listingsService.findOne(application.listing.id)
if (application.applicant.emailAddress) {
Expand Down

0 comments on commit 46a4556

Please sign in to comment.