mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-18 13:50:31 +00:00
feat: activate/inactive contacts.
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import { check, param, query, body, ValidationChain } from 'express-validator';
|
||||
import { Router, Request, Response, NextFunction } from 'express';
|
||||
import { Inject, Service } from 'typedi';
|
||||
import * as R from 'ramda';
|
||||
import { ServiceError } from 'exceptions';
|
||||
import BaseController from 'api/controllers/BaseController';
|
||||
import ContactsService from 'services/Contacts/ContactsService';
|
||||
import DynamicListingService from 'services/DynamicListing/DynamicListService';
|
||||
@@ -34,6 +34,20 @@ export default class ContactsController extends BaseController {
|
||||
this.validationResult,
|
||||
this.asyncMiddleware(this.getContact.bind(this))
|
||||
);
|
||||
router.post(
|
||||
'/:id/inactivate',
|
||||
[param('id').exists().isNumeric().toInt()],
|
||||
this.validationResult,
|
||||
this.asyncMiddleware(this.inactivateContact.bind(this)),
|
||||
this.handlerServiceErrors
|
||||
);
|
||||
router.post(
|
||||
'/:id/activate',
|
||||
[param('id').exists().isNumeric().toInt()],
|
||||
this.validationResult,
|
||||
this.asyncMiddleware(this.activateContact.bind(this)),
|
||||
this.handlerServiceErrors
|
||||
);
|
||||
return router;
|
||||
}
|
||||
|
||||
@@ -302,4 +316,81 @@ export default class ContactsController extends BaseController {
|
||||
get specificContactSchema(): ValidationChain[] {
|
||||
return [param('id').exists().isNumeric().toInt()];
|
||||
}
|
||||
|
||||
/**
|
||||
* Activates the given contact.
|
||||
* @param {Request} req
|
||||
* @param {Response} res
|
||||
* @param {NextFunction} next
|
||||
*/
|
||||
async activateContact(req: Request, res: Response, next: NextFunction) {
|
||||
const { tenantId } = req;
|
||||
const { id: contactId } = req.params;
|
||||
|
||||
try {
|
||||
await this.contactsService.activateContact(tenantId, contactId);
|
||||
|
||||
return res.status(200).send({
|
||||
id: contactId,
|
||||
message: 'The given contact activated successfully.',
|
||||
});
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Inactivate the given contact.
|
||||
* @param {Request} req
|
||||
* @param {Response} res
|
||||
* @param {NextFunction} next
|
||||
*/
|
||||
async inactivateContact(req: Request, res: Response, next: NextFunction) {
|
||||
const { tenantId } = req;
|
||||
const { id: contactId } = req.params;
|
||||
|
||||
try {
|
||||
await this.contactsService.inactivateContact(tenantId, contactId);
|
||||
|
||||
return res.status(200).send({
|
||||
id: contactId,
|
||||
message: 'The given contact inactivated successfully.',
|
||||
});
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles service errors.
|
||||
* @param {Error} error
|
||||
* @param {Request} req
|
||||
* @param {Response} res
|
||||
* @param {NextFunction} next
|
||||
*/
|
||||
private handlerServiceErrors(
|
||||
error: Error,
|
||||
req: Request,
|
||||
res: Response,
|
||||
next: NextFunction
|
||||
) {
|
||||
if (error instanceof ServiceError) {
|
||||
if (error.errorType === 'contact_not_found') {
|
||||
return res.boom.badRequest(null, {
|
||||
errors: [{ type: 'CONTACT.NOT.FOUND', code: 100 }],
|
||||
});
|
||||
}
|
||||
if (error.errorType === 'CONTACT_ALREADY_ACTIVE') {
|
||||
return res.boom.badRequest(null, {
|
||||
errors: [{ type: 'CONTACT_ALREADY_ACTIVE', code: 700 }],
|
||||
});
|
||||
}
|
||||
if (error.errorType === 'CONTACT_ALREADY_INACTIVE') {
|
||||
return res.boom.badRequest(null, {
|
||||
errors: [{ type: 'CONTACT_ALREADY_INACTIVE', code: 800 }],
|
||||
});
|
||||
}
|
||||
}
|
||||
next(error);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,13 +12,10 @@ import {
|
||||
IContactsAutoCompleteFilter,
|
||||
} from 'interfaces';
|
||||
import JournalPoster from '../Accounting/JournalPoster';
|
||||
import { ERRORS } from './constants';
|
||||
|
||||
type TContactService = 'customer' | 'vendor';
|
||||
|
||||
const ERRORS = {
|
||||
OPENING_BALANCE_DATE_REQUIRED: 'OPENING_BALANCE_DATE_REQUIRED',
|
||||
};
|
||||
|
||||
@Service()
|
||||
export default class ContactsService {
|
||||
@Inject()
|
||||
@@ -361,4 +358,34 @@ export default class ContactsService {
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Inactive the given contact.
|
||||
* @param {number} tenantId - Tenant id.
|
||||
* @param {number} contactId - Contact id.
|
||||
*/
|
||||
async inactivateContact(tenantId: number, contactId: number): Promise<void> {
|
||||
const { Contact } = this.tenancy.models(tenantId);
|
||||
const contact = await this.getContactByIdOrThrowError(tenantId, contactId);
|
||||
|
||||
if(!contact.active) {
|
||||
throw new ServiceError(ERRORS.CONTACT_ALREADY_INACTIVE);
|
||||
}
|
||||
await Contact.query().findById(contactId).update({ active: false });
|
||||
}
|
||||
|
||||
/**
|
||||
* Inactive the given contact.
|
||||
* @param {number} tenantId - Tenant id.
|
||||
* @param {number} contactId - Contact id.
|
||||
*/
|
||||
async activateContact(tenantId: number, contactId: number): Promise<void> {
|
||||
const { Contact } = this.tenancy.models(tenantId);
|
||||
const contact = await this.getContactByIdOrThrowError(tenantId, contactId);
|
||||
|
||||
if(contact.active) {
|
||||
throw new ServiceError(ERRORS.CONTACT_ALREADY_ACTIVE);
|
||||
}
|
||||
await Contact.query().findById(contactId).update({ active: true });
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,3 +20,10 @@ export const DEFAULT_VIEWS = [
|
||||
columns: DEFAULT_VIEW_COLUMNS,
|
||||
},
|
||||
];
|
||||
|
||||
|
||||
export const ERRORS = {
|
||||
OPENING_BALANCE_DATE_REQUIRED: 'OPENING_BALANCE_DATE_REQUIRED',
|
||||
CONTACT_ALREADY_INACTIVE: 'CONTACT_ALREADY_INACTIVE',
|
||||
CONTACT_ALREADY_ACTIVE: 'CONTACT_ALREADY_ACTIVE'
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user