Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(Analytics Controller) Issues in sending csv email and adding proper error codes and swagger improvements #1735

Merged
merged 3 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion backend/packages/Upgrade/.env.test
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ TOKEN_SECRET_KEY = <replaceme>
#
# Email
#
EMAIL_FROM = "[email protected]",
EMAIL_FROM = "[email protected]"
EMAIL_EXPIRE_AFTER_SECONDS = 36000
EMAIL_BUCKET = "upgrade-csv-upload"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down
18 changes: 14 additions & 4 deletions backend/packages/Upgrade/src/api/services/AnalyticsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: '',
Expand Down Expand Up @@ -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 {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try catch should only wrap sendEmail function

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 },
Expand All @@ -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.';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ describe('Analytics Controller Testing', () => {
beforeAll(() => {
configureLogger();
routingUseContainer(Container);
//ormUseContainer(Container);
classValidatorUseContainer(Container);

// set mock container
Expand Down
Loading