Spaces:
Sleeping
Sleeping
Download controller/notificationController.js from Mahmoud7456/MultiUsersSalesSystem: direct link, hf CLI and curl.
- Browser
- Download file 5.55 kB
-
https://huggingface.co/spaces/Mahmoud7456/MultiUsersSalesSystem/resolve/main/controller/notificationController.js
- Command line
-
hf download hf://spaces/Mahmoud7456/MultiUsersSalesSystem/controller/notificationController.js
-
curl -L -o notificationController.js https://huggingface.co/spaces/Mahmoud7456/MultiUsersSalesSystem/resolve/main/controller/notificationController.js
5.55 kB
| import Notification from "../models/NotificationModel.js"; | |
| import WhatsAppService from "../utils/whatsappService.js"; | |
| import { catchAsync } from "../utils/catchAsync.js"; | |
| // Get all notifications | |
| export const getAllNotifications = catchAsync(async (req, res) => { | |
| const { tenantId } = req.params; | |
| const { status, invoiceId, page = 1, limit = 20 } = req.query; | |
| const filter = { tenant: tenantId }; | |
| if (status) filter.status = status; | |
| if (invoiceId) filter.invoice = invoiceId; | |
| const skip = (page - 1) * limit; | |
| const notifications = await Notification.find(filter) | |
| .populate("invoice", "invoiceNumber totalAmount status") | |
| .sort({ createdAt: -1 }) | |
| .skip(skip) | |
| .limit(Number(limit)); | |
| const total = await Notification.countDocuments(filter); | |
| res.status(200).json({ | |
| status: "success", | |
| data: notifications, | |
| pagination: { | |
| total, | |
| page: Number(page), | |
| pages: Math.ceil(total / limit), | |
| }, | |
| }); | |
| }); | |
| // Get notification by ID | |
| export const getNotificationById = catchAsync(async (req, res) => { | |
| const { id } = req.params; | |
| const notification = await Notification.findById(id).populate("invoice"); | |
| if (!notification) { | |
| return res.status(404).json({ | |
| status: "error", | |
| message: "الإشعار غير موجود", | |
| }); | |
| } | |
| res.status(200).json({ | |
| status: "success", | |
| data: notification, | |
| }); | |
| }); | |
| // Send notification manually | |
| export const sendNotification = catchAsync(async (req, res) => { | |
| const { id } = req.params; | |
| const notification = await Notification.findById(id); | |
| if (!notification) { | |
| return res.status(404).json({ | |
| status: "error", | |
| message: "الإشعار غير موجود", | |
| }); | |
| } | |
| if (notification.status !== "queued") { | |
| return res.status(400).json({ | |
| status: "error", | |
| message: "يمكن فقط إرسال الإشعارات المعلقة", | |
| }); | |
| } | |
| try { | |
| await WhatsAppService.sendWhatsAppMessage(notification); | |
| res.status(200).json({ | |
| status: "success", | |
| message: "تم إرسال الإشعار بنجاح", | |
| data: notification, | |
| }); | |
| } catch (error) { | |
| console.error("Send notification error:", error); | |
| res.status(500).json({ | |
| status: "error", | |
| message: "فشل إرسال الإشعار", | |
| }); | |
| } | |
| }); | |
| // Send all queued notifications | |
| export const sendQueuedNotifications = catchAsync(async (req, res) => { | |
| try { | |
| const result = await WhatsAppService.sendQueuedNotifications(); | |
| res.status(200).json({ | |
| status: "success", | |
| message: `تم معالجة ${result.processed} إشعار`, | |
| data: result, | |
| }); | |
| } catch (error) { | |
| console.error("Send queued notifications error:", error); | |
| res.status(500).json({ | |
| status: "error", | |
| message: "فشلت معالجة الإشعارات", | |
| }); | |
| } | |
| }); | |
| // Get order notifications | |
| export const getInvoiceNotifications = catchAsync(async (req, res) => { | |
| const { tenantId, invoiceId } = req.params; | |
| const notifications = await Notification.find({ | |
| tenant: tenantId, | |
| invoice: invoiceId, | |
| }).sort({ createdAt: -1 }); | |
| res.status(200).json({ | |
| status: "success", | |
| data: notifications, | |
| }); | |
| }); | |
| // Delete notification | |
| export const deleteNotification = catchAsync(async (req, res) => { | |
| const { id } = req.params; | |
| const notification = await Notification.findByIdAndDelete(id); | |
| if (!notification) { | |
| return res.status(404).json({ | |
| status: "error", | |
| message: "الإشعار غير موجود", | |
| }); | |
| } | |
| res.status(200).json({ | |
| status: "success", | |
| message: "تم حذف الإشعار بنجاح", | |
| }); | |
| }); | |
| // Retry failed notification | |
| export const retryNotification = catchAsync(async (req, res) => { | |
| const { id } = req.params; | |
| const notification = await Notification.findById(id); | |
| if (!notification) { | |
| return res.status(404).json({ | |
| status: "error", | |
| message: "الإشعار غير موجود", | |
| }); | |
| } | |
| if (notification.status !== "failed") { | |
| return res.status(400).json({ | |
| status: "error", | |
| message: "يمكن فقط إعادة محاولة الإشعارات الفاشلة", | |
| }); | |
| } | |
| // Reset for retry | |
| notification.status = "queued"; | |
| notification.retryCount = 0; | |
| notification.errorMessage = null; | |
| await notification.save(); | |
| try { | |
| await WhatsAppService.sendWhatsAppMessage(notification); | |
| res.status(200).json({ | |
| status: "success", | |
| message: "تم إعادة محاولة الإشعار", | |
| data: notification, | |
| }); | |
| } catch (error) { | |
| console.error("Retry notification error:", error); | |
| res.status(500).json({ | |
| status: "error", | |
| message: "فشلت إعادة المحاولة", | |
| }); | |
| } | |
| }); | |
| // Get notification statistics | |
| export const getNotificationStats = catchAsync(async (req, res) => { | |
| const { tenantId } = req.params; | |
| const stats = await Notification.aggregate([ | |
| { $match: { tenant: tenantId } }, | |
| { | |
| $group: { | |
| _id: "$status", | |
| count: { $sum: 1 }, | |
| }, | |
| }, | |
| ]); | |
| const formattedStats = { | |
| queued: 0, | |
| sent: 0, | |
| delivered: 0, | |
| failed: 0, | |
| }; | |
| stats.forEach((stat) => { | |
| formattedStats[stat._id] = stat.count; | |
| }); | |
| res.status(200).json({ | |
| status: "success", | |
| data: formattedStats, | |
| }); | |
| }); | |