updated savefcm
This commit is contained in:
parent
bd884a4e93
commit
7879774886
@ -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);
|
||||
}
|
||||
|
@ -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);
|
||||
|
@ -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");
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
);
|
||||
|
Loading…
Reference in New Issue
Block a user