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 all commits
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
7 changes: 7 additions & 0 deletions e2e/items.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { test, expect, Page } from '@playwright/test';

test.describe('item', () => {
test('should validate all required fields.', () => {});
test('should save the item successfully.', () => {});
test('should item code be unqiue.', () => {});
});
61 changes: 31 additions & 30 deletions packages/server/src/api/controllers/Purchases/Bills.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
import { Service, Inject } from 'typedi';
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;

@Inject()
private billPayments: BillPaymentsService;

/**
* Router constructor.
*/
Expand Down Expand Up @@ -97,7 +98,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 +143,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 +185,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 +204,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,17 +217,16 @@ 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
);

return res.status(200).send({
id: storedBill.id,
message: 'The bill has been created successfully.',
Expand All @@ -241,13 +241,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 +263,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 +285,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 +304,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 +326,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 +338,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 +356,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 +375,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 All @@ -383,7 +384,7 @@ export default class BillsController extends BaseController {
const { id: billId } = req.params;

try {
const billPayments = await this.billPayments.getBillPayments(
const billPayments = await this.billsApplication.getBillPayments(
tenantId,
billId
);
Expand Down
Loading