init to statistics and manage pos type users

This commit is contained in:
2026-04-13 13:21:50 +03:30
parent 9cef733370
commit 388aa25de4
31 changed files with 385 additions and 251 deletions
@@ -75,6 +75,12 @@ export class LicensesService {
id: partner_id,
},
}
} else if (currentLicense?.partner_id) {
dataToUpdate.partner = {
disconnect: {
id: currentLicense?.partner_id,
},
}
}
if (starts_at) {
+2 -2
View File
@@ -18,7 +18,7 @@ export class AuthController {
if (result?.data?.accessToken) {
// @ts-ignore
res.cookie('accessToken', result.data.accessToken, {
httpOnly: true,
httpOnly: false,
secure: process.env.NODE_ENV === 'production',
sameSite: 'lax',
maxAge: 1000 * 60 * 60 * 24 * 7, // 7 days
@@ -34,7 +34,7 @@ export class AuthController {
if (result?.data?.accessToken) {
// @ts-ignore
res.cookie('accessToken', result.data.accessToken, {
httpOnly: true,
httpOnly: false,
secure: process.env.NODE_ENV === 'production',
sameSite: 'lax',
maxAge: 1000 * 60 * 60 * 24 * 7, // 7 days
+12 -2
View File
@@ -1,6 +1,11 @@
import { checkAndDecodeJwtToken } from '@/common/utils/jwt-user.util'
import { PrismaService } from '@/prisma/prisma.service'
import { ForbiddenException, Injectable, NestMiddleware } from '@nestjs/common'
import {
ForbiddenException,
Injectable,
NestMiddleware,
UnauthorizedException,
} from '@nestjs/common'
import { JwtService } from '@nestjs/jwt'
import { NextFunction, Request, Response } from 'express'
@@ -22,7 +27,7 @@ export class ConsumerMiddleware implements NestMiddleware {
tokenAccount?.type !== 'CONSUMER' ||
!(tokenAccount.role === 'OWNER' || tokenAccount.role === 'MANAGER')
) {
return doForbidden()
throw new UnauthorizedException()
}
const consumerAccount = await this.prisma.consumerAccount.findUnique({
@@ -43,10 +48,15 @@ export class ConsumerMiddleware implements NestMiddleware {
account_id: consumerAccount.id,
id: consumerAccount.consumer_id,
}
console.log(consumerAccount)
req.decodedToken = tokenAccount
return next()
} catch (error) {
if (error && typeof error === 'object') {
throw error
}
return doForbidden()
}
}
+2
View File
@@ -6,6 +6,7 @@ import { ConsumerController } from './consumer.controller'
import { ConsumerMiddleware } from './consumer.middleware'
import { ConsumerService } from './consumer.service'
import { ConsumerCustomersModule } from './customers/customers.module'
import { ConsumerStatisticsModule } from './statistics/statistics.module'
@Module({
controllers: [ConsumerController],
@@ -13,6 +14,7 @@ import { ConsumerCustomersModule } from './customers/customers.module'
ConsumerAccountsModule,
ConsumerBusinessActivitiesModule,
ConsumerCustomersModule,
ConsumerStatisticsModule,
],
providers: [JwtService, ConsumerService],
})
+1 -1
View File
@@ -7,7 +7,7 @@ export class ConsumerService {
constructor(private readonly prisma: PrismaService) {}
async getInfo(consumer_id: string) {
const consumer = await this.prisma.consumer.findUnique({
const consumer = await this.prisma.consumer.findUniqueOrThrow({
where: {
id: consumer_id,
},
@@ -31,7 +31,7 @@ export class CustomerSaleInvoicesService {
},
},
},
account: {
consumer_account: {
select: {
role: true,
consumer: {
@@ -0,0 +1,15 @@
import { TokenAccount } from '@/common/decorators/tokenInfo.decorator'
import { Controller, Get } from '@nestjs/common'
import { ApiTags } from '@nestjs/swagger'
import { SaleInvoicesService } from './saleInvoices.service'
@ApiTags('ConsumerStatistics')
@Controller('consumer/statistics')
export class StatisticsController {
constructor(private readonly service: SaleInvoicesService) {}
@Get('invoices')
async getInvoices(@TokenAccount('userId') consumerId: string) {
return this.service.getInvoices(consumerId)
}
}
@@ -0,0 +1,12 @@
import { PrismaModule } from '@/prisma/prisma.module'
import { Module } from '@nestjs/common'
import { StatisticsController } from './saleInvoices.controller'
import { SaleInvoicesService } from './saleInvoices.service'
@Module({
imports: [PrismaModule],
controllers: [StatisticsController],
providers: [SaleInvoicesService],
exports: [SaleInvoicesService],
})
export class ConsumerSaleInvoicesModule {}
@@ -0,0 +1,113 @@
import {
SalesInvoiceWhereInput,
SalesInvoiceWhereUniqueInput,
} from '@/generated/prisma/models'
import { PrismaService } from '@/prisma/prisma.service'
import { Injectable } from '@nestjs/common'
import { ResponseMapper } from 'common/response/response-mapper'
@Injectable()
export class SaleInvoicesService {
constructor(private readonly prisma: PrismaService) {}
private readonly defaultSelect = {
id: true,
code: true,
total_amount: true,
unknown_customer: true,
invoice_date: true,
consumer_account: {
select: {
role: true,
account: {
select: {
username: true,
},
},
},
},
pos: {
select: {
id: true,
name: true,
complex: {
select: {
id: true,
name: true,
business_activity: {
select: {
id: true,
name: true,
},
},
},
},
},
},
customer: {
select: {
id: true,
type: true,
customer_legal: {
select: {
company_name: true,
},
},
customer_individual: {
select: {
first_name: true,
last_name: true,
},
},
},
},
}
async getInvoices(consumer_id: string, page = 1, pageSize = 10) {
const invoicesWhere: SalesInvoiceWhereInput = {
consumer_account: {
consumer_id,
},
}
const [invoices, count] = await this.prisma.$transaction(async tx => [
await tx.salesInvoice.findMany({
where: invoicesWhere,
select: this.defaultSelect,
skip: (page - 1) * pageSize,
take: pageSize,
}),
await tx.salesInvoice.count({ where: invoicesWhere }),
])
return ResponseMapper.paginate(invoices, { page, pageSize, count })
}
async getInvoice(consumer_id: string, id: string) {
const invoicesWhere: SalesInvoiceWhereUniqueInput = {
id,
consumer_account: {
consumer_id,
},
}
const invoice = await this.prisma.salesInvoice.findUniqueOrThrow({
where: invoicesWhere,
select: {
...this.defaultSelect,
notes: true,
created_at: true,
items: {
select: {
id: true,
good: {
select: {
id: true,
name: true,
},
},
},
},
},
})
return ResponseMapper.single(invoice)
}
}
@@ -0,0 +1,15 @@
import { TokenAccount } from '@/common/decorators/tokenInfo.decorator'
import { Controller, Get } from '@nestjs/common'
import { ApiTags } from '@nestjs/swagger'
import { StatisticsService } from './statistics.service'
@ApiTags('ConsumerStatistics')
@Controller('consumer/statistics')
export class StatisticsController {
constructor(private readonly service: StatisticsService) {}
@Get('invoices')
async getInvoices(@TokenAccount('userId') consumerId: string) {
return this.service.getInvoices(consumerId)
}
}
@@ -0,0 +1,12 @@
import { PrismaModule } from '@/prisma/prisma.module'
import { Module } from '@nestjs/common'
import { StatisticsController } from './statistics.controller'
import { StatisticsService } from './statistics.service'
@Module({
imports: [PrismaModule],
controllers: [StatisticsController],
providers: [StatisticsService],
exports: [StatisticsService],
})
export class ConsumerStatisticsModule {}
@@ -0,0 +1,59 @@
import { PrismaService } from '@/prisma/prisma.service'
import { Injectable } from '@nestjs/common'
import { ResponseMapper } from 'common/response/response-mapper'
@Injectable()
export class StatisticsService {
constructor(private readonly prisma: PrismaService) {}
async getInvoices(consumer_id: string) {
const startOfToday = new Date()
startOfToday.setHours(0, 0, 0, 0)
const invoices = await this.prisma.salesInvoice.findMany({
where: {
consumer_account: {
consumer_id,
},
created_at: {
gte: startOfToday,
},
},
select: {
id: true,
code: true,
total_amount: true,
created_at: true,
consumer_account: {
select: {
role: true,
account: {
select: {
username: true,
},
},
},
},
pos: {
select: {
id: true,
name: true,
complex: {
select: {
id: true,
name: true,
business_activity: {
select: {
id: true,
name: true,
},
},
},
},
},
},
},
})
return ResponseMapper.list(invoices)
}
}
+4 -1
View File
@@ -13,10 +13,13 @@ export class PosController {
async getInfo(@PosInfo('pos_id') pos_id: string, @Res({ passthrough: true }) res) {
const result = await this.service.getInfo(pos_id)
console.log('firstaaaaa')
if (result) {
console.log('first')
// @ts-ignore
res.cookie('posId', pos_id, {
httpOnly: true,
httpOnly: false,
secure: process.env.NODE_ENV === 'production',
sameSite: 'lax',
maxAge: 1000 * 60 * 60 * 24 * 7, // 7 days
+15 -2
View File
@@ -4,8 +4,8 @@ import { PrismaService } from '@/prisma/prisma.service'
import {
ForbiddenException,
Injectable,
MisdirectedException,
NestMiddleware,
PreconditionFailedException,
} from '@nestjs/common'
import { JwtService } from '@nestjs/jwt'
import { NextFunction, Request, Response } from 'express'
@@ -32,6 +32,11 @@ export class PosMiddleware implements NestMiddleware {
return doForbidden()
}
if (req.url.startsWith('/api/v1/pos/accessible')) {
req.decodedToken = tokenAccount!
return next()
}
if (!posId || typeof posId !== 'string') {
const pos = await this.prisma.pos.findMany({
where: {
@@ -46,6 +51,13 @@ export class PosMiddleware implements NestMiddleware {
},
},
},
permission_pos: {
some: {
permission: {
consumer_account_id: account_id,
},
},
},
},
})
@@ -54,7 +66,8 @@ export class PosMiddleware implements NestMiddleware {
}
if (pos.length === 1) {
posId = pos[0].id
} else throw new MisdirectedException()
} else
throw new PreconditionFailedException('پایانه‌ی مورد نظر خود را انتخاب کنید.')
}
const pos = await this.prisma.pos.findUnique({
+23
View File
@@ -68,6 +68,29 @@ export class PosService {
},
},
},
permission_pos: {
some: {
permission: {
consumer_account_id: account_id,
},
},
},
},
select: {
id: true,
name: true,
complex: {
select: {
id: true,
name: true,
business_activity: {
select: {
id: true,
name: true,
},
},
},
},
},
})
@@ -166,7 +166,7 @@ export class SalesInvoicesService {
// id: newCustomerId || undefined,
// },
// },
account: {
consumer_account: {
connect: {
id: consumer_account_id,
},
@@ -1,20 +0,0 @@
import { ApiProperty, PartialType } from '@nestjs/swagger'
import { IsOptional, IsString } from 'class-validator'
export class CreateServiceCategoryDto {
@IsString()
@ApiProperty()
name: string
@IsOptional()
@IsString()
@ApiProperty({ required: false })
description?: string
@IsOptional()
@IsString()
@ApiProperty({ required: false })
imageUrl?: string
}
export class UpdateServiceCategoryDto extends PartialType(CreateServiceCategoryDto) {}
@@ -1,22 +0,0 @@
import { Body, Controller, Get, Param, Post } from '@nestjs/common'
import { ServiceCategoriesService } from './service-categories.service'
@Controller('service_categories')
export class ServiceCategoriesController {
constructor(private readonly serviceCategoriesService: ServiceCategoriesService) {}
@Get()
findAll() {
return this.serviceCategoriesService.findAll()
}
@Get(':id')
findOne(@Param('id') id: string) {
return this.serviceCategoriesService.findOne(+id)
}
@Post()
create(@Body() data: any) {
return this.serviceCategoriesService.create(data)
}
}
@@ -1,9 +0,0 @@
import { Module } from '@nestjs/common'
import { ServiceCategoriesController } from './service-categories.controller'
import { ServiceCategoriesService } from './service-categories.service'
@Module({
controllers: [ServiceCategoriesController],
providers: [ServiceCategoriesService],
})
export class ServiceCategoriesModule {}
@@ -1,19 +0,0 @@
import { Injectable } from '@nestjs/common'
@Injectable()
export class ServiceCategoriesService {
findAll() {
// TODO: Implement fetching all service categories
return []
}
findOne(id: number) {
// TODO: Implement fetching a single service category
return {}
}
create(data: any) {
// TODO: Implement service category creation
return data
}
}
@@ -1,34 +0,0 @@
import { ApiProperty, PartialType } from '@nestjs/swagger'
import { IsNumber, IsOptional, IsString } from 'class-validator'
export class CreateServiceDto {
@IsString()
@ApiProperty()
name: string
@IsOptional()
@IsString()
@ApiProperty({ required: false })
description?: string
@IsString()
@ApiProperty()
sku: string
@IsOptional()
@IsString()
@ApiProperty({ required: false })
barcode?: string
@IsOptional()
@IsString()
@ApiProperty({ required: false })
categoryId?: string
@IsOptional()
@IsNumber()
@ApiProperty({ required: false })
baseSalePrice?: number
}
export class UpdateServiceDto extends PartialType(CreateServiceDto) {}
@@ -1,22 +0,0 @@
import { Body, Controller, Get, Param, Post } from '@nestjs/common'
import { ServicesService } from './services.service'
@Controller('services')
export class ServicesController {
constructor(private readonly servicesService: ServicesService) {}
@Get()
findAll() {
return this.servicesService.findAll()
}
@Get(':id')
findOne(@Param('id') id: string) {
return this.servicesService.findOne(+id)
}
@Post()
create(@Body() data: any) {
return this.servicesService.create(data)
}
}
-9
View File
@@ -1,9 +0,0 @@
import { Module } from '@nestjs/common'
import { ServicesController } from './services.controller'
import { ServicesService } from './services.service'
@Module({
controllers: [ServicesController],
providers: [ServicesService],
})
export class ServicesModule {}
-19
View File
@@ -1,19 +0,0 @@
import { Injectable } from '@nestjs/common'
@Injectable()
export class ServicesService {
findAll() {
// TODO: Implement fetching all services
return []
}
findOne(id: number) {
// TODO: Implement fetching a single service
return {}
}
create(data: any) {
// TODO: Implement service creation
return data
}
}