diff --git a/Utils/rejectKyc.js b/Utils/rejectKyc.js index 2bb5eb2..afbd015 100644 --- a/Utils/rejectKyc.js +++ b/Utils/rejectKyc.js @@ -4,19 +4,23 @@ import { sendPushNotification } from "./sendPushNotification.js"; export const rejectKYC = async (userId, reason) => { // Try to find the user in SalesCoordinator model + console.log(userId); let user = await SalesCoOrdinator.findById(userId); if (!user) { // If not found, try to find the user in TerritoryManager model user = await TerritoryManager.findById(userId); } - + console.log(user); // Get the user's FCM token const userToken = user ? user.fcm_token : null; - + // const userToken = + // "dRnjl8F3S8GA6_BnBfloWZ:APA91bFvuiA4pEQr03Kymqtw2N207VDHwzLlfz_OPWzhTtdAAWmPLF8cQSx0WYmiaL9g-PIbzvGrmzDzxNiyq58w9Gws6p2tDlDeqycqU17W74gi36xkGSUqlzNiFoTiDDNp7OFDdVPK"; + console.log(userToken); if (userToken) { // Send the push notification const message = `Your KYC has been rejected. Reason: ${reason}`; await sendPushNotification(userToken, message); + console.log("sent to device "); } else { console.error("No FCM token found for user:", userId); } diff --git a/Utils/sendPushNotification.js b/Utils/sendPushNotification.js index 66a5037..6cebb9d 100644 --- a/Utils/sendPushNotification.js +++ b/Utils/sendPushNotification.js @@ -19,7 +19,7 @@ export const sendPushNotification = async (userToken, message) => { }; try { - const response = await admin.messaging().send(message); + const response = await admin.messaging().send(payload); console.log("Successfully sent message:", response); } catch (error) { console.error("Error sending message:", error); diff --git a/resources/KYC/KycController.js b/resources/KYC/KycController.js index c26db50..7cb0f0d 100644 --- a/resources/KYC/KycController.js +++ b/resources/KYC/KycController.js @@ -205,13 +205,13 @@ export const updateKycStatus = async (req, res) => { } if (status === "reject") { - await rejectKYC(req.user._id, rejectionReason); + await rejectKYC(kyc.addedBy, rejectionReason); await Notification.create({ title: "KYC Rejected", msg: rejectionReason, kyc_ref: kyc._id, - added_for: req.user._id, - userType: req.userType, + added_for: kyc.addedBy, + // userType: req.userType, }); } // Update the status @@ -271,7 +271,7 @@ export const saveFCMTokenForSC = async (req, res) => { } // Check if the new FCM token is different from the current one - if (user.fcm_token === fcmToken) { + if (user.fcm_token && user.fcm_token === fcmToken) { return res.status(200).send("FCM Token is already up to date"); } @@ -299,7 +299,7 @@ export const saveFCMTokenForTM = async (req, res) => { } // Check if the new FCM token is different from the current one - if (user.fcm_token === fcmToken) { + if (user.fcm_token && user.fcm_token === fcmToken) { return res.status(200).send("FCM Token is already up to date"); } diff --git a/resources/Notification/notificationModal.js b/resources/Notification/notificationModal.js index 15cceb8..65a44ae 100644 --- a/resources/Notification/notificationModal.js +++ b/resources/Notification/notificationModal.js @@ -16,14 +16,14 @@ const notificationSchema = new Schema( }, added_for: { type: mongoose.Schema.Types.ObjectId, - refPath: "userType", - required: true, // Assuming this should be required - }, - userType: { - type: String, - required: true, // Assuming this should be required - enum: ["SalesCoOrdinator", "TerritoryManager"], + // refPath: "KYC", + // required: true, // Assuming this should be required }, + // userType: { + // type: String, + // required: true, // Assuming this should be required + // enum: ["SalesCoOrdinator", "TerritoryManager"], + // }, }, { timestamps: true } // Adds createdAt and updatedAt timestamps );