From d59be5995d93ea6404d314147d9cc15f5014e0d3 Mon Sep 17 00:00:00 2001 From: ahasani194 Date: Fri, 26 Dec 2025 16:47:34 +0330 Subject: [PATCH] feat(pos): update POS controller and service to handle posId for stock and product categories --- src/modules/pos/pos.controller.ts | 34 +++++++++----- src/modules/pos/pos.service.ts | 74 +++++++++++++++++++++++++++---- 2 files changed, 87 insertions(+), 21 deletions(-) diff --git a/src/modules/pos/pos.controller.ts b/src/modules/pos/pos.controller.ts index 4a5cc97..88c872e 100644 --- a/src/modules/pos/pos.controller.ts +++ b/src/modules/pos/pos.controller.ts @@ -1,4 +1,4 @@ -import { Body, Controller, Get, Post } from '@nestjs/common' +import { Body, Controller, Get, Param, Post, Query } from '@nestjs/common' import { CreateOrderDto } from './dto/create-pos.dto' import { PosService } from './pos.service' @@ -6,21 +6,31 @@ import { PosService } from './pos.service' export class PosController { constructor(private readonly posService: PosService) {} - @Get() - getInfo() { - return this.posService.getInfo() + @Get('/:posId') + getInfo(@Param('posId') posId: string) { + return this.posService.getInfo(Number(posId)) } - @Get('/stock') - async getStock() { - const inventoryId = await this.posService.getDefaultInventoryId() - return this.posService.getStock(inventoryId!) + @Get('/:posId/stock') + async getStock( + @Param('posId') posId: string, + @Query('isAvailable') isAvailable?: string, + @Query('page') page?: string, + @Query('pageSize') pageSize?: string, + @Query('q') q?: string, + ) { + const options = { + isAvailable: isAvailable ? isAvailable === 'true' : undefined, + page: page ? Number(page) : undefined, + pageSize: pageSize ? Number(pageSize) : undefined, + q, + } + return this.posService.getStock(Number(posId), options) } - @Get('/product-categories') - async getProductCategories() { - const inventoryId = await this.posService.getDefaultInventoryId() - return this.posService.getProductCategories(inventoryId!) + @Get('/:posId/product-categories') + async getProductCategories(@Param('posId') posId: string) { + return this.posService.getProductCategories(Number(posId)) } @Post('/orders/create') diff --git a/src/modules/pos/pos.service.ts b/src/modules/pos/pos.service.ts index 599ed20..e8f7fd7 100644 --- a/src/modules/pos/pos.service.ts +++ b/src/modules/pos/pos.service.ts @@ -16,19 +16,60 @@ export class PosService { return inventory?.id || null } - async getInfo() { - const info = await this.prisma.inventory.findFirst({ - where: { isPointOfSale: true }, - select: { - id: true, - name: true, + async getDefaultPosId(inventoryId: number) { + const pos = await this.prisma.posAccount.findFirst({ + where: { inventoryId }, + select: { id: true }, + }) + return pos?.id || null + } + + async getInfo(posId: number) { + const info = await this.prisma.posAccount.findUniqueOrThrow({ + where: { id: posId }, + include: { + inventory: { + select: { + id: true, + name: true, + }, + }, + bankAccount: { + select: { + id: true, + name: true, + accountNumber: true, + branch: { + select: { + id: true, + name: true, + bank: { select: { id: true, name: true } }, + }, + }, + }, + }, }, }) - return ResponseMapper.single({ store: info }) + return ResponseMapper.single({ ...info }) } - async getStock(inventoryId: number, isAvailable?: boolean, page = 1, pageSize = 50) { + async getStock( + posId: number, + options: { + isAvailable?: boolean + page?: number + pageSize?: number + q?: string + } = {}, + ) { + const { isAvailable, page = 1, pageSize = 50, q } = options + const pos = await this.prisma.posAccount.findUniqueOrThrow({ + where: { id: posId }, + select: { inventoryId: true }, + }) + const inventoryId = pos.inventoryId + const query: Prisma.StockBalanceFindManyArgs = { where: { inventoryId, @@ -38,8 +79,17 @@ export class PosService { : isAvailable === false ? { lte: 0 } : undefined, + ...(q && { + product: { + OR: [{ name: { contains: q } }, { sku: { contains: q } }], + }, + }), + }, + orderBy: { + quantity: 'desc', }, } + const [items, count] = await this.prisma.$transaction([ this.prisma.stockBalance.findMany({ where: query.where, @@ -74,7 +124,13 @@ export class PosService { return ResponseMapper.paginate(mapped, count, page, pageSize) } - async getProductCategories(inventoryId: number) { + async getProductCategories(posId: number) { + const pos = await this.prisma.posAccount.findUniqueOrThrow({ + where: { id: posId }, + select: { inventoryId: true }, + }) + const inventoryId = pos.inventoryId + const balances = await this.prisma.stockBalance.findMany({ where: { inventoryId }, select: {