102 lines
2.3 KiB
TypeScript
102 lines
2.3 KiB
TypeScript
import {
|
|
Injectable,
|
|
NotFoundException,
|
|
ForbiddenException,
|
|
} from '@nestjs/common';
|
|
import { PrismaService } from '../prisma/prisma.service';
|
|
|
|
export interface B2BWholesaleOrderItem {
|
|
productId: string;
|
|
quantity: number;
|
|
}
|
|
|
|
@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: B2BWholesaleOrderItem[]; 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 },
|
|
});
|
|
}
|
|
|
|
async getAllPartnerAccounts() {
|
|
return this.prisma.partnerAccount.findMany({
|
|
include: { user: true },
|
|
orderBy: { createdAt: 'desc' },
|
|
});
|
|
}
|
|
}
|