Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: split the services to multiple service classes #202

Merged
merged 12 commits into from
Aug 10, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat: refactor sales and purchases services to smaller services
  • Loading branch information
abouolia committed Aug 3, 2023
commit adfbd60df50bc8ee3dd8831a295c2b52412ea98e
51 changes: 29 additions & 22 deletions packages/server/src/api/controllers/Purchases/Bills.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
import { Router, Request, Response, NextFunction } from 'express';
import { check, param, query } from 'express-validator';
import { Service, Inject } from 'typedi';
import { AbilitySubject, BillAction, IBillDTO, IBillEditDTO } from '@/interfaces';
import {
AbilitySubject,
BillAction,
IBillDTO,
IBillEditDTO,
} from '@/interfaces';
import asyncMiddleware from '@/api/middleware/asyncMiddleware';
import BillsService from '@/services/Purchases/Bills';
import BaseController from '@/api/controllers/BaseController';
import DynamicListingService from '@/services/DynamicListing/DynamicListService';
import { ServiceError } from '@/exceptions';
import CheckPolicies from '@/api/middleware/CheckPolicies';
import BillPaymentsService from '@/services/Purchases/BillPaymentsService';
import { BillsApplication } from '@/services/Purchases/Bills/BillsApplication';

@Service()
export default class BillsController extends BaseController {
@Inject()
private billsService: BillsService;
private billsApplication: BillsApplication;

@Inject()
private dynamicListService: DynamicListingService;
Expand Down Expand Up @@ -97,7 +103,7 @@ export default class BillsController extends BaseController {
/**
* Common validation schema.
*/
get billValidationSchema() {
private get billValidationSchema() {
return [
check('bill_number').exists().trim().escape(),
check('reference_no').optional().trim().escape(),
Expand Down Expand Up @@ -142,7 +148,7 @@ export default class BillsController extends BaseController {
/**
* Common validation schema.
*/
get billEditValidationSchema() {
private get billEditValidationSchema() {
return [
check('bill_number').optional().trim().escape(),
check('reference_no').optional().trim().escape(),
Expand Down Expand Up @@ -184,14 +190,14 @@ export default class BillsController extends BaseController {
/**
* Bill validation schema.
*/
get specificBillValidationSchema() {
private get specificBillValidationSchema() {
return [param('id').exists().isNumeric().toInt()];
}

/**
* Bills list validation schema.
*/
get billsListingValidationSchema() {
private get billsListingValidationSchema() {
return [
query('view_slug').optional().isString().trim(),
query('stringified_filter_roles').optional().isJSON(),
Expand All @@ -203,7 +209,7 @@ export default class BillsController extends BaseController {
];
}

get dueBillsListingValidationSchema() {
private get dueBillsListingValidationSchema() {
return [
query('vendor_id').optional().trim().escape(),
query('payment_made_id').optional().trim().escape(),
Expand All @@ -216,12 +222,12 @@ export default class BillsController extends BaseController {
* @param {Response} res
* @param {Function} next
*/
async newBill(req: Request, res: Response, next: NextFunction) {
private async newBill(req: Request, res: Response, next: NextFunction) {
const { tenantId, user } = req;
const billDTO: IBillDTO = this.matchedBodyData(req);

try {
const storedBill = await this.billsService.createBill(
const storedBill = await this.billsApplication.createBill(
tenantId,
billDTO,
user
Expand All @@ -241,13 +247,13 @@ export default class BillsController extends BaseController {
* @param {Request} req
* @param {Response} res
*/
async editBill(req: Request, res: Response, next: NextFunction) {
private async editBill(req: Request, res: Response, next: NextFunction) {
const { id: billId } = req.params;
const { tenantId, user } = req;
const billDTO: IBillEditDTO = this.matchedBodyData(req);

try {
await this.billsService.editBill(tenantId, billId, billDTO, user);
await this.billsApplication.editBill(tenantId, billId, billDTO, user);

return res.status(200).send({
id: billId,
Expand All @@ -263,12 +269,12 @@ export default class BillsController extends BaseController {
* @param {Request} req -
* @param {Response} res -
*/
async openBill(req: Request, res: Response, next: NextFunction) {
private async openBill(req: Request, res: Response, next: NextFunction) {
const { id: billId } = req.params;
const { tenantId } = req;

try {
await this.billsService.openBill(tenantId, billId);
await this.billsApplication.openBill(tenantId, billId);

return res.status(200).send({
id: billId,
Expand All @@ -285,12 +291,12 @@ export default class BillsController extends BaseController {
* @param {Response} res
* @return {Response}
*/
async getBill(req: Request, res: Response, next: NextFunction) {
private async getBill(req: Request, res: Response, next: NextFunction) {
const { tenantId } = req;
const { id: billId } = req.params;

try {
const bill = await this.billsService.getBill(tenantId, billId);
const bill = await this.billsApplication.getBill(tenantId, billId);

return res.status(200).send(this.transfromToResponse({ bill }));
} catch (error) {
Expand All @@ -304,12 +310,12 @@ export default class BillsController extends BaseController {
* @param {Response} res -
* @return {Response}
*/
async deleteBill(req: Request, res: Response, next: NextFunction) {
private async deleteBill(req: Request, res: Response, next: NextFunction) {
const billId = req.params.id;
const { tenantId } = req;

try {
await this.billsService.deleteBill(tenantId, billId);
await this.billsApplication.deleteBill(tenantId, billId);

return res.status(200).send({
id: billId,
Expand All @@ -326,7 +332,7 @@ export default class BillsController extends BaseController {
* @param {Response} res -
* @return {Response}
*/
public async billsList(req: Request, res: Response, next: NextFunction) {
private async billsList(req: Request, res: Response, next: NextFunction) {
const { tenantId } = req;
const filter = {
page: 1,
Expand All @@ -338,7 +344,7 @@ export default class BillsController extends BaseController {

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

return res.status(200).send({
bills: this.transfromToResponse(bills),
Expand All @@ -356,12 +362,13 @@ export default class BillsController extends BaseController {
* @param {Response} res
* @param {NextFunction} next
*/
public async getDueBills(req: Request, res: Response, next: NextFunction) {
private async getDueBills(req: Request, res: Response, next: NextFunction) {
const { tenantId } = req;
const { vendorId } = this.matchedQueryData(req);

try {
const bills = await this.billsService.getDueBills(tenantId, vendorId);
const bills = await this.billsApplication.getDueBills(tenantId, vendorId);

return res.status(200).send({ bills });
} catch (error) {
next(error);
Expand All @@ -374,7 +381,7 @@ export default class BillsController extends BaseController {
* @param {Response} res
* @param {NextFunction} next
*/
public getBillPaymentsTransactions = async (
private getBillPaymentsTransactions = async (
req: Request,
res: Response,
next: NextFunction
Expand Down
70 changes: 48 additions & 22 deletions packages/server/src/api/controllers/Purchases/BillsPayments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { check, param, query, ValidationChain } from 'express-validator';
import asyncMiddleware from '@/api/middleware/asyncMiddleware';
import { ServiceError } from '@/exceptions';
import BaseController from '@/api/controllers/BaseController';
import BillPaymentsService from '@/services/Purchases/BillPayments/BillPaymentsApplication';
import { BillPaymentsApplication } from '@/services/Purchases/BillPayments/BillPaymentsApplication';
import BillPaymentsPages from '@/services/Purchases/BillPayments/BillPaymentsPages';
import DynamicListingService from '@/services/DynamicListing/DynamicListService';
import CheckPolicies from '@/api/middleware/CheckPolicies';
Expand All @@ -17,13 +17,13 @@ import { AbilitySubject, IPaymentMadeAction } from '@/interfaces';
@Service()
export default class BillsPayments extends BaseController {
@Inject()
billPaymentService: BillPaymentsService;
private billPaymentsApplication: BillPaymentsApplication;

@Inject()
dynamicListService: DynamicListingService;
private dynamicListService: DynamicListingService;

@Inject()
billPaymentsPages: BillPaymentsPages;
private billPaymentsPages: BillPaymentsPages;

/**
* Router constructor.
Expand Down Expand Up @@ -106,7 +106,7 @@ export default class BillsPayments extends BaseController {
* Bill payments schema validation.
* @return {ValidationChain[]}
*/
get billPaymentSchemaValidation(): ValidationChain[] {
private get billPaymentSchemaValidation(): ValidationChain[] {
return [
check('vendor_id').exists().isNumeric().toInt(),
check('exchange_rate').optional().isFloat({ gt: 0 }).toFloat(),
Expand All @@ -129,15 +129,15 @@ export default class BillsPayments extends BaseController {
* Specific bill payment schema validation.
* @returns {ValidationChain[]}
*/
get specificBillPaymentValidateSchema(): ValidationChain[] {
private get specificBillPaymentValidateSchema(): ValidationChain[] {
return [param('id').exists().isNumeric().toInt()];
}

/**
* Bills payment list validation schema.
* @returns {ValidationChain[]}
*/
get listingValidationSchema(): ValidationChain[] {
private get listingValidationSchema(): ValidationChain[] {
return [
query('custom_view_id').optional().isNumeric().toInt(),
query('stringified_filter_roles').optional().isJSON(),
Expand All @@ -154,7 +154,7 @@ export default class BillsPayments extends BaseController {
* @param {Request} req -
* @param {Response} res -
*/
async getBillPaymentNewPageEntries(req: Request, res: Response) {
private async getBillPaymentNewPageEntries(req: Request, res: Response) {
const { tenantId } = req;
const { vendorId } = this.matchedQueryData(req);

Expand All @@ -174,7 +174,7 @@ export default class BillsPayments extends BaseController {
* @param {Request} req
* @param {Response} res
*/
async getBillPaymentEditPage(
private async getBillPaymentEditPage(
req: Request,
res: Response,
next: NextFunction
Expand Down Expand Up @@ -205,16 +205,19 @@ export default class BillsPayments extends BaseController {
* @param {Response} res
* @param {Response} res
*/
async createBillPayment(req: Request, res: Response, next: NextFunction) {
private async createBillPayment(
req: Request,
res: Response,
next: NextFunction
) {
const { tenantId } = req;
const billPaymentDTO = this.matchedBodyData(req);

try {
const billPayment = await this.billPaymentService.createBillPayment(
const billPayment = await this.billPaymentsApplication.createBillPayment(
tenantId,
billPaymentDTO
);

return res.status(200).send({
id: billPayment.id,
message: 'Payment made has been created successfully.',
Expand All @@ -229,13 +232,17 @@ export default class BillsPayments extends BaseController {
* @param {Request} req
* @param {Response} res
*/
async editBillPayment(req: Request, res: Response, next: NextFunction) {
private async editBillPayment(
req: Request,
res: Response,
next: NextFunction
) {
const { tenantId } = req;
const billPaymentDTO = this.matchedBodyData(req);
const { id: billPaymentId } = req.params;

try {
const paymentMade = await this.billPaymentService.editBillPayment(
const paymentMade = await this.billPaymentsApplication.editBillPayment(
tenantId,
billPaymentId,
billPaymentDTO
Expand All @@ -256,12 +263,19 @@ export default class BillsPayments extends BaseController {
* @param {Response} res -
* @return {Response} res -
*/
async deleteBillPayment(req: Request, res: Response, next: NextFunction) {
private async deleteBillPayment(
req: Request,
res: Response,
next: NextFunction
) {
const { tenantId } = req;
const { id: billPaymentId } = req.params;

try {
await this.billPaymentService.deleteBillPayment(tenantId, billPaymentId);
await this.billPaymentsApplication.deleteBillPayment(
tenantId,
billPaymentId
);

return res.status(200).send({
id: billPaymentId,
Expand All @@ -277,12 +291,16 @@ export default class BillsPayments extends BaseController {
* @param {Request} req
* @param {Response} res
*/
async getBillPayment(req: Request, res: Response, next: NextFunction) {
private async getBillPayment(
req: Request,
res: Response,
next: NextFunction
) {
const { tenantId } = req;
const { id: billPaymentId } = req.params;

try {
const billPayment = await this.billPaymentService.getBillPayment(
const billPayment = await this.billPaymentsApplication.getBillPayment(
tenantId,
billPaymentId
);
Expand All @@ -301,12 +319,16 @@ export default class BillsPayments extends BaseController {
* @param {Response} res
* @param {NextFunction} next
*/
async getPaymentBills(req: Request, res: Response, next: NextFunction) {
private async getPaymentBills(
req: Request,
res: Response,
next: NextFunction
) {
const { tenantId } = req;
const { id: billPaymentId } = req.params;

try {
const bills = await this.billPaymentService.getPaymentBills(
const bills = await this.billPaymentsApplication.getPaymentBills(
tenantId,
billPaymentId
);
Expand All @@ -322,7 +344,11 @@ export default class BillsPayments extends BaseController {
* @param {Response} res -
* @return {Response}
*/
async getBillsPayments(req: Request, res: Response, next: NextFunction) {
private async getBillsPayments(
req: Request,
res: Response,
next: NextFunction
) {
const { tenantId } = req;
const billPaymentsFilter = {
page: 1,
Expand All @@ -335,7 +361,7 @@ export default class BillsPayments extends BaseController {

try {
const { billPayments, pagination, filterMeta } =
await this.billPaymentService.listBillPayments(
await this.billPaymentsApplication.getBillPayments(
tenantId,
billPaymentsFilter
);
Expand Down
Loading