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

@@ -88,7 +88,8 @@ export class CreateExpense {
public newExpense = async (
tenantId: number,
expenseDTO: IExpenseCreateDTO,
authorizedUser: ISystemUser
authorizedUser: ISystemUser,
trx?: Knex.Transaction
): Promise<IExpense> => {
const { Expense } = await this.tenancy.models(tenantId);
@@ -103,28 +104,32 @@ export class CreateExpense {
);
// Writes the expense transaction with associated transactions under
// unit-of-work envirement.
return this.uow.withTransaction(tenantId, async (trx: Knex.Transaction) => {
// Triggers `onExpenseCreating` event.
await this.eventPublisher.emitAsync(events.expenses.onCreating, {
trx,
tenantId,
expenseDTO,
} as IExpenseCreatingPayload);
return this.uow.withTransaction(
tenantId,
async (trx: Knex.Transaction) => {
// Triggers `onExpenseCreating` event.
await this.eventPublisher.emitAsync(events.expenses.onCreating, {
trx,
tenantId,
expenseDTO,
} as IExpenseCreatingPayload);
// Creates a new expense transaction graph.
const expense: IExpense = await Expense.query(trx).upsertGraph(
expenseObj
);
// Triggers `onExpenseCreated` event.
await this.eventPublisher.emitAsync(events.expenses.onCreated, {
tenantId,
expenseId: expense.id,
authorizedUser,
expense,
trx,
} as IExpenseCreatedPayload);
// Creates a new expense transaction graph.
const expense: IExpense = await Expense.query(trx).upsertGraph(
expenseObj
);
// Triggers `onExpenseCreated` event.
await this.eventPublisher.emitAsync(events.expenses.onCreated, {
tenantId,
expenseId: expense.id,
authorizedUser,
expense,
trx,
} as IExpenseCreatedPayload);
return expense;
});
return expense;
},
trx
);
};
}

View File

@@ -0,0 +1,46 @@
import { Inject, Service } from 'typedi';
import { Knex } from 'knex';
import { IExpenseCreateDTO } from '@/interfaces';
import { Importable } from '../Import/Importable';
import { CreateExpense } from './CRUD/CreateExpense';
import { ExpensesSampleData } from './constants';
@Service()
export class ExpensesImportable extends Importable {
@Inject()
private createExpenseService: CreateExpense;
/**
* Importing to account service.
* @param {number} tenantId
* @param {IAccountCreateDTO} createAccountDTO
* @returns
*/
public importable(
tenantId: number,
createAccountDTO: IExpenseCreateDTO,
trx?: Knex.Transaction
) {
return this.createExpenseService.newExpense(
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 ExpensesSampleData;
}
}

View File

@@ -36,3 +36,43 @@ export const ERRORS = {
EXPENSE_ALREADY_PUBLISHED: 'expense_already_published',
EXPENSE_HAS_ASSOCIATED_LANDED_COST: 'EXPENSE_HAS_ASSOCIATED_LANDED_COST',
};
export const ExpensesSampleData = [
{
'Payment Date': '2024-03-01',
'Reference No.': 'REF-1',
'Payment Account': 'Petty Cash',
Description: 'Vel et dolorem architecto veniam.',
'Currency Code': '',
'Exchange Rate': '',
'Expense Account': 'Utilities Expense',
Amount: 9000,
'Line Description': 'Voluptates voluptas corporis vel.',
Publish: 'T',
},
{
'Payment Date': '2024-03-02',
'Reference No.': 'REF-2',
'Payment Account': 'Petty Cash',
Description: 'Id est molestias.',
'Currency Code': '',
'Exchange Rate': '',
'Expense Account': 'Utilities Expense',
Amount: 9000,
'Line Description': 'Eos voluptatem cumque et voluptate reiciendis.',
Publish: 'T',
},
{
'Payment Date': '2024-03-03',
'Reference No.': 'REF-3',
'Payment Account': 'Petty Cash',
Description: 'Quam cupiditate at nihil dicta dignissimos non fugit illo.',
'Currency Code': '',
'Exchange Rate': '',
'Expense Account': 'Utilities Expense',
Amount: 9000,
'Line Description':
'Hic alias rerum sed commodi dolores sint animi perferendis.',
Publish: 'T',
},
];