feat(backend): implement WholesaleModule for B2B wholesale applications and admin approvals
This commit is contained in:
1 parent
0046fb7106
commit
7cc5c7d176
7 files changed
+174
-21
No files matched your search
@@ -17,6 +17,7 @@ import { BlogsModule } from './blogs/blogs.module';
|
||||
import { WikiModule } from './wiki/wiki.module';
|
||||
import { SeoModule } from './seo/seo.module';
|
||||
import { CmsModule } from './cms/cms.module';
|
||||
import { WholesaleModule } from './wholesale/wholesale.module';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
@@ -38,6 +39,7 @@ import { CmsModule } from './cms/cms.module';
|
||||
WikiModule,
|
||||
SeoModule,
|
||||
CmsModule,
|
||||
WholesaleModule,
|
||||
],
|
||||
controllers: [MetricsController],
|
||||
providers: [
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
import { IsString, IsNotEmpty, IsOptional } from 'class-validator';
|
||||
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
|
||||
|
||||
export class WholesaleApplyDto {
|
||||
@ApiProperty({ description: 'نام مجموعه / کلینیک / داروخانه' })
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
businessName: string;
|
||||
|
||||
@ApiProperty({ description: 'شماره پروانه کسب / نظام دامپزشکی' })
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
licenseNumber: string;
|
||||
|
||||
@ApiPropertyOptional({ description: 'آدرس تصویر/فایل پروانه کسب' })
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
licenseDocumentUrl?: string;
|
||||
|
||||
@ApiPropertyOptional({ description: 'توضیحات تکمیلی' })
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
notes?: string;
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
import { Controller, Post, Get, Put, Body, Param, UseGuards, Request } from '@nestjs/common';
|
||||
import { WholesaleService } from './wholesale.service';
|
||||
import { WholesaleApplyDto } from './dto/wholesale-apply.dto';
|
||||
import { ApiTags, ApiOperation, ApiBearerAuth } from '@nestjs/swagger';
|
||||
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
|
||||
import { RolesGuard } from '../auth/roles.guard';
|
||||
import { Roles } from '../auth/roles.decorator';
|
||||
|
||||
@ApiTags('Wholesale - عمدهفروشی B2B')
|
||||
@Controller('wholesale')
|
||||
export class WholesaleController {
|
||||
constructor(private readonly wholesaleService: WholesaleService) {}
|
||||
|
||||
@Post('apply')
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@ApiBearerAuth()
|
||||
@ApiOperation({ summary: 'ثبت درخواست همکاری عمدهفروشی (ارسال پروانه کلینیک/داروخانه)' })
|
||||
applyForWholesale(@Request() req: any, @Body() dto: WholesaleApplyDto) {
|
||||
return this.wholesaleService.applyForWholesale(req.user.id, dto);
|
||||
}
|
||||
|
||||
@Get('requests')
|
||||
@UseGuards(JwtAuthGuard, RolesGuard)
|
||||
@Roles('ADMIN')
|
||||
@ApiBearerAuth()
|
||||
@ApiOperation({ summary: 'لیست تمام درخواستهای همکاری عمدهفروشی (مخصوص ادمین)' })
|
||||
getWholesaleRequests() {
|
||||
return this.wholesaleService.getWholesaleRequests();
|
||||
}
|
||||
|
||||
@Put('approve/:userId')
|
||||
@UseGuards(JwtAuthGuard, RolesGuard)
|
||||
@Roles('ADMIN')
|
||||
@ApiBearerAuth()
|
||||
@ApiOperation({ summary: 'تایید درخواست و ارتقا به خریدار عمده (User_Wholesale)' })
|
||||
approveWholesaleRequest(@Param('userId') userId: string) {
|
||||
return this.wholesaleService.approveWholesaleRequest(userId);
|
||||
}
|
||||
|
||||
@Put('reject/:userId')
|
||||
@UseGuards(JwtAuthGuard, RolesGuard)
|
||||
@Roles('ADMIN')
|
||||
@ApiBearerAuth()
|
||||
@ApiOperation({ summary: 'رد درخواست همکاری عمدهفروشی' })
|
||||
rejectWholesaleRequest(@Param('userId') userId: string) {
|
||||
return this.wholesaleService.rejectWholesaleRequest(userId);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { WholesaleController } from './wholesale.controller';
|
||||
import { WholesaleService } from './wholesale.service';
|
||||
import { PrismaModule } from '../prisma/prisma.module';
|
||||
|
||||
@Module({
|
||||
imports: [PrismaModule],
|
||||
controllers: [WholesaleController],
|
||||
providers: [WholesaleService],
|
||||
exports: [WholesaleService],
|
||||
})
|
||||
export class WholesaleModule {}
|
||||
@@ -0,0 +1,67 @@
|
||||
import { Injectable, BadRequestException } from '@nestjs/common';
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
import { WholesaleApplyDto } from './dto/wholesale-apply.dto';
|
||||
|
||||
@Injectable()
|
||||
export class WholesaleService {
|
||||
constructor(private prisma: PrismaService) {}
|
||||
|
||||
async applyForWholesale(userId: string, dto: WholesaleApplyDto) {
|
||||
const user = await this.prisma.user.findUnique({ where: { id: userId } });
|
||||
if (!user) throw new BadRequestException('کاربر یافت نشد');
|
||||
|
||||
// Update user role to pending B2B or User_Wholesale with business info
|
||||
const updatedUser = await this.prisma.user.update({
|
||||
where: { id: userId },
|
||||
data: {
|
||||
role: 'User_B2B_Pending',
|
||||
},
|
||||
});
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: 'درخواست همکاری عمدهفروشی با موفقیت ثبت شد و در حال بررسی توسط ادمین است.',
|
||||
user: {
|
||||
id: updatedUser.id,
|
||||
role: updatedUser.role,
|
||||
businessName: dto.businessName,
|
||||
licenseNumber: dto.licenseNumber,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
async getWholesaleRequests() {
|
||||
return this.prisma.user.findMany({
|
||||
where: {
|
||||
role: { in: ['User_B2B_Pending', 'User_Wholesale', 'User_B2B'] },
|
||||
},
|
||||
orderBy: { createdAt: 'desc' },
|
||||
});
|
||||
}
|
||||
|
||||
async approveWholesaleRequest(userId: string) {
|
||||
const updated = await this.prisma.user.update({
|
||||
where: { id: userId },
|
||||
data: { role: 'User_Wholesale' },
|
||||
});
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: 'حساب کاربری با موفقیت به خریدار عمده (User_Wholesale) ارتقا یافت.',
|
||||
user: updated,
|
||||
};
|
||||
}
|
||||
|
||||
async rejectWholesaleRequest(userId: string) {
|
||||
const updated = await this.prisma.user.update({
|
||||
where: { id: userId },
|
||||
data: { role: 'User_PetOwner' },
|
||||
});
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: 'درخواست همکاری رد شد و نقش کاربر به مشتری عادی تغییر کرد.',
|
||||
user: updated,
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user