fcm token for RD
This commit is contained in:
parent
5cb19705eb
commit
1ec1a81033
@ -6,6 +6,7 @@ import { RdOrder } from "./rdOrderModal.js";
|
|||||||
import { PDStock } from "../Stock/PdStockModel.js";
|
import { PDStock } from "../Stock/PdStockModel.js";
|
||||||
import { createKYC } from "../../Utils/rejectKyc.js";
|
import { createKYC } from "../../Utils/rejectKyc.js";
|
||||||
import { Notification } from "../Notification/notificationModal.js";
|
import { Notification } from "../Notification/notificationModal.js";
|
||||||
|
import { sendPushNotification } from "../../Utils/sendPushNotification.js";
|
||||||
|
|
||||||
// Controller to create a new order by RD
|
// Controller to create a new order by RD
|
||||||
export const createOrderRD = async (req, res) => {
|
export const createOrderRD = async (req, res) => {
|
||||||
@ -496,9 +497,30 @@ export const processOrder = async (req, res) => {
|
|||||||
|
|
||||||
// Only update order status if all items have been fully processed
|
// Only update order status if all items have been fully processed
|
||||||
if (allItemsProcessed) {
|
if (allItemsProcessed) {
|
||||||
order.status = "processing"; // All items are fully processed
|
order.status = "processing";
|
||||||
|
await sendPushNotification(
|
||||||
|
order.addedBy?.fcm_token,
|
||||||
|
"Exciting news",
|
||||||
|
"Your order got processed"
|
||||||
|
);
|
||||||
|
await Notification.create({
|
||||||
|
title: "Exciting news",
|
||||||
|
msg: `Your order got processed`,
|
||||||
|
added_for: order.addedBy?._id,
|
||||||
|
});
|
||||||
|
// All items are fully processed
|
||||||
} else {
|
} else {
|
||||||
order.status = "pending"; // There are still remaining quantities
|
order.status = "pending";
|
||||||
|
await sendPushNotification(
|
||||||
|
order.addedBy?.fcm_token,
|
||||||
|
"Exciting news",
|
||||||
|
"Some of your items got in processing "
|
||||||
|
);
|
||||||
|
await Notification.create({
|
||||||
|
title: "Exciting news",
|
||||||
|
msg: `Some of your items got in processing `,
|
||||||
|
added_for: order.addedBy?._id,
|
||||||
|
}); // There are still remaining quantities
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add the invoice to the order
|
// Add the invoice to the order
|
||||||
@ -751,7 +773,7 @@ export const getInvoiceDetailsByIdForPD = async (req, res) => {
|
|||||||
populate: {
|
populate: {
|
||||||
path: "addedBy",
|
path: "addedBy",
|
||||||
model: "RetailDistributor",
|
model: "RetailDistributor",
|
||||||
select: "name email mobile_number ",
|
select: "name email mobile_number fcm_token ",
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
if (!invoice) {
|
if (!invoice) {
|
||||||
@ -772,7 +794,7 @@ export const updateCourierStatusToDispatchedForPD = async (req, res) => {
|
|||||||
path: "orderId",
|
path: "orderId",
|
||||||
populate: {
|
populate: {
|
||||||
path: "addedBy",
|
path: "addedBy",
|
||||||
select: "email",
|
select: "email fcm_token",
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -797,7 +819,16 @@ export const updateCourierStatusToDispatchedForPD = async (req, res) => {
|
|||||||
order.status = "dispatched";
|
order.status = "dispatched";
|
||||||
await order.save();
|
await order.save();
|
||||||
}
|
}
|
||||||
|
await sendPushNotification(
|
||||||
|
order.addedBy?.fcm_token,
|
||||||
|
"Exciting news",
|
||||||
|
"Order dispatched "
|
||||||
|
);
|
||||||
|
await Notification.create({
|
||||||
|
title: "Exciting news",
|
||||||
|
msg: `Order dispatched`,
|
||||||
|
added_for: order.addedBy?._id,
|
||||||
|
});
|
||||||
// Send email to the user
|
// Send email to the user
|
||||||
await sendEmail({
|
await sendEmail({
|
||||||
to: `${order?.addedBy?.email}`, // Assuming 'addedBy' references the user who placed the order
|
to: `${order?.addedBy?.email}`, // Assuming 'addedBy' references the user who placed the order
|
||||||
@ -904,7 +935,7 @@ export const updateCourierStatusToDeliveredForPD = async (req, res) => {
|
|||||||
path: "orderId",
|
path: "orderId",
|
||||||
populate: {
|
populate: {
|
||||||
path: "addedBy",
|
path: "addedBy",
|
||||||
select: "email",
|
select: "email fcm_token",
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
if (!invoice) {
|
if (!invoice) {
|
||||||
@ -929,7 +960,16 @@ export const updateCourierStatusToDeliveredForPD = async (req, res) => {
|
|||||||
}
|
}
|
||||||
// Get the userId from the order's addedBy
|
// Get the userId from the order's addedBy
|
||||||
const userId = order?.addedBy?._id;
|
const userId = order?.addedBy?._id;
|
||||||
|
await sendPushNotification(
|
||||||
|
order?.addedBy?.fcm_token,
|
||||||
|
"Exciting news",
|
||||||
|
"Order Delivered "
|
||||||
|
);
|
||||||
|
await Notification.create({
|
||||||
|
title: "Exciting news",
|
||||||
|
msg: `Order Delivered `,
|
||||||
|
added_for: order?.addedBy?._id,
|
||||||
|
});
|
||||||
if (!userId) {
|
if (!userId) {
|
||||||
return res.status(400).json({ error: "User not found for the order" });
|
return res.status(400).json({ error: "User not found for the order" });
|
||||||
}
|
}
|
||||||
@ -1219,6 +1259,16 @@ export const cancelOrderController = async (req, res) => {
|
|||||||
|
|
||||||
if (order.invoices.length === 0) {
|
if (order.invoices.length === 0) {
|
||||||
// If no invoices are associated with the order
|
// If no invoices are associated with the order
|
||||||
|
await sendPushNotification(
|
||||||
|
order.addedBy?.fcm_token,
|
||||||
|
"Sorry! order cancelled fully.",
|
||||||
|
`Order has been cancelled due to ${cancellationReason}`
|
||||||
|
);
|
||||||
|
await Notification.create({
|
||||||
|
title: "Sorry! order cancelled fully.",
|
||||||
|
msg: `Order dispatched`,
|
||||||
|
added_for: order.addedBy?._id,
|
||||||
|
});
|
||||||
await sendEmail({
|
await sendEmail({
|
||||||
to: `${order.addedBy.email}`, // Change to your recipient
|
to: `${order.addedBy.email}`, // Change to your recipient
|
||||||
from: `${process.env.SEND_EMAIL_FROM}`, // Change to your verified sender
|
from: `${process.env.SEND_EMAIL_FROM}`, // Change to your verified sender
|
||||||
@ -1335,6 +1385,18 @@ export const cancelOrderController = async (req, res) => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
await sendPushNotification(
|
||||||
|
order.addedBy?.fcm_token,
|
||||||
|
"Sorry! order cancelled partially.",
|
||||||
|
`Order has been cancelled due to ${
|
||||||
|
cancellationReason ? cancellationReason : "some reason"
|
||||||
|
}`
|
||||||
|
);
|
||||||
|
await Notification.create({
|
||||||
|
title: "Sorry! order cancelled fully.",
|
||||||
|
msg: `Order dispatched`,
|
||||||
|
added_for: order.addedBy?._id,
|
||||||
|
});
|
||||||
await sendEmail({
|
await sendEmail({
|
||||||
to: `${order.addedBy.email}`, // Change to your recipient
|
to: `${order.addedBy.email}`, // Change to your recipient
|
||||||
from: `${process.env.SEND_EMAIL_FROM}`, // Change to your verified sender
|
from: `${process.env.SEND_EMAIL_FROM}`, // Change to your verified sender
|
||||||
@ -1552,17 +1614,25 @@ export const getAllOrdersByDistributor = async (req, res) => {
|
|||||||
export const gettotalorderandvalueofrd = async (req, res) => {
|
export const gettotalorderandvalueofrd = async (req, res) => {
|
||||||
const { distributorId } = req.params;
|
const { distributorId } = req.params;
|
||||||
try {
|
try {
|
||||||
const orders = await RdOrder.find({ addedBy: distributorId }).sort({ createdAt: -1 });
|
const orders = await RdOrder.find({ addedBy: distributorId }).sort({
|
||||||
|
createdAt: -1,
|
||||||
|
});
|
||||||
|
|
||||||
const totalOrders = orders.length;
|
const totalOrders = orders.length;
|
||||||
const totalValue = orders.reduce((acc, order) => acc + order.grandTotal, 0).toFixed(2);
|
const totalValue = orders
|
||||||
|
.reduce((acc, order) => acc + order.grandTotal, 0)
|
||||||
|
.toFixed(2);
|
||||||
|
|
||||||
// Get the date of the last order
|
// Get the date of the last order
|
||||||
const lastPurchaseOrderDate = totalOrders > 0 ? orders[0].createdAt : null;
|
const lastPurchaseOrderDate = totalOrders > 0 ? orders[0].createdAt : null;
|
||||||
|
|
||||||
res.status(200).json({ totalOrders, totalValue: parseFloat(totalValue), lastPurchaseOrderDate });
|
res.status(200).json({
|
||||||
|
totalOrders,
|
||||||
|
totalValue: parseFloat(totalValue),
|
||||||
|
lastPurchaseOrderDate,
|
||||||
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Error fetching orders:", error);
|
console.error("Error fetching orders:", error);
|
||||||
res.status(500).json({ message: "Server error", error });
|
res.status(500).json({ message: "Server error", error });
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -10,6 +10,7 @@ import {
|
|||||||
getmyProfileRD,
|
getmyProfileRD,
|
||||||
getRDId,
|
getRDId,
|
||||||
loginRD,
|
loginRD,
|
||||||
|
saveFCMTokenForRD,
|
||||||
UpdateProfileRD,
|
UpdateProfileRD,
|
||||||
updateRDMapped,
|
updateRDMapped,
|
||||||
updateunmapRD,
|
updateunmapRD,
|
||||||
@ -29,6 +30,7 @@ router.put(
|
|||||||
ChangePasswordRD
|
ChangePasswordRD
|
||||||
);
|
);
|
||||||
router.patch("/rd-profile/update", isAuthenticatedRD, UpdateProfileRD);
|
router.patch("/rd-profile/update", isAuthenticatedRD, UpdateProfileRD);
|
||||||
|
router.post("/rd-save-fcm-token", isAuthenticatedRD, saveFCMTokenForRD);
|
||||||
//admin and maping
|
//admin and maping
|
||||||
router
|
router
|
||||||
.route("/getAllRD")
|
.route("/getAllRD")
|
||||||
@ -37,7 +39,7 @@ router
|
|||||||
authorizeRoles("admin"),
|
authorizeRoles("admin"),
|
||||||
getAllRetailDistributorApproved
|
getAllRetailDistributorApproved
|
||||||
);
|
);
|
||||||
router
|
router
|
||||||
.route("/getAllRDandorder")
|
.route("/getAllRDandorder")
|
||||||
.get(
|
.get(
|
||||||
isAuthenticatedUser,
|
isAuthenticatedUser,
|
||||||
@ -60,7 +62,7 @@ router
|
|||||||
router
|
router
|
||||||
.route("/mapped/:id")
|
.route("/mapped/:id")
|
||||||
.put(isAuthenticatedUser, authorizeRoles("admin"), updateRDMapped);
|
.put(isAuthenticatedUser, authorizeRoles("admin"), updateRDMapped);
|
||||||
router
|
router
|
||||||
.route("/unmap/:id")
|
.route("/unmap/:id")
|
||||||
.patch(isAuthenticatedUser, authorizeRoles("admin"), updateunmapRD);
|
.patch(isAuthenticatedUser, authorizeRoles("admin"), updateunmapRD);
|
||||||
export default router;
|
export default router;
|
||||||
|
@ -1366,3 +1366,31 @@ export const updateunmapRD = async (req, res) => {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const saveFCMTokenForRD = async (req, res) => {
|
||||||
|
const { fcmToken } = req.body;
|
||||||
|
const userId = req.user._id;
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Fetch the current FCM token for the user
|
||||||
|
const user = await RetailDistributor.findById(userId);
|
||||||
|
|
||||||
|
if (!user) {
|
||||||
|
return res.status(404).send("User not found");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if the new FCM token is different from the current one
|
||||||
|
if (user.fcm_token && user.fcm_token === fcmToken) {
|
||||||
|
return res.status(200).send("FCM Token is already up to date");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update the FCM token
|
||||||
|
user.fcm_token = fcmToken;
|
||||||
|
await user.save();
|
||||||
|
|
||||||
|
res.status(200).send("FCM Token saved successfully");
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error saving FCM Token:", error);
|
||||||
|
res.status(500).send("Internal Server Error");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user