feat: more resources support importing

This commit is contained in:
Ahmed Bouhuolia
2024-04-05 02:00:12 +02:00
parent 3851d34ba4
commit dd9098bdc1
46 changed files with 1510 additions and 149 deletions

View File

@@ -43,7 +43,8 @@ export class CreateSaleEstimate {
*/
public async createEstimate(
tenantId: number,
estimateDTO: ISaleEstimateDTO
estimateDTO: ISaleEstimateDTO,
trx?: Knex.Transaction
): Promise<ISaleEstimate> {
const { SaleEstimate, Contact } = this.tenancy.models(tenantId);
@@ -75,28 +76,32 @@ export class CreateSaleEstimate {
estimateDTO.entries
);
// Creates a sale estimate transaction with associated transactions as UOW.
return this.uow.withTransaction(tenantId, async (trx: Knex.Transaction) => {
// Triggers `onSaleEstimateCreating` event.
await this.eventPublisher.emitAsync(events.saleEstimate.onCreating, {
estimateDTO,
tenantId,
trx,
} as ISaleEstimateCreatingPayload);
return this.uow.withTransaction(
tenantId,
async (trx: Knex.Transaction) => {
// Triggers `onSaleEstimateCreating` event.
await this.eventPublisher.emitAsync(events.saleEstimate.onCreating, {
estimateDTO,
tenantId,
trx,
} as ISaleEstimateCreatingPayload);
// Upsert the sale estimate graph to the storage.
const saleEstimate = await SaleEstimate.query(trx).upsertGraphAndFetch({
...estimateObj,
});
// Triggers `onSaleEstimateCreated` event.
await this.eventPublisher.emitAsync(events.saleEstimate.onCreated, {
tenantId,
saleEstimate,
saleEstimateId: saleEstimate.id,
saleEstimateDTO: estimateDTO,
trx,
} as ISaleEstimateCreatedPayload);
// Upsert the sale estimate graph to the storage.
const saleEstimate = await SaleEstimate.query(trx).upsertGraphAndFetch({
...estimateObj,
});
// Triggers `onSaleEstimateCreated` event.
await this.eventPublisher.emitAsync(events.saleEstimate.onCreated, {
tenantId,
saleEstimate,
saleEstimateId: saleEstimate.id,
saleEstimateDTO: estimateDTO,
trx,
} as ISaleEstimateCreatedPayload);
return saleEstimate;
});
return saleEstimate;
},
trx
);
}
}

View File

@@ -41,7 +41,10 @@ export class SaleEstimateValidators {
}
});
if (foundSaleEstimate) {
throw new ServiceError(ERRORS.SALE_ESTIMATE_NUMBER_EXISTANCE);
throw new ServiceError(
ERRORS.SALE_ESTIMATE_NUMBER_EXISTANCE,
'The given sale estimate is not unique.'
);
}
}

View File

@@ -0,0 +1,45 @@
import { Inject, Service } from 'typedi';
import { Knex } from 'knex';
import { ISaleEstimateDTO } from '@/interfaces';
import { CreateSaleEstimate } from './CreateSaleEstimate';
import { Importable } from '@/services/Import/Importable';
import { SaleEstimatesSampleData } from './constants';
@Service()
export class SaleEstimatesImportable extends Importable {
@Inject()
private createEstimateService: CreateSaleEstimate;
/**
* Importing to account service.
* @param {number} tenantId
* @param {IAccountCreateDTO} createAccountDTO
* @returns
*/
public importable(
tenantId: number,
createEstimateDTO: ISaleEstimateDTO,
trx?: Knex.Transaction
) {
return this.createEstimateService.createEstimate(
tenantId,
createEstimateDTO,
trx
);
}
/**
* Concurrrency controlling of the importing process.
* @returns {number}
*/
public get concurrency() {
return 1;
}
/**
* Retrieves the sample data that used to download accounts sample sheet.
*/
public sampleData(): any[] {
return SaleEstimatesSampleData;
}
}

View File

@@ -122,3 +122,54 @@ export const DEFAULT_VIEWS = [
columns: DEFAULT_VIEW_COLUMNS,
},
];
export const SaleEstimatesSampleData = [
{
Customer: 'Ambrose Olson',
'Estimate Date': '2024-01-01',
'Expiration Date': '2025-01-01',
'Estimate No.': 'EST-0001',
'Reference No.': 'REF-0001',
Currency: '',
'Exchange Rate': '',
Note: 'Vel autem quis aut ab.',
'Terms & Conditions': 'Provident illo architecto sit iste in.',
Delivered: 'T',
'Item Name': 'Hettinger, Schumm and Bartoletti',
Quantity: 1000,
Rate: 20,
'Line Description': 'Rem esse doloremque praesentium harum maiores.',
},
{
Customer: 'Ambrose Olson',
'Estimate Date': '2024-01-02',
'Expiration Date': '2025-01-02',
'Estimate No.': 'EST-0002',
'Reference No.': 'REF-0002',
Currency: '',
'Exchange Rate': '',
Note: 'Tempora voluptas odio deleniti rerum vitae consequatur nihil quis sunt.',
'Terms & Conditions': 'Ut eum incidunt quibusdam rerum vero.',
Delivered: 'T',
'Item Name': 'Hettinger, Schumm and Bartoletti',
Quantity: 1000,
Rate: 20,
'Line Description': 'Qui voluptate aliquam maxime aliquam.',
},
{
Customer: 'Ambrose Olson',
'Estimate Date': '2024-01-03',
'Expiration Date': '2025-01-03',
'Estimate No.': 'EST-0003',
'Reference No.': 'REF-0003',
Currency: '',
'Exchange Rate': '',
Note: 'Quia voluptatem delectus doloremque.',
'Terms & Conditions': 'Facilis porro vitae ratione.',
Delivered: 'T',
'Item Name': 'Hettinger, Schumm and Bartoletti',
Quantity: 1000,
Rate: 20,
'Line Description': 'Qui suscipit ducimus qui qui.',
},
];

View File

@@ -51,7 +51,8 @@ export class CreateSaleInvoice {
public createSaleInvoice = async (
tenantId: number,
saleInvoiceDTO: ISaleInvoiceCreateDTO,
authorizedUser: ITenantUser
authorizedUser: ITenantUser,
trx?: Knex.Transaction
): Promise<ISaleInvoice> => {
const { SaleInvoice, SaleEstimate, Contact } =
this.tenancy.models(tenantId);
@@ -96,33 +97,37 @@ export class CreateSaleInvoice {
);
}
// Creates a new sale invoice and associated transactions under unit of work env.
return this.uow.withTransaction(tenantId, async (trx: Knex.Transaction) => {
// Triggers `onSaleInvoiceCreating` event.
await this.eventPublisher.emitAsync(events.saleInvoice.onCreating, {
saleInvoiceDTO,
tenantId,
trx,
} as ISaleInvoiceCreatingPaylaod);
return this.uow.withTransaction(
tenantId,
async (trx: Knex.Transaction) => {
// Triggers `onSaleInvoiceCreating` event.
await this.eventPublisher.emitAsync(events.saleInvoice.onCreating, {
saleInvoiceDTO,
tenantId,
trx,
} as ISaleInvoiceCreatingPaylaod);
// Create sale invoice graph to the storage.
const saleInvoice = await SaleInvoice.query(trx).upsertGraph(
saleInvoiceObj
);
const eventPayload: ISaleInvoiceCreatedPayload = {
tenantId,
saleInvoice,
saleInvoiceDTO,
saleInvoiceId: saleInvoice.id,
authorizedUser,
trx,
};
// Triggers the event `onSaleInvoiceCreated`.
await this.eventPublisher.emitAsync(
events.saleInvoice.onCreated,
eventPayload
);
return saleInvoice;
});
// Create sale invoice graph to the storage.
const saleInvoice = await SaleInvoice.query(trx).upsertGraph(
saleInvoiceObj
);
const eventPayload: ISaleInvoiceCreatedPayload = {
tenantId,
saleInvoice,
saleInvoiceDTO,
saleInvoiceId: saleInvoice.id,
authorizedUser,
trx,
};
// Triggers the event `onSaleInvoiceCreated`.
await this.eventPublisher.emitAsync(
events.saleInvoice.onCreated,
eventPayload
);
return saleInvoice;
},
trx
);
};
/**

View File

@@ -0,0 +1,45 @@
import { Inject, Service } from 'typedi';
import { Knex } from 'knex';
import { ISaleInvoiceCreateDTO } from '@/interfaces';
import { CreateSaleInvoice } from './CreateSaleInvoice';
import { Importable } from '@/services/Import/Importable';
@Service()
export class SaleInvoicesImportable extends Importable {
@Inject()
private createInvoiceService: CreateSaleInvoice;
/**
* Importing to account service.
* @param {number} tenantId
* @param {IAccountCreateDTO} createAccountDTO
* @returns
*/
public importable(
tenantId: number,
createAccountDTO: ISaleInvoiceCreateDTO,
trx?: Knex.Transaction
) {
return this.createInvoiceService.createSaleInvoice(
tenantId,
createAccountDTO,
{},
trx
);
}
/**
* Concurrrency controlling of the importing process.
* @returns {number}
*/
public get concurrency() {
return 1;
}
/**
* Retrieves the sample data that used to download accounts sample sheet.
*/
public sampleData(): any[] {
return [];
}
}