44 lines
1.1 KiB
TypeScript
44 lines
1.1 KiB
TypeScript
import { Injectable, NotFoundException } from '@nestjs/common';
|
|
import { PrismaService } from '../prisma/prisma.service';
|
|
import { UpdateOrderStatusDto } from './dto/update-order-status.dto';
|
|
|
|
@Injectable()
|
|
export class OrdersService {
|
|
constructor(private readonly prisma: PrismaService) {}
|
|
|
|
list() {
|
|
return this.prisma.order.findMany({
|
|
orderBy: { createdAt: 'desc' },
|
|
select: {
|
|
id: true,
|
|
customerName: true,
|
|
customerPhone: true,
|
|
itemCount: true,
|
|
status: true,
|
|
createdAt: true,
|
|
items: true,
|
|
},
|
|
});
|
|
}
|
|
|
|
async findOne(id: number) {
|
|
const order = await this.prisma.order.findUnique({
|
|
where: { id },
|
|
include: { items: true },
|
|
});
|
|
if (!order) {
|
|
throw new NotFoundException('سفارش یافت نشد');
|
|
}
|
|
return order;
|
|
}
|
|
|
|
async updateStatus(id: number, dto: UpdateOrderStatusDto) {
|
|
await this.findOne(id);
|
|
return this.prisma.order.update({
|
|
where: { id },
|
|
data: { status: dto.status },
|
|
include: { items: true },
|
|
});
|
|
}
|
|
}
|