feat(pos): update POS controller and service to handle posId for stock and product categories
This commit is contained in:
@@ -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')
|
||||
|
||||
@@ -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: {
|
||||
|
||||
Reference in New Issue
Block a user