Skip to content

Commit

Permalink
Refactor: point of sale service (#203)
Browse files Browse the repository at this point in the history
Also includes refactor for the `container` and `product` service.
  • Loading branch information
JustSamuel authored Jul 9, 2024
1 parent ea4bd38 commit 15bf646
Show file tree
Hide file tree
Showing 17 changed files with 412 additions and 712 deletions.
12 changes: 4 additions & 8 deletions src/controller/container-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import { RequestWithToken } from '../middleware/token-middleware';
import ContainerService from '../service/container-service';
import { PaginatedContainerResponse } from './response/container-response';
import ContainerRevision from '../entity/container/container-revision';
import ProductService from '../service/product-service';
import Container from '../entity/container/container';
import { asNumber } from '../helpers/validators';
import { parseRequestPagination } from '../helpers/pagination';
Expand Down Expand Up @@ -164,9 +163,7 @@ export default class ContainerController extends BaseController {
* @tags containers - Operations of container controller
* @param {integer} id.path.required - The id of the container which should be returned
* @security JWT
* @param {integer} take.query - How many products the endpoint should return
* @param {integer} skip.query - How many products should be skipped (for pagination)
* @return {PaginatedProductResponse} 200 - All products in the container
* @return {Array.<ProductResponse>} 200 - All products in the container
* @return {string} 404 - Not found error
* @return {string} 500 - Internal server error
*/
Expand All @@ -176,8 +173,6 @@ export default class ContainerController extends BaseController {

this.logger.trace('Get all products in container', containerId, 'by user', req.token.user);

const { take, skip } = parseRequestPagination(req);

try {
// Check if we should return a 404.
const exist = await ContainerRevision.findOne({ where: { container: { id: containerId } } });
Expand All @@ -186,7 +181,8 @@ export default class ContainerController extends BaseController {
return;
}

res.json(await ProductService.getProducts({ containerId }, { take, skip }));
const products = (await ContainerService.getSingleContainer({ containerId, returnProducts: true })).products;
res.json(products ? products : []);
} catch (error) {
this.logger.error('Could not return all products in container:', error);
res.status(500).json('Internal server error.');
Expand Down Expand Up @@ -297,7 +293,7 @@ export default class ContainerController extends BaseController {
return;
}

res.json(await ContainerService.directContainerUpdate(request));
res.json(await ContainerService.updateContainer(request));
} catch (error) {
this.logger.error('Could not update container:', error);
res.status(500).json('Internal server error.');
Expand Down
18 changes: 7 additions & 11 deletions src/controller/point-of-sale-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import Policy from './policy';
import { RequestWithToken } from '../middleware/token-middleware';
import PointOfSaleService from '../service/point-of-sale-service';
import ContainerService from '../service/container-service';
import ProductService from '../service/product-service';
import PointOfSale from '../entity/point-of-sale/point-of-sale';
import { asNumber } from '../helpers/validators';
import { parseRequestPagination } from '../helpers/pagination';
Expand All @@ -39,6 +38,7 @@ import {
} from './request/validators/point-of-sale-request-spec';
import userTokenInOrgan from '../helpers/token-helper';
import TransactionService from '../service/transaction-service';
import { PointOfSaleWithContainersResponse } from './response/point-of-sale-response';

export default class PointOfSaleController extends BaseController {
private logger: Logger = log4js.getLogger('PointOfSaleController');
Expand Down Expand Up @@ -198,7 +198,7 @@ export default class PointOfSaleController extends BaseController {
}

const pointOfSale = (await PointOfSaleService
.getPointsOfSale({ pointOfSaleId, returnContainers: true })).records[0];
.getPointsOfSale({ pointOfSaleId, returnContainers: true, returnProducts: true })).records[0];
if (pointOfSale) {
res.json(pointOfSale);
}
Expand Down Expand Up @@ -247,7 +247,7 @@ export default class PointOfSaleController extends BaseController {
return;
}

res.json(await PointOfSaleService.directPointOfSaleUpdate(params));
res.json(await PointOfSaleService.updatePointOfSale(params));
} catch (error) {
this.logger.error('Could not update Point of Sale:', error);
res.status(500).json('Internal server error.');
Expand Down Expand Up @@ -291,22 +291,18 @@ export default class PointOfSaleController extends BaseController {
* @tags pointofsale - Operations of the point of sale controller
* @security JWT
* @param {integer} id.path.required - The id of the point of sale
* @param {integer} take.query - How many products the endpoint should return
* @param {integer} skip.query - How many products should be skipped (for pagination)
* @return {PaginatedProductResponse} 200 - All products of the requested Point of Sale
* @return {Array.<ProductResponse>} 200 - All products of the requested Point of Sale
* @return {string} 500 - Internal server error
*/
public async returnAllPointOfSaleProducts(req: RequestWithToken, res: Response): Promise<void> {
const { id } = req.params;
this.logger.trace('Get all point of sale products', id, 'by user', req.token.user);

const { take, skip } = parseRequestPagination(req);

// Handle request
try {
const products = await ProductService.getProducts({
pointOfSaleId: parseInt(id, 10),
}, { take, skip });
const pointOfSaleId = parseInt(id, 10);
const products = (await PointOfSaleService.getPointsOfSale({ pointOfSaleId, returnContainers: true, returnProducts: true }))
.records.flatMap((p: PointOfSaleWithContainersResponse) => p.containers).flatMap((c) => c.products);
res.json(products);
} catch (error) {
this.logger.error('Could not return all point of sale products:', error);
Expand Down
2 changes: 1 addition & 1 deletion src/controller/product-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ export default class ProductController extends BaseController {
return;
}

res.json(await ProductService.directProductUpdate(params));
res.json(await ProductService.updateProduct(params));
} catch (error) {
this.logger.error('Could not update product:', error);
res.status(500).json('Internal server error.');
Expand Down
2 changes: 1 addition & 1 deletion src/controller/response/debtor-response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import BaseResponse from './base-response';
import { PaginationResult } from '../../helpers/pagination';
import { BaseUserResponse } from './user-response';
import BalanceResponse from './balance-response';
import {Dinero} from "dinero.js";
import { Dinero } from 'dinero.js';

/**
* @typedef {object} UserToFineResponse
Expand Down
5 changes: 1 addition & 4 deletions src/entity/point-of-sale/point-of-sale-revision.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,7 @@ export default class PointOfSaleRevision extends BaseEntityWithoutId {

@ManyToOne(() => PointOfSale, {
nullable: false,
// eager: true | I removed this because of a bug in typeorm. Typeorm prioritises
// the eager keyword over the relations that you are trying to load additionally.
// So once you specified eager it is not possible to get
// PointOfSaleRevision -> PointOfSale -> User(owner). This is unfortunate. Lets wait for le fix
eager: true,
})
@JoinColumn({ name: 'pointOfSaleId' })
public readonly pointOfSale: PointOfSale;
Expand Down
3 changes: 1 addition & 2 deletions src/service/balance-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ import { toMySQLString } from '../helpers/timestamps';
import { Dinero } from 'dinero.js';
import { OrderingDirection } from '../helpers/ordering';
import { defaultPagination, PaginationParameters } from '../helpers/pagination';
import User, { UserType } from '../entity/user/user';
import UserService from './user-service';
import { UserType } from '../entity/user/user';

export enum BalanceOrderColumn {
ID = 'id',
Expand Down
Loading

0 comments on commit 15bf646

Please sign in to comment.