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
+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)
}
}