mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-17 05:10:31 +00:00
feat: rewrite repositories with base entity repository class.
feat: sales and purchases status. feat: sales and purchases auto-increment number. fix: settings find query with extra columns.
This commit is contained in:
@@ -27,10 +27,13 @@ export default class ContactsService {
|
||||
* @return {Promise<IContact>}
|
||||
*/
|
||||
public async getContactByIdOrThrowError(tenantId: number, contactId: number, contactService: TContactService) {
|
||||
const { Contact } = this.tenancy.models(tenantId);
|
||||
const { contactRepository } = this.tenancy.repositories(tenantId);
|
||||
|
||||
this.logger.info('[contact] trying to validate contact existance.', { tenantId, contactId });
|
||||
const contact = await Contact.query().findById(contactId).where('contact_service', contactService);
|
||||
const contact = await contactRepository.findOne({
|
||||
id: contactId,
|
||||
contactService: contactService,
|
||||
});
|
||||
|
||||
if (!contact) {
|
||||
throw new ServiceError('contact_not_found');
|
||||
@@ -70,7 +73,7 @@ export default class ContactsService {
|
||||
const contactObj = this.transformContactObj(contactDTO);
|
||||
|
||||
this.logger.info('[contacts] trying to insert contact to the storage.', { tenantId, contactDTO });
|
||||
const contact = await contactRepository.insert({ contactService, ...contactObj });
|
||||
const contact = await contactRepository.create({ contactService, ...contactObj });
|
||||
|
||||
this.logger.info('[contacts] contact inserted successfully.', { tenantId, contact });
|
||||
return contact;
|
||||
@@ -84,13 +87,13 @@ export default class ContactsService {
|
||||
* @param {IContactDTO} contactDTO
|
||||
*/
|
||||
async editContact(tenantId: number, contactId: number, contactDTO: IContactEditDTO, contactService: TContactService) {
|
||||
const { Contact } = this.tenancy.models(tenantId);
|
||||
const { contactRepository } = this.tenancy.repositories(tenantId);
|
||||
const contactObj = this.transformContactObj(contactDTO);
|
||||
|
||||
const contact = await this.getContactByIdOrThrowError(tenantId, contactId, contactService);
|
||||
|
||||
this.logger.info('[contacts] trying to edit the given contact details.', { tenantId, contactId, contactDTO });
|
||||
await Contact.query().findById(contactId).patch({ ...contactObj })
|
||||
await contactRepository.update({ ...contactObj }, { id: contactId });
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -105,6 +108,8 @@ export default class ContactsService {
|
||||
const contact = await this.getContactByIdOrThrowError(tenantId, contactId, contactService);
|
||||
|
||||
this.logger.info('[contacts] trying to delete the given contact.', { tenantId, contactId });
|
||||
|
||||
// Deletes contact of the given id.
|
||||
await contactRepository.deleteById(contactId);
|
||||
}
|
||||
|
||||
@@ -151,7 +156,7 @@ export default class ContactsService {
|
||||
const { contactRepository } = this.tenancy.repositories(tenantId);
|
||||
this.getContactsOrThrowErrorNotFound(tenantId, contactsIds, contactService);
|
||||
|
||||
await contactRepository.bulkDelete(contactsIds);
|
||||
await contactRepository.deleteWhereIdIn(contactsIds);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -15,13 +15,15 @@ import {
|
||||
ICustomersFilter,
|
||||
IContactNewDTO,
|
||||
IContactEditDTO,
|
||||
IContact
|
||||
IContact,
|
||||
ISaleInvoice
|
||||
} from 'interfaces';
|
||||
import { ServiceError } from 'exceptions';
|
||||
import TenancyService from 'services/Tenancy/TenancyService';
|
||||
import DynamicListingService from 'services/DynamicListing/DynamicListService';
|
||||
import events from 'subscribers/events';
|
||||
import moment from 'moment';
|
||||
import SaleInvoiceRepository from 'repositories/SaleInvoiceRepository';
|
||||
|
||||
@Service()
|
||||
export default class CustomersService {
|
||||
@@ -68,6 +70,7 @@ export default class CustomersService {
|
||||
}
|
||||
|
||||
private transformContactToCustomer(contactModel: IContact) {
|
||||
console.log(contactModel);
|
||||
return {
|
||||
...omit(contactModel.toJSON(), ['contactService', 'contactType']),
|
||||
customerType: contactModel.contactType,
|
||||
@@ -263,8 +266,10 @@ export default class CustomersService {
|
||||
* @return {Promise<void>}
|
||||
*/
|
||||
private async customerHasNoInvoicesOrThrowError(tenantId: number, customerId: number) {
|
||||
const { customerRepository } = this.tenancy.repositories(tenantId);
|
||||
const salesInvoice = await customerRepository.getSalesInvoices(customerId);
|
||||
const { saleInvoiceRepository } = this.tenancy.repositories(tenantId);
|
||||
|
||||
// Retrieve the sales invoices that assocaited to the given customer.
|
||||
const salesInvoice = await saleInvoiceRepository.find({ customer_id: customerId });
|
||||
|
||||
if (salesInvoice.length > 0) {
|
||||
throw new ServiceError('customer_has_invoices');
|
||||
@@ -279,14 +284,13 @@ export default class CustomersService {
|
||||
* @return {Promise<void>}
|
||||
*/
|
||||
private async customersHaveNoInvoicesOrThrowError(tenantId: number, customersIds: number[]) {
|
||||
const { customerRepository } = this.tenancy.repositories(tenantId);
|
||||
const { saleInvoiceRepository } = this.tenancy.repositories(tenantId);
|
||||
|
||||
const customersWithInvoices = await customerRepository.customersWithSalesInvoices(
|
||||
customersIds,
|
||||
const customersInvoices = await saleInvoiceRepository.findWhereIn(
|
||||
'customer_id', customersIds
|
||||
);
|
||||
const customersIdsWithInvoice = customersWithInvoices
|
||||
.filter((customer: ICustomer) => customer.salesInvoices.length > 0)
|
||||
.map((customer: ICustomer) => customer.id);
|
||||
const customersIdsWithInvoice = customersInvoices
|
||||
.map((saleInvoice: ISaleInvoice) => saleInvoice.customerId);
|
||||
|
||||
const customersHaveInvoices = difference(customersIds, customersIdsWithInvoice);
|
||||
|
||||
|
||||
@@ -194,8 +194,10 @@ export default class VendorsService {
|
||||
* @param {number} vendorId
|
||||
*/
|
||||
private async vendorHasNoBillsOrThrowError(tenantId: number, vendorId: number) {
|
||||
const { vendorRepository } = this.tenancy.repositories(tenantId);
|
||||
const bills = await vendorRepository.getBills(vendorId);
|
||||
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')
|
||||
@@ -209,14 +211,14 @@ export default class VendorsService {
|
||||
* @throws {ServiceError}
|
||||
*/
|
||||
private async vendorsHaveNoBillsOrThrowError(tenantId: number, vendorsIds: number[]) {
|
||||
const { vendorRepository } = this.tenancy.repositories(tenantId);
|
||||
const { billRepository } = this.tenancy.repositories(tenantId);
|
||||
|
||||
const vendorsWithBills = await vendorRepository.vendorsWithBills(vendorsIds);
|
||||
const vendorsIdsWithBills = vendorsWithBills
|
||||
.filter((vendor: IVendor) => vendor.bills.length > 0)
|
||||
.map((vendor: IVendor) => vendor.id);
|
||||
// Retrieves bills that assocaited to the given vendors.
|
||||
const vendorsBills = await billRepository.findWhereIn('vendor_id', vendorsIds);
|
||||
const billsVendorsIds = vendorsBills.map((bill) => bill.vendorId);
|
||||
|
||||
const vendorsHaveInvoices = difference(vendorsIds, vendorsIdsWithBills);
|
||||
// The difference between the vendors ids and bills vendors ids.
|
||||
const vendorsHaveInvoices = difference(vendorsIds, billsVendorsIds);
|
||||
|
||||
if (vendorsHaveInvoices.length > 0) {
|
||||
throw new ServiceError('some_vendors_have_bills');
|
||||
|
||||
Reference in New Issue
Block a user