feat: reponse readable text and linting some pages.

This commit is contained in:
a.bouhuolia
2021-01-02 10:44:38 +02:00
parent d30e76c5cf
commit b6392e4208
9 changed files with 164 additions and 85 deletions

View File

@@ -1,4 +1,3 @@
import { Router, Request, Response, NextFunction } from 'express';
import { Service, Inject } from 'typedi';
import { check, param, query, ValidationChain } from 'express-validator';
@@ -31,46 +30,51 @@ export default class BillsPayments extends BaseController {
router() {
const router = Router();
router.post('/', [
...this.billPaymentSchemaValidation,
],
router.post(
'/',
[...this.billPaymentSchemaValidation],
this.validationResult,
asyncMiddleware(this.createBillPayment.bind(this)),
this.handleServiceError,
this.handleServiceError
);
router.post('/:id', [
...this.billPaymentSchemaValidation,
...this.specificBillPaymentValidateSchema,
],
this.validationResult,
asyncMiddleware(this.editBillPayment.bind(this)),
this.handleServiceError,
)
router.delete('/:id', [
router.post(
'/:id',
[
...this.billPaymentSchemaValidation,
...this.specificBillPaymentValidateSchema,
],
this.validationResult,
asyncMiddleware(this.deleteBillPayment.bind(this)),
this.handleServiceError,
asyncMiddleware(this.editBillPayment.bind(this)),
this.handleServiceError
);
router.get('/:id/bills',
router.delete(
'/:id',
[...this.specificBillPaymentValidateSchema],
this.validationResult,
asyncMiddleware(this.deleteBillPayment.bind(this)),
this.handleServiceError
);
router.get(
'/:id/bills',
this.specificBillPaymentValidateSchema,
this.validationResult,
asyncMiddleware(this.getPaymentBills.bind(this)),
this.handleServiceError,
this.handleServiceError
);
router.get('/:id',
router.get(
'/:id',
this.specificBillPaymentValidateSchema,
this.validationResult,
asyncMiddleware(this.getBillPayment.bind(this)),
this.handleServiceError,
this.handleServiceError
);
router.get('/',
router.get(
'/',
this.listingValidationSchema,
this.validationResult,
asyncMiddleware(this.getBillsPayments.bind(this)),
this.handleServiceError,
this.dynamicListService.handlerErrorsToResponse,
this.dynamicListService.handlerErrorsToResponse
);
return router;
}
@@ -97,9 +101,7 @@ export default class BillsPayments extends BaseController {
* Specific bill payment schema validation.
*/
get specificBillPaymentValidateSchema(): ValidationChain[] {
return [
param('id').exists().isNumeric().toInt(),
];
return [param('id').exists().isNumeric().toInt()];
}
/**
@@ -119,16 +121,19 @@ export default class BillsPayments extends BaseController {
/**
* Creates a bill payment.
* @async
* @param {Request} req
* @param {Response} res
* @param {Response} res
* @param {Request} req
* @param {Response} res
* @param {Response} res
*/
async createBillPayment(req: Request, res: Response, next: NextFunction) {
const { tenantId } = req;
const billPaymentDTO = this.matchedBodyData(req);
try {
const billPayment = await this.billPaymentService.createBillPayment(tenantId, billPaymentDTO);
const billPayment = await this.billPaymentService.createBillPayment(
tenantId,
billPaymentDTO
);
return res.status(200).send({
id: billPayment.id,
@@ -142,8 +147,8 @@ export default class BillsPayments extends BaseController {
/**
* Edits the given bill payment details.
* @param {Request} req
* @param {Response} res
* @param {Request} req
* @param {Response} res
*/
async editBillPayment(req: Request, res: Response, next: NextFunction) {
const { tenantId } = req;
@@ -190,8 +195,8 @@ export default class BillsPayments extends BaseController {
/**
* Retrieve the bill payment.
* @param {Request} req
* @param {Response} res
* @param {Request} req
* @param {Response} res
*/
async getBillPayment(req: Request, res: Response, next: NextFunction) {
const { tenantId } = req;
@@ -206,8 +211,8 @@ export default class BillsPayments extends BaseController {
return res.status(200).send({
bill_payment: this.transfromToResponse({ ...billPayment }),
payable_bills: this.transfromToResponse([ ...payableBills ]),
payment_bills: this.transfromToResponse([ ...paymentMadeBills ]),
payable_bills: this.transfromToResponse([...payableBills]),
payment_bills: this.transfromToResponse([...paymentMadeBills]),
});
} catch (error) {
next(error);
@@ -216,16 +221,19 @@ export default class BillsPayments extends BaseController {
/**
* Retrieve associated bills for the given payment made.
* @param {Request} req
* @param {Response} res
* @param {NextFunction} next
* @param {Request} req
* @param {Response} res
* @param {NextFunction} next
*/
async getPaymentBills(req: Request, res: Response, next: NextFunction) {
const { tenantId } = req;
const { id: billPaymentId } = req.params;
try {
const bills = await this.billPaymentService.getPaymentBills(tenantId, billPaymentId);
const bills = await this.billPaymentService.getPaymentBills(
tenantId,
billPaymentId
);
return res.status(200).send({ bills });
} catch (error) {
next(error);
@@ -233,7 +241,7 @@ export default class BillsPayments extends BaseController {
}
/**
* Retrieve bills payments listing with pagination metadata.
* Retrieve bills payments listing with pagination metadata.
* @param {Request} req -
* @param {Response} res -
* @return {Response}
@@ -250,12 +258,19 @@ export default class BillsPayments extends BaseController {
};
try {
const { billPayments, pagination, filterMeta } = await this.billPaymentService.listBillPayments(tenantId, billPaymentsFilter);
const {
billPayments,
pagination,
filterMeta,
} = await this.billPaymentService.listBillPayments(
tenantId,
billPaymentsFilter
);
return res.status(200).send({
bill_payments: billPayments,
pagination: this.transfromToResponse(pagination),
filter_meta: this.transfromToResponse(filterMeta)
filter_meta: this.transfromToResponse(filterMeta),
});
} catch (error) {
next(error);
@@ -264,46 +279,54 @@ export default class BillsPayments extends BaseController {
/**
* Handle service errors.
* @param {Error} error
* @param {Request} req
* @param {Response} res
* @param {NextFunction} next
* @param {Error} error
* @param {Request} req
* @param {Response} res
* @param {NextFunction} next
*/
handleServiceError(error: Error, req: Request, res: Response, next: NextFunction) {
handleServiceError(
error: Error,
req: Request,
res: Response,
next: NextFunction
) {
if (error instanceof ServiceError) {
if (error.errorType === 'PAYMENT_MADE_NOT_FOUND') {
return res.status(404).send({
message: 'Payment made not found.',
errors: [{ type: 'BILL_NOT_FOUND', code: 100 }],
});
}
if (error.errorType === 'VENDOR_NOT_FOUND') {
return res.status(400).send({
errors: [{ type: 'BILL.PAYMENT.VENDOR.NOT.FOUND', code: 500 }],
errors: [{ type: 'BILL.PAYMENT.VENDOR.NOT.FOUND', code: 200 }],
});
}
if (error.errorType === 'PAYMENT_ACCOUNT_NOT_CURRENT_ASSET_TYPE') {
return res.status(400).send({
errors: [{ type: 'PAYMENT_ACCOUNT.NOT.CURRENT_ASSET.TYPE', code: 100 }],
errors: [
{ type: 'PAYMENT_ACCOUNT.NOT.CURRENT_ASSET.TYPE', code: 300 },
],
});
}
if (error.errorType === 'BILL_PAYMENT_NUMBER_NOT_UNQIUE') {
return res.status(400).send({
errors: [{ type: 'PAYMENT.NUMBER.NOT.UNIQUE', code: 300 }],
errors: [{ type: 'PAYMENT.NUMBER.NOT.UNIQUE', code: 400 }],
});
}
if (error.errorType === 'PAYMENT_ACCOUNT_NOT_FOUND') {
return res.status(400).send({
errors: [{ type: 'PAYMENT.ACCOUNT.NOT.FOUND', code: 200 }],
errors: [{ type: 'PAYMENT.ACCOUNT.NOT.FOUND', code: 500 }],
});
}
if (error.errorType === 'PAYMENT_ACCOUNT_NOT_FOUND') {
return res.status(400).send({
errors: [{ type: 'PAYMENT.ACCOUNT.NOT.FOUND', code: 200 }],
errors: [{ type: 'PAYMENT.ACCOUNT.NOT.FOUND', code: 600 }],
});
}
if (error.errorType === '') {
return res.status(400).send({
errors: [{ type: 'BILLS.IDS.NOT.EXISTS', code: 600 }],
errors: [{ type: 'BILLS.IDS.NOT.EXISTS', code: 700 }],
});
}
if (error.errorType === 'BILL_PAYMENT_ENTRIES_NOT_FOUND') {
@@ -313,15 +336,15 @@ export default class BillsPayments extends BaseController {
}
if (error.errorType === 'INVALID_BILL_PAYMENT_AMOUNT') {
return res.status(400).send({
errors: [{ type: 'INVALID_BILL_PAYMENT_AMOUNT', code: 100 }],
errors: [{ type: 'INVALID_BILL_PAYMENT_AMOUNT', code: 900 }],
});
}
if (error.errorType === 'BILL_ENTRIES_IDS_NOT_FOUND') {
return res.status(400).send({
errors: [{ type: 'BILLS_NOT_FOUND', code: 100 }],
})
errors: [{ type: 'BILLS_NOT_FOUND', code: 1000 }],
});
}
}
next(error);
}
}
}