Skip to content

Commit

Permalink
Feat/file download in manual entry of records (#841)
Browse files Browse the repository at this point in the history
  • Loading branch information
chavda-bhavik authored Oct 11, 2024
2 parents 9b0f2cf + cfa4df9 commit c3e0e51
Show file tree
Hide file tree
Showing 6 changed files with 16,047 additions and 11,726 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ export class StartProcess {
this.queueService.publishToQueue(QueuesEnum.END_IMPORT, {
uploadId: _uploadId,
destination: destination,
uploadedFileId: uploadInfo._uploadedFileId,
});

return { uploadInfo, importedData, email };
Expand Down
1 change: 1 addition & 0 deletions apps/queue-manager/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"axios": "1.6.2",
"dotenv": "^16.0.2",
"envalid": "^7.3.1",
"papaparse": "^5.4.1",
"xml2js": "^0.6.2"
},
"devDependencies": {
Expand Down
46 changes: 43 additions & 3 deletions apps/queue-manager/src/consumers/end-import.consumer.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
import { DalService, UploadRepository } from '@impler/dal';
import { DalService, FileRepository, UploadRepository } from '@impler/dal';
import { QueuesEnum, EndImportData, FileMimeTypesEnum, DestinationsEnum } from '@impler/shared';
import { FileNameService, PaymentAPIService, StorageService } from '@impler/services';

import { BaseConsumer } from './base.consumer';
import { publishToQueue } from '../bootstrap';
import * as Papa from 'papaparse';
import { getStorageServiceClass } from '../helpers/serivces.helper';

export class EndImportConsumer extends BaseConsumer {
private dalService: DalService = new DalService();
private fileRepository: FileRepository = new FileRepository();
private fileNameService: FileNameService = new FileNameService();
private storageService: StorageService = getStorageServiceClass();
private paymentAPIService: PaymentAPIService = new PaymentAPIService();
private uploadRepository: UploadRepository = new UploadRepository();

async message(message: { content: string }) {
const data = JSON.parse(message.content) as EndImportData;
await this.convertRecordsToJsonFile(data.uploadId);
await this.convertRecordsToJsonFile(data.uploadId, data.uploadedFileId);
const userEmail = await this.uploadRepository.getUserEmailFromUploadId(data.uploadId);

const dataProcessingAllowed = await this.paymentAPIService.checkEvent({
Expand All @@ -35,10 +37,48 @@ export class EndImportConsumer extends BaseConsumer {
}
}

private async convertRecordsToJsonFile(uploadId: string) {
private async convertRecordsToJsonFile(uploadId: string, uploadedFileId?: string): Promise<void> {
const importData = await this.dalService.getAllRecords(uploadId);
const allJsonDataFilePath = this.fileNameService.getAllJsonDataFilePath(uploadId);
await this.storageService.uploadFile(allJsonDataFilePath, JSON.stringify(importData), FileMimeTypesEnum.JSON);
await this.dalService.dropRecordCollection(uploadId);

if (!uploadedFileId) {
const csvData = await this.convertImportDataToCsv(importData);
const importedFileName = this.fileNameService.getImportedFileName(uploadId);
const filePath = this.fileNameService.getImportedFilePath(uploadId);
const uploadedFileEntry = await this.fileRepository.create({
mimeType: FileMimeTypesEnum.CSV,
name: importedFileName,
originalName: importedFileName,
path: filePath,
});
await this.storageService.uploadFile(filePath, csvData, FileMimeTypesEnum.CSV);
await this.uploadRepository.findOneAndUpdate(
{
_id: uploadId,
},
{
$set: {
_uploadedFileId: uploadedFileEntry._id,
originalFileName: importedFileName,
originalFileType: FileMimeTypesEnum.CSV,
},
}
);
}
}

async convertImportDataToCsv(importData) {
const recordsData = importData.map((item) => item.record);

const csv = Papa.unparse(recordsData, {
header: true,
quotes: true,
quoteChar: '"',
escapeChar: '"',
});

return csv;
}
}
6 changes: 6 additions & 0 deletions libs/services/src/name/file-name.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ export class FileNameService {
getUploadedFilePath(uploadId: string, fileName: string): string {
return `${uploadId}/${this.getUploadedFileName(fileName)}`;
}
getImportedFileName(uploadId: string) {
return `imported-${uploadId}.csv`;
}
getImportedFilePath(uploadId: string) {
return `${uploadId}/${this.getImportedFileName(uploadId)}`;
}
getUploadedFileName(fileName: string): string {
return `uploaded.${this.getFileExtension(fileName)}`;
}
Expand Down
1 change: 1 addition & 0 deletions libs/shared/src/types/upload/upload.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ export type PublishToQueueData = SendWebhookData;

export type EndImportData = {
uploadId: string;
uploadedFileId?: string;
destination: DestinationsEnum;
};

Expand Down
Loading

0 comments on commit c3e0e51

Please sign in to comment.