Files
fabric_trade_server/dist/checks/checks.service.js
T

124 lines
5.3 KiB
JavaScript
Raw Normal View History

"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ChecksService = void 0;
const common_1 = require("@nestjs/common");
const client_1 = require("@prisma/client");
const prisma_service_1 = require("../prisma/prisma.service");
const notifications_service_1 = require("../notifications/notifications.service");
let ChecksService = class ChecksService {
prisma;
notifications;
constructor(prisma, notifications) {
this.prisma = prisma;
this.notifications = notifications;
}
async findByOrder(orderId) {
const order = await this.prisma.order.findUnique({
where: { id: orderId },
});
if (!order)
throw new common_1.NotFoundException('سفارش یافت نشد');
const checks = await this.prisma.check.findMany({
where: { orderId },
orderBy: { row: 'asc' },
});
return checks.map((c) => this.map(c));
}
async updateStatus(id, adminId, dto) {
const check = await this.prisma.check.findUnique({
where: { id },
include: { order: { include: { checks: true } } },
});
if (!check)
throw new common_1.NotFoundException('چک یافت نشد');
let notifyStatus = null;
const updated = await this.prisma.$transaction(async (tx) => {
const result = await tx.check.update({
where: { id },
data: {
status: dto.status,
notes: dto.note
? [check.notes, dto.note].filter(Boolean).join('\n')
: check.notes,
},
});
const siblings = await tx.check.findMany({
where: { orderId: check.orderId },
});
const allApproved = siblings.every((c) => c.status === client_1.CheckStatus.approved);
const anyRejected = siblings.some((c) => c.status === client_1.CheckStatus.rejected);
let nextStatus = null;
if (anyRejected) {
nextStatus = client_1.OrderStatus.check_needs_fix;
}
else if (allApproved) {
nextStatus = client_1.OrderStatus.checks_approved;
}
if (nextStatus && nextStatus !== check.order.status) {
await tx.order.update({
where: { id: check.orderId },
data: { status: nextStatus },
});
await tx.orderStatusHistory.create({
data: {
orderId: check.orderId,
fromStatus: check.order.status,
toStatus: nextStatus,
changedBy: `admin:${adminId}`,
note: dto.note || `وضعیت چک به ${dto.status} تغییر کرد`,
},
});
notifyStatus = nextStatus;
}
return result;
});
if (notifyStatus === client_1.OrderStatus.checks_approved) {
this.notifications.dispatchOrderEvent(check.orderId, 'checks_approved');
}
else if (notifyStatus === client_1.OrderStatus.check_needs_fix) {
this.notifications.dispatchOrderEvent(check.orderId, 'checks_need_fix', {
note: dto.note ?? null,
});
}
return this.map(updated);
}
map(c) {
return {
id: c.id,
orderId: c.orderId,
row: c.row,
amount: c.amount,
dueDate: c.dueDate.toISOString(),
sayadId: c.sayadId ?? '',
checkNumber: c.checkNumber ?? undefined,
ownerNationalId: c.ownerNationalId ?? undefined,
ownerFirstName: c.ownerFirstName ?? undefined,
ownerLastName: c.ownerLastName ?? undefined,
bankName: c.bankName ?? '',
branchName: c.branchName ?? undefined,
frontImage: c.frontImage ?? undefined,
backImage: c.backImage ?? undefined,
notes: c.notes ?? undefined,
mode: c.mode,
status: c.status,
createdAt: c.createdAt.toISOString(),
updatedAt: c.updatedAt.toISOString(),
};
}
};
exports.ChecksService = ChecksService;
exports.ChecksService = ChecksService = __decorate([
(0, common_1.Injectable)(),
__metadata("design:paramtypes", [prisma_service_1.PrismaService,
notifications_service_1.NotificationsService])
], ChecksService);
//# sourceMappingURL=checks.service.js.map