|
| 1 | +/* eslint-disable multiline-comment-style */ |
| 2 | +import '../../config'; |
| 3 | +import { AppModule } from '../../app.module'; |
| 4 | + |
| 5 | +import { NestFactory } from '@nestjs/core'; |
| 6 | +import { ExcelFileService } from '@shared/services'; |
| 7 | +import { ColumnRepository, TemplateRepository } from '@impler/dal'; |
| 8 | +import { AzureStorageService, FileNameService } from '@impler/services'; |
| 9 | +import { ColumnTypesEnum, FileMimeTypesEnum, ColumnDelimiterEnum } from '@impler/shared'; |
| 10 | +import { IExcelFileHeading } from '@shared/types/file.types'; |
| 11 | + |
| 12 | +export async function run() { |
| 13 | + let app; |
| 14 | + |
| 15 | + try { |
| 16 | + console.log('start migration - regenerating sample excel files for all templates'); |
| 17 | + |
| 18 | + app = await NestFactory.create(AppModule, { |
| 19 | + logger: false, |
| 20 | + }); |
| 21 | + |
| 22 | + // Create services manually (since DI is not working for these services) |
| 23 | + const templateRepository = new TemplateRepository(); |
| 24 | + const columnRepository = new ColumnRepository(); |
| 25 | + const excelFileService = new ExcelFileService(); |
| 26 | + const storageService = new AzureStorageService(); |
| 27 | + const fileNameService = new FileNameService(); |
| 28 | + |
| 29 | + // Check if Azure connection string is available |
| 30 | + const azureConnectionString = process.env.AZURE_STORAGE_CONNECTION_STRING; |
| 31 | + let azureStorageService = null; |
| 32 | + |
| 33 | + if (azureConnectionString) { |
| 34 | + try { |
| 35 | + azureStorageService = new AzureStorageService(); |
| 36 | + } catch (error) {} |
| 37 | + } |
| 38 | + |
| 39 | + const templates = await templateRepository.find( |
| 40 | + { |
| 41 | + sampleFileUrl: { |
| 42 | + $exists: true, |
| 43 | + $nin: ['', null], |
| 44 | + }, |
| 45 | + }, |
| 46 | + 'sampleFileUrl name' |
| 47 | + ); |
| 48 | + |
| 49 | + if (templates.length === 0) { |
| 50 | + return; |
| 51 | + } |
| 52 | + |
| 53 | + const templateIds = templates.map((template) => template._id); |
| 54 | + |
| 55 | + const columns = await columnRepository.find({ |
| 56 | + _templateId: { |
| 57 | + $in: templateIds, |
| 58 | + }, |
| 59 | + }); |
| 60 | + |
| 61 | + for (let i = 0; i < templates.length; i++) { |
| 62 | + const template = templates[i]; |
| 63 | + try { |
| 64 | + const templateColumns = columns.filter((column) => column._templateId.toString() === template._id.toString()); |
| 65 | + |
| 66 | + if (templateColumns.length > 0) { |
| 67 | + const columnKeys: IExcelFileHeading[] = templateColumns |
| 68 | + .map((columnItem) => ({ |
| 69 | + key: columnItem.key, |
| 70 | + type: columnItem.type as ColumnTypesEnum, |
| 71 | + selectValues: columnItem.selectValues, |
| 72 | + isFrozen: columnItem.isFrozen, |
| 73 | + delimiter: columnItem.delimiter || ColumnDelimiterEnum.COMMA, |
| 74 | + isRequired: columnItem.isRequired, |
| 75 | + dateFormats: columnItem.dateFormats, |
| 76 | + allowMultiSelect: columnItem.allowMultiSelect, |
| 77 | + description: columnItem.description, |
| 78 | + })) |
| 79 | + .sort((a, b) => (a.isFrozen === b.isFrozen ? 0 : a.isFrozen ? -1 : 1)); |
| 80 | + |
| 81 | + const hasMultiSelect = templateColumns.some( |
| 82 | + (columnItem) => columnItem.type === ColumnTypesEnum.SELECT && columnItem.allowMultiSelect |
| 83 | + ); |
| 84 | + |
| 85 | + const fileName = fileNameService.getSampleFileName(template._id, hasMultiSelect); |
| 86 | + const sampleFileUrl = fileNameService.getSampleFileUrl(template._id, hasMultiSelect); |
| 87 | + |
| 88 | + const sampleExcelFile = await excelFileService.getExcelFileForHeadings(columnKeys); |
| 89 | + |
| 90 | + await storageService.uploadFile(fileName, sampleExcelFile, FileMimeTypesEnum.EXCELM); |
| 91 | + |
| 92 | + await templateRepository.update({ _id: template._id }, { sampleFileUrl }); |
| 93 | + |
| 94 | + if (azureStorageService) { |
| 95 | + try { |
| 96 | + await azureStorageService.uploadFile(template.sampleFileUrl, sampleExcelFile, template._id); |
| 97 | + } catch (error) {} |
| 98 | + } |
| 99 | + } |
| 100 | + } catch (error) { |
| 101 | + continue; |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + console.log('end migration - regenerating sample excel files for all templates'); |
| 106 | + } catch (error) { |
| 107 | + process.exit(1); |
| 108 | + } finally { |
| 109 | + if (app) { |
| 110 | + await app.close(); |
| 111 | + } |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +run() |
| 116 | + .then(() => { |
| 117 | + console.log('Migration completed successfully'); |
| 118 | + process.exit(0); |
| 119 | + }) |
| 120 | + .catch((error) => { |
| 121 | + console.error('Migration failed:', error); |
| 122 | + process.exit(1); |
| 123 | + }); |
0 commit comments