feat(backend): implement Phase A database models and controllers for cross-boundary dependencies
Deploy Canina / deploy (push) Failing after 28s
Deploy Canina / deploy (push) Failing after 28s
This commit is contained in:
1 parent
74fcaa6a4e
commit
1ae4c99ffc
26 files changed
+1205
-345
No files matched your search
@@ -0,0 +1,84 @@
|
||||
import {
|
||||
Controller,
|
||||
Get,
|
||||
Post,
|
||||
Patch,
|
||||
Body,
|
||||
Param,
|
||||
UseGuards,
|
||||
Req,
|
||||
} from '@nestjs/common';
|
||||
import { B2BService } from './b2b.service';
|
||||
import { ApiTags, ApiOperation, ApiBearerAuth } from '@nestjs/swagger';
|
||||
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
|
||||
import { RolesGuard } from '../common/guards/roles.guard';
|
||||
import { Roles } from '../common/decorators/roles.decorator';
|
||||
|
||||
@ApiTags('B2B - خدمات عمدهفروشی و همکاران تجاری')
|
||||
@Controller('b2b')
|
||||
export class B2BController {
|
||||
constructor(private readonly b2bService: B2BService) {}
|
||||
|
||||
@Post('inquiries')
|
||||
@ApiOperation({ summary: 'ثبت درخواست همکاری عمده (B2B)' })
|
||||
createInquiry(
|
||||
@Body()
|
||||
body: {
|
||||
companyName: string;
|
||||
contactName: string;
|
||||
email: string;
|
||||
phone: string;
|
||||
businessType: string;
|
||||
estimatedVolume?: string;
|
||||
message: string;
|
||||
},
|
||||
) {
|
||||
return this.b2bService.createInquiry(body);
|
||||
}
|
||||
|
||||
@UseGuards(JwtAuthGuard, RolesGuard)
|
||||
@Roles('Admin')
|
||||
@ApiBearerAuth()
|
||||
@Get('inquiries')
|
||||
@ApiOperation({
|
||||
summary: 'دریافت لیست درخواستهای همکاری B2B (نیازمند ادمین)',
|
||||
})
|
||||
findAllInquiries() {
|
||||
return this.b2bService.findAllInquiries();
|
||||
}
|
||||
|
||||
@UseGuards(JwtAuthGuard, RolesGuard)
|
||||
@Roles('Admin')
|
||||
@ApiBearerAuth()
|
||||
@Patch('inquiries/:id')
|
||||
@ApiOperation({
|
||||
summary: 'بررسی و تغییر وضعیت درخواست همکاری (نیازمند ادمین)',
|
||||
})
|
||||
updateInquiryStatus(
|
||||
@Param('id') id: string,
|
||||
@Body() body: { status: string; adminNotes?: string },
|
||||
) {
|
||||
return this.b2bService.updateInquiryStatus(id, body);
|
||||
}
|
||||
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@ApiBearerAuth()
|
||||
@Get('partner')
|
||||
@ApiOperation({ summary: 'دریافت اطلاعات حساب همکار تجاری' })
|
||||
getPartnerProfile(@Req() req: any) {
|
||||
const userId = req.user.id || req.user.userId;
|
||||
return this.b2bService.getPartnerProfile(userId);
|
||||
}
|
||||
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@ApiBearerAuth()
|
||||
@Post('orders')
|
||||
@ApiOperation({ summary: 'ثبت سفارش عمدهفروشی B2B' })
|
||||
createWholesaleOrder(
|
||||
@Req() req: any,
|
||||
@Body() body: { items: any[]; totalAmount: number },
|
||||
) {
|
||||
const userId = req.user.id || req.user.userId;
|
||||
return this.b2bService.createWholesaleOrder(userId, body);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { B2BController } from './b2b.controller';
|
||||
import { B2BService } from './b2b.service';
|
||||
import { PrismaModule } from '../prisma/prisma.module';
|
||||
|
||||
@Module({
|
||||
imports: [PrismaModule],
|
||||
controllers: [B2BController],
|
||||
providers: [B2BService],
|
||||
exports: [B2BService],
|
||||
})
|
||||
export class B2BModule {}
|
||||
@@ -0,0 +1,89 @@
|
||||
import {
|
||||
Injectable,
|
||||
NotFoundException,
|
||||
ForbiddenException,
|
||||
} from '@nestjs/common';
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
|
||||
@Injectable()
|
||||
export class B2BService {
|
||||
constructor(private prisma: PrismaService) {}
|
||||
|
||||
async createInquiry(data: {
|
||||
companyName: string;
|
||||
contactName: string;
|
||||
email: string;
|
||||
phone: string;
|
||||
businessType: string;
|
||||
estimatedVolume?: string;
|
||||
message: string;
|
||||
}) {
|
||||
return this.prisma.b2BInquiry.create({
|
||||
data: {
|
||||
...data,
|
||||
status: 'PENDING',
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async findAllInquiries() {
|
||||
return this.prisma.b2BInquiry.findMany({
|
||||
orderBy: { createdAt: 'desc' },
|
||||
});
|
||||
}
|
||||
|
||||
async updateInquiryStatus(
|
||||
id: string,
|
||||
data: { status: string; adminNotes?: string },
|
||||
) {
|
||||
const item = await this.prisma.b2BInquiry.findUnique({ where: { id } });
|
||||
if (!item) {
|
||||
throw new NotFoundException(`B2BInquiry with ID ${id} not found`);
|
||||
}
|
||||
return this.prisma.b2BInquiry.update({
|
||||
where: { id },
|
||||
data,
|
||||
});
|
||||
}
|
||||
|
||||
async getPartnerProfile(userId: string) {
|
||||
const partner = await this.prisma.partnerAccount.findUnique({
|
||||
where: { userId },
|
||||
include: { user: true },
|
||||
});
|
||||
if (!partner) {
|
||||
throw new NotFoundException(
|
||||
`Partner account for user ${userId} not found`,
|
||||
);
|
||||
}
|
||||
return partner;
|
||||
}
|
||||
|
||||
async createWholesaleOrder(
|
||||
userId: string,
|
||||
data: { items: any[]; totalAmount: number },
|
||||
) {
|
||||
const partner = await this.prisma.partnerAccount.findUnique({
|
||||
where: { userId },
|
||||
});
|
||||
if (!partner || partner.status !== 'ACTIVE') {
|
||||
throw new ForbiddenException('User is not an active wholesale partner');
|
||||
}
|
||||
|
||||
return this.prisma.order.create({
|
||||
data: {
|
||||
userId,
|
||||
totalAmount: data.totalAmount,
|
||||
status: 'processing',
|
||||
isRefill: false,
|
||||
orderItems: {
|
||||
create: data.items.map((item) => ({
|
||||
productId: item.productId,
|
||||
quantity: item.quantity,
|
||||
})),
|
||||
},
|
||||
},
|
||||
include: { orderItems: true },
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user