This commit is contained in:
Sibunnayak 2024-08-08 16:05:42 +05:30
commit 1c4fce921c
3 changed files with 31 additions and 12 deletions

View File

@ -2,7 +2,7 @@ import SalesCoOrdinator from "../resources/SalesCoOrdinators/SalesCoOrdinatorMod
import TerritoryManager from "../resources/TerritoryManagers/TerritoryManagerModel.js"; import TerritoryManager from "../resources/TerritoryManagers/TerritoryManagerModel.js";
import { sendPushNotification } from "./sendPushNotification.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 // Try to find the user in SalesCoordinator model
console.log(userId); console.log(userId);
let user = await SalesCoOrdinator.findById(userId); let user = await SalesCoOrdinator.findById(userId);
@ -18,8 +18,8 @@ export const rejectKYC = async (userId, reason) => {
console.log(userToken); console.log(userToken);
if (userToken) { if (userToken) {
// Send the push notification // Send the push notification
const message = `Your KYC has been rejected. Reason: ${reason}`; // const message = `Your KYC has been rejected. Reason: ${reason}`;
await sendPushNotification(userToken, message); await sendPushNotification(userToken, title, message);
console.log("sent to device "); console.log("sent to device ");
} else { } else {
console.error("No FCM token found for user:", userId); console.error("No FCM token found for user:", userId);

View File

@ -1,6 +1,6 @@
import admin from "firebase-admin"; import admin from "firebase-admin";
import serviceAccount from "../googlefirebasePushnotification.json" with { type: "json" }; 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 admin = require("firebase-admin");
// const serviceAccount = require("./path/to/your-firebase-adminsdk.json"); // const serviceAccount = require("./path/to/your-firebase-adminsdk.json");
@ -12,7 +12,7 @@ export const sendPushNotification = async (userToken, message) => {
const payload = { const payload = {
notification: { notification: {
title: "KYC Rejected ", title: title,
body: message, body: message,
}, },
token: userToken token: userToken

View File

@ -203,26 +203,45 @@ export const updateKycStatus = async (req, res) => {
if (kyc.principal_distributer.toString() !== req.user._id.toString()) { if (kyc.principal_distributer.toString() !== req.user._id.toString()) {
return res.status(403).json({ message: "Access denied" }); 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") { 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({ await Notification.create({
title: "KYC Rejected", title: "KYC Rejected",
msg: rejectionReason, msg: `KYC for ${trade_name} has been rejected. Reason: ${rejectionReason}`,
kyc_ref: kyc._id, kyc_ref: kyc._id,
added_for: kyc.addedBy, added_for: kyc.addedBy,
// userType: req.userType, // userType: req.userType,
}); });
} }
// Update the status // Update the status
if (status) { // if (status) {
kyc.status = status; // kyc.status = status;
} // }
// Add rejection reason to notes if status is reject // Add rejection reason to notes if status is reject
if (kyc.status === "reject" || status === "reject") { if (kyc.status === "reject" || status === "reject") {
// kyc.rejection_reason = rejectionReason; // kyc.rejection_reason = rejectionReason;
kyc.status = status;
kyc.notes.push({ kyc.notes.push({
message: rejectionReason, message: rejectionReason,
user: user, user: user,