Skip to content
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
32 changes: 22 additions & 10 deletions backend/packages/Upgrade/src/api/services/AnalyticsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,22 @@ export class AnalyticsService {

public async getCSVData(experimentId: string, email: string, logger: UpgradeLogger): Promise<string> {
logger.info({ message: `Inside getCSVData ${experimentId} , ${email}` });

const experimentDetails: ExperimentDetailsForCSVData[] =
await this.experimentService.getExperimentDetailsForCSVDataExport(experimentId);
if (!experimentDetails || experimentDetails.length === 0) {
throw new HttpError(404, `Experiment not found for id: ${experimentId}`);
}

this.sendExportData(experimentDetails, email, logger);
return 'Exporting CSV data for the experiment...';
}

private async sendExportData(
experimentDetails: ExperimentDetailsForCSVData[],
email: string,
logger: UpgradeLogger
): Promise<string> {
try {
const timeStamp = new Date().toISOString();
const folderPath = 'src/api/assets/files/';
Expand All @@ -181,13 +197,6 @@ export class AnalyticsService {

const userRepository: UserRepository = Container.getCustomRepository(UserRepository, 'export');
const user = await userRepository.findOneBy({ email });

const experimentDetails: ExperimentDetailsForCSVData[] =
await this.experimentService.getExperimentDetailsForCSVDataExport(experimentId);
if (!experimentDetails || experimentDetails.length === 0) {
throw new HttpError(404, `Experiment not found for id: ${experimentId}`);
}

const experimentMap = new Map<string, ExperimentDetailsForCSVData>();

const formattedExperiment: ExperimentDetailsForCSVData[] = experimentDetails.reduce((acc, item) => {
Expand Down Expand Up @@ -225,12 +234,15 @@ export class AnalyticsService {

let csvExportData: CSVExportDataRow[];
if (experimentDetails[0].assignmentUnit === ASSIGNMENT_UNIT.WITHIN_SUBJECTS) {
csvExportData = await this.analyticsRepository.getCSVDataForWithInSubExport(experimentId);
csvExportData = await this.analyticsRepository.getCSVDataForWithInSubExport(experimentDetails[0].experimentId);
} else {
csvExportData = await this.analyticsRepository.getCSVDataForSimpleExport(formattedExperiment[0], experimentId);
csvExportData = await this.analyticsRepository.getCSVDataForSimpleExport(
formattedExperiment[0],
experimentDetails[0].experimentId
);
}

const queryData = await this.logRepository.getLogPerExperimentQuery(experimentId);
const queryData = await this.logRepository.getLogPerExperimentQuery(experimentDetails[0].experimentId);

type queryDataArrayType = typeof queryData;
type queryDataType = queryDataArrayType[0];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ describe('ExperimentEffects', () => {
};
notificationService = {
showError: jest.fn(),
showInfo: jest.fn(),
showSuccess: jest.fn(),
};
translate = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -456,8 +456,12 @@ export class ExperimentEffects {
this.experimentDataService.exportExperimentInfo(experimentId, email).pipe(
tap(() => {
email
? this.notificationService.showSuccess(`Email will be sent to ${email}`)
: this.notificationService.showSuccess('Email will be sent to registered email');
? this.notificationService.showInfo(
`Export requested. If the export succeeds, email will be sent to ${email}`
)
: this.notificationService.showInfo(
'Export requested. If the export succeeds, email will be sent to registered email'
);
}),
map(() => experimentAction.actionExportExperimentInfoSuccess()),
catchError(() => [experimentAction.actionExportExperimentInfoFailure()])
Expand Down
Loading