Skip to content

Commit 1f0fcd1

Browse files
author
Nagarakanti Kiran
authored
Update notificationHelpers.js
Before: Using require() for modern modules that don't support it After: Switched to dynamic import() which works with all module types
1 parent 8d36f9b commit 1f0fcd1

1 file changed

Lines changed: 45 additions & 18 deletions

File tree

server/utills/notificationHelpers.js

Lines changed: 45 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,21 @@
11
const { PrismaClient } = require('@prisma/client');
2-
const { nanoid } = require('nanoid');
32

43
const prisma = new PrismaClient();
54

5+
/**
6+
* Generate ID using nanoid with dynamic import
7+
*/
8+
const generateId = async () => {
9+
try {
10+
const { nanoid } = await import('nanoid');
11+
return nanoid();
12+
} catch (error) {
13+
console.error('Error generating nanoid:', error);
14+
// Fallback ID generation
15+
return Math.random().toString(36).substr(2, 10);
16+
}
17+
};
18+
619
/**
720
* Create an order update notification
821
*/
@@ -47,9 +60,11 @@ const createOrderUpdateNotification = async (userId, orderStatus, orderId, total
4760
priority: 'NORMAL'
4861
};
4962

63+
const notificationId = await generateId();
64+
5065
const notification = await prisma.notification.create({
5166
data: {
52-
id: nanoid(),
67+
id: notificationId,
5368
userId: userId,
5469
title: statusInfo.title,
5570
message: statusInfo.message,
@@ -101,9 +116,11 @@ const createPaymentNotification = async (userId, paymentStatus, amount, orderId)
101116
priority: 'NORMAL'
102117
};
103118

119+
const notificationId = await generateId();
120+
104121
const notification = await prisma.notification.create({
105122
data: {
106-
id: nanoid(),
123+
id: notificationId,
107124
userId: userId,
108125
title: statusInfo.title,
109126
message: statusInfo.message,
@@ -131,9 +148,11 @@ const createPaymentNotification = async (userId, paymentStatus, amount, orderId)
131148
*/
132149
const createPromotionNotification = async (userId, title, message, promoCode = null, discount = null) => {
133150
try {
151+
const notificationId = await generateId();
152+
134153
const notification = await prisma.notification.create({
135154
data: {
136-
id: nanoid(),
155+
id: notificationId,
137156
userId: userId,
138157
title: title,
139158
message: message,
@@ -160,9 +179,11 @@ const createPromotionNotification = async (userId, title, message, promoCode = n
160179
*/
161180
const createSystemAlertNotification = async (userId, title, message, priority = 'HIGH') => {
162181
try {
182+
const notificationId = await generateId();
183+
163184
const notification = await prisma.notification.create({
164185
data: {
165-
id: nanoid(),
186+
id: notificationId,
166187
userId: userId,
167188
title: title,
168189
message: message,
@@ -188,23 +209,29 @@ const createSystemAlertNotification = async (userId, title, message, priority =
188209
*/
189210
const createBulkNotifications = async (userIds, title, message, type = 'SYSTEM_ALERT', priority = 'NORMAL', metadata = {}) => {
190211
try {
191-
const notifications = userIds.map(userId => ({
192-
id: nanoid(),
193-
userId: userId,
194-
title: title,
195-
message: message,
196-
type: type,
197-
priority: priority,
198-
isRead: false,
199-
metadata: metadata
200-
}));
212+
// Generate all IDs first
213+
const notificationData = await Promise.all(
214+
userIds.map(async (userId) => {
215+
const notificationId = await generateId();
216+
return {
217+
id: notificationId,
218+
userId: userId,
219+
title: title,
220+
message: message,
221+
type: type,
222+
priority: priority,
223+
isRead: false,
224+
metadata: metadata
225+
};
226+
})
227+
);
201228

202229
await prisma.notification.createMany({
203-
data: notifications
230+
data: notificationData
204231
});
205232

206233
console.log(`✅ Bulk notifications created for ${userIds.length} users: ${title}`);
207-
return notifications.length;
234+
return notificationData.length;
208235
} catch (error) {
209236
console.error('❌ Error creating bulk notifications:', error);
210237
throw error;
@@ -217,4 +244,4 @@ module.exports = {
217244
createPromotionNotification,
218245
createSystemAlertNotification,
219246
createBulkNotifications
220-
};
247+
};

0 commit comments

Comments
 (0)