mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-16 21:00:31 +00:00
feat: remove path alias.
feat: remove Webpack and depend on nodemon. feat: refactoring expenses. feat: optimize system users with caching. feat: architecture tenant optimize.
This commit is contained in:
70
server/src/api/controllers/Contacts/Contacts.ts
Normal file
70
server/src/api/controllers/Contacts/Contacts.ts
Normal file
@@ -0,0 +1,70 @@
|
||||
import { check, param, query } from 'express-validator';
|
||||
import BaseController from "api/controllers/BaseController";
|
||||
|
||||
export default class ContactsController extends BaseController {
|
||||
|
||||
/**
|
||||
* Contact DTO schema.
|
||||
*/
|
||||
get contactDTOSchema() {
|
||||
return [
|
||||
check('first_name').optional().trim().escape(),
|
||||
check('last_name').optional().trim().escape(),
|
||||
|
||||
check('company_name').optional().trim().escape(),
|
||||
check('display_name').exists().trim().escape(),
|
||||
|
||||
check('email').optional().isEmail().trim().escape(),
|
||||
check('work_phone').optional().trim().escape(),
|
||||
check('personal_phone').optional().trim().escape(),
|
||||
|
||||
check('billing_address_city').optional().trim().escape(),
|
||||
check('billing_address_country').optional().trim().escape(),
|
||||
check('billing_address_email').optional().isEmail().trim().escape(),
|
||||
check('billing_address_zipcode').optional().trim().escape(),
|
||||
check('billing_address_phone').optional().trim().escape(),
|
||||
check('billing_address_state').optional().trim().escape(),
|
||||
|
||||
check('shipping_address_city').optional().trim().escape(),
|
||||
check('shipping_address_country').optional().trim().escape(),
|
||||
check('shipping_address_email').optional().isEmail().trim().escape(),
|
||||
check('shipping_address_zip_code').optional().trim().escape(),
|
||||
check('shipping_address_phone').optional().trim().escape(),
|
||||
check('shipping_address_state').optional().trim().escape(),
|
||||
|
||||
check('note').optional().trim().escape(),
|
||||
check('active').optional().isBoolean().toBoolean(),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Contact new DTO schema.
|
||||
*/
|
||||
get contactNewDTOSchema() {
|
||||
return [
|
||||
check('balance').optional().isNumeric().toInt(),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Contact edit DTO schema.
|
||||
*/
|
||||
get contactEditDTOSchema() {
|
||||
return [
|
||||
|
||||
]
|
||||
}
|
||||
|
||||
get specificContactSchema() {
|
||||
return [
|
||||
param('id').exists().isNumeric().toInt(),
|
||||
];
|
||||
}
|
||||
|
||||
get bulkContactsSchema() {
|
||||
return [
|
||||
query('ids').isArray({ min: 2 }),
|
||||
query('ids.*').isNumeric().toInt(),
|
||||
]
|
||||
}
|
||||
}
|
||||
196
server/src/api/controllers/Contacts/Customers.ts
Normal file
196
server/src/api/controllers/Contacts/Customers.ts
Normal file
@@ -0,0 +1,196 @@
|
||||
import { Request, Response, Router, NextFunction } from 'express';
|
||||
import { Service, Inject } from 'typedi';
|
||||
import { check } from 'express-validator';
|
||||
import ContactsController from 'api/controllers/Contacts/Contacts';
|
||||
import CustomersService from 'services/Contacts/CustomersService';
|
||||
import { ServiceError } from 'exceptions';
|
||||
import { ICustomerNewDTO, ICustomerEditDTO } from 'interfaces';
|
||||
import asyncMiddleware from 'api/middleware/asyncMiddleware';
|
||||
|
||||
@Service()
|
||||
export default class CustomersController extends ContactsController {
|
||||
@Inject()
|
||||
customersService: CustomersService;
|
||||
|
||||
/**
|
||||
* Express router.
|
||||
*/
|
||||
router() {
|
||||
const router = Router();
|
||||
|
||||
router.post('/', [
|
||||
...this.contactDTOSchema,
|
||||
...this.contactNewDTOSchema,
|
||||
...this.customerDTOSchema,
|
||||
],
|
||||
this.validationResult,
|
||||
asyncMiddleware(this.newCustomer.bind(this))
|
||||
);
|
||||
router.post('/:id', [
|
||||
...this.contactDTOSchema,
|
||||
...this.contactEditDTOSchema,
|
||||
...this.customerDTOSchema,
|
||||
],
|
||||
this.validationResult,
|
||||
asyncMiddleware(this.editCustomer.bind(this))
|
||||
);
|
||||
router.delete('/:id', [
|
||||
...this.specificContactSchema,
|
||||
],
|
||||
this.validationResult,
|
||||
asyncMiddleware(this.deleteCustomer.bind(this))
|
||||
);
|
||||
router.delete('/', [
|
||||
...this.bulkContactsSchema,
|
||||
],
|
||||
this.validationResult,
|
||||
asyncMiddleware(this.deleteBulkCustomers.bind(this))
|
||||
);
|
||||
router.get('/:id', [
|
||||
...this.specificContactSchema,
|
||||
],
|
||||
this.validationResult,
|
||||
asyncMiddleware(this.getCustomer.bind(this))
|
||||
);
|
||||
return router;
|
||||
}
|
||||
|
||||
/**
|
||||
* Customer DTO schema.
|
||||
*/
|
||||
get customerDTOSchema() {
|
||||
return [
|
||||
check('customer_type').exists().trim().escape(),
|
||||
check('opening_balance').optional().isNumeric().toInt(),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new customer.
|
||||
* @param {Request} req
|
||||
* @param {Response} res
|
||||
* @param {NextFunction} next
|
||||
*/
|
||||
async newCustomer(req: Request, res: Response, next: NextFunction) {
|
||||
const contactDTO: ICustomerNewDTO = this.matchedBodyData(req);
|
||||
const { tenantId } = req;
|
||||
|
||||
try {
|
||||
const contact = await this.customersService.newCustomer(tenantId, contactDTO);
|
||||
return res.status(200).send({ id: contact.id });
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Edits the given customer details.
|
||||
* @param {Request} req
|
||||
* @param {Response} res
|
||||
* @param {NextFunction} next
|
||||
*/
|
||||
async editCustomer(req: Request, res: Response, next: NextFunction) {
|
||||
const contactDTO: ICustomerEditDTO = this.matchedBodyData(req);
|
||||
const { tenantId } = req;
|
||||
const { id: contactId } = req.params;
|
||||
|
||||
try {
|
||||
await this.customersService.editCustomer(tenantId, contactId, contactDTO);
|
||||
return res.status(200).send({ id: contactId });
|
||||
} catch (error) {
|
||||
if (error instanceof ServiceError) {
|
||||
if (error.errorType === 'contact_not_found') {
|
||||
return res.boom.badRequest(null, {
|
||||
errors: [{ type: 'CUSTOMER.NOT.FOUND', code: 100 }],
|
||||
});
|
||||
}
|
||||
}
|
||||
next(error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes the given customer from the storage.
|
||||
* @param {Request} req
|
||||
* @param {Response} res
|
||||
* @param {NextFunction} next
|
||||
*/
|
||||
async deleteCustomer(req: Request, res: Response, next: NextFunction) {
|
||||
const { tenantId } = req;
|
||||
const { id: contactId } = req.params;
|
||||
|
||||
try {
|
||||
await this.customersService.deleteCustomer(tenantId, contactId)
|
||||
return res.status(200).send({ id: contactId });
|
||||
} catch (error) {
|
||||
if (error instanceof ServiceError) {
|
||||
if (error.errorType === 'contact_not_found') {
|
||||
return res.boom.badRequest(null, {
|
||||
errors: [{ type: 'CUSTOMER.NOT.FOUND', code: 100 }],
|
||||
});
|
||||
}
|
||||
if (error.errorType === 'customer_has_invoices') {
|
||||
return res.boom.badRequest(null, {
|
||||
errors: [{ type: 'CUSTOMER.HAS.SALES_INVOICES', code: 200 }],
|
||||
});
|
||||
}
|
||||
}
|
||||
next(error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve details of the given customer id.
|
||||
* @param {Request} req
|
||||
* @param {Response} res
|
||||
* @param {NextFunction} next
|
||||
*/
|
||||
async getCustomer(req: Request, res: Response, next: NextFunction) {
|
||||
const { tenantId } = req;
|
||||
const { id: contactId } = req.params;
|
||||
|
||||
try {
|
||||
const contact = await this.customersService.getCustomer(tenantId, contactId)
|
||||
return res.status(200).send({ contact });
|
||||
} catch (error) {
|
||||
if (error instanceof ServiceError) {
|
||||
if (error.errorType === 'contact_not_found') {
|
||||
return res.boom.badRequest(null, {
|
||||
errors: [{ type: 'CONTACT.NOT.FOUND', code: 100 }],
|
||||
});
|
||||
}
|
||||
}
|
||||
next(error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes customers in bulk.
|
||||
* @param {Request} req
|
||||
* @param {Response} res
|
||||
* @param {NextFunction} next
|
||||
*/
|
||||
async deleteBulkCustomers(req: Request, res: Response, next: NextFunction) {
|
||||
const { ids: contactsIds } = req.query;
|
||||
const { tenantId } = req;
|
||||
|
||||
try {
|
||||
await this.customersService.deleteBulkCustomers(tenantId, contactsIds)
|
||||
return res.status(200).send({ ids: contactsIds });
|
||||
} catch (error) {
|
||||
if (error instanceof ServiceError) {
|
||||
if (error.errorType === 'contacts_not_found') {
|
||||
return res.boom.badRequest(null, {
|
||||
errors: [{ type: 'CUSTOMERS.NOT.FOUND', code: 100 }],
|
||||
});
|
||||
}
|
||||
if (error.errorType === 'some_customers_have_invoices') {
|
||||
return res.boom.badRequest(null, {
|
||||
errors: [{ type: 'SOME.CUSTOMERS.HAVE.SALES_INVOICES', code: 200 }],
|
||||
});
|
||||
}
|
||||
}
|
||||
next(error);
|
||||
}
|
||||
}
|
||||
}
|
||||
195
server/src/api/controllers/Contacts/Vendors.ts
Normal file
195
server/src/api/controllers/Contacts/Vendors.ts
Normal file
@@ -0,0 +1,195 @@
|
||||
import { Request, Response, Router, NextFunction } from 'express';
|
||||
import { Service, Inject } from 'typedi';
|
||||
import { check } from 'express-validator';
|
||||
import ContactsController from 'api/controllers/Contacts/Contacts';
|
||||
import VendorsService from 'services/Contacts/VendorsService';
|
||||
import { ServiceError } from 'exceptions';
|
||||
import { IVendorNewDTO, IVendorEditDTO } from 'interfaces';
|
||||
import asyncMiddleware from 'api/middleware/asyncMiddleware';
|
||||
|
||||
@Service()
|
||||
export default class VendorsController extends ContactsController {
|
||||
@Inject()
|
||||
vendorsService: VendorsService;
|
||||
|
||||
/**
|
||||
* Express router.
|
||||
*/
|
||||
router() {
|
||||
const router = Router();
|
||||
|
||||
router.post('/', [
|
||||
...this.contactDTOSchema,
|
||||
...this.contactNewDTOSchema,
|
||||
...this.vendorDTOSchema,
|
||||
],
|
||||
this.validationResult,
|
||||
asyncMiddleware(this.newVendor.bind(this))
|
||||
);
|
||||
router.post('/:id', [
|
||||
...this.contactDTOSchema,
|
||||
...this.contactEditDTOSchema,
|
||||
...this.vendorDTOSchema,
|
||||
],
|
||||
this.validationResult,
|
||||
asyncMiddleware(this.editVendor.bind(this))
|
||||
);
|
||||
router.delete('/:id', [
|
||||
...this.specificContactSchema,
|
||||
],
|
||||
this.validationResult,
|
||||
asyncMiddleware(this.deleteVendor.bind(this))
|
||||
);
|
||||
router.delete('/', [
|
||||
...this.bulkContactsSchema,
|
||||
],
|
||||
this.validationResult,
|
||||
asyncMiddleware(this.deleteBulkVendors.bind(this))
|
||||
);
|
||||
router.get('/:id', [
|
||||
...this.specificContactSchema,
|
||||
],
|
||||
this.validationResult,
|
||||
asyncMiddleware(this.getVendor.bind(this))
|
||||
);
|
||||
return router;
|
||||
}
|
||||
|
||||
/**
|
||||
* Vendor DTO schema.
|
||||
*/
|
||||
get vendorDTOSchema() {
|
||||
return [
|
||||
check('opening_balance').optional().isNumeric().toInt(),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new vendor.
|
||||
* @param {Request} req
|
||||
* @param {Response} res
|
||||
* @param {NextFunction} next
|
||||
*/
|
||||
async newVendor(req: Request, res: Response, next: NextFunction) {
|
||||
const contactDTO: IVendorNewDTO = this.matchedBodyData(req);
|
||||
const { tenantId } = req;
|
||||
|
||||
try {
|
||||
const contact = await this.vendorsService.newVendor(tenantId, contactDTO);
|
||||
return res.status(200).send({ id: contact.id });
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Edits the given vendor details.
|
||||
* @param {Request} req
|
||||
* @param {Response} res
|
||||
* @param {NextFunction} next
|
||||
*/
|
||||
async editVendor(req: Request, res: Response, next: NextFunction) {
|
||||
const contactDTO: IVendorEditDTO = this.matchedBodyData(req);
|
||||
const { tenantId } = req;
|
||||
const { id: contactId } = req.params;
|
||||
|
||||
try {
|
||||
await this.vendorsService.editVendor(tenantId, contactId, contactDTO);
|
||||
return res.status(200).send({ id: contactId });
|
||||
} catch (error) {
|
||||
if (error instanceof ServiceError) {
|
||||
if (error.errorType === 'contact_not_found') {
|
||||
return res.status(400).send({
|
||||
errors: [{ type: 'VENDOR.NOT.FOUND', code: 100 }],
|
||||
});
|
||||
}
|
||||
}
|
||||
next(error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes the given vendor from the storage.
|
||||
* @param {Request} req
|
||||
* @param {Response} res
|
||||
* @param {NextFunction} next
|
||||
*/
|
||||
async deleteVendor(req: Request, res: Response, next: NextFunction) {
|
||||
const { tenantId } = req;
|
||||
const { id: contactId } = req.params;
|
||||
|
||||
try {
|
||||
await this.vendorsService.deleteVendor(tenantId, contactId)
|
||||
return res.status(200).send({ id: contactId });
|
||||
} catch (error) {
|
||||
if (error instanceof ServiceError) {
|
||||
if (error.errorType === 'contact_not_found') {
|
||||
return res.status(400).send({
|
||||
errors: [{ type: 'VENDOR.NOT.FOUND', code: 100 }],
|
||||
});
|
||||
}
|
||||
if (error.errorType === 'vendor_has_bills') {
|
||||
return res.status(400).send({
|
||||
errors: [{ type: 'VENDOR.HAS.BILLS', code: 200 }],
|
||||
});
|
||||
}
|
||||
}
|
||||
next(error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve details of the given vendor id.
|
||||
* @param {Request} req
|
||||
* @param {Response} res
|
||||
* @param {NextFunction} next
|
||||
*/
|
||||
async getVendor(req: Request, res: Response, next: NextFunction) {
|
||||
const { tenantId } = req;
|
||||
const { id: vendorId } = req.params;
|
||||
|
||||
try {
|
||||
const vendor = await this.vendorsService.getVendor(tenantId, vendorId)
|
||||
return res.status(200).send({ vendor });
|
||||
} catch (error) {
|
||||
if (error instanceof ServiceError) {
|
||||
if (error.errorType === 'contact_not_found') {
|
||||
return res.status(400).send({
|
||||
errors: [{ type: 'VENDOR.NOT.FOUND', code: 100 }],
|
||||
});
|
||||
}
|
||||
}
|
||||
next(error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes vendors in bulk.
|
||||
* @param {Request} req
|
||||
* @param {Response} res
|
||||
* @param {NextFunction} next
|
||||
*/
|
||||
async deleteBulkVendors(req: Request, res: Response, next: NextFunction) {
|
||||
const { ids: contactsIds } = req.query;
|
||||
const { tenantId } = req;
|
||||
|
||||
try {
|
||||
await this.vendorsService.deleteBulkVendors(tenantId, contactsIds)
|
||||
return res.status(200).send({ ids: contactsIds });
|
||||
} catch (error) {
|
||||
if (error instanceof ServiceError) {
|
||||
if (error.errorType === 'contacts_not_found') {
|
||||
return res.boom.badRequest(null, {
|
||||
errors: [{ type: 'VENDORS.NOT.FOUND', code: 100 }],
|
||||
});
|
||||
}
|
||||
if (error.errorType === 'some_vendors_have_bills') {
|
||||
return res.boom.badRequest(null, {
|
||||
errors: [{ type: 'SOME.VENDORS.HAVE.BILLS', code: 200 }],
|
||||
});
|
||||
}
|
||||
}
|
||||
next(error);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user