Files
fabric_trade_server/dist/notifications/notifications.service.js
T
mohmdsdgandCursor d4aaa8e997 Rebuild order flow: settle after pricing, notifications, and invoice expiry.
Hide prices until admin sends the invoice, enforce status transitions, add customer/admin SMS+in-app notifications, schedule cash invoice reminders, and ship updated dist/prisma-client/deploy-bundle for git-pull deploys.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-08-09 12:00:13 +03:30

185 lines
7.0 KiB
JavaScript

"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);
};
var NotificationsService_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.NotificationsService = void 0;
const common_1 = require("@nestjs/common");
const config_1 = require("@nestjs/config");
const client_1 = require("@prisma/client");
const prisma_service_1 = require("../prisma/prisma.service");
const sms_service_1 = require("../sms/sms.service");
const notification_events_1 = require("./notification-events");
let NotificationsService = NotificationsService_1 = class NotificationsService {
prisma;
sms;
config;
logger = new common_1.Logger(NotificationsService_1.name);
constructor(prisma, sms, config) {
this.prisma = prisma;
this.sms = sms;
this.config = config;
}
async notifyOrderEvent(orderId, event, options = {}) {
try {
const order = await this.prisma.order.findUnique({
where: { id: orderId },
select: {
id: true,
orderNumber: true,
userId: true,
finalAmount: true,
totalAmount: true,
user: { select: { phone: true } },
},
});
if (!order)
return;
const template = notification_events_1.CUSTOMER_EVENTS[event];
const ctx = {
orderNumber: order.orderNumber,
appName: this.appName(),
amount: options.amount ?? order.finalAmount ?? order.totalAmount ?? undefined,
note: options.note ?? null,
carrier: options.carrier ?? null,
trackingNumber: options.trackingNumber ?? null,
};
const notification = await this.prisma.customerNotification.create({
data: {
customerId: order.userId,
orderId: order.id,
event,
title: template.title,
body: template.body(ctx),
tone: template.tone,
link: `/orders/${order.id}`,
},
});
if (!template.sms || !template.smsSwitch)
return;
const enabled = await this.isSmsEventEnabled(template.smsSwitch);
if (!enabled || !this.sms.isEnabled())
return;
const result = await this.sms.send(order.user.phone, template.sms(ctx));
if (result.success) {
await this.prisma.customerNotification.update({
where: { id: notification.id },
data: { smsSent: true },
});
}
}
catch (err) {
this.logger.error(`notifyOrderEvent(${event}) failed for order ${orderId}`, err instanceof Error ? err.stack : err);
}
}
dispatchOrderEvent(orderId, event, options = {}) {
void this.notifyOrderEvent(orderId, event, options);
}
async notifyAdmin(input) {
try {
await this.prisma.adminNotification.create({
data: {
title: input.title,
body: input.body,
tone: input.tone ?? client_1.NotificationTone.info,
link: input.link ?? null,
},
});
}
catch (err) {
this.logger.error('notifyAdmin failed', err instanceof Error ? err.stack : err);
}
}
dispatchAdmin(input) {
void this.notifyAdmin(input);
}
async listForCustomer(customerId, limit = 50) {
const rows = await this.prisma.customerNotification.findMany({
where: { customerId },
orderBy: { sentAt: 'desc' },
take: limit,
});
return rows.map((n) => ({
id: n.id,
orderId: n.orderId ?? undefined,
event: n.event,
title: n.title,
body: n.body,
tone: n.tone ?? client_1.NotificationTone.info,
link: n.link ?? undefined,
isRead: n.isRead,
sentAt: n.sentAt.toISOString(),
}));
}
async unreadCount(customerId) {
const count = await this.prisma.customerNotification.count({
where: { customerId, isRead: false },
});
return { count };
}
async markRead(customerId, id) {
const existing = await this.prisma.customerNotification.findFirst({
where: { id, customerId },
select: { id: true },
});
if (!existing)
throw new common_1.NotFoundException('اعلان یافت نشد');
await this.prisma.customerNotification.update({
where: { id },
data: { isRead: true },
});
return this.unreadCount(customerId);
}
async markAllRead(customerId) {
await this.prisma.customerNotification.updateMany({
where: { customerId, isRead: false },
data: { isRead: true },
});
return { count: 0 };
}
async markAdminRead(id) {
await this.prisma.adminNotification.updateMany({
where: { id },
data: { isRead: true },
});
return { ok: true };
}
async markAllAdminRead() {
await this.prisma.adminNotification.updateMany({
where: { isRead: false },
data: { isRead: true },
});
return { ok: true };
}
appName() {
return this.config.get('SMS_APP_NAME', 'پارچه') ?? 'پارچه';
}
async isSmsEventEnabled(key) {
const settings = await this.prisma.systemSettings.findUnique({
where: { id: 'default' },
select: {
smsOrderRegistered: true,
smsPricingReady: true,
smsPaymentReminder: true,
smsCheckReview: true,
smsShippingUpdate: true,
},
});
return settings ? settings[key] : true;
}
};
exports.NotificationsService = NotificationsService;
exports.NotificationsService = NotificationsService = NotificationsService_1 = __decorate([
(0, common_1.Injectable)(),
__metadata("design:paramtypes", [prisma_service_1.PrismaService,
sms_service_1.SmsService,
config_1.ConfigService])
], NotificationsService);
//# sourceMappingURL=notifications.service.js.map