feat(pos): update POS controller and service to handle posId for stock and product categories

This commit is contained in:
2025-12-26 16:47:34 +03:30
parent b4f226fb3a
commit d59be5995d
2 changed files with 87 additions and 21 deletions
+22 -12
View File
@@ -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 { CreateOrderDto } from './dto/create-pos.dto'
import { PosService } from './pos.service' import { PosService } from './pos.service'
@@ -6,21 +6,31 @@ import { PosService } from './pos.service'
export class PosController { export class PosController {
constructor(private readonly posService: PosService) {} constructor(private readonly posService: PosService) {}
@Get() @Get('/:posId')
getInfo() { getInfo(@Param('posId') posId: string) {
return this.posService.getInfo() return this.posService.getInfo(Number(posId))
} }
@Get('/stock') @Get('/:posId/stock')
async getStock() { async getStock(
const inventoryId = await this.posService.getDefaultInventoryId() @Param('posId') posId: string,
return this.posService.getStock(inventoryId!) @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') @Get('/:posId/product-categories')
async getProductCategories() { async getProductCategories(@Param('posId') posId: string) {
const inventoryId = await this.posService.getDefaultInventoryId() return this.posService.getProductCategories(Number(posId))
return this.posService.getProductCategories(inventoryId!)
} }
@Post('/orders/create') @Post('/orders/create')
+62 -6
View File
@@ -16,19 +16,60 @@ export class PosService {
return inventory?.id || null return inventory?.id || null
} }
async getInfo() { async getDefaultPosId(inventoryId: number) {
const info = await this.prisma.inventory.findFirst({ const pos = await this.prisma.posAccount.findFirst({
where: { isPointOfSale: true }, 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: { select: {
id: true, id: true,
name: 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 = { const query: Prisma.StockBalanceFindManyArgs = {
where: { where: {
inventoryId, inventoryId,
@@ -38,8 +79,17 @@ export class PosService {
: isAvailable === false : isAvailable === false
? { lte: 0 } ? { lte: 0 }
: undefined, : undefined,
...(q && {
product: {
OR: [{ name: { contains: q } }, { sku: { contains: q } }],
},
}),
},
orderBy: {
quantity: 'desc',
}, },
} }
const [items, count] = await this.prisma.$transaction([ const [items, count] = await this.prisma.$transaction([
this.prisma.stockBalance.findMany({ this.prisma.stockBalance.findMany({
where: query.where, where: query.where,
@@ -74,7 +124,13 @@ export class PosService {
return ResponseMapper.paginate(mapped, count, page, pageSize) 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({ const balances = await this.prisma.stockBalance.findMany({
where: { inventoryId }, where: { inventoryId },
select: { select: {