consumer pos
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import { TUploadedFileTypes } from '@/common/models'
|
||||
import { Injectable } from '@nestjs/common'
|
||||
import { UploaderService } from './uploader.service'
|
||||
|
||||
@@ -7,14 +8,8 @@ export class ImageUploaderService {
|
||||
|
||||
async uploadImage(
|
||||
file: Express.Multer.File,
|
||||
account_id: string,
|
||||
type: string,
|
||||
type: TUploadedFileTypes,
|
||||
): Promise<string> {
|
||||
return this.uploaderService.uploadFile(
|
||||
file.buffer,
|
||||
file.originalname,
|
||||
account_id,
|
||||
type,
|
||||
)
|
||||
return this.uploaderService.uploadFile(file.buffer, file.originalname, type)
|
||||
}
|
||||
}
|
||||
|
||||
+5
-4
@@ -1,9 +1,10 @@
|
||||
import type { TUploadedFileTypes } from '@/common/models'
|
||||
import { Body, Controller, Post, UploadedFile, UseInterceptors } from '@nestjs/common'
|
||||
import { FileInterceptor } from '@nestjs/platform-express'
|
||||
import { ImageUploaderService } from './image-uploader.service'
|
||||
|
||||
@Controller('uploaders')
|
||||
export class UploadersController {
|
||||
@Controller('uploader')
|
||||
export class UploaderController {
|
||||
constructor(private readonly imageUploaderService: ImageUploaderService) {}
|
||||
|
||||
@Post('image')
|
||||
@@ -11,9 +12,9 @@ export class UploadersController {
|
||||
async uploadImage(
|
||||
@UploadedFile() file: Express.Multer.File,
|
||||
@Body('accountId') accountId: string,
|
||||
@Body('type') type: string,
|
||||
@Body('type') type: TUploadedFileTypes,
|
||||
) {
|
||||
const url = await this.imageUploaderService.uploadImage(file, accountId, type)
|
||||
const url = await this.imageUploaderService.uploadImage(file, type)
|
||||
return { url }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
import { checkAndDecodeJwtToken } from '@/common/utils/jwt-user.util'
|
||||
import { PrismaService } from '@/prisma/prisma.service'
|
||||
import { ForbiddenException, Injectable, NestMiddleware } from '@nestjs/common'
|
||||
import { JwtService } from '@nestjs/jwt'
|
||||
import { NextFunction, Request, Response } from 'express'
|
||||
|
||||
@Injectable()
|
||||
export class PosMiddleware implements NestMiddleware {
|
||||
constructor(
|
||||
private prisma: PrismaService,
|
||||
private jwtService: JwtService,
|
||||
) {}
|
||||
|
||||
async use(req: Request, _res: Response, next: NextFunction) {
|
||||
const doForbidden = () => {
|
||||
throw new ForbiddenException('شما دسترسی لازم را ندارید')
|
||||
}
|
||||
try {
|
||||
const tokenAccount = checkAndDecodeJwtToken(req, this.jwtService)
|
||||
|
||||
const { type, account_id } = tokenAccount!
|
||||
if (type === 'ADMIN' || type === 'CONSUMER') {
|
||||
const account = await this.prisma.account.findFirst({
|
||||
where: {
|
||||
OR: [
|
||||
{
|
||||
admin_account: {
|
||||
id: account_id,
|
||||
},
|
||||
},
|
||||
{
|
||||
consumer_account: {
|
||||
id: account_id,
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
})
|
||||
if (account) return next()
|
||||
}
|
||||
throw doForbidden()
|
||||
} catch (error) {
|
||||
if (error && typeof error === 'object') {
|
||||
throw error
|
||||
}
|
||||
|
||||
return doForbidden()
|
||||
}
|
||||
}
|
||||
}
|
||||
+3
-3
@@ -1,11 +1,11 @@
|
||||
import { Module } from '@nestjs/common'
|
||||
import { ImageUploaderService } from './image-uploader.service'
|
||||
import { UploaderController } from './uploader.controller'
|
||||
import { UploaderService } from './uploader.service'
|
||||
import { UploadersController } from './uploaders.controller'
|
||||
|
||||
@Module({
|
||||
providers: [UploaderService, ImageUploaderService],
|
||||
controllers: [UploadersController],
|
||||
controllers: [UploaderController],
|
||||
exports: [UploaderService, ImageUploaderService],
|
||||
})
|
||||
export class UploadersModule {}
|
||||
export class UploaderModule {}
|
||||
@@ -1,3 +1,4 @@
|
||||
import { TUploadedFileTypes } from '@/common/models'
|
||||
import { Injectable } from '@nestjs/common'
|
||||
import { promises as fs } from 'fs'
|
||||
import { join } from 'path'
|
||||
@@ -10,16 +11,15 @@ export class UploaderService {
|
||||
async uploadFile(
|
||||
buffer: Buffer,
|
||||
originalname: string,
|
||||
accountId: string,
|
||||
type: string,
|
||||
type: TUploadedFileTypes,
|
||||
): Promise<string> {
|
||||
const ext = originalname.split('.').pop()
|
||||
const filename = `${uuidv4()}.${ext}`
|
||||
const dirPath = join(this.uploadDir, accountId, type)
|
||||
const dirPath = join(this.uploadDir, type)
|
||||
const filePath = join(dirPath, filename)
|
||||
await fs.mkdir(dirPath, { recursive: true })
|
||||
await fs.writeFile(filePath, buffer)
|
||||
// Return a URL path, e.g. /uploads/{accountId}/{type}/{filename}
|
||||
return `/uploads/${accountId}/${type}/${filename}`
|
||||
return `/uploads/${type}/${filename}`
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user