Skip to content

Commit

Permalink
hotfix(server): Unhandled thrown errors of services (#329)
Browse files Browse the repository at this point in the history
  • Loading branch information
abouolia authored Jan 22, 2024
1 parent e42adca commit 7eb8447
Show file tree
Hide file tree
Showing 8 changed files with 171 additions and 224 deletions.
15 changes: 6 additions & 9 deletions packages/server/src/api/controllers/Purchases/Bills.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ export default class BillsController extends BaseController {
try {
const bill = await this.billsApplication.getBill(tenantId, billId);

return res.status(200).send(this.transfromToResponse({ bill }));
return res.status(200).send({ bill });
} catch (error) {
next(error);
}
Expand Down Expand Up @@ -348,14 +348,11 @@ export default class BillsController extends BaseController {
};

try {
const { bills, pagination, filterMeta } =
await this.billsApplication.getBills(tenantId, filter);

return res.status(200).send({
bills: this.transfromToResponse(bills),
pagination: this.transfromToResponse(pagination),
filter_meta: this.transfromToResponse(filterMeta),
});
const billsWithPagination = await this.billsApplication.getBills(
tenantId,
filter
);
return res.status(200).send(billsWithPagination);
} catch (error) {
next(error);
}
Expand Down
35 changes: 10 additions & 25 deletions packages/server/src/api/controllers/Purchases/BillsPayments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,15 +158,11 @@ export default class BillsPayments extends BaseController {
const { tenantId } = req;
const { vendorId } = this.matchedQueryData(req);

try {
const entries = await this.billPaymentsPages.getNewPageEntries(
tenantId,
vendorId
);
return res.status(200).send({
entries: this.transfromToResponse(entries),
});
} catch (error) {}
const entries = await this.billPaymentsPages.getNewPageEntries(
tenantId,
vendorId
);
return res.status(200).send({ entries });
}

/**
Expand All @@ -183,16 +179,12 @@ export default class BillsPayments extends BaseController {
const { id: paymentReceiveId } = req.params;

try {
const { billPayment, entries } =
const billPaymentsWithEditEntries =
await this.billPaymentsPages.getBillPaymentEditPage(
tenantId,
paymentReceiveId
);

return res.status(200).send({
bill_payment: this.transfromToResponse(billPayment),
entries: this.transfromToResponse(entries),
});
return res.status(200).send(billPaymentsWithEditEntries);
} catch (error) {
next(error);
}
Expand Down Expand Up @@ -304,9 +296,7 @@ export default class BillsPayments extends BaseController {
tenantId,
billPaymentId
);
return res.status(200).send({
bill_payment: this.transfromToResponse(billPayment),
});
return res.status(200).send({ billPayment });
} catch (error) {
next(error);
}
Expand Down Expand Up @@ -359,17 +349,12 @@ export default class BillsPayments extends BaseController {
};

try {
const { billPayments, pagination, filterMeta } =
const billPaymentsWithPagination =
await this.billPaymentsApplication.getBillPayments(
tenantId,
billPaymentsFilter
);

return res.status(200).send({
bill_payments: this.transfromToResponse(billPayments),
pagination: this.transfromToResponse(pagination),
filter_meta: this.transfromToResponse(filterMeta),
});
return res.status(200).send(billPaymentsWithPagination);
} catch (error) {
next(error);
}
Expand Down
51 changes: 21 additions & 30 deletions packages/server/src/api/controllers/Sales/CreditNotes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import GetCreditNoteAssociatedInvoicesToApply from '@/services/CreditNotes/GetCr
import GetCreditNoteAssociatedAppliedInvoices from '@/services/CreditNotes/GetCreditNoteAssociatedAppliedInvoices';
import GetRefundCreditTransaction from '@/services/CreditNotes/GetRefundCreditNoteTransaction';
import GetCreditNotePdf from '../../../services/CreditNotes/GetCreditNotePdf';
import { ACCEPT_TYPE } from '@/interfaces/Http';
/**
* Credit notes controller.
* @service
Expand Down Expand Up @@ -293,7 +294,7 @@ export default class PaymentReceivesController extends BaseController {
return [
check('from_account_id').exists().isNumeric().toInt(),
check('description').optional(),

check('amount').exists().isNumeric().toFloat(),
check('exchange_rate').optional().isFloat({ gt: 0 }).toFloat(),

Expand Down Expand Up @@ -438,7 +439,7 @@ export default class PaymentReceivesController extends BaseController {
};

/**
* Retrieve the payment receive details.
* Retrieve the credit note details.
* @param {Request} req
* @param {Response} res
* @param {NextFunction} next
Expand All @@ -451,38 +452,28 @@ export default class PaymentReceivesController extends BaseController {
const { tenantId } = req;
const { id: creditNoteId } = req.params;

try {
const creditNote = await this.getCreditNoteService.getCreditNote(
const accept = this.accepts(req);

const acceptType = accept.types([
ACCEPT_TYPE.APPLICATION_JSON,
ACCEPT_TYPE.APPLICATION_PDF,
]);
if (ACCEPT_TYPE.APPLICATION_PDF === acceptType) {
const pdfContent = await this.creditNotePdf.getCreditNotePdf(
tenantId,
creditNoteId
);
const ACCEPT_TYPE = {
APPLICATION_PDF: 'application/pdf',
APPLICATION_JSON: 'application/json',
};
// Response formatter.
res.format({
// Json content type.
[ACCEPT_TYPE.APPLICATION_JSON]: () => {
return res
.status(200)
.send({ credit_note: this.transfromToResponse(creditNote) });
},
// Pdf content type.
[ACCEPT_TYPE.APPLICATION_PDF]: async () => {
const pdfContent = await this.creditNotePdf.getCreditNotePdf(
tenantId,
creditNote
);
res.set({
'Content-Type': 'application/pdf',
'Content-Length': pdfContent.length,
});
res.send(pdfContent);
},
res.set({
'Content-Type': 'application/pdf',
'Content-Length': pdfContent.length,
});
} catch (error) {
next(error);
res.send(pdfContent);
} else {
const creditNote = await this.getCreditNoteService.getCreditNote(
tenantId,
creditNoteId
);
return res.status(200).send({ creditNote });
}
};

Expand Down
77 changes: 35 additions & 42 deletions packages/server/src/api/controllers/Sales/PaymentReceives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import DynamicListingService from '@/services/DynamicListing/DynamicListService'
import { PaymentReceivesApplication } from '@/services/Sales/PaymentReceives/PaymentReceivesApplication';
import CheckPolicies from '@/api/middleware/CheckPolicies';
import { ServiceError } from '@/exceptions';
import { ACCEPT_TYPE } from '@/interfaces/Http';

@Service()
export default class PaymentReceivesController extends BaseController {
Expand Down Expand Up @@ -348,17 +349,12 @@ export default class PaymentReceivesController extends BaseController {
};

try {
const { paymentReceives, pagination, filterMeta } =
const paymentsReceivedWithPagination =
await this.paymentReceiveApplication.getPaymentReceives(
tenantId,
filter
);

return res.status(200).send({
payment_receives: this.transfromToResponse(paymentReceives),
pagination: this.transfromToResponse(pagination),
filter_meta: this.transfromToResponse(filterMeta),
});
return res.status(200).send(paymentsReceivedWithPagination);
} catch (error) {
next(error);
}
Expand Down Expand Up @@ -435,37 +431,34 @@ export default class PaymentReceivesController extends BaseController {
const { tenantId } = req;
const { id: paymentReceiveId } = req.params;

try {
const ACCEPT_TYPE = {
APPLICATION_PDF: 'application/pdf',
APPLICATION_JSON: 'application/json',
};
res.format({
[ACCEPT_TYPE.APPLICATION_JSON]: async () => {
const paymentReceive =
await this.paymentReceiveApplication.getPaymentReceive(
tenantId,
paymentReceiveId
);
return res.status(200).send({
payment_receive: paymentReceive,
});
},
[ACCEPT_TYPE.APPLICATION_PDF]: async () => {
const pdfContent =
await this.paymentReceiveApplication.getPaymentReceivePdf(
tenantId,
paymentReceiveId
);
res.set({
'Content-Type': 'application/pdf',
'Content-Length': pdfContent.length,
});
res.send(pdfContent);
},
const accept = this.accepts(req);

const acceptType = accept.types([
ACCEPT_TYPE.APPLICATION_JSON,
ACCEPT_TYPE.APPLICATION_PDF,
]);
// Response in pdf format.
if (ACCEPT_TYPE.APPLICATION_PDF === acceptType) {
const pdfContent =
await this.paymentReceiveApplication.getPaymentReceivePdf(
tenantId,
paymentReceiveId
);
res.set({
'Content-Type': 'application/pdf',
'Content-Length': pdfContent.length,
});
res.send(pdfContent);
// Response in json format.
} else {
const paymentReceive =
await this.paymentReceiveApplication.getPaymentReceive(
tenantId,
paymentReceiveId
);
return res.status(200).send({
payment_receive: paymentReceive,
});
} catch (error) {
next(error);
}
}

Expand Down Expand Up @@ -499,7 +492,7 @@ export default class PaymentReceivesController extends BaseController {
};

/**
*
* Retrieves the sms details of the given payment receive.
* @param {Request} req
* @param {Response} res
* @param {NextFunction} next
Expand Down Expand Up @@ -588,10 +581,10 @@ export default class PaymentReceivesController extends BaseController {

/**
* Handles service errors.
* @param error
* @param req
* @param res
* @param next
* @param {Error} error
* @param {Request} req
* @param {Response} res
* @param {NextFunction} next
*/
private handleServiceErrors(
error: Error,
Expand Down
66 changes: 25 additions & 41 deletions packages/server/src/api/controllers/Sales/SalesEstimates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,6 @@ export default class SalesEstimatesController extends BaseController {
tenantId,
estimateId
);

return res.status(200).send({
id: estimateId,
message: 'The sale estimate has been approved successfully.',
Expand Down Expand Up @@ -363,7 +362,6 @@ export default class SalesEstimatesController extends BaseController {
tenantId,
estimateId
);

return res.status(200).send({
id: estimateId,
message: 'The sale estimate has been rejected successfully.',
Expand All @@ -383,33 +381,30 @@ export default class SalesEstimatesController extends BaseController {
const { id: estimateId } = req.params;
const { tenantId } = req;

try {
// Response formatter.
res.format({
// JSON content type.
[ACCEPT_TYPE.APPLICATION_JSON]: async () => {
const estimate = await this.saleEstimatesApplication.getSaleEstimate(
tenantId,
estimateId
);
return res.status(200).send({ estimate });
},
// PDF content type.
[ACCEPT_TYPE.APPLICATION_PDF]: async () => {
const pdfContent =
await this.saleEstimatesApplication.getSaleEstimatePdf(
tenantId,
estimateId
);
res.set({
'Content-Type': 'application/pdf',
'Content-Length': pdfContent.length,
});
res.send(pdfContent);
},
const accept = this.accepts(req);

const acceptType = accept.types([
ACCEPT_TYPE.APPLICATION_JSON,
ACCEPT_TYPE.APPLICATION_PDF,
]);
// Retrieves estimate in pdf format.
if (ACCEPT_TYPE.APPLICATION_PDF == acceptType) {
const pdfContent = await this.saleEstimatesApplication.getSaleEstimatePdf(
tenantId,
estimateId
);
res.set({
'Content-Type': 'application/pdf',
'Content-Length': pdfContent.length,
});
} catch (error) {
next(error);
res.send(pdfContent);
// Retrieves estimates in json format.
} else {
const estimate = await this.saleEstimatesApplication.getSaleEstimate(
tenantId,
estimateId
);
return res.status(200).send({ estimate });
}
}

Expand All @@ -427,22 +422,11 @@ export default class SalesEstimatesController extends BaseController {
pageSize: 12,
...this.matchedQueryData(req),
};

try {
const { salesEstimates, pagination, filterMeta } =
const salesEstimatesWithPagination =
await this.saleEstimatesApplication.getSaleEstimates(tenantId, filter);

res.format({
[ACCEPT_TYPE.APPLICATION_JSON]: () => {
return res.status(200).send(
this.transfromToResponse({
salesEstimates,
pagination,
filterMeta,
})
);
},
});
return res.status(200).send(salesEstimatesWithPagination);
} catch (error) {
next(error);
}
Expand Down
Loading

1 comment on commit 7eb8447

@vercel
Copy link

@vercel vercel bot commented on 7eb8447 Jan 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.