add currency functionality

This commit is contained in:
pawan-dot 2024-06-13 17:31:10 +05:30
parent 56438a92aa
commit fe7c3f8dd9
4 changed files with 223 additions and 0 deletions

3
app.js
View File

@ -146,6 +146,7 @@ import loginImageRoute from "./resources/LoginImage/LoginImageRoute.js";
import shopImageRoute from "./resources/ShopPageImage/ShopPageImageRoute.js";
import ContentRoute from "./resources/Content/ContentRoutes.js";
import UserAddressRoute from "./resources/userAddress/useAddressRoute.js";
import CurrencyRoute from "./resources/Currency/CurrencyRoute.js";
//business_Type
// import Business_TypeRoute from "./resources/setting/Business_Type/Business_routes.js";
@ -211,6 +212,8 @@ app.use("/api/purpose", PurposeRoute);
app.use("/api/business", orderRoute);
//Tax
app.use("/api/tax", TaxRouter);
//Currency Route
app.use("/api/currency", CurrencyRoute);
//config
app.use("/api/config", ConfigRouter);

View File

@ -0,0 +1,159 @@
import mongoose from "mongoose";
import { CurrencyModel } from "./CurrencyModel.js";
// Add new Currency
export const createnew = async (req, res) => {
const { CurrencyName, CurrencySymbol } = req.body;
// console.log(CurrencyName, CurrencySymbol);
if (!CurrencyName || !CurrencySymbol) {
return res
.status(400)
.json({ message: "Currency name & Currency symbol are required" });
}
try {
// Check if the currency already exists
const normalizedCurrencyName = CurrencyName.trim().toUpperCase();
// console.log(normalizedCurrencyName);
const trimmedCurrencySymbol = CurrencySymbol.trim();
// Check if the currency already exists
const existingCurrency = await CurrencyModel.findOne({
CurrencyName: normalizedCurrencyName,
});
if (existingCurrency) {
return res
.status(400)
.json({ message: " This Currency Name already exists" });
}
// console.log("existingCurrency", existingCurrency);
// console.log(req.user?._id);
// Create new currency
// const newCurrency = new CurrencyModel({
// CurrencyName: normalizedCurrencyName,
// CurrencySymbol: trimmedCurrencySymbol,
// addedBy: req.user?._id,
// });
// console.log("newCurrency", newCurrency);
const newCurrency = await CurrencyModel.create({
CurrencyName: normalizedCurrencyName,
CurrencySymbol: trimmedCurrencySymbol,
addedBy: req.user?._id,
});
// await newCurrency.save();
return res.status(201).json({
message: "Currency created successfully",
currency: newCurrency,
});
} catch (error) {
if (error.code === 11000) {
return res.status(400).json({ message: "Currency already exists" });
}
res.status(500).json({
message: error.message
? error.message
: "An error occurred while creating the currency",
});
}
};
export const getAllcarrency = async (req, res) => {
try {
const currency = await CurrencyModel.find().sort({
createdAt: -1,
});
if (!currency) {
return res.status(404).json({ message: "No currencies found" });
}
res.status(200).json({ success: true, currency });
} catch (error) {
res.status(500).json({
success: false,
message: error.message ? error.message : "Something went wrong",
});
}
};
export const updatecarrency = async (req, res) => {
const { CurrencyName, CurrencySymbol } = req.body;
const currencyId = req.params.id;
if (!CurrencyName?.trim() || !CurrencySymbol?.trim()) {
return res
.status(400)
.json({ message: "Currency name & Currency symbol are required" });
}
const normalizedCurrencyName = CurrencyName.trim().toUpperCase();
const trimmedCurrencySymbol = CurrencySymbol.trim();
try {
// Check if the currency with the same name already exists
const existingCurrency = await CurrencyModel.findOne({
CurrencyName: normalizedCurrencyName,
_id: { $ne: currencyId }, // Exclude the current currency being updated
});
if (existingCurrency) {
return res.status(400).json({ message: "Currency name already exists" });
}
// Update currency
const updatedCurrency = await CurrencyModel.findByIdAndUpdate(
currencyId,
{
CurrencyName: normalizedCurrencyName,
CurrencySymbol: trimmedCurrencySymbol,
},
{ new: true }
); // To return the updated document
if (!updatedCurrency) {
return res.status(404).json({ message: "Currency not found" });
}
return res.status(200).json({
message: "Currency updated successfully",
currency: updatedCurrency,
});
} catch (error) {
res.status(500).json({
message: error.message
? error.message
: "An error occurred while updating the currency",
});
}
};
export const deletecarrency = async (req, res) => {
const currencyId = req.params.id;
try {
// Check if the currency exists
const currency = await CurrencyModel.findById(currencyId);
if (!currency) {
return res.status(404).json({ message: "Currency not found" });
}
// Perform the deletion
await CurrencyModel.findByIdAndDelete(currencyId);
return res.status(200).json({
message: "Currency deleted successfully",
currency: currency,
});
} catch (error) {
// console.error("Error deleting currency:", error); // Log for debugging
res.status(500).json({
message: error.message
? error.message
: "An error occurred while deleting the currency",
});
}
};

View File

@ -0,0 +1,22 @@
import mongoose from "mongoose";
const CurrencySchema = new mongoose.Schema(
{
CurrencyName: {
type: String,
required: true,
},
CurrencySymbol: {
type: String,
required: true,
trim: true,
},
addedBy: {
type: mongoose.Schema.Types.ObjectId,
ref: "User",
},
},
{ timestamps: true }
);
export const CurrencyModel = mongoose.model("Currency", CurrencySchema);

View File

@ -0,0 +1,39 @@
import express from "express";
import {
createnew,
getAllcarrency,
updatecarrency,
deletecarrency,
} from "./CurrencyController.js";
import { isAuthenticatedUser, authorizeRoles } from "../../middlewares/auth.js";
const router = express.Router();
router
.route("/new")
.post(isAuthenticatedUser, authorizeRoles("admin", "Employee"), createnew);
router.route("/getall").get(getAllcarrency);
// router.route("/getoneblog/:id").get(getOneBlog);
router
.route("/:id")
.delete(
isAuthenticatedUser,
authorizeRoles("admin", "Employee"),
deletecarrency
);
// router
// .route("/deleteImage/jatinMor/Blog/:public_id")
// .delete(
// isAuthenticatedUser,
// authorizeRoles("admin", "Employee"),
// deleteImageFromCloudinary
// );
router
.route("/:id")
.patch(
isAuthenticatedUser,
authorizeRoles("admin", "Employee"),
updatecarrency
);
export default router;