mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-16 12:50:38 +00:00
refactor(nestjs): import module
This commit is contained in:
@@ -30,7 +30,7 @@ export class ImportController {
|
||||
@ApiOperation({ summary: 'Upload import file' })
|
||||
@ApiResponse({ status: 200, description: 'File uploaded successfully' })
|
||||
@UseInterceptors(
|
||||
FileInterceptor('file', { storage: uploadImportFileMulterOptions }),
|
||||
FileInterceptor('file', uploadImportFileMulterOptions),
|
||||
)
|
||||
async fileUpload(
|
||||
@Res() res: Response,
|
||||
|
||||
@@ -17,10 +17,16 @@ import { TenancyModule } from '../Tenancy/Tenancy.module';
|
||||
import { AccountsModule } from '../Accounts/Accounts.module';
|
||||
import { ImportController } from './Import.controller';
|
||||
import { ImportableRegistry } from './ImportableRegistry';
|
||||
import { InjectSystemModel } from '../System/SystemModels/SystemModels.module';
|
||||
import { ImportModel } from './models/Import';
|
||||
import { ImportDeleteExpiredFilesJobs } from './jobs/ImportDeleteExpiredFilesJob';
|
||||
|
||||
const models = [InjectSystemModel(ImportModel)];
|
||||
|
||||
@Module({
|
||||
imports: [ResourceModule, TenancyModule, AccountsModule],
|
||||
providers: [
|
||||
...models,
|
||||
ImportAls,
|
||||
ImportSampleService,
|
||||
ImportResourceApplication,
|
||||
@@ -34,9 +40,10 @@ import { ImportableRegistry } from './ImportableRegistry';
|
||||
ImportFileDataValidator,
|
||||
ImportFileDataTransformer,
|
||||
ImportFileCommon,
|
||||
ImportableRegistry
|
||||
ImportableRegistry,
|
||||
ImportDeleteExpiredFilesJobs
|
||||
],
|
||||
controllers: [ImportController],
|
||||
exports: [ImportAls],
|
||||
exports: [ImportAls, ...models],
|
||||
})
|
||||
export class ImportModule {}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import bluebird from 'bluebird';
|
||||
import * as bluebird from 'bluebird';
|
||||
import * as R from 'ramda';
|
||||
import { first } from 'lodash';
|
||||
import { ImportFileDataValidator } from './ImportFileDataValidator';
|
||||
|
||||
@@ -16,7 +16,7 @@ export class ImportFileMapping {
|
||||
private readonly resource: ResourceService,
|
||||
|
||||
@Inject(ImportModel.name)
|
||||
private readonly importModel: () => typeof ImportModel,
|
||||
private readonly importModel: typeof ImportModel,
|
||||
) {}
|
||||
|
||||
/**
|
||||
@@ -28,7 +28,7 @@ export class ImportFileMapping {
|
||||
importId: string,
|
||||
maps: ImportMappingAttr[],
|
||||
): Promise<ImportFileMapPOJO> {
|
||||
const importFile = await this.importModel()
|
||||
const importFile = await this.importModel
|
||||
.query()
|
||||
.findOne('filename', importId)
|
||||
.throwIfNotFound();
|
||||
@@ -46,7 +46,7 @@ export class ImportFileMapping {
|
||||
|
||||
const mappingStringified = JSON.stringify(maps);
|
||||
|
||||
await this.importModel().query().findById(importFile.id).patch({
|
||||
await this.importModel.query().findById(importFile.id).patch({
|
||||
mapping: mappingStringified,
|
||||
});
|
||||
return {
|
||||
|
||||
@@ -27,7 +27,6 @@ export class ImportFileProcess {
|
||||
|
||||
/**
|
||||
* Preview the imported file results before commiting the transactions.
|
||||
* @param {number} tenantId
|
||||
* @param {number} importId
|
||||
* @returns {Promise<ImportFilePreviewPOJO>}
|
||||
*/
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { Inject, Injectable } from '@nestjs/common';
|
||||
import {
|
||||
deleteImportFile,
|
||||
getResourceColumns,
|
||||
@@ -10,7 +11,6 @@ import { ImportFileCommon } from './ImportFileCommon';
|
||||
import { ImportFileDataValidator } from './ImportFileDataValidator';
|
||||
import { ImportFileUploadPOJO } from './interfaces';
|
||||
import { parseSheetData } from './sheet_utils';
|
||||
import { Inject, Injectable } from '@nestjs/common';
|
||||
import { ImportModel } from './models/Import';
|
||||
import { TenancyContext } from '../Tenancy/TenancyContext.service';
|
||||
|
||||
@@ -29,9 +29,8 @@ export class ImportFileUploadService {
|
||||
/**
|
||||
* Imports the specified file for the given resource.
|
||||
* Deletes the file if an error occurs during the import process.
|
||||
* @param {number} tenantId
|
||||
* @param {string} resourceName
|
||||
* @param {string} filename
|
||||
* @param {string} resourceName - Resource name.
|
||||
* @param {string} filename - File name.
|
||||
* @param {Record<string, number | string>} params
|
||||
* @returns {Promise<ImportFileUploadPOJO>}
|
||||
*/
|
||||
|
||||
@@ -6,7 +6,6 @@ export const getImportsStoragePath = () => {
|
||||
return path.join(global.__static_dirname, `/imports`);
|
||||
};
|
||||
|
||||
|
||||
export function allowSheetExtensions(req, file, cb) {
|
||||
if (
|
||||
file.mimetype !== 'text/csv' &&
|
||||
@@ -35,5 +34,5 @@ const storage = Multer.diskStorage({
|
||||
export const uploadImportFileMulterOptions = {
|
||||
storage,
|
||||
limits: { fileSize: 5 * 1024 * 1024 },
|
||||
fileFilter: allowSheetExtensions,
|
||||
// fileFilter: allowSheetExtensions,
|
||||
};
|
||||
@@ -19,6 +19,7 @@ export class ImportDeleteExpiredFiles {
|
||||
const expiredImports = await this.importModel
|
||||
.query()
|
||||
.where('createdAt', '<', yesterday);
|
||||
|
||||
await bluebird.map(
|
||||
expiredImports,
|
||||
async (expiredImport) => {
|
||||
|
||||
@@ -434,7 +434,7 @@ export const getMapToPath = (to: string, group = '') =>
|
||||
group ? `${group}.${to}` : to;
|
||||
|
||||
export const getImportsStoragePath = () => {
|
||||
return path.join(global.__storage_dir, `/imports`);
|
||||
return path.join(global.__static_dirname, `/imports`);
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,28 +1,23 @@
|
||||
// import Container, { Service } from 'typedi';
|
||||
// import { ImportDeleteExpiredFiles } from '../ImportRemoveExpiredFiles';
|
||||
import { Cron } from '@nestjs/schedule';
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { ImportDeleteExpiredFiles } from '../ImportRemoveExpiredFiles';
|
||||
|
||||
// @Service()
|
||||
// export class ImportDeleteExpiredFilesJobs {
|
||||
// /**
|
||||
// * Constructor method.
|
||||
// */
|
||||
// constructor(agenda) {
|
||||
// agenda.define('delete-expired-imported-files', this.handler);
|
||||
// }
|
||||
@Injectable()
|
||||
export class ImportDeleteExpiredFilesJobs {
|
||||
constructor(
|
||||
private readonly importDeleteExpiredFiles: ImportDeleteExpiredFiles,
|
||||
) {}
|
||||
|
||||
// /**
|
||||
// * Triggers sending invoice mail.
|
||||
// */
|
||||
// private handler = async (job, done: Function) => {
|
||||
// const importDeleteExpiredFiles = Container.get(ImportDeleteExpiredFiles);
|
||||
|
||||
// try {
|
||||
// console.log('Delete expired import files has started.');
|
||||
// await importDeleteExpiredFiles.deleteExpiredFiles();
|
||||
// done();
|
||||
// } catch (error) {
|
||||
// console.log(error);
|
||||
// done(error);
|
||||
// }
|
||||
// };
|
||||
// }
|
||||
/**
|
||||
* Triggers sending invoice mail.
|
||||
*/
|
||||
@Cron('* * * * *')
|
||||
async importDeleteExpiredJob() {
|
||||
try {
|
||||
console.log('Delete expired import files has started.');
|
||||
await this.importDeleteExpiredFiles.deleteExpiredFiles();
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -67,7 +67,7 @@ export class ImportModel extends BaseModel {
|
||||
* Relationship mapping.
|
||||
*/
|
||||
static get relationMappings() {
|
||||
const Tenant = require('system/models/Tenant');
|
||||
const { TenantModel } = require('../../System/models/TenantModel');
|
||||
|
||||
return {
|
||||
/**
|
||||
@@ -75,7 +75,7 @@ export class ImportModel extends BaseModel {
|
||||
*/
|
||||
tenant: {
|
||||
relation: Model.BelongsToOneRelation,
|
||||
modelClass: Tenant.default,
|
||||
modelClass: TenantModel,
|
||||
join: {
|
||||
from: 'imports.tenantId',
|
||||
to: 'tenants.id',
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import XLSX from 'xlsx';
|
||||
import * as XLSX from 'xlsx';
|
||||
import { first } from 'lodash';
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user