mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-17 05:10:31 +00:00
feat: customer form styling.
feat: customers list.
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { Inject, Service } from 'typedi';
|
||||
import { difference, upperFirst } from 'lodash';
|
||||
import { difference, upperFirst, omit } from 'lodash';
|
||||
import { ServiceError } from "exceptions";
|
||||
import TenancyService from 'services/Tenancy/TenancyService';
|
||||
import {
|
||||
@@ -38,17 +38,39 @@ export default class ContactsService {
|
||||
return contact;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts contact DTO object to model object attributes to insert or update.
|
||||
* @param {IContactNewDTO | IContactEditDTO} contactDTO
|
||||
*/
|
||||
private transformContactObj(contactDTO: IContactNewDTO | IContactEditDTO) {
|
||||
return {
|
||||
...omit(contactDTO, [
|
||||
'billingAddress1', 'billingAddress2',
|
||||
'shippingAddress1', 'shippingAddress2',
|
||||
]),
|
||||
billing_address_1: contactDTO?.billingAddress1,
|
||||
billing_address_2: contactDTO?.billingAddress2,
|
||||
shipping_address_1: contactDTO?.shippingAddress1,
|
||||
shipping_address_2: contactDTO?.shippingAddress2,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new contact on the storage.
|
||||
* @param {number} tenantId
|
||||
* @param {TContactService} contactService
|
||||
* @param {IContactDTO} contactDTO
|
||||
*/
|
||||
async newContact(tenantId: number, contactDTO: IContactNewDTO, contactService: TContactService) {
|
||||
async newContact(
|
||||
tenantId: number,
|
||||
contactDTO: IContactNewDTO,
|
||||
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.insert({ contactService, ...contactDTO });
|
||||
const contact = await contactRepository.insert({ contactService, ...contactObj });
|
||||
|
||||
this.logger.info('[contacts] contact inserted successfully.', { tenantId, contact });
|
||||
return contact;
|
||||
@@ -63,10 +85,12 @@ export default class ContactsService {
|
||||
*/
|
||||
async editContact(tenantId: number, contactId: number, contactDTO: IContactEditDTO, contactService: TContactService) {
|
||||
const { Contact } = this.tenancy.models(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({ ...contactDTO })
|
||||
await Contact.query().findById(contactId).patch({ ...contactObj })
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -12,12 +12,15 @@ import {
|
||||
ICustomerEditDTO,
|
||||
ICustomer,
|
||||
IPaginationMeta,
|
||||
ICustomersFilter
|
||||
ICustomersFilter,
|
||||
IContactNewDTO,
|
||||
IContactEditDTO
|
||||
} 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';
|
||||
|
||||
@Service()
|
||||
export default class CustomersService {
|
||||
@@ -41,7 +44,7 @@ export default class CustomersService {
|
||||
* @param {ICustomerNewDTO|ICustomerEditDTO} customerDTO
|
||||
* @returns {IContactDTO}
|
||||
*/
|
||||
private customerToContactDTO(customerDTO: ICustomerNewDTO | ICustomerEditDTO) {
|
||||
private customerToContactDTO(customerDTO: ICustomerNewDTO|ICustomerEditDTO): IContactNewDTO|IContactEditDTO {
|
||||
return {
|
||||
...omit(customerDTO, ['customerType']),
|
||||
contactType: customerDTO.customerType,
|
||||
@@ -50,6 +53,18 @@ export default class CustomersService {
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Transforms new customer DTO to contact.
|
||||
* @param customerDTO
|
||||
*/
|
||||
private transformNewCustomerDTO(customerDTO: ICustomerNewDTO): IContactNewDTO {
|
||||
return {
|
||||
...this.customerToContactDTO(customerDTO),
|
||||
openingBalanceAt: customerDTO?.openingBalanceAt
|
||||
? moment(customerDTO.openingBalanceAt).toMySqlDateTime() : null,
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new customer.
|
||||
* @param {number} tenantId
|
||||
@@ -62,8 +77,8 @@ export default class CustomersService {
|
||||
): Promise<ICustomer> {
|
||||
this.logger.info('[customer] trying to create a new customer.', { tenantId, customerDTO });
|
||||
|
||||
const contactDTO = this.customerToContactDTO(customerDTO)
|
||||
const customer = await this.contactService.newContact(tenantId, contactDTO, 'customer');
|
||||
const customerObj = this.transformNewCustomerDTO(customerDTO);
|
||||
const customer = await this.contactService.newContact(tenantId, customerObj, 'customer');
|
||||
|
||||
this.logger.info('[customer] created successfully.', { tenantId, customerDTO });
|
||||
await this.eventDispatcher.dispatch(events.customers.onCreated, {
|
||||
|
||||
Reference in New Issue
Block a user