Retailers update where retailer,kyc and address update with image delete and update
BIN
public/temp/tmp-1-1731042283301
Normal file
After Width: | Height: | Size: 30 KiB |
BIN
public/temp/tmp-1-1731042308916
Normal file
After Width: | Height: | Size: 30 KiB |
BIN
public/temp/tmp-1-1731045048421
Normal file
After Width: | Height: | Size: 10 KiB |
BIN
public/temp/tmp-1-1731048173679
Normal file
After Width: | Height: | Size: 30 KiB |
BIN
public/temp/tmp-1-1731048914310
Normal file
After Width: | Height: | Size: 17 KiB |
BIN
public/temp/tmp-2-1731045048423
Normal file
After Width: | Height: | Size: 30 KiB |
BIN
public/temp/tmp-2-1731048173680
Normal file
After Width: | Height: | Size: 26 KiB |
BIN
public/temp/tmp-2-1731048914311
Normal file
After Width: | Height: | Size: 138 KiB |
@ -131,6 +131,78 @@ export const createKyc = async (req, res) => {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
export const deletekycImageFromCloudinary = async (req, res) => {
|
||||||
|
const { public_id, folder } = req.params;
|
||||||
|
const { kycid, imageType } = req.body;
|
||||||
|
// console.log(req.body);
|
||||||
|
// console.log(req.params);
|
||||||
|
// Map frontend field names to backend field names in the KYC schema
|
||||||
|
const imageFieldMapping = {
|
||||||
|
selfieEntranceImg: "selfie_entrance_img",
|
||||||
|
panImg: "pan_img",
|
||||||
|
aadharImg: "aadhar_img",
|
||||||
|
gstImg: "gst_img",
|
||||||
|
pesticideLicenseImg: "pesticide_license_img",
|
||||||
|
fertilizerLicenseImg: "fertilizer_license_img",
|
||||||
|
};
|
||||||
|
|
||||||
|
try {
|
||||||
|
if (!public_id || !imageType) {
|
||||||
|
return res.status(400).json({
|
||||||
|
success: false,
|
||||||
|
msg: "Please provide a valid image public ID and image type!",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get the corresponding backend field name
|
||||||
|
const backendImageField = imageFieldMapping[imageType];
|
||||||
|
if (!backendImageField) {
|
||||||
|
return res.status(400).json({
|
||||||
|
success: false,
|
||||||
|
msg: "Invalid image type provided!",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Define the full Cloudinary public ID path
|
||||||
|
const folderPath = "KYC";
|
||||||
|
const fullPublicId = `${folderPath}/${folder}/${public_id}`;
|
||||||
|
|
||||||
|
// Delete the image from Cloudinary
|
||||||
|
const response = await cloudinary.v2.uploader.destroy(fullPublicId);
|
||||||
|
|
||||||
|
if (response.result === "ok") {
|
||||||
|
// Update the KYC document to set the image field to null
|
||||||
|
const updateField = {};
|
||||||
|
updateField[backendImageField] = null;
|
||||||
|
|
||||||
|
const kycRecord = await KYC.findByIdAndUpdate(
|
||||||
|
kycid,
|
||||||
|
{ $set: updateField },
|
||||||
|
{ new: true }
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!kycRecord) {
|
||||||
|
return res.status(404).json({
|
||||||
|
success: false,
|
||||||
|
msg: "KYC record not found!",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return res.status(200).json({
|
||||||
|
success: true,
|
||||||
|
msg: "Image deleted successfully!",
|
||||||
|
data: kycRecord,
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
throw new Error("Failed to delete image from Cloudinary.");
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
return res.status(500).json({
|
||||||
|
success: false,
|
||||||
|
msg: error.message || "Something went wrong!",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
export const createretaildistributor = async (req, res) => {
|
export const createretaildistributor = async (req, res) => {
|
||||||
const {
|
const {
|
||||||
name,
|
name,
|
||||||
|
@ -5,6 +5,7 @@ const router = express.Router();
|
|||||||
import {
|
import {
|
||||||
createKyc,
|
createKyc,
|
||||||
createretaildistributor,
|
createretaildistributor,
|
||||||
|
deletekycImageFromCloudinary,
|
||||||
getAllKyc,
|
getAllKyc,
|
||||||
getAllKycApproved,
|
getAllKycApproved,
|
||||||
getAllKycRejected,
|
getAllKycRejected,
|
||||||
@ -76,4 +77,11 @@ router
|
|||||||
router
|
router
|
||||||
.route("/kyc/save-fcm-tm/")
|
.route("/kyc/save-fcm-tm/")
|
||||||
.post(isAuthenticatedTerritoryManager, saveFCMTokenForTM);
|
.post(isAuthenticatedTerritoryManager, saveFCMTokenForTM);
|
||||||
|
router
|
||||||
|
.route("/deleteImage/KYC/:folder/:public_id")
|
||||||
|
.delete(
|
||||||
|
isAuthenticatedUser,
|
||||||
|
authorizeRoles("admin"),
|
||||||
|
deletekycImageFromCloudinary
|
||||||
|
);
|
||||||
export default router;
|
export default router;
|
||||||
|
@ -15,6 +15,7 @@ import {
|
|||||||
updateRDMapped,
|
updateRDMapped,
|
||||||
updateunmapRD,
|
updateunmapRD,
|
||||||
uploadRetaildistributors,
|
uploadRetaildistributors,
|
||||||
|
updateretaildistributorwithKYC,
|
||||||
} from "./RetailDistributorController.js";
|
} from "./RetailDistributorController.js";
|
||||||
import { isAuthenticatedRD } from "../../middlewares/rdAuth.js";
|
import { isAuthenticatedRD } from "../../middlewares/rdAuth.js";
|
||||||
import { authorizeRoles, isAuthenticatedUser } from "../../middlewares/auth.js";
|
import { authorizeRoles, isAuthenticatedUser } from "../../middlewares/auth.js";
|
||||||
@ -34,6 +35,12 @@ router.put(
|
|||||||
ChangePasswordRD
|
ChangePasswordRD
|
||||||
);
|
);
|
||||||
router.patch("/rd-profile/update", isAuthenticatedRD, UpdateProfileRD);
|
router.patch("/rd-profile/update", isAuthenticatedRD, UpdateProfileRD);
|
||||||
|
router.put(
|
||||||
|
"/retailer/update-admin/:id",
|
||||||
|
isAuthenticatedUser,
|
||||||
|
authorizeRoles("admin"),
|
||||||
|
updateretaildistributorwithKYC
|
||||||
|
);
|
||||||
router.post("/rd-save-fcm-token", isAuthenticatedRD, saveFCMTokenForRD);
|
router.post("/rd-save-fcm-token", isAuthenticatedRD, saveFCMTokenForRD);
|
||||||
//admin and maping
|
//admin and maping
|
||||||
router
|
router
|
||||||
|
@ -11,6 +11,7 @@ import XLSX from "xlsx";
|
|||||||
import fs from "fs";
|
import fs from "fs";
|
||||||
import path from "path";
|
import path from "path";
|
||||||
import ShippingAddressRD from "../ShippingAddressesRD/RDShippingAddressModel.js";
|
import ShippingAddressRD from "../ShippingAddressesRD/RDShippingAddressModel.js";
|
||||||
|
import cloudinary from "../../Utils/cloudinary.js";
|
||||||
|
|
||||||
export const uploadRetaildistributors = async (req, res) => {
|
export const uploadRetaildistributors = async (req, res) => {
|
||||||
try {
|
try {
|
||||||
@ -334,6 +335,124 @@ export const uploadRetaildistributors = async (req, res) => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
// Helper function to upload images to Cloudinary
|
||||||
|
const uploadImage = async (image, folder) => {
|
||||||
|
if (!image) return null;
|
||||||
|
const result = await cloudinary.v2.uploader.upload(image.tempFilePath, {
|
||||||
|
folder,
|
||||||
|
});
|
||||||
|
return {
|
||||||
|
public_id: result.public_id,
|
||||||
|
url: result.secure_url,
|
||||||
|
};
|
||||||
|
};
|
||||||
|
export const updateretaildistributorwithKYC = async (req, res) => {
|
||||||
|
try {
|
||||||
|
const { id } = req.params;
|
||||||
|
const retailDistributorData = req.body;
|
||||||
|
const files = req.files;
|
||||||
|
|
||||||
|
// Update RetailDistributor model
|
||||||
|
const updatedRetailDistributor = await RetailDistributor.findByIdAndUpdate(
|
||||||
|
id,
|
||||||
|
{
|
||||||
|
name: retailDistributorData.name,
|
||||||
|
email: retailDistributorData.email,
|
||||||
|
mobile_number: retailDistributorData.mobile_number,
|
||||||
|
},
|
||||||
|
{ new: true }
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!updatedRetailDistributor) {
|
||||||
|
return res.status(404).json({ message: "Retail Distributor not found" });
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get the associated KYC document
|
||||||
|
const kycId = updatedRetailDistributor.kyc;
|
||||||
|
const kycUpdates = {
|
||||||
|
name: retailDistributorData.name,
|
||||||
|
email: retailDistributorData.email,
|
||||||
|
trade_name: retailDistributorData.trade_name,
|
||||||
|
address: retailDistributorData.address,
|
||||||
|
state: retailDistributorData.state,
|
||||||
|
city: retailDistributorData.city,
|
||||||
|
district: retailDistributorData.district,
|
||||||
|
pincode: retailDistributorData.pincode,
|
||||||
|
mobile_number: retailDistributorData.mobile_number,
|
||||||
|
pan_number: retailDistributorData.pan_number,
|
||||||
|
aadhar_number: retailDistributorData.aadhar_number,
|
||||||
|
gst_number: retailDistributorData.gst_number,
|
||||||
|
};
|
||||||
|
|
||||||
|
// Upload images to Cloudinary if they are provided in req.files
|
||||||
|
if (files) {
|
||||||
|
if (files.panImg) {
|
||||||
|
kycUpdates.pan_img = await uploadImage(files.panImg, "KYC/pan");
|
||||||
|
}
|
||||||
|
if (files.aadharImg) {
|
||||||
|
kycUpdates.aadhar_img = await uploadImage(files.aadharImg, "KYC/aadhar");
|
||||||
|
}
|
||||||
|
if (files.gstImg) {
|
||||||
|
kycUpdates.gst_img = await uploadImage(files.gstImg, "KYC/gst");
|
||||||
|
}
|
||||||
|
if (files.pesticideLicenseImg) {
|
||||||
|
kycUpdates.pesticide_license_img = await uploadImage(files.pesticideLicenseImg, "KYC/pesticide_license");
|
||||||
|
}
|
||||||
|
if (files.fertilizerLicenseImg) {
|
||||||
|
kycUpdates.fertilizer_license_img = await uploadImage(files.fertilizerLicenseImg, "KYC/fertilizer_license");
|
||||||
|
}
|
||||||
|
if (files.selfieEntranceImg) {
|
||||||
|
kycUpdates.selfie_entrance_img = await uploadImage(files.selfieEntranceImg, "KYC/selfie_entrance");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update the KYC model
|
||||||
|
const updatedKYC = await KYC.findByIdAndUpdate(kycId, kycUpdates, { new: true });
|
||||||
|
if (!updatedKYC) {
|
||||||
|
return res.status(404).json({ message: "KYC record not found" });
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update ShippingAddressRD model with new data
|
||||||
|
const shippingAddressUpdate = {
|
||||||
|
Name: retailDistributorData.name,
|
||||||
|
phoneNumber: retailDistributorData.mobile_number,
|
||||||
|
street: retailDistributorData.address,
|
||||||
|
district: retailDistributorData.district,
|
||||||
|
city: retailDistributorData.city,
|
||||||
|
state: retailDistributorData.state,
|
||||||
|
postalCode: retailDistributorData.pincode,
|
||||||
|
tradeName: retailDistributorData.trade_name,
|
||||||
|
};
|
||||||
|
|
||||||
|
// Find and update the default or first ShippingAddressRD record
|
||||||
|
let shippingAddress = await ShippingAddressRD.findOneAndUpdate(
|
||||||
|
{ user: id, isDefault: true },
|
||||||
|
shippingAddressUpdate,
|
||||||
|
{ new: true }
|
||||||
|
);
|
||||||
|
|
||||||
|
// If no default address found, update the first one found
|
||||||
|
if (!shippingAddress) {
|
||||||
|
shippingAddress = await ShippingAddressRD.findOneAndUpdate(
|
||||||
|
{ user: id },
|
||||||
|
shippingAddressUpdate,
|
||||||
|
{ new: true }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
res.status(200).json({
|
||||||
|
message: "Retail Distributor, KYC, and Shipping Address updated successfully",
|
||||||
|
success: true,
|
||||||
|
retailDistributor: updatedRetailDistributor,
|
||||||
|
kyc: updatedKYC,
|
||||||
|
shippingAddress: shippingAddress,
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
console.error(error);
|
||||||
|
res.status(500).json({ message: "Internal Server Error" });
|
||||||
|
}
|
||||||
|
};
|
||||||
export const loginRD = async (req, res) => {
|
export const loginRD = async (req, res) => {
|
||||||
const { email, password } = req.body;
|
const { email, password } = req.body;
|
||||||
|
|
||||||
|
@ -891,7 +891,7 @@ export const getStockPD = async (req, res) => {
|
|||||||
|
|
||||||
// // Find the product in existing PDStock (if any)
|
// // Find the product in existing PDStock (if any)
|
||||||
// const existingProductInStock = stock?.products.find(
|
// const existingProductInStock = stock?.products.find(
|
||||||
// (p) => p.productid.toString() === productInSystem._id.toString()
|
// (p) => p.SKU === productInSystem.SKU
|
||||||
// );
|
// );
|
||||||
|
|
||||||
// if (existingProductInStock) {
|
// if (existingProductInStock) {
|
||||||
|