update models and create consumer domain accounts permissions
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import { PosInfo } from '@/common/decorators/posInfo.decorator'
|
||||
import { Controller, Get } from '@nestjs/common'
|
||||
import { TokenAccount } from '@/common/decorators/tokenInfo.decorator'
|
||||
import { Controller, Get, Res } from '@nestjs/common'
|
||||
import { ApiTags } from '@nestjs/swagger'
|
||||
import { PosService } from './pos.service'
|
||||
|
||||
@@ -9,7 +10,24 @@ export class PosController {
|
||||
constructor(private service: PosService) {}
|
||||
|
||||
@Get()
|
||||
async getInfo(@PosInfo('pos_id') pos_id: string) {
|
||||
return await this.service.getInfo(pos_id)
|
||||
async getInfo(@PosInfo('pos_id') pos_id: string, @Res({ passthrough: true }) res) {
|
||||
const result = await this.service.getInfo(pos_id)
|
||||
|
||||
if (result) {
|
||||
// @ts-ignore
|
||||
res.cookie('posId', pos_id, {
|
||||
httpOnly: true,
|
||||
secure: process.env.NODE_ENV === 'production',
|
||||
sameSite: 'lax',
|
||||
maxAge: 1000 * 60 * 60 * 24 * 7, // 7 days
|
||||
})
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
@Get('/accessible')
|
||||
async getAccessiblePos(@TokenAccount('account_id') account_id) {
|
||||
return await this.service.getAccessible(account_id)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,12 @@
|
||||
import { checkAndDecodeJwtToken } from '@/common/utils/jwt-user.util'
|
||||
import { ConsumerRole } from '@/generated/prisma/enums'
|
||||
import { PrismaService } from '@/prisma/prisma.service'
|
||||
import { ForbiddenException, Injectable, NestMiddleware } from '@nestjs/common'
|
||||
import {
|
||||
ForbiddenException,
|
||||
Injectable,
|
||||
MisdirectedException,
|
||||
NestMiddleware,
|
||||
} from '@nestjs/common'
|
||||
import { JwtService } from '@nestjs/jwt'
|
||||
import { NextFunction, Request, Response } from 'express'
|
||||
|
||||
@@ -18,79 +24,114 @@ export class PosMiddleware implements NestMiddleware {
|
||||
try {
|
||||
const tokenAccount = checkAndDecodeJwtToken(req, this.jwtService)
|
||||
|
||||
// const posId = req.cookies['posId']
|
||||
const posId = req.headers.pos_id as string
|
||||
const { type, role, account_id } = tokenAccount!
|
||||
|
||||
if (tokenAccount?.type !== 'CONSUMER' || !posId || typeof posId !== 'string') {
|
||||
let posId = req.cookies['posId']
|
||||
|
||||
if (type !== 'CONSUMER') {
|
||||
return doForbidden()
|
||||
}
|
||||
|
||||
const consumerAccount = await this.prisma.consumerAccount.findUnique({
|
||||
where: {
|
||||
id: tokenAccount.account_id,
|
||||
},
|
||||
})
|
||||
if (!consumerAccount) {
|
||||
return doForbidden()
|
||||
}
|
||||
|
||||
const complex = await this.prisma.complex.findFirst({
|
||||
where: {
|
||||
pos_list: {
|
||||
some: {
|
||||
id: posId,
|
||||
if (!posId || typeof posId !== 'string') {
|
||||
const pos = await this.prisma.pos.findMany({
|
||||
where: {
|
||||
complex: {
|
||||
business_activity: {
|
||||
consumer: {
|
||||
consumer_accounts: {
|
||||
some: {
|
||||
id: account_id,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
if (!pos?.length) {
|
||||
throw new ForbiddenException('پایانهی فروشی برای شما یافت نشد')
|
||||
}
|
||||
if (pos.length === 1) {
|
||||
posId = pos[0].id
|
||||
} else throw new MisdirectedException()
|
||||
}
|
||||
|
||||
const pos = await this.prisma.pos.findUnique({
|
||||
where: {
|
||||
id: posId,
|
||||
},
|
||||
include: {
|
||||
business_activity: {
|
||||
include: {
|
||||
guild: true,
|
||||
select: {
|
||||
id: true,
|
||||
complex: {
|
||||
select: {
|
||||
id: true,
|
||||
business_activity: {
|
||||
select: {
|
||||
id: true,
|
||||
guild_id: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
if (!complex) {
|
||||
|
||||
if (!pos) {
|
||||
return doForbidden()
|
||||
}
|
||||
|
||||
// if (account?.role !== ConsumerRole.OWNER) {
|
||||
// const accountPermissions = await this.prisma.permissionConsumer.findUnique({
|
||||
// where: {
|
||||
// account_id: account?.id,
|
||||
// },
|
||||
// include: {
|
||||
// posPermissions: true,
|
||||
// businessPermissions: true,
|
||||
// complexPermissions: true,
|
||||
// },
|
||||
// })
|
||||
if (role !== ConsumerRole.OWNER) {
|
||||
const accountPermissions = await this.prisma.permissionConsumer.findUnique({
|
||||
where: {
|
||||
consumer_account_id: account_id,
|
||||
},
|
||||
select: {
|
||||
pos_permissions: {
|
||||
select: {
|
||||
pos_id: true,
|
||||
},
|
||||
},
|
||||
business_permissions: {
|
||||
select: {
|
||||
business_id: true,
|
||||
},
|
||||
},
|
||||
complex_permissions: {
|
||||
select: {
|
||||
complex_id: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
// if (
|
||||
// !(
|
||||
// accountPermissions?.businessPermissions.some(
|
||||
// permission => permission.business_id,
|
||||
// ) ||
|
||||
// accountPermissions?.complexPermissions.some(
|
||||
// permission => permission.complex_id,
|
||||
// ) ||
|
||||
// accountPermissions?.posPermissions.some(permission => permission.pos_id)
|
||||
// )
|
||||
// ) {
|
||||
// return doForbidden()
|
||||
// }
|
||||
// }
|
||||
if (
|
||||
!// accountPermissions?.business_permissions.some(
|
||||
// permission => permission.id,
|
||||
// ) ||
|
||||
// accountPermissions?.complex_permissions.some(
|
||||
// permission => permission.complex_id,
|
||||
// ) ||
|
||||
accountPermissions?.pos_permissions.some(permission => permission.pos_id)
|
||||
) {
|
||||
return doForbidden()
|
||||
}
|
||||
}
|
||||
req.posData = {
|
||||
pos_id: posId,
|
||||
complex_id: complex.id,
|
||||
business_id: complex.business_activity_id,
|
||||
guild_id: complex.business_activity.guild_id,
|
||||
consumer_account_id: consumerAccount.id,
|
||||
complex_id: pos.complex.id,
|
||||
business_id: pos.complex.id,
|
||||
guild_id: pos.complex.business_activity.guild_id,
|
||||
consumer_account_id: account_id,
|
||||
}
|
||||
req.decodedToken = tokenAccount
|
||||
req.decodedToken = tokenAccount!
|
||||
|
||||
return next()
|
||||
} catch (error) {
|
||||
if (error && typeof error === 'object') {
|
||||
throw error
|
||||
}
|
||||
|
||||
return doForbidden()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -53,4 +53,24 @@ export class PosService {
|
||||
guild,
|
||||
})
|
||||
}
|
||||
|
||||
async getAccessible(account_id: string) {
|
||||
const poses = await this.prisma.pos.findMany({
|
||||
where: {
|
||||
complex: {
|
||||
business_activity: {
|
||||
consumer: {
|
||||
consumer_accounts: {
|
||||
some: {
|
||||
id: account_id,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
return ResponseMapper.list(poses)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,8 +40,6 @@ export class SalesInvoicesService {
|
||||
}
|
||||
|
||||
async create(data: CreateSalesInvoiceDto, posInfo: IPosPayload) {
|
||||
console.log(posInfo)
|
||||
|
||||
data.invoice_date = new Date(data.invoice_date).toISOString() as any
|
||||
|
||||
const { complex_id, pos_id, consumer_account_id } = posInfo
|
||||
|
||||
Reference in New Issue
Block a user