diff --git a/backend/packages/Upgrade/.env.test b/backend/packages/Upgrade/.env.test index d04135e853..22b925bd5a 100644 --- a/backend/packages/Upgrade/.env.test +++ b/backend/packages/Upgrade/.env.test @@ -77,7 +77,7 @@ TOKEN_SECRET_KEY = # # Email # -EMAIL_FROM = "Name@companyemail.com", +EMAIL_FROM = "Name@companyemail.com" EMAIL_EXPIRE_AFTER_SECONDS = 36000 EMAIL_BUCKET = "upgrade-csv-upload" diff --git a/backend/packages/Upgrade/src/api/controllers/AnalyticsController.ts b/backend/packages/Upgrade/src/api/controllers/AnalyticsController.ts index 4232149a8b..8982e3b23b 100644 --- a/backend/packages/Upgrade/src/api/controllers/AnalyticsController.ts +++ b/backend/packages/Upgrade/src/api/controllers/AnalyticsController.ts @@ -66,6 +66,12 @@ export class AnalyticsController { * type: string * minLength: 1 * example: exp01 + * '400': + * description: BadRequestError - InvalidParameterValue + * '401': + * description: AuthorizationRequiredError + * '500': + * description: Internal Server Error */ @Post('/enrollment') public async analyticsService( @@ -157,6 +163,12 @@ export class AnalyticsController { * groups: * type: number * example: 3 + * '400': + * description: BadRequestError - InvalidParameterValue + * '401': + * description: AuthorizationRequiredError + * '500': + * description: Internal Server Error */ @Post('/enrollment/detail') public async analyticsDetailService( @@ -247,6 +259,12 @@ export class AnalyticsController { * required: * - id * - conditions + * '400': + * description: BadRequestError - InvalidParameterValue + * '401': + * description: AuthorizationRequiredError + * '500': + * description: Internal Server Error */ @Post('/enrollment/date') public async enrollmentByDate( @@ -276,12 +294,18 @@ export class AnalyticsController { * type: string * email: * type: string - * description: Get Csv files in given mail id + * description: Export CSV data file to the given mail id * tags: * - Analytics * responses: * '200': - * description: Get CSV files + * description: Export CSV data file to the given mail id + * '400': + * description: BadRequestError - InvalidParameterValue + * '401': + * description: AuthorizationRequiredError + * '500': + * description: Internal Server Error */ @Get('/csv') public async downloadCSV( diff --git a/backend/packages/Upgrade/src/api/services/AnalyticsService.ts b/backend/packages/Upgrade/src/api/services/AnalyticsService.ts index 4580027a9a..b602d542f5 100644 --- a/backend/packages/Upgrade/src/api/services/AnalyticsService.ts +++ b/backend/packages/Upgrade/src/api/services/AnalyticsService.ts @@ -376,8 +376,8 @@ export class AnalyticsService { const email_from = env.email.from; const fileName = `${folderPath}${simpleExportCSV}`; - if (!fs.existsSync(fileName)) { - // if file doesn't exist create a empty file + if (!fs.existsSync(fileName) || csvExportData.length === 0) { + // if file doesn't exist or no data, create an empty file const csvRows = [ { ExperimentId: '', @@ -431,7 +431,17 @@ export class AnalyticsService { const emailSubject = `Exported Data for the experiment: ${experimentQueryResult[0].experimentName}`; // send email to the user logger.info({ message: `Sending export data email to ${email}` }); - await this.awsService.sendEmail(email_from, email, emailText, emailSubject); + try { + await this.awsService.sendEmail(email_from, email, emailText, emailSubject); + } catch (err) { + const error = { + type: SERVER_ERROR.EMAIL_SEND_ERROR, + message: 'Email send error', + httpCode: 500, + }; + logger.error({ message: `Export Data email unsuccessful:`, details: error }); + throw error; + } await this.experimentAuditLogRepository.saveRawJson( EXPERIMENT_LOG_TYPE.EXPERIMENT_DATA_EXPORTED, { experimentName: experimentQueryResult[0].experimentName }, @@ -447,6 +457,6 @@ export class AnalyticsService { logger.info({ message: 'Completing experiment data export' }); - return ''; + return 'Exported CSV data sent, you should receive an email shortly.'; } } diff --git a/backend/packages/Upgrade/test/unit/controllers/AnalyticsController.test.ts b/backend/packages/Upgrade/test/unit/controllers/AnalyticsController.test.ts index ae0bf9b98a..e57521d053 100644 --- a/backend/packages/Upgrade/test/unit/controllers/AnalyticsController.test.ts +++ b/backend/packages/Upgrade/test/unit/controllers/AnalyticsController.test.ts @@ -14,7 +14,6 @@ describe('Analytics Controller Testing', () => { beforeAll(() => { configureLogger(); routingUseContainer(Container); - //ormUseContainer(Container); classValidatorUseContainer(Container); // set mock container