feat: ability to change customer/vendor opening balance.

This commit is contained in:
a.bouhuolia
2020-12-16 11:55:52 +02:00
parent 704dfcacdf
commit 0e5e13597f
7 changed files with 191 additions and 3 deletions

View File

@@ -32,6 +32,17 @@ export default class CustomersController extends ContactsController {
asyncMiddleware(this.newCustomer.bind(this)),
this.handlerServiceErrors
);
router.post(
'/:id/opening_balance',
[
...this.specificContactSchema,
check('opening_balance').exists().isNumeric().toFloat(),
check('opening_balance_at').optional().isISO8601(),
],
this.validationResult,
asyncMiddleware(this.editOpeningBalanceCustomer.bind(this)),
this.handlerServiceErrors,
);
router.post('/:id', [
...this.contactDTOSchema,
...this.contactEditDTOSchema,
@@ -160,6 +171,36 @@ export default class CustomersController extends ContactsController {
}
}
/**
* Changes the opening balance of the given customer.
* @param {Request} req -
* @param {Response} res -
* @param {NextFunction} next -
*/
async editOpeningBalanceCustomer(req: Request, res: Response, next: NextFunction) {
const { tenantId } = req;
const { id: customerId } = req.params;
const {
openingBalance,
openingBalanceAt,
} = this.matchedBodyData(req);
try {
await this.customersService.changeOpeningBalance(
tenantId,
customerId,
openingBalance,
openingBalanceAt,
);
return res.status(200).send({
id: customerId,
message: 'The opening balance of the given customer has been changed successfully.',
});
} catch (error) {
next(error);
}
}
/**
* Deletes the given customer from the storage.
* @param {Request} req
@@ -283,6 +324,11 @@ export default class CustomersController extends ContactsController {
errors: [{ type: 'CUSTOMER.HAS.SALES_INVOICES', code: 400 }],
});
}
if (error.errorType === 'OPENING_BALANCE_DATE_REQUIRED') {
return res.boom.badRequest(null, {
errors: [{ type: 'OPENING_BALANCE_DATE_REQUIRED', code: 500 }],
});
}
}
next(error);
}

View File

@@ -28,6 +28,17 @@ export default class VendorsController extends ContactsController {
asyncMiddleware(this.newVendor.bind(this)),
this.handlerServiceErrors,
);
router.post(
'/:id/opening_balance',
[
...this.specificContactSchema,
check('opening_balance').exists().isNumeric().toFloat(),
check('opening_balance_at').optional().isISO8601(),
],
this.validationResult,
asyncMiddleware(this.editOpeningBalanceVendor.bind(this)),
this.handlerServiceErrors,
);
router.post('/:id', [
...this.contactDTOSchema,
...this.contactEditDTOSchema,
@@ -144,6 +155,36 @@ export default class VendorsController extends ContactsController {
}
}
/**
* Changes the opening balance of the given vendor.
* @param {Request} req -
* @param {Response} res -
* @param {NextFunction} next -
*/
async editOpeningBalanceVendor(req: Request, res: Response, next: NextFunction) {
const { tenantId } = req;
const { id: vendorId } = req.params;
const {
openingBalance,
openingBalanceAt,
} = this.matchedBodyData(req);
try {
await this.vendorsService.changeOpeningBalance(
tenantId,
vendorId,
openingBalance,
openingBalanceAt,
);
return res.status(200).send({
id: vendorId,
message: 'The opening balance of the given vendor has been changed successfully.',
});
} catch (error) {
next(error);
}
}
/**
* Deletes the given vendor from the storage.
* @param {Request} req
@@ -261,6 +302,11 @@ export default class VendorsController extends ContactsController {
errors: [{ type: 'VENDOR.HAS.BILLS', code: 400 }],
});
}
if (error.errorType === 'OPENING_BALANCE_DATE_REQUIRED') {
return res.boom.badRequest(null, {
errors: [{ type: 'OPENING_BALANCE_DATE_REQUIRED', code: 500 }],
});
}
}
next(error);
}