From 46a4556c9076562189aeb941bdd7b1e830042b72 Mon Sep 17 00:00:00 2001 From: Michal Plebanski Date: Mon, 21 Jun 2021 10:35:33 +0200 Subject: [PATCH] Throw 429 on application creation retry failure --- .../application-flagged-sets.service.ts | 7 ++- .../src/applications/applications.service.ts | 50 ++++++++++++------- 2 files changed, 38 insertions(+), 19 deletions(-) diff --git a/backend/core/src/application-flagged-sets/application-flagged-sets.service.ts b/backend/core/src/application-flagged-sets/application-flagged-sets.service.ts index 8aa95dacf0..cf0dbed6b3 100644 --- a/backend/core/src/application-flagged-sets/application-flagged-sets.service.ts +++ b/backend/core/src/application-flagged-sets/application-flagged-sets.service.ts @@ -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 }) } diff --git a/backend/core/src/applications/applications.service.ts b/backend/core/src/applications/applications.service.ts index 0cbc942dfc..4d5966a0eb 100644 --- a/backend/core/src/applications/applications.service.ts +++ b/backend/core/src/applications/applications.service.ts @@ -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" @@ -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) {