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

Feat/file download in manual entry of records #841

Merged
merged 6 commits into from
Oct 11, 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
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
Loading