feat: licenses administration basic authentication.

feat: accounts slug.
feat: duplicate accounts_balance table and merge balance with accounts table.
feat: refactoring customers and vendors.
feat: system user soft deleting.
feat: preventing build tenant database without any subscription.
feat: remove 'password' property from 'req.user' object.
feat: refactoring JournalPoster class.
feat: delete duplicated directories and files.
This commit is contained in:
Ahmed Bouhuolia
2020-09-09 21:30:19 +02:00
parent 98bba3d3a0
commit ad00f140d1
77 changed files with 2431 additions and 1848 deletions

View File

@@ -0,0 +1,131 @@
import { Inject, Service } from 'typedi';
import { difference } from 'lodash';
import { ServiceError } from "@/exceptions";
import TenancyService from '@/services/Tenancy/TenancyService';
import {
IContact,
IContactNewDTO,
IContactEditDTO,
} from "@/interfaces";
type TContactService = 'customer' | 'vendor';
@Service()
export default class ContactsService {
@Inject()
tenancy: TenancyService;
@Inject('logger')
logger: any;
/**
* Get the given contact or throw not found contact.
* @param {number} tenantId
* @param {number} contactId
* @param {TContactService} contactService
* @return {Promise<IContact>}
*/
private async getContactByIdOrThrowError(tenantId: number, contactId: number, contactService: TContactService) {
const { Contact } = this.tenancy.models(tenantId);
this.logger.info('[contact] trying to validate contact existance.', { tenantId, contactId });
const contact = await Contact.query().findById(contactId).where('contact_service', contactService);
if (!contact) {
throw new ServiceError('contact_not_found');
}
return contact;
}
/**
* Creates a new contact on the storage.
* @param {number} tenantId
* @param {TContactService} contactService
* @param {IContactDTO} contactDTO
*/
async newContact(tenantId: number, contactDTO: IContactNewDTO, contactService: TContactService) {
const { Contact } = this.tenancy.models(tenantId);
this.logger.info('[contacts] trying to insert contact to the storage.', { tenantId, contactDTO });
const contact = await Contact.query().insert({ contactService, ...contactDTO });
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 {TContactService} contactService
* @param {IContactDTO} contactDTO
*/
async editContact(tenantId: number, contactId: number, contactDTO: IContactEditDTO, contactService: TContactService) {
const { Contact } = this.tenancy.models(tenantId);
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({ ...contactDTO })
}
/**
* Deletes the given contact from the storage.
* @param {number} tenantId
* @param {number} contactId
* @param {TContactService} contactService
* @return {Promise<void>}
*/
async deleteContact(tenantId: number, contactId: number, contactService: TContactService) {
const { Contact } = this.tenancy.models(tenantId);
const contact = await this.getContactByIdOrThrowError(tenantId, contactId, contactService);
this.logger.info('[contacts] trying to delete the given contact.', { tenantId, contactId });
await Contact.query().findById(contactId).delete();
}
/**
* Get contact details of the given contact id.
* @param {number} tenantId
* @param {number} contactId
* @param {TContactService} contactService
* @returns {Promise<IContact>}
*/
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 {TContactService} contactService
* @return {Promise<IContact>}
*/
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 storedContactsIds = contacts.map((contact: IContact) => contact.id);
const notFoundCustomers = difference(contactsIds, storedContactsIds);
if (notFoundCustomers.length > 0) {
throw new ServiceError('contacts_not_found');
}
return contacts;
}
/**
* Deletes the given contacts in bulk.
* @param {number} tenantId
* @param {number[]} contactsIds
* @param {TContactService} contactService
* @return {Promise<void>}
*/
async deleteBulkContacts(tenantId: number, contactsIds: number[], contactService: TContactService) {
const { Contact } = this.tenancy.models(tenantId);
this.getContactsOrThrowErrorNotFound(tenantId, contactsIds, contactService);
await Contact.query().whereIn('id', contactsIds).delete();
}
}

View File

@@ -0,0 +1,171 @@
import { Inject, Service } from 'typedi';
import { omit, difference } from 'lodash';
import JournalPoster from "@/services/Accounting/JournalPoster";
import JournalCommands from "@/services/Accounting/JournalCommands";
import ContactsService from '@/services/Contacts/ContactsService';
import {
ICustomerNewDTO,
ICustomerEditDTO,
} from '@/interfaces';
import { ServiceError } from '@/exceptions';
import TenancyService from '@/services/Tenancy/TenancyService';
import { ICustomer } from 'src/interfaces';
@Service()
export default class CustomersService {
@Inject()
contactService: ContactsService;
@Inject()
tenancy: TenancyService;
/**
* Converts customer to contact DTO.
* @param {ICustomerNewDTO|ICustomerEditDTO} customerDTO
* @returns {IContactDTO}
*/
customerToContactDTO(customerDTO: ICustomerNewDTO|ICustomerEditDTO) {
return {
...omit(customerDTO, ['customerType']),
contactType: customerDTO.customerType,
active: (typeof customerDTO.active === 'undefined') ?
true : customerDTO.active,
};
}
/**
* Creates a new customer.
* @param {number} tenantId
* @param {ICustomerNewDTO} customerDTO
* @return {Promise<void>}
*/
async newCustomer(tenantId: number, customerDTO: ICustomerNewDTO) {
const contactDTO = this.customerToContactDTO(customerDTO)
const customer = await this.contactService.newContact(tenantId, contactDTO, 'customer');
// Writes the customer opening balance journal entries.
if (customer.openingBalance) {
await this.writeCustomerOpeningBalanceJournal(
tenantId,
customer.id,
customer.openingBalance,
);
}
return customer;
}
/**
* Edits details of the given customer.
* @param {number} tenantId
* @param {ICustomerEditDTO} customerDTO
*/
async editCustomer(tenantId: number, customerId: number, customerDTO: ICustomerEditDTO) {
const contactDTO = this.customerToContactDTO(customerDTO);
return this.contactService.editContact(tenantId, customerId, contactDTO, 'customer');
}
/**
* Deletes the given customer from the storage.
* @param {number} tenantId
* @param {number} customerId
* @return {Promise<void>}
*/
async deleteCustomer(tenantId: number, customerId: number) {
await this.customerHasNoInvoicesOrThrowError(tenantId, customerId);
return this.contactService.deleteContact(tenantId, customerId, 'customer');
}
/**
* Retrieve the given customer details.
* @param {number} tenantId
* @param {number} customerId
*/
async getCustomer(tenantId: number, customerId: number) {
return this.contactService.getContact(tenantId, customerId, 'customer');
}
/**
* Writes customer opening balance journal entries.
* @param {number} tenantId
* @param {number} customerId
* @param {number} openingBalance
* @return {Promise<void>}
*/
async writeCustomerOpeningBalanceJournal(
tenantId: number,
customerId: number,
openingBalance: number,
) {
const journal = new JournalPoster(tenantId);
const journalCommands = new JournalCommands(journal);
await journalCommands.customerOpeningBalance(customerId, openingBalance)
await Promise.all([
journal.saveBalance(),
journal.saveEntries(),
]);
}
/**
* Retrieve the given customers or throw error if one of them not found.
* @param {numebr} tenantId
* @param {number[]} customersIds
*/
getCustomersOrThrowErrorNotFound(tenantId: number, customersIds: number[]) {
return this.contactService.getContactsOrThrowErrorNotFound(tenantId, customersIds, 'customer');
}
/**
* Deletes the given customers from the storage.
* @param {number} tenantId
* @param {number[]} customersIds
* @return {Promise<void>}
*/
async deleteBulkCustomers(tenantId: number, customersIds: number[]) {
const { Contact } = this.tenancy.models(tenantId);
await this.getCustomersOrThrowErrorNotFound(tenantId, customersIds);
await this.customersHaveNoInvoicesOrThrowError(tenantId, customersIds);
await Contact.query().whereIn('id', customersIds).delete();
}
/**
* Validates the customer has no associated sales invoice
* or throw service error.
* @param {number} tenantId
* @param {number} customerId
*/
async customerHasNoInvoicesOrThrowError(tenantId: number, customerId: number) {
const { customerRepository } = this.tenancy.repositories(tenantId);
const salesInvoice = await customerRepository.getSalesInvoices(customerId);
if (salesInvoice.length > 0) {
throw new ServiceError('customer_has_invoices');
}
}
/**
* Throws error in case one of customers have associated sales invoices.
* @param {number} tenantId
* @param {number[]} customersIds
* @throws {ServiceError}
*/
async customersHaveNoInvoicesOrThrowError(tenantId: number, customersIds: number[]) {
const { customerRepository } = this.tenancy.repositories(tenantId);
const customersWithInvoices = await customerRepository.customersWithSalesInvoices(
customersIds,
);
const customersIdsWithInvoice = customersWithInvoices
.filter((customer: ICustomer) => customer.salesInvoices.length > 0)
.map((customer: ICustomer) => customer.id);
const customersHaveInvoices = difference(customersIds, customersIdsWithInvoice);
if (customersHaveInvoices.length > 0) {
throw new ServiceError('some_customers_have_invoices');
}
}
}

View File

@@ -0,0 +1,167 @@
import { Inject, Service } from 'typedi';
import { difference } from 'lodash';
import JournalPoster from "@/services/Accounting/JournalPoster";
import JournalCommands from "@/services/Accounting/JournalCommands";
import ContactsService from '@/services/Contacts/ContactsService';
import {
IVendorNewDTO,
IVendorEditDTO,
IVendor
} from '@/interfaces';
import { ServiceError } from '@/exceptions';
import TenancyService from '@/services/Tenancy/TenancyService';
@Service()
export default class VendorsService {
@Inject()
contactService: ContactsService;
@Inject()
tenancy: TenancyService;
/**
* Converts vendor to contact DTO.
* @param {IVendorNewDTO|IVendorEditDTO} vendorDTO
* @returns {IContactDTO}
*/
vendorToContactDTO(vendorDTO: IVendorNewDTO|IVendorEditDTO) {
return {
...vendorDTO,
active: (typeof vendorDTO.active === 'undefined') ?
true : vendorDTO.active,
};
}
/**
* Creates a new vendor.
* @param {number} tenantId
* @param {IVendorNewDTO} vendorDTO
* @return {Promise<void>}
*/
async newVendor(tenantId: number, vendorDTO: IVendorNewDTO) {
const contactDTO = this.vendorToContactDTO(vendorDTO)
const vendor = await this.contactService.newContact(tenantId, contactDTO, 'vendor');
// Writes the vendor opening balance journal entries.
if (vendor.openingBalance) {
await this.writeVendorOpeningBalanceJournal(
tenantId,
vendor.id,
vendor.openingBalance,
);
}
return vendor;
}
/**
* Edits details of the given vendor.
* @param {number} tenantId
* @param {IVendorEditDTO} vendorDTO
*/
async editVendor(tenantId: number, vendorId: number, vendorDTO: IVendorEditDTO) {
const contactDTO = this.vendorToContactDTO(vendorDTO);
return this.contactService.editContact(tenantId, vendorId, contactDTO, 'vendor');
}
/**
* Deletes the given vendor from the storage.
* @param {number} tenantId
* @param {number} vendorId
* @return {Promise<void>}
*/
async deleteVendor(tenantId: number, vendorId: number) {
await this.vendorHasNoBillsOrThrowError(tenantId, vendorId);
return this.contactService.deleteContact(tenantId, vendorId, 'vendor');
}
/**
* Retrieve the given vendor details.
* @param {number} tenantId
* @param {number} vendorId
*/
async getVendor(tenantId: number, vendorId: number) {
return this.contactService.getContact(tenantId, vendorId, 'vendor');
}
/**
* Writes vendor opening balance journal entries.
* @param {number} tenantId
* @param {number} vendorId
* @param {number} openingBalance
* @return {Promise<void>}
*/
async writeVendorOpeningBalanceJournal(
tenantId: number,
vendorId: number,
openingBalance: number,
) {
const journal = new JournalPoster(tenantId);
const journalCommands = new JournalCommands(journal);
await journalCommands.vendorOpeningBalance(vendorId, openingBalance)
await Promise.all([
journal.saveBalance(),
journal.saveEntries(),
]);
}
/**
* Retrieve the given vendors or throw error if one of them not found.
* @param {numebr} tenantId
* @param {number[]} vendorsIds
*/
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
* @return {Promise<void>}
*/
async deleteBulkVendors(tenantId: number, vendorsIds: number[]) {
const { Contact } = this.tenancy.models(tenantId);
await this.getVendorsOrThrowErrorNotFound(tenantId, vendorsIds);
await this.vendorsHaveNoBillsOrThrowError(tenantId, vendorsIds);
await Contact.query().whereIn('id', vendorsIds).delete();
}
/**
* Validates the vendor has no associated bills or throw service error.
* @param {number} tenantId
* @param {number} vendorId
*/
async vendorHasNoBillsOrThrowError(tenantId: number, vendorId: number) {
const { vendorRepository } = this.tenancy.repositories(tenantId);
const bills = await vendorRepository.getBills(vendorId);
if (bills) {
throw new ServiceError('vendor_has_bills')
}
}
/**
* Throws error in case one of vendors have associated bills.
* @param {number} tenantId
* @param {number[]} customersIds
* @throws {ServiceError}
*/
async vendorsHaveNoBillsOrThrowError(tenantId: number, vendorsIds: number[]) {
const { vendorRepository } = 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);
const vendorsHaveInvoices = difference(vendorsIds, vendorsIdsWithBills);
if (vendorsHaveInvoices.length > 0) {
throw new ServiceError('some_vendors_have_bills');
}
}
}