mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-17 05:10:31 +00:00
fix: system repositories.
This commit is contained in:
@@ -1,13 +1,9 @@
|
||||
import { Inject, Service } from 'typedi';
|
||||
import { difference, upperFirst, omit } from 'lodash';
|
||||
import moment from 'moment';
|
||||
import { ServiceError } from "exceptions";
|
||||
import { ServiceError } from 'exceptions';
|
||||
import TenancyService from 'services/Tenancy/TenancyService';
|
||||
import {
|
||||
IContact,
|
||||
IContactNewDTO,
|
||||
IContactEditDTO,
|
||||
} from "interfaces";
|
||||
import { IContact, IContactNewDTO, IContactEditDTO } from 'interfaces';
|
||||
import JournalPoster from '../Accounting/JournalPoster';
|
||||
|
||||
type TContactService = 'customer' | 'vendor';
|
||||
@@ -26,8 +22,8 @@ export default class ContactsService {
|
||||
|
||||
/**
|
||||
* Get the given contact or throw not found contact.
|
||||
* @param {number} tenantId
|
||||
* @param {number} contactId
|
||||
* @param {number} tenantId
|
||||
* @param {number} contactId
|
||||
* @param {TContactService} contactService
|
||||
* @return {Promise<IContact>}
|
||||
*/
|
||||
@@ -38,10 +34,13 @@ export default class ContactsService {
|
||||
) {
|
||||
const { contactRepository } = this.tenancy.repositories(tenantId);
|
||||
|
||||
this.logger.info('[contact] trying to validate contact existance.', { tenantId, contactId });
|
||||
this.logger.info('[contact] trying to validate contact existance.', {
|
||||
tenantId,
|
||||
contactId,
|
||||
});
|
||||
const contact = await contactRepository.findOne({
|
||||
id: contactId,
|
||||
...(contactService) && ({ contactService }),
|
||||
...(contactService && { contactService }),
|
||||
});
|
||||
|
||||
if (!contact) {
|
||||
@@ -52,13 +51,15 @@ export default class ContactsService {
|
||||
|
||||
/**
|
||||
* Converts contact DTO object to model object attributes to insert or update.
|
||||
* @param {IContactNewDTO | IContactEditDTO} contactDTO
|
||||
* @param {IContactNewDTO | IContactEditDTO} contactDTO
|
||||
*/
|
||||
private transformContactObj(contactDTO: IContactNewDTO | IContactEditDTO) {
|
||||
return {
|
||||
...omit(contactDTO, [
|
||||
'billingAddress1', 'billingAddress2',
|
||||
'shippingAddress1', 'shippingAddress2',
|
||||
'billingAddress1',
|
||||
'billingAddress2',
|
||||
'shippingAddress1',
|
||||
'shippingAddress2',
|
||||
]),
|
||||
billing_address_1: contactDTO?.billingAddress1,
|
||||
billing_address_2: contactDTO?.billingAddress2,
|
||||
@@ -69,54 +70,87 @@ export default class ContactsService {
|
||||
|
||||
/**
|
||||
* Creates a new contact on the storage.
|
||||
* @param {number} tenantId
|
||||
* @param {number} tenantId
|
||||
* @param {TContactService} contactService
|
||||
* @param {IContactDTO} contactDTO
|
||||
* @param {IContactDTO} contactDTO
|
||||
*/
|
||||
async newContact(
|
||||
tenantId: number,
|
||||
contactDTO: IContactNewDTO,
|
||||
contactService: TContactService,
|
||||
contactService: TContactService
|
||||
) {
|
||||
const { contactRepository } = this.tenancy.repositories(tenantId);
|
||||
const contactObj = this.transformContactObj(contactDTO);
|
||||
|
||||
this.logger.info('[contacts] trying to insert contact to the storage.', { tenantId, contactDTO });
|
||||
const contact = await contactRepository.create({ contactService, ...contactObj });
|
||||
this.logger.info('[contacts] trying to insert contact to the storage.', {
|
||||
tenantId,
|
||||
contactDTO,
|
||||
});
|
||||
const contact = await contactRepository.create({
|
||||
contactService,
|
||||
...contactObj,
|
||||
});
|
||||
|
||||
this.logger.info('[contacts] contact inserted successfully.', { tenantId, contact });
|
||||
this.logger.info('[contacts] contact inserted successfully.', {
|
||||
tenantId,
|
||||
contact,
|
||||
});
|
||||
return contact;
|
||||
}
|
||||
|
||||
/**
|
||||
* Edit details of the given on the storage.
|
||||
* @param {number} tenantId
|
||||
* @param {number} contactId
|
||||
* @param {number} tenantId
|
||||
* @param {number} contactId
|
||||
* @param {TContactService} contactService
|
||||
* @param {IContactDTO} contactDTO
|
||||
* @param {IContactDTO} contactDTO
|
||||
*/
|
||||
async editContact(tenantId: number, contactId: number, contactDTO: IContactEditDTO, contactService: TContactService) {
|
||||
async editContact(
|
||||
tenantId: number,
|
||||
contactId: number,
|
||||
contactDTO: IContactEditDTO,
|
||||
contactService: TContactService
|
||||
) {
|
||||
const { contactRepository } = this.tenancy.repositories(tenantId);
|
||||
const contactObj = this.transformContactObj(contactDTO);
|
||||
|
||||
const contact = await this.getContactByIdOrThrowError(tenantId, contactId, contactService);
|
||||
const contact = await this.getContactByIdOrThrowError(
|
||||
tenantId,
|
||||
contactId,
|
||||
contactService
|
||||
);
|
||||
|
||||
this.logger.info('[contacts] trying to edit the given contact details.', { tenantId, contactId, contactDTO });
|
||||
this.logger.info('[contacts] trying to edit the given contact details.', {
|
||||
tenantId,
|
||||
contactId,
|
||||
contactDTO,
|
||||
});
|
||||
await contactRepository.update({ ...contactObj }, { id: contactId });
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes the given contact from the storage.
|
||||
* @param {number} tenantId
|
||||
* @param {number} tenantId
|
||||
* @param {number} contactId
|
||||
* @param {TContactService} contactService
|
||||
* @param {TContactService} contactService
|
||||
* @return {Promise<void>}
|
||||
*/
|
||||
async deleteContact(tenantId: number, contactId: number, contactService: TContactService) {
|
||||
async deleteContact(
|
||||
tenantId: number,
|
||||
contactId: number,
|
||||
contactService: TContactService
|
||||
) {
|
||||
const { contactRepository } = this.tenancy.repositories(tenantId);
|
||||
const contact = await this.getContactByIdOrThrowError(tenantId, contactId, contactService);
|
||||
const contact = await this.getContactByIdOrThrowError(
|
||||
tenantId,
|
||||
contactId,
|
||||
contactService
|
||||
);
|
||||
|
||||
this.logger.info('[contacts] trying to delete the given contact.', { tenantId, contactId });
|
||||
this.logger.info('[contacts] trying to delete the given contact.', {
|
||||
tenantId,
|
||||
contactId,
|
||||
});
|
||||
|
||||
// Deletes contact of the given id.
|
||||
await contactRepository.deleteById(contactId);
|
||||
@@ -124,26 +158,36 @@ export default class ContactsService {
|
||||
|
||||
/**
|
||||
* Get contact details of the given contact id.
|
||||
* @param {number} tenantId
|
||||
* @param {number} contactId
|
||||
* @param {number} tenantId
|
||||
* @param {number} contactId
|
||||
* @param {TContactService} contactService
|
||||
* @returns {Promise<IContact>}
|
||||
*/
|
||||
async getContact(tenantId: number, contactId: number, contactService: TContactService) {
|
||||
async getContact(
|
||||
tenantId: number,
|
||||
contactId: number,
|
||||
contactService: TContactService
|
||||
) {
|
||||
return this.getContactByIdOrThrowError(tenantId, contactId, contactService);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve contacts or throw not found error if one of ids were not found
|
||||
* on the storage.
|
||||
* @param {number} tenantId
|
||||
* @param {number[]} contactsIds
|
||||
* @param {number} tenantId
|
||||
* @param {number[]} contactsIds
|
||||
* @param {TContactService} contactService
|
||||
* @return {Promise<IContact>}
|
||||
*/
|
||||
async getContactsOrThrowErrorNotFound(tenantId: number, contactsIds: number[], contactService: TContactService) {
|
||||
async getContactsOrThrowErrorNotFound(
|
||||
tenantId: number,
|
||||
contactsIds: number[],
|
||||
contactService: TContactService
|
||||
) {
|
||||
const { Contact } = this.tenancy.models(tenantId);
|
||||
const contacts = await Contact.query().whereIn('id', contactsIds).where('contact_service', contactService);
|
||||
const contacts = await Contact.query()
|
||||
.whereIn('id', contactsIds)
|
||||
.where('contact_service', contactService);
|
||||
const storedContactsIds = contacts.map((contact: IContact) => contact.id);
|
||||
|
||||
const notFoundCustomers = difference(contactsIds, storedContactsIds);
|
||||
@@ -156,12 +200,16 @@ export default class ContactsService {
|
||||
|
||||
/**
|
||||
* Deletes the given contacts in bulk.
|
||||
* @param {number} tenantId
|
||||
* @param {number[]} contactsIds
|
||||
* @param {number} tenantId
|
||||
* @param {number[]} contactsIds
|
||||
* @param {TContactService} contactService
|
||||
* @return {Promise<void>}
|
||||
*/
|
||||
async deleteBulkContacts(tenantId: number, contactsIds: number[], contactService: TContactService) {
|
||||
async deleteBulkContacts(
|
||||
tenantId: number,
|
||||
contactsIds: number[],
|
||||
contactService: TContactService
|
||||
) {
|
||||
const { contactRepository } = this.tenancy.repositories(tenantId);
|
||||
this.getContactsOrThrowErrorNotFound(tenantId, contactsIds, contactService);
|
||||
|
||||
@@ -170,9 +218,9 @@ export default class ContactsService {
|
||||
|
||||
/**
|
||||
* Reverts journal entries of the given contacts.
|
||||
* @param {number} tenantId
|
||||
* @param {number[]} contactsIds
|
||||
* @param {TContactService} contactService
|
||||
* @param {number} tenantId
|
||||
* @param {number[]} contactsIds
|
||||
* @param {TContactService} contactService
|
||||
*/
|
||||
async revertJEntriesContactsOpeningBalance(
|
||||
tenantId: number,
|
||||
@@ -189,17 +237,14 @@ export default class ContactsService {
|
||||
journal.loadEntries(contactsTransactions);
|
||||
journal.removeEntries();
|
||||
|
||||
await Promise.all([
|
||||
journal.saveBalance(),
|
||||
journal.deleteEntries(),
|
||||
]);
|
||||
await Promise.all([journal.saveBalance(), journal.deleteEntries()]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Chanages the opening balance of the given contact.
|
||||
* @param {number} tenantId
|
||||
* @param {number} tenantId
|
||||
* @param {number} contactId
|
||||
* @param {ICustomerChangeOpeningBalanceDTO} changeOpeningBalance
|
||||
* @param {ICustomerChangeOpeningBalanceDTO} changeOpeningBalance
|
||||
* @return {Promise<void>}
|
||||
*/
|
||||
public async changeOpeningBalance(
|
||||
@@ -207,27 +252,34 @@ export default class ContactsService {
|
||||
contactId: number,
|
||||
contactService: string,
|
||||
openingBalance: number,
|
||||
openingBalanceAt?: Date|string,
|
||||
openingBalanceAt?: Date | string
|
||||
): Promise<void> {
|
||||
const { contactRepository } = this.tenancy.repositories(tenantId);
|
||||
|
||||
// Retrieve the given contact details or throw not found service error.
|
||||
const contact = await this.getContactByIdOrThrowError(tenantId, contactId, contactService);
|
||||
const contact = await this.getContactByIdOrThrowError(
|
||||
tenantId,
|
||||
contactId,
|
||||
contactService
|
||||
);
|
||||
|
||||
// Should the opening balance date be required.
|
||||
if (!contact.openingBalanceAt && !openingBalanceAt) {
|
||||
throw new ServiceError(ERRORS.OPENING_BALANCE_DATE_REQUIRED);
|
||||
};
|
||||
}
|
||||
// Changes the customer the opening balance and opening balance date.
|
||||
await contactRepository.update({
|
||||
openingBalance: openingBalance,
|
||||
await contactRepository.update(
|
||||
{
|
||||
openingBalance: openingBalance,
|
||||
|
||||
...(openingBalanceAt) && ({
|
||||
openingBalanceAt: moment(openingBalanceAt).toMySqlDateTime(),
|
||||
}),
|
||||
}, {
|
||||
id: contactId,
|
||||
contactService,
|
||||
});
|
||||
...(openingBalanceAt && {
|
||||
openingBalanceAt: moment(openingBalanceAt).toMySqlDateTime(),
|
||||
}),
|
||||
},
|
||||
{
|
||||
id: contactId,
|
||||
contactService,
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -181,11 +181,10 @@ export default class CustomersService {
|
||||
pagination: IPaginationMeta,
|
||||
filterMeta: IFilterMeta,
|
||||
}> {
|
||||
const { Contact } = this.tenancy.models(tenantId);
|
||||
const dynamicList = await this.dynamicListService.dynamicList(tenantId, Contact, customersFilter);
|
||||
const { Customer } = this.tenancy.models(tenantId);
|
||||
const dynamicList = await this.dynamicListService.dynamicList(tenantId, Customer, customersFilter);
|
||||
|
||||
const { results, pagination } = await Contact.query().onBuild((query) => {
|
||||
query.modify('customer');
|
||||
const { results, pagination } = await Customer.query().onBuild((query) => {
|
||||
dynamicList.buildQuery()(query);
|
||||
}).pagination(
|
||||
customersFilter.page - 1,
|
||||
|
||||
@@ -4,17 +4,17 @@ import {
|
||||
EventDispatcher,
|
||||
EventDispatcherInterface,
|
||||
} from 'decorators/eventDispatcher';
|
||||
import JournalPoster from "services/Accounting/JournalPoster";
|
||||
import JournalCommands from "services/Accounting/JournalCommands";
|
||||
import JournalPoster from 'services/Accounting/JournalPoster';
|
||||
import JournalCommands from 'services/Accounting/JournalCommands';
|
||||
import ContactsService from 'services/Contacts/ContactsService';
|
||||
import {
|
||||
import {
|
||||
IVendorNewDTO,
|
||||
IVendorEditDTO,
|
||||
IVendor,
|
||||
IVendorsFilter,
|
||||
IPaginationMeta,
|
||||
IFilterMeta
|
||||
} from 'interfaces';
|
||||
IFilterMeta,
|
||||
} from 'interfaces';
|
||||
import { ServiceError } from 'exceptions';
|
||||
import DynamicListingService from 'services/DynamicListing/DynamicListService';
|
||||
import TenancyService from 'services/Tenancy/TenancyService';
|
||||
@@ -39,10 +39,10 @@ export default class VendorsService {
|
||||
|
||||
/**
|
||||
* Converts vendor to contact DTO.
|
||||
* @param {IVendorNewDTO|IVendorEditDTO} vendorDTO
|
||||
* @param {IVendorNewDTO|IVendorEditDTO} vendorDTO
|
||||
* @returns {IContactDTO}
|
||||
*/
|
||||
private vendorToContactDTO(vendorDTO: IVendorNewDTO|IVendorEditDTO) {
|
||||
private vendorToContactDTO(vendorDTO: IVendorNewDTO | IVendorEditDTO) {
|
||||
return {
|
||||
...vendorDTO,
|
||||
active: defaultTo(vendorDTO.active, true),
|
||||
@@ -51,31 +51,49 @@ export default class VendorsService {
|
||||
|
||||
/**
|
||||
* Creates a new vendor.
|
||||
* @param {number} tenantId
|
||||
* @param {IVendorNewDTO} vendorDTO
|
||||
* @param {number} tenantId
|
||||
* @param {IVendorNewDTO} vendorDTO
|
||||
* @return {Promise<void>}
|
||||
*/
|
||||
public async newVendor(tenantId: number, vendorDTO: IVendorNewDTO) {
|
||||
this.logger.info('[vendor] trying create a new vendor.', { tenantId, vendorDTO });
|
||||
this.logger.info('[vendor] trying create a new vendor.', {
|
||||
tenantId,
|
||||
vendorDTO,
|
||||
});
|
||||
|
||||
const contactDTO = this.vendorToContactDTO(vendorDTO);
|
||||
const vendor = await this.contactService.newContact(tenantId, contactDTO, 'vendor');
|
||||
const vendor = await this.contactService.newContact(
|
||||
tenantId,
|
||||
contactDTO,
|
||||
'vendor'
|
||||
);
|
||||
|
||||
// Triggers `onVendorCreated` event.
|
||||
await this.eventDispatcher.dispatch(events.vendors.onCreated, {
|
||||
tenantId, vendorId: vendor.id, vendor,
|
||||
tenantId,
|
||||
vendorId: vendor.id,
|
||||
vendor,
|
||||
});
|
||||
return vendor;
|
||||
}
|
||||
|
||||
/**
|
||||
* Edits details of the given vendor.
|
||||
* @param {number} tenantId
|
||||
* @param {IVendorEditDTO} vendorDTO
|
||||
* @param {number} tenantId
|
||||
* @param {IVendorEditDTO} vendorDTO
|
||||
*/
|
||||
public async editVendor(tenantId: number, vendorId: number, vendorDTO: IVendorEditDTO) {
|
||||
public async editVendor(
|
||||
tenantId: number,
|
||||
vendorId: number,
|
||||
vendorDTO: IVendorEditDTO
|
||||
) {
|
||||
const contactDTO = this.vendorToContactDTO(vendorDTO);
|
||||
const vendor = await this.contactService.editContact(tenantId, vendorId, contactDTO, 'vendor');
|
||||
const vendor = await this.contactService.editContact(
|
||||
tenantId,
|
||||
vendorId,
|
||||
contactDTO,
|
||||
'vendor'
|
||||
);
|
||||
|
||||
await this.eventDispatcher.dispatch(events.vendors.onEdited);
|
||||
|
||||
@@ -84,17 +102,21 @@ export default class VendorsService {
|
||||
|
||||
/**
|
||||
* Retrieve the given vendor details by id or throw not found.
|
||||
* @param {number} tenantId
|
||||
* @param {number} customerId
|
||||
* @param {number} tenantId
|
||||
* @param {number} customerId
|
||||
*/
|
||||
private getVendorByIdOrThrowError(tenantId: number, customerId: number) {
|
||||
return this.contactService.getContactByIdOrThrowError(tenantId, customerId, 'vendor');
|
||||
return this.contactService.getContactByIdOrThrowError(
|
||||
tenantId,
|
||||
customerId,
|
||||
'vendor'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes the given vendor from the storage.
|
||||
* @param {number} tenantId
|
||||
* @param {number} vendorId
|
||||
* @param {number} tenantId
|
||||
* @param {number} vendorId
|
||||
* @return {Promise<void>}
|
||||
*/
|
||||
public async deleteVendor(tenantId: number, vendorId: number) {
|
||||
@@ -103,17 +125,23 @@ export default class VendorsService {
|
||||
await this.getVendorByIdOrThrowError(tenantId, vendorId);
|
||||
await this.vendorHasNoBillsOrThrowError(tenantId, vendorId);
|
||||
|
||||
this.logger.info('[vendor] trying to delete vendor.', { tenantId, vendorId });
|
||||
this.logger.info('[vendor] trying to delete vendor.', {
|
||||
tenantId,
|
||||
vendorId,
|
||||
});
|
||||
await Contact.query().findById(vendorId).delete();
|
||||
|
||||
await this.eventDispatcher.dispatch(events.vendors.onDeleted, { tenantId, vendorId });
|
||||
await this.eventDispatcher.dispatch(events.vendors.onDeleted, {
|
||||
tenantId,
|
||||
vendorId,
|
||||
});
|
||||
this.logger.info('[vendor] deleted successfully.', { tenantId, vendorId });
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve the given vendor details.
|
||||
* @param {number} tenantId
|
||||
* @param {number} vendorId
|
||||
* @param {number} tenantId
|
||||
* @param {number} vendorId
|
||||
*/
|
||||
public async getVendor(tenantId: number, vendorId: number) {
|
||||
return this.contactService.getContact(tenantId, vendorId, 'vendor');
|
||||
@@ -121,26 +149,26 @@ export default class VendorsService {
|
||||
|
||||
/**
|
||||
* Writes vendor opening balance journal entries.
|
||||
* @param {number} tenantId
|
||||
* @param {number} vendorId
|
||||
* @param {number} openingBalance
|
||||
* @param {number} tenantId
|
||||
* @param {number} vendorId
|
||||
* @param {number} openingBalance
|
||||
* @return {Promise<void>}
|
||||
*/
|
||||
public async writeVendorOpeningBalanceJournal(
|
||||
tenantId: number,
|
||||
vendorId: number,
|
||||
openingBalance: number,
|
||||
openingBalance: number
|
||||
) {
|
||||
const journal = new JournalPoster(tenantId);
|
||||
const journalCommands = new JournalCommands(journal);
|
||||
|
||||
this.logger.info('[vendor] writing opening balance journal entries.', { tenantId, vendorId });
|
||||
await journalCommands.vendorOpeningBalance(vendorId, openingBalance)
|
||||
|
||||
await Promise.all([
|
||||
journal.saveBalance(),
|
||||
journal.saveEntries(),
|
||||
]);
|
||||
this.logger.info('[vendor] writing opening balance journal entries.', {
|
||||
tenantId,
|
||||
vendorId,
|
||||
});
|
||||
await journalCommands.vendorOpeningBalance(vendorId, openingBalance);
|
||||
|
||||
await Promise.all([journal.saveBalance(), journal.saveEntries()]);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -149,28 +177,43 @@ export default class VendorsService {
|
||||
* @param {number} vendorId -
|
||||
* @return {Promise<void>}
|
||||
*/
|
||||
public async revertOpeningBalanceEntries(tenantId: number, vendorId: number|number[]) {
|
||||
public async revertOpeningBalanceEntries(
|
||||
tenantId: number,
|
||||
vendorId: number | number[]
|
||||
) {
|
||||
const id = Array.isArray(vendorId) ? vendorId : [vendorId];
|
||||
|
||||
this.logger.info('[customer] trying to revert opening balance journal entries.', { tenantId, customerId });
|
||||
this.logger.info(
|
||||
'[customer] trying to revert opening balance journal entries.',
|
||||
{ tenantId, customerId }
|
||||
);
|
||||
await this.contactService.revertJEntriesContactsOpeningBalance(
|
||||
tenantId, id, 'vendor',
|
||||
tenantId,
|
||||
id,
|
||||
'vendor'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve the given vendors or throw error if one of them not found.
|
||||
* @param {numebr} tenantId
|
||||
* @param {numebr} tenantId
|
||||
* @param {number[]} vendorsIds
|
||||
*/
|
||||
private getVendorsOrThrowErrorNotFound(tenantId: number, vendorsIds: number[]) {
|
||||
return this.contactService.getContactsOrThrowErrorNotFound(tenantId, vendorsIds, 'vendor');
|
||||
private getVendorsOrThrowErrorNotFound(
|
||||
tenantId: number,
|
||||
vendorsIds: number[]
|
||||
) {
|
||||
return this.contactService.getContactsOrThrowErrorNotFound(
|
||||
tenantId,
|
||||
vendorsIds,
|
||||
'vendor'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes the given vendors from the storage.
|
||||
* @param {number} tenantId
|
||||
* @param {number[]} vendorsIds
|
||||
* @param {number} tenantId
|
||||
* @param {number[]} vendorsIds
|
||||
* @return {Promise<void>}
|
||||
*/
|
||||
public async deleteBulkVendors(
|
||||
@@ -183,38 +226,53 @@ export default class VendorsService {
|
||||
await this.vendorsHaveNoBillsOrThrowError(tenantId, vendorsIds);
|
||||
|
||||
await Contact.query().whereIn('id', vendorsIds).delete();
|
||||
await this.eventDispatcher.dispatch(events.vendors.onBulkDeleted, { tenantId, vendorsIds });
|
||||
await this.eventDispatcher.dispatch(events.vendors.onBulkDeleted, {
|
||||
tenantId,
|
||||
vendorsIds,
|
||||
});
|
||||
|
||||
this.logger.info('[vendor] bulk deleted successfully.', { tenantId, vendorsIds });
|
||||
this.logger.info('[vendor] bulk deleted successfully.', {
|
||||
tenantId,
|
||||
vendorsIds,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates the vendor has no associated bills or throw service error.
|
||||
* @param {number} tenantId
|
||||
* @param {number} vendorId
|
||||
* @param {number} tenantId
|
||||
* @param {number} vendorId
|
||||
*/
|
||||
private async vendorHasNoBillsOrThrowError(tenantId: number, vendorId: number) {
|
||||
private async vendorHasNoBillsOrThrowError(
|
||||
tenantId: number,
|
||||
vendorId: number
|
||||
) {
|
||||
const { billRepository } = this.tenancy.repositories(tenantId);
|
||||
|
||||
// Retrieve the bill that associated to the given vendor id.
|
||||
const bills = await billRepository.find({ vendor_id: vendorId });
|
||||
|
||||
if (bills.length > 0) {
|
||||
throw new ServiceError('vendor_has_bills')
|
||||
throw new ServiceError('vendor_has_bills');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Throws error in case one of vendors have associated bills.
|
||||
* @param {number} tenantId
|
||||
* @param {number[]} customersIds
|
||||
* @param {number} tenantId
|
||||
* @param {number[]} customersIds
|
||||
* @throws {ServiceError}
|
||||
*/
|
||||
private async vendorsHaveNoBillsOrThrowError(tenantId: number, vendorsIds: number[]) {
|
||||
private async vendorsHaveNoBillsOrThrowError(
|
||||
tenantId: number,
|
||||
vendorsIds: number[]
|
||||
) {
|
||||
const { billRepository } = this.tenancy.repositories(tenantId);
|
||||
|
||||
// Retrieves bills that assocaited to the given vendors.
|
||||
const vendorsBills = await billRepository.findWhereIn('vendor_id', vendorsIds);
|
||||
const vendorsBills = await billRepository.findWhereIn(
|
||||
'vendor_id',
|
||||
vendorsIds
|
||||
);
|
||||
const billsVendorsIds = vendorsBills.map((bill) => bill.vendorId);
|
||||
|
||||
// The difference between the vendors ids and bills vendors ids.
|
||||
@@ -233,18 +291,24 @@ export default class VendorsService {
|
||||
public async getVendorsList(
|
||||
tenantId: number,
|
||||
vendorsFilter: IVendorsFilter
|
||||
): Promise<{ vendors: IVendor[], pagination: IPaginationMeta, filterMeta: IFilterMeta }> {
|
||||
const { Contact } = this.tenancy.models(tenantId);
|
||||
const dynamicFilter = await this.dynamicListService.dynamicList(tenantId, Contact, vendorsFilter);
|
||||
|
||||
const { results, pagination } = await Contact.query().onBuild((builder) => {
|
||||
builder.modify('vendor');
|
||||
dynamicFilter.buildQuery()(builder);
|
||||
}).pagination(
|
||||
vendorsFilter.page - 1,
|
||||
vendorsFilter.pageSize,
|
||||
): Promise<{
|
||||
vendors: IVendor[];
|
||||
pagination: IPaginationMeta;
|
||||
filterMeta: IFilterMeta;
|
||||
}> {
|
||||
const { Vendor } = this.tenancy.models(tenantId);
|
||||
const dynamicFilter = await this.dynamicListService.dynamicList(
|
||||
tenantId,
|
||||
Vendor,
|
||||
vendorsFilter
|
||||
);
|
||||
|
||||
const { results, pagination } = await Vendor.query()
|
||||
.onBuild((builder) => {
|
||||
dynamicFilter.buildQuery()(builder);
|
||||
})
|
||||
.pagination(vendorsFilter.page - 1, vendorsFilter.pageSize);
|
||||
|
||||
return {
|
||||
vendors: results,
|
||||
pagination,
|
||||
@@ -254,28 +318,33 @@ export default class VendorsService {
|
||||
|
||||
/**
|
||||
* Changes the opeing balance of the given vendor.
|
||||
* @param {number} tenantId
|
||||
* @param {number} vendorId
|
||||
* @param {number} openingBalance
|
||||
* @param {Date|string} openingBalanceAt
|
||||
* @param {number} tenantId
|
||||
* @param {number} vendorId
|
||||
* @param {number} openingBalance
|
||||
* @param {Date|string} openingBalanceAt
|
||||
*/
|
||||
public async changeOpeningBalance(
|
||||
tenantId: number,
|
||||
vendorId: number,
|
||||
openingBalance: number,
|
||||
openingBalanceAt: Date|string,
|
||||
openingBalanceAt: Date | string
|
||||
): Promise<void> {
|
||||
|
||||
await this.contactService.changeOpeningBalance(
|
||||
tenantId,
|
||||
vendorId,
|
||||
'vendor',
|
||||
openingBalance,
|
||||
openingBalanceAt,
|
||||
openingBalanceAt
|
||||
);
|
||||
// Triggers `onOpeingBalanceChanged` event.
|
||||
await this.eventDispatcher.dispatch(events.vendors.onOpeningBalanceChanged, {
|
||||
tenantId, vendorId, openingBalance, openingBalanceAt
|
||||
});
|
||||
await this.eventDispatcher.dispatch(
|
||||
events.vendors.onOpeningBalanceChanged,
|
||||
{
|
||||
tenantId,
|
||||
vendorId,
|
||||
openingBalance,
|
||||
openingBalanceAt,
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user