mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-15 12:20:31 +00:00
feat: import sheet files on initializing demo account
This commit is contained in:
@@ -7,6 +7,7 @@ import { OneClickDemo } from '@/system/models/OneclickDemo';
|
||||
import { SystemUser } from '@/system/models';
|
||||
import { IAuthSignInPOJO } from '@/interfaces';
|
||||
import { ICreateOneClickDemoPOJO } from './interfaces';
|
||||
import { initalizeTenantServices } from '@/api/middleware/TenantDependencyInjection';
|
||||
|
||||
@Service()
|
||||
export class CreateOneClickDemo {
|
||||
@@ -33,6 +34,9 @@ export class CreateOneClickDemo {
|
||||
const tenantId = signedIn.tenant.id;
|
||||
const userId = signedIn.user.id;
|
||||
|
||||
// Injects the given tenant IoC services.
|
||||
await initalizeTenantServices(tenantId);
|
||||
|
||||
// Creates a new one-click demo.
|
||||
await OneClickDemo.query().insert({ key: demoId, tenantId, userId });
|
||||
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
|
||||
|
||||
|
||||
export class SeedDemoAbstract{
|
||||
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
import { SeedDemoAbstract } from './SeedDemoAbstract';
|
||||
|
||||
export class SeedDemoAccountItems extends SeedDemoAbstract {
|
||||
/**
|
||||
* Retrieves the seeder file mapping.
|
||||
*/
|
||||
get mapping() {
|
||||
return [
|
||||
{ from: 'Item Type', to: 'type' },
|
||||
{ from: 'Item Name', to: 'name' },
|
||||
{ from: 'Item Code', to: 'code' },
|
||||
{ from: 'Sellable', to: 'sellable' },
|
||||
{ from: 'Purchasable', to: 'purchasable' },
|
||||
{ from: 'Sell Price', to: 'sellPrice' },
|
||||
{ from: 'Cost Price', to: 'cost_price' },
|
||||
{ from: 'Cost Account', to: 'costAccount' },
|
||||
{ from: 'Sell Account', to: 'sellAccount' },
|
||||
{ from: 'Inventory Account', to: 'inventoryAccount' },
|
||||
{ from: 'Sell Description', to: 'sellDescription' },
|
||||
{ from: 'Purchase Description', to: 'purchaseDescription' },
|
||||
{ from: 'Note', to: 'note' },
|
||||
{ from: 'Category', to: 'category' },
|
||||
{ from: 'Active', to: 'active' },
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the seeder file name.
|
||||
* @returns {string}
|
||||
*/
|
||||
get importFileName() {
|
||||
return `items.csv`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve the resource name of the seeder.
|
||||
* @returns {string}
|
||||
*/
|
||||
get resource() {
|
||||
return 'Item';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
import { Inject } from 'typedi';
|
||||
import { promises as fs } from 'fs';
|
||||
import path from 'path';
|
||||
import events from '@/subscribers/events';
|
||||
import { PromisePool } from '@supercharge/promise-pool';
|
||||
import { IOrganizationBuildEventPayload } from '@/interfaces';
|
||||
import { SeedDemoAccountItems } from '../DemoSeeders/SeedDemoItems';
|
||||
import { ImportResourceApplication } from '@/services/Import/ImportResourceApplication';
|
||||
import { getImportsStoragePath } from '@/services/Import/_utils';
|
||||
import { OneClickDemo } from '@/system/models/OneclickDemo';
|
||||
|
||||
export class SeedInitialDemoAccountDataOnOrgBuild {
|
||||
@Inject()
|
||||
private importApp: ImportResourceApplication;
|
||||
|
||||
/**
|
||||
* Attaches events with handlers.
|
||||
*/
|
||||
public attach = (bus) => {
|
||||
bus.subscribe(
|
||||
events.organization.build,
|
||||
this.seedInitialDemoAccountDataOnOrgBuild.bind(this)
|
||||
);
|
||||
};
|
||||
|
||||
/**
|
||||
* Demo account seeder.
|
||||
*/
|
||||
get seedDemoAccountSeeders() {
|
||||
return [SeedDemoAccountItems];
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize the seeder sheet file to the import storage first.
|
||||
* @param {string} fileName -
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
async initiateSeederFile(fileName: string) {
|
||||
const destination = path.join(getImportsStoragePath(), fileName);
|
||||
const source = path.join(global.__views_dir, `/demo-sheets`, fileName);
|
||||
|
||||
// Use the fs.promises.copyFile method to copy the file
|
||||
await fs.copyFile(source, destination);
|
||||
}
|
||||
|
||||
/**
|
||||
* Seeds initial demo account data on organization build
|
||||
* @param {IOrganizationBuildEventPayload}
|
||||
*/
|
||||
async seedInitialDemoAccountDataOnOrgBuild({
|
||||
tenantId,
|
||||
}: IOrganizationBuildEventPayload) {
|
||||
const foundDemo = await OneClickDemo.query().findOne('tenantId', tenantId);
|
||||
|
||||
// Can't continue if the found demo is not exists.
|
||||
// Means that account is not demo account.
|
||||
if (!foundDemo) {
|
||||
return null;
|
||||
}
|
||||
const results = await PromisePool.for(this.seedDemoAccountSeeders)
|
||||
.withConcurrency(1)
|
||||
.process(async (SeedDemoAccountSeeder) => {
|
||||
const seederInstance = new SeedDemoAccountSeeder();
|
||||
|
||||
await this.initiateSeederFile(seederInstance.importFileName);
|
||||
|
||||
const importedFile = await this.importApp.import(
|
||||
tenantId,
|
||||
seederInstance.resource,
|
||||
seederInstance.importFileName,
|
||||
{}
|
||||
);
|
||||
await this.importApp.mapping(
|
||||
tenantId,
|
||||
importedFile.import.importId,
|
||||
seederInstance.mapping
|
||||
);
|
||||
await this.importApp.process(tenantId, importedFile.import.importId);
|
||||
});
|
||||
|
||||
if (results.errors) {
|
||||
throw results.errors;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user