diff --git a/Utils/rejectKyc.js b/Utils/rejectKyc.js index afbd015..32260b8 100644 --- a/Utils/rejectKyc.js +++ b/Utils/rejectKyc.js @@ -2,7 +2,7 @@ import SalesCoOrdinator from "../resources/SalesCoOrdinators/SalesCoOrdinatorMod import TerritoryManager from "../resources/TerritoryManagers/TerritoryManagerModel.js"; import { sendPushNotification } from "./sendPushNotification.js"; -export const rejectKYC = async (userId, reason) => { +export const rejectKYC = async (userId, title, message) => { // Try to find the user in SalesCoordinator model console.log(userId); let user = await SalesCoOrdinator.findById(userId); @@ -18,8 +18,8 @@ export const rejectKYC = async (userId, reason) => { console.log(userToken); if (userToken) { // Send the push notification - const message = `Your KYC has been rejected. Reason: ${reason}`; - await sendPushNotification(userToken, message); + // const message = `Your KYC has been rejected. Reason: ${reason}`; + await sendPushNotification(userToken, title, 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 6cebb9d..6082800 100644 --- a/Utils/sendPushNotification.js +++ b/Utils/sendPushNotification.js @@ -1,6 +1,6 @@ import admin from "firebase-admin"; import serviceAccount from "../googlefirebasePushnotification.json" with { type: "json" }; -export const sendPushNotification = async (userToken, message) => { +export const sendPushNotification = async (userToken,title, message) => { // const admin = require("firebase-admin"); // const serviceAccount = require("./path/to/your-firebase-adminsdk.json"); @@ -12,7 +12,7 @@ export const sendPushNotification = async (userToken, message) => { const payload = { notification: { - title: "KYC Rejected ", + title: title, body: message, }, token: userToken diff --git a/resources/KYC/KycController.js b/resources/KYC/KycController.js index 7cb0f0d..0319510 100644 --- a/resources/KYC/KycController.js +++ b/resources/KYC/KycController.js @@ -203,26 +203,45 @@ export const updateKycStatus = async (req, res) => { if (kyc.principal_distributer.toString() !== req.user._id.toString()) { return res.status(403).json({ message: "Access denied" }); } - + const trade_name = kyc.trade_name; + if (status === "approved") { + kyc.status = status; + await rejectKYC( + kyc.addedBy, + "KYC Approved", + `Your KYC for ${trade_name} has been approved.` + ); + await Notification.create({ + title: "KYC Approved", + msg: `Your KYC for ${trade_name} has been approved.`, + kyc_ref: kyc._id, + added_for: kyc.addedBy, + // userType: req.userType, + }); + } if (status === "reject") { - await rejectKYC(kyc.addedBy, rejectionReason); + await rejectKYC( + kyc.addedBy, + "KYC application Rejected", + `KYC for ${trade_name} has been rejected. Reason: ${rejectionReason}` + ); await Notification.create({ title: "KYC Rejected", - msg: rejectionReason, + msg: `KYC for ${trade_name} has been rejected. Reason: ${rejectionReason}`, kyc_ref: kyc._id, added_for: kyc.addedBy, // userType: req.userType, }); } // Update the status - if (status) { - kyc.status = status; - } + // if (status) { + // kyc.status = status; + // } // Add rejection reason to notes if status is reject if (kyc.status === "reject" || status === "reject") { // kyc.rejection_reason = rejectionReason; - + kyc.status = status; kyc.notes.push({ message: rejectionReason, user: user,