Skip to content

Commit

Permalink
product offer done
Browse files Browse the repository at this point in the history
  • Loading branch information
navneethvi committed Jan 29, 2024
1 parent 942402c commit 01cff24
Show file tree
Hide file tree
Showing 4 changed files with 167 additions and 16 deletions.
47 changes: 43 additions & 4 deletions controllers/productController.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,11 @@ const deleteSingleImage = async (req, res) => {
if (fs.existsSync(imagePath)) {
await fs.unlinkSync(imagePath);
console.log(`Image ${image} deleted successfully`);
res.json({success : true})
res.json({ success: true })
} else {
console.log(`Image ${image} not found`);
}

// res.redirect(`/admin/editProduct?id=${product._id}`)

} catch (error) {
Expand Down Expand Up @@ -169,7 +169,7 @@ const getAllProducts = async (req, res) => {
{ brand: { $regex: new RegExp(".*" + search + ".*", "i") } }
],
})
.sort({createdOn : -1})
.sort({ createdOn: -1 })
.limit(limit * 1)
.skip((page - 1) * limit)
.exec()
Expand Down Expand Up @@ -221,6 +221,43 @@ const getUnblockProduct = async (req, res) => {



const addProductOffer = async (req, res) => {
try {
// console.log(req.body);
const { productId, percentage } = req.body
const findProduct = await Product.findOne({ _id: productId })
// console.log(findProduct);

findProduct.salePrice = findProduct.salePrice - Math.floor(findProduct.regularPrice * (percentage / 100))
findProduct.productOffer = parseInt(percentage)
await findProduct.save()

res.json({ status: true })

} catch (error) {
console.log(error.message);
}
}



const removeProductOffer = async (req, res) => {
try {
// console.log(req.body);
const {productId} = req.body
const findProduct = await Product.findOne({_id : productId})
// console.log(findProduct);
const percentage = findProduct.productOffer
findProduct.salePrice = findProduct.salePrice + Math.floor(findProduct.regularPrice * (percentage / 100))
findProduct.productOffer = 0
await findProduct.save()
res.json({status : true})
} catch (error) {
console.log(error.message);
}
}




module.exports = {
Expand All @@ -231,5 +268,7 @@ module.exports = {
getUnblockProduct,
getEditProduct,
editProduct,
deleteSingleImage
deleteSingleImage,
addProductOffer,
removeProductOffer
}
4 changes: 4 additions & 0 deletions models/productSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ const productSchema = Mongoose.Schema({
processor : {
type : String,
required : true
},
productOffer : {
type : Number,
default : 0
}
})

Expand Down
4 changes: 4 additions & 0 deletions routes/adminRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ Router.post("/editProduct/:id", isAdmin, upload.array("images", 5), productContr
Router.post("/deleteImage", isAdmin, productController.deleteSingleImage)
Router.get("/blockProduct", isAdmin, productController.getBlockProduct)
Router.get("/unBlockProduct", isAdmin, productController.getUnblockProduct)
Router.post("/addProductOffer", isAdmin, productController.addProductOffer)
Router.post("/removeProductOffer", isAdmin, productController.removeProductOffer)



// Order Management
Router.get("/orderList", isAdmin, orderContoller.getOrderListPageAdmin)
Expand Down
128 changes: 116 additions & 12 deletions views/admin/products.ejs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
<%- include("../partials/adminHeader") %>


<head>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/sweetalert2@10/dist/sweetalert2.min.css">

</head>

<div class="content-header">
<div>
<h2 class="content-title card-title">Products</h2>
Expand All @@ -25,17 +30,6 @@
















<div class="right mt-5">
Expand All @@ -45,7 +39,11 @@
<th scope="col"><b>Product name</b></th>
<th scope="col"><b>Brand</b></th>
<th scope="col"><b>Category</b></th>
<th scope="col"><b>Regular Price</b></th>
<th scope="col"><b>Sale Price</b></th>
<th scope="col"><b>Offer Price</b></th>
<th scope="col"><b>Offer</b></th>

<th scope="col"><b>Action</b></th>
<th scope="col"><b>Edit</b></th>
</tr>
Expand All @@ -56,8 +54,29 @@
<td><%= data[i].productName %></td>
<td><%= data[i].brand %></td>
<td><%= data[i].category %></td>
<td><%= data[i].regularPrice %></td>
<td><%= data[i].salePrice %></td>
<td>
<% if (locals.data[i].productOffer) { %>
<%= data[i].productOffer %>%
<% }else{ %>
0%
<% } %>
</td>
<td>
<% if (locals.data[i].productOffer===0) { %>
<button class="btn btn-info" onclick="addOffer('<%= data[i]._id %>')"
style="width: 100px;">
<a href="#" class="text-white">Add Offer</a>
</button>
<% }else{ %>
<button class="btn btn-info"
onclick="removeOffer('<%= data[i]._id %>')"
style="width: 100px;">
<a href="#" class="text-white">Remove</a>
</button>
<% } %>
</td>
<td>
<% if (data[i].isBlocked === false) { %>
<button class="btn btn-danger" style="width: 80px;">
Expand Down Expand Up @@ -101,4 +120,89 @@
</nav>
</div>

<script src="https://cdn.jsdelivr.net/npm/sweetalert2@10/dist/sweetalert2.min.js"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>


<script>
async function addOffer(productId){
// alert("hi")
const { value: amount } = await Swal.fire({
title: 'Offer in percentage',
input: 'number',
inputLabel: 'Percentage',
inputPlaceholder: '%'
})
$.ajax({
url: "/admin/addProductOffer",
method: "post",
data: {
percentage: amount,
productId : productId
},
success: (response) => {
console.log(response);
if (response.status === true) {
location.reload()
// Swal.fire('Offer added', 'The offer has been removed.', 'success');
} else {
alert("failed")
}
}
})
}
function removeOffer(productId){
// alert(productId)
try {
Swal.fire({
title: 'Remove Offer',
text: 'Are you sure you want to remove this offer?',
icon: 'warning',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Yes, remove it!',
timer: 5000, // 5 seconds
timerProgressBar: true
}).then(async (result) => {
if (result.isConfirmed) {
$.ajax({
url: "/admin/removeProductOffer",
method: "post",
data: {
productId: productId
},
success: (response) => {
console.log(response);
if (response.status === true) {
// Swal.fire('Removed!', 'The offer has been removed.', 'success');
location.reload()
} else if (response.status === false) {
Swal.fire('failed');
} else {
alert("failed")
}
}
});
}
});
} catch (error) {
console.error(error);
}
}
</script>



<%- include("../partials/adminFooter") %>

0 comments on commit 01cff24

Please sign in to comment.