1+ /* eslint-disable multiline-comment-style */
12import { Model } from 'mongoose' ;
23import { Writable } from 'stream' ;
34import { Injectable , BadRequestException , InternalServerErrorException } from '@nestjs/common' ;
45
56import { APIMessages } from '@shared/constants' ;
6- import { EMAIL_SUBJECT } from '@impler/shared' ;
7+ import { BILLABLEMETRIC_CODE_ENUM , EMAIL_SUBJECT } from '@impler/shared' ;
78import { BaseReview } from './base-review.usecase' ;
89import { UniqueWithValidationType , ValidationTypesEnum } from '@impler/client' ;
910import { BATCH_LIMIT } from '@shared/services/sandbox' ;
@@ -16,7 +17,9 @@ import {
1617 DalService ,
1718 TemplateEntity ,
1819 TemplateRepository ,
20+ EnvironmentRepository ,
1921} from '@impler/dal' ;
22+ import { UsageLimitExceededException } from '@shared/exceptions/import-limit-exceeded.exception' ;
2023
2124interface ISaveResults {
2225 uploadId : string ;
@@ -32,6 +35,7 @@ export class DoReview extends BaseReview {
3235
3336 constructor (
3437 private templateRepository : TemplateRepository ,
38+ private environmentRepository : EnvironmentRepository ,
3539 private storageService : StorageService ,
3640 private uploadRepository : UploadRepository ,
3741 private validatorRepository : ValidatorRepository ,
@@ -44,6 +48,7 @@ export class DoReview extends BaseReview {
4448 }
4549
4650 async execute ( _uploadId : string ) {
51+ console . log ( 'Called The Do Review.execute' ) ;
4752 this . _modal = this . dalService . getRecordCollection ( _uploadId ) ;
4853 const userEmail = await this . uploadRepository . getUserEmailFromUploadId ( _uploadId ) ;
4954
@@ -205,7 +210,28 @@ export class DoReview extends BaseReview {
205210 throw new InternalServerErrorException ( APIMessages . ERROR_DURING_VALIDATION ) ;
206211 }
207212
208- await this . saveResults ( response ) ;
213+ try {
214+ await this . saveResults ( response ) ;
215+ } catch ( error ) {
216+ const emailContents = this . emailService . getEmailContent ( {
217+ type : 'IMPORT_LIMIT_EXCEEDED_EMAIL' ,
218+ data : {
219+ limitType : 'Import Rows' ,
220+ currentUsage : 'All available units including grace percentage' ,
221+ planName : 'Current Plan' ,
222+ upgradeUrl : `${ process . env . WEB_BASE_URL } /pricing` ,
223+ } ,
224+ } ) ;
225+ await this . emailService . sendEmail ( {
226+ from : process . env . EMAIL_FROM ,
227+ html : emailContents ,
228+ subject : 'Usage limit exceeded' ,
229+ to : userEmail ,
230+ senderName : process . env . EMAIL_FROM_NAME ,
231+ } ) ;
232+
233+ throw new UsageLimitExceededException ( error . message ) ;
234+ }
209235
210236 return response ;
211237 }
@@ -235,32 +261,66 @@ export class DoReview extends BaseReview {
235261 }
236262
237263 private async saveResults ( { uploadId, totalRecords, validRecords, invalidRecords, _templateId } : ISaveResults ) {
238- await this . uploadRepository . update (
239- { _id : uploadId } ,
240- {
241- status : UploadStatusEnum . REVIEWING ,
242- totalRecords,
243- validRecords,
244- invalidRecords,
245- }
246- ) ;
247- await this . templateRepository . findOneAndUpdate (
248- {
249- _id : _templateId ,
250- } ,
251- {
252- $inc : {
253- totalUploads : 1 ,
254- totalRecords : totalRecords ,
255- totalInvalidRecords : invalidRecords ,
256- } ,
257- }
258- ) ;
259264 const userExternalIdOrEmail = await this . uploadRepository . getUserEmailFromUploadId ( uploadId ) ;
260265
261- await this . paymentAPIService . createEvent (
262- { uploadId, totalRecords, validRecords, invalidRecords } ,
263- userExternalIdOrEmail
264- ) ;
266+ try {
267+ await this . paymentAPIService . createEvent (
268+ { units : totalRecords , billableMetricCode : BILLABLEMETRIC_CODE_ENUM . ROWS } ,
269+ userExternalIdOrEmail
270+ ) ;
271+
272+ // Only update database if payment event creation succeeds
273+ await this . uploadRepository . update (
274+ { _id : uploadId } ,
275+ {
276+ status : UploadStatusEnum . REVIEWING ,
277+ totalRecords,
278+ validRecords,
279+ invalidRecords,
280+ }
281+ ) ;
282+
283+ await this . templateRepository . findOneAndUpdate (
284+ {
285+ _id : _templateId ,
286+ } ,
287+ {
288+ $inc : {
289+ totalUploads : 1 ,
290+ totalRecords : totalRecords ,
291+ totalInvalidRecords : invalidRecords ,
292+ } ,
293+ }
294+ ) ;
295+ } catch ( error ) {
296+ const template = await this . templateRepository . findById ( _templateId ) ;
297+ const environment = await this . environmentRepository . getProjectTeamMembers ( template . _projectId ) ;
298+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
299+ // @ts -ignore
300+ const teamMemberEmails = environment . map ( ( teamMember ) => teamMember . _userId . email ) ;
301+ console . log ( teamMemberEmails ) ;
302+
303+ const emailContents = this . emailService . getEmailContent ( {
304+ type : 'IMPORT_LIMIT_EXCEEDED_EMAIL' ,
305+ data : {
306+ limitType : 'Import Rows' ,
307+ currentUsage : 'All available units including grace percentage' ,
308+ planName : 'Current Plan' ,
309+ upgradeUrl : `${ process . env . WEB_BASE_URL } /pricing` ,
310+ } ,
311+ } ) ;
312+
313+ teamMemberEmails . forEach ( async ( email ) => {
314+ await this . emailService . sendEmail ( {
315+ to : email ,
316+ subject : EMAIL_SUBJECT . IMPORT_LIMIT_EXCEEDED ,
317+ html : emailContents ,
318+ from : process . env . ALERT_EMAIL_FROM ,
319+ senderName : process . env . EMAIL_FROM_NAME ,
320+ } ) ;
321+ } ) ;
322+
323+ throw new UsageLimitExceededException ( error . message ) ;
324+ }
265325 }
266326}
0 commit comments