mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-17 21:30:31 +00:00
refactor: dynamic list to nestjs
This commit is contained in:
@@ -1,90 +1,82 @@
|
||||
// import { Inject, Service } from 'typedi';
|
||||
// import { Knex } from 'knex';
|
||||
// import { ICreditNote } from '@/interfaces';
|
||||
// import InventoryService from '@/services/Inventory/Inventory';
|
||||
// import ItemsEntriesService from '@/services/Items/ItemsEntriesService';
|
||||
import { Injectable } from '@nestjs/common';
|
||||
|
||||
// @Service()
|
||||
// export default class CreditNoteInventoryTransactions {
|
||||
// @Inject()
|
||||
// inventoryService: InventoryService;
|
||||
import { InventoryService } from '@/modules/InventoryCost/Inventory';
|
||||
import { ItemsEntriesService } from '@/modules/Items/ItemsEntries.service';
|
||||
import { CreditNote } from '../models/CreditNote';
|
||||
import { Knex } from 'knex';
|
||||
@Injectable()
|
||||
export class CreditNoteInventoryTransactions {
|
||||
constructor(
|
||||
private readonly inventoryService: InventoryService,
|
||||
private readonly itemsEntriesService: ItemsEntriesService,
|
||||
) {}
|
||||
|
||||
// @Inject()
|
||||
// itemsEntriesService: ItemsEntriesService;
|
||||
/**
|
||||
* Creates credit note inventory transactions.
|
||||
* @param {number} tenantId
|
||||
* @param {ICreditNote} creditNote
|
||||
*/
|
||||
public createInventoryTransactions = async (
|
||||
creditNote: CreditNote,
|
||||
trx?: Knex.Transaction,
|
||||
): Promise<void> => {
|
||||
// Loads the inventory items entries of the given sale invoice.
|
||||
const inventoryEntries =
|
||||
await this.itemsEntriesService.filterInventoryEntries(creditNote.entries);
|
||||
|
||||
// /**
|
||||
// * Creates credit note inventory transactions.
|
||||
// * @param {number} tenantId
|
||||
// * @param {ICreditNote} creditNote
|
||||
// */
|
||||
// public createInventoryTransactions = async (
|
||||
// tenantId: number,
|
||||
// creditNote: ICreditNote,
|
||||
// trx?: Knex.Transaction
|
||||
// ): Promise<void> => {
|
||||
// // Loads the inventory items entries of the given sale invoice.
|
||||
// const inventoryEntries =
|
||||
// await this.itemsEntriesService.filterInventoryEntries(
|
||||
// tenantId,
|
||||
// creditNote.entries
|
||||
// );
|
||||
// const transaction = {
|
||||
// transactionId: creditNote.id,
|
||||
// transactionType: 'CreditNote',
|
||||
// transactionNumber: creditNote.creditNoteNumber,
|
||||
// exchangeRate: creditNote.exchangeRate,
|
||||
// date: creditNote.creditNoteDate,
|
||||
// direction: 'IN',
|
||||
// entries: inventoryEntries,
|
||||
// createdAt: creditNote.createdAt,
|
||||
// warehouseId: creditNote.warehouseId,
|
||||
// };
|
||||
// // Writes inventory tranactions.
|
||||
// await this.inventoryService.recordInventoryTransactionsFromItemsEntries(
|
||||
// tenantId,
|
||||
// transaction,
|
||||
// false,
|
||||
// trx
|
||||
// );
|
||||
// };
|
||||
const transaction = {
|
||||
transactionId: creditNote.id,
|
||||
transactionType: 'CreditNote',
|
||||
transactionNumber: creditNote.creditNoteNumber,
|
||||
exchangeRate: creditNote.exchangeRate,
|
||||
date: creditNote.creditNoteDate,
|
||||
direction: 'IN',
|
||||
entries: inventoryEntries,
|
||||
createdAt: creditNote.createdAt,
|
||||
warehouseId: creditNote.warehouseId,
|
||||
};
|
||||
// Writes inventory tranactions.
|
||||
await this.inventoryService.recordInventoryTransactionsFromItemsEntries(
|
||||
transaction,
|
||||
false,
|
||||
trx,
|
||||
);
|
||||
};
|
||||
|
||||
// /**
|
||||
// * Edits vendor credit associated inventory transactions.
|
||||
// * @param {number} tenantId
|
||||
// * @param {number} creditNoteId
|
||||
// * @param {ICreditNote} creditNote
|
||||
// * @param {Knex.Transactions} trx
|
||||
// */
|
||||
// public editInventoryTransactions = async (
|
||||
// tenantId: number,
|
||||
// creditNoteId: number,
|
||||
// creditNote: ICreditNote,
|
||||
// trx?: Knex.Transaction
|
||||
// ): Promise<void> => {
|
||||
// // Deletes inventory transactions.
|
||||
// await this.deleteInventoryTransactions(tenantId, creditNoteId, trx);
|
||||
/**
|
||||
* Edits vendor credit associated inventory transactions.
|
||||
* @param {number} tenantId
|
||||
* @param {number} creditNoteId
|
||||
* @param {ICreditNote} creditNote
|
||||
* @param {Knex.Transactions} trx
|
||||
*/
|
||||
public editInventoryTransactions = async (
|
||||
creditNoteId: number,
|
||||
creditNote: CreditNote,
|
||||
trx?: Knex.Transaction,
|
||||
): Promise<void> => {
|
||||
// Deletes inventory transactions.
|
||||
await this.deleteInventoryTransactions(creditNoteId, trx);
|
||||
|
||||
// // Re-write inventory transactions.
|
||||
// await this.createInventoryTransactions(tenantId, creditNote, trx);
|
||||
// };
|
||||
// Re-write inventory transactions.
|
||||
await this.createInventoryTransactions(creditNote, trx);
|
||||
};
|
||||
|
||||
// /**
|
||||
// * Deletes credit note associated inventory transactions.
|
||||
// * @param {number} tenantId - Tenant id.
|
||||
// * @param {number} creditNoteId - Credit note id.
|
||||
// * @param {Knex.Transaction} trx -
|
||||
// */
|
||||
// public deleteInventoryTransactions = async (
|
||||
// tenantId: number,
|
||||
// creditNoteId: number,
|
||||
// trx?: Knex.Transaction
|
||||
// ): Promise<void> => {
|
||||
// // Deletes the inventory transactions by the given reference id and type.
|
||||
// await this.inventoryService.deleteInventoryTransactions(
|
||||
// tenantId,
|
||||
// creditNoteId,
|
||||
// 'CreditNote',
|
||||
// trx
|
||||
// );
|
||||
// };
|
||||
// }
|
||||
/**
|
||||
* Deletes credit note associated inventory transactions.
|
||||
* @param {number} tenantId - Tenant id.
|
||||
* @param {number} creditNoteId - Credit note id.
|
||||
* @param {Knex.Transaction} trx -
|
||||
*/
|
||||
public deleteInventoryTransactions = async (
|
||||
creditNoteId: number,
|
||||
trx?: Knex.Transaction,
|
||||
): Promise<void> => {
|
||||
// Deletes the inventory transactions by the given reference id and type.
|
||||
await this.inventoryService.deleteInventoryTransactions(
|
||||
creditNoteId,
|
||||
'CreditNote',
|
||||
trx,
|
||||
);
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1,98 +1,74 @@
|
||||
// import { Service, Inject } from 'typedi';
|
||||
// import events from '@/subscribers/events';
|
||||
// import CreditNoteInventoryTransactions from '../commands/CreditNotesInventoryTransactions';
|
||||
// import {
|
||||
// ICreditNoteCreatedPayload,
|
||||
// ICreditNoteDeletedPayload,
|
||||
// ICreditNoteEditedPayload,
|
||||
// } from '@/interfaces';
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import {
|
||||
ICreditNoteCreatedPayload,
|
||||
ICreditNoteDeletedPayload,
|
||||
ICreditNoteEditedPayload,
|
||||
} from '../types/CreditNotes.types';
|
||||
import { OnEvent } from '@nestjs/event-emitter';
|
||||
import { events } from '@/common/events/events';
|
||||
import { CreditNoteInventoryTransactions } from '../commands/CreditNotesInventoryTransactions';
|
||||
|
||||
// @Service()
|
||||
// export default class CreditNoteInventoryTransactionsSubscriber {
|
||||
// @Inject()
|
||||
// inventoryTransactions: CreditNoteInventoryTransactions;
|
||||
@Injectable()
|
||||
export class CreditNoteInventoryTransactionsSubscriber {
|
||||
constructor(
|
||||
private readonly inventoryTransactions: CreditNoteInventoryTransactions;
|
||||
) {}
|
||||
|
||||
// /**
|
||||
// * Attaches events with publisher.
|
||||
// */
|
||||
// public attach(bus) {
|
||||
// bus.subscribe(
|
||||
// events.creditNote.onCreated,
|
||||
// this.writeInventoryTranscationsOnceCreated
|
||||
// );
|
||||
// bus.subscribe(
|
||||
// events.creditNote.onEdited,
|
||||
// this.rewriteInventoryTransactionsOnceEdited
|
||||
// );
|
||||
// bus.subscribe(
|
||||
// events.creditNote.onDeleted,
|
||||
// this.revertInventoryTransactionsOnceDeleted
|
||||
// );
|
||||
// bus.subscribe(
|
||||
// events.creditNote.onOpened,
|
||||
// this.writeInventoryTranscationsOnceCreated
|
||||
// );
|
||||
// }
|
||||
/**
|
||||
* Writes inventory transactions once credit note created.
|
||||
* @param {ICreditNoteCreatedPayload} payload -
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
@OnEvent(events.creditNote.onCreated)
|
||||
@OnEvent(events.creditNote.onOpened)
|
||||
public async writeInventoryTranscationsOnceCreated({
|
||||
creditNote,
|
||||
trx,
|
||||
}: ICreditNoteCreatedPayload) {
|
||||
// Can't continue if the credit note is open yet.
|
||||
if (!creditNote.isOpen) return;
|
||||
|
||||
// /**
|
||||
// * Writes inventory transactions once credit note created.
|
||||
// * @param {ICreditNoteCreatedPayload} payload -
|
||||
// * @returns {Promise<void>}
|
||||
// */
|
||||
// public writeInventoryTranscationsOnceCreated = async ({
|
||||
// tenantId,
|
||||
// creditNote,
|
||||
// trx,
|
||||
// }: ICreditNoteCreatedPayload) => {
|
||||
// // Can't continue if the credit note is open yet.
|
||||
// if (!creditNote.isOpen) return;
|
||||
await this.inventoryTransactions.createInventoryTransactions(
|
||||
creditNote,
|
||||
trx
|
||||
);
|
||||
};
|
||||
|
||||
// await this.inventoryTransactions.createInventoryTransactions(
|
||||
// tenantId,
|
||||
// creditNote,
|
||||
// trx
|
||||
// );
|
||||
// };
|
||||
/**
|
||||
* Rewrites inventory transactions once credit note edited.
|
||||
* @param {ICreditNoteEditedPayload} payload -
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
@OnEvent(events.creditNote.onEdited)
|
||||
public async rewriteInventoryTransactionsOnceEdited({
|
||||
creditNote,
|
||||
trx,
|
||||
}: ICreditNoteEditedPayload) {
|
||||
// Can't continue if the credit note is open yet.
|
||||
if (!creditNote.isOpen) return;
|
||||
|
||||
// /**
|
||||
// * Rewrites inventory transactions once credit note edited.
|
||||
// * @param {ICreditNoteEditedPayload} payload -
|
||||
// * @returns {Promise<void>}
|
||||
// */
|
||||
// public rewriteInventoryTransactionsOnceEdited = async ({
|
||||
// tenantId,
|
||||
// creditNoteId,
|
||||
// creditNote,
|
||||
// trx,
|
||||
// }: ICreditNoteEditedPayload) => {
|
||||
// // Can't continue if the credit note is open yet.
|
||||
// if (!creditNote.isOpen) return;
|
||||
await this.inventoryTransactions.editInventoryTransactions(
|
||||
creditNoteId,
|
||||
creditNote,
|
||||
trx
|
||||
);
|
||||
};
|
||||
|
||||
// await this.inventoryTransactions.editInventoryTransactions(
|
||||
// tenantId,
|
||||
// creditNoteId,
|
||||
// creditNote,
|
||||
// trx
|
||||
// );
|
||||
// };
|
||||
/**
|
||||
* Reverts inventory transactions once credit note deleted.
|
||||
* @param {ICreditNoteDeletedPayload} payload -
|
||||
*/
|
||||
@OnEvent(events.creditNote.onDeleted)
|
||||
public async revertInventoryTransactionsOnceDeleted({
|
||||
oldCreditNote,
|
||||
trx,
|
||||
}: ICreditNoteDeletedPayload) {
|
||||
// Can't continue if the credit note is open yet.
|
||||
if (!oldCreditNote.isOpen) return;
|
||||
|
||||
// /**
|
||||
// * Reverts inventory transactions once credit note deleted.
|
||||
// * @param {ICreditNoteDeletedPayload} payload -
|
||||
// */
|
||||
// public revertInventoryTransactionsOnceDeleted = async ({
|
||||
// tenantId,
|
||||
// creditNoteId,
|
||||
// oldCreditNote,
|
||||
// trx,
|
||||
// }: ICreditNoteDeletedPayload) => {
|
||||
// // Can't continue if the credit note is open yet.
|
||||
// if (!oldCreditNote.isOpen) return;
|
||||
|
||||
// await this.inventoryTransactions.deleteInventoryTransactions(
|
||||
// tenantId,
|
||||
// creditNoteId,
|
||||
// trx
|
||||
// );
|
||||
// };
|
||||
// }
|
||||
await this.inventoryTransactions.deleteInventoryTransactions(
|
||||
creditNoteId,
|
||||
trx
|
||||
);
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user