-
Notifications
You must be signed in to change notification settings - Fork 118
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1002 from haseebzaki-07/new_branch_7
Promotions and Deals Management APIs
- Loading branch information
Showing
10 changed files
with
234 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
|
||
const RentProduct = require('../../model/rent/rentProduct'); | ||
const SeasonalPricing = require('../../model/rent/seasonalPricing'); | ||
|
||
// Apply seasonal pricing adjustment for a product | ||
exports.applySeasonalPricing = async (req, res) => { | ||
try { | ||
const { productId, season, discountPercentage, startDate, endDate } = req.body; | ||
|
||
// Check if the product exists | ||
const product = await RentProduct.findById(productId); | ||
if (!product) { | ||
return res.status(404).json({ error: 'Product not found' }); | ||
} | ||
|
||
const newSeasonalPricing = new SeasonalPricing({ | ||
product: productId, | ||
season, | ||
discountPercentage, | ||
startDate, | ||
endDate | ||
}); | ||
|
||
await newSeasonalPricing.save(); | ||
|
||
// Apply the seasonal price change immediately | ||
product.rentalPricePerDay = product.rentalPricePerDay * (1 - discountPercentage / 100); | ||
await product.save(); | ||
|
||
res.status(201).json({ message: 'Seasonal pricing applied successfully', seasonalPricing: newSeasonalPricing }); | ||
} catch (error) { | ||
console.error('Error applying seasonal pricing:', error.message); | ||
res.status(500).json({ error: 'Failed to apply seasonal pricing' }); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
|
||
const FeaturedProduct = require('../../model/rent/featuredProduct'); | ||
const RentProduct = require('../../model/rent/rentProduct'); | ||
|
||
// Feature a product | ||
exports.featureProduct = async (req, res) => { | ||
try { | ||
const { productId, description, startDate, endDate } = req.body; | ||
|
||
// Check if the product exists | ||
const product = await RentProduct.findById(productId); | ||
if (!product) { | ||
return res.status(404).json({ error: 'Product not found' }); | ||
} | ||
|
||
const newFeaturedProduct = new FeaturedProduct({ | ||
product: productId, | ||
description, | ||
startDate, | ||
endDate, | ||
status: 'active' | ||
}); | ||
|
||
await newFeaturedProduct.save(); | ||
res.status(201).json({ message: 'Product featured successfully', featuredProduct: newFeaturedProduct }); | ||
} catch (error) { | ||
console.error('Error featuring product:', error.message); | ||
res.status(500).json({ error: 'Failed to feature product' }); | ||
} | ||
}; | ||
|
||
// Get all featured products | ||
exports.getFeaturedProducts = async (req, res) => { | ||
try { | ||
const featuredProducts = await FeaturedProduct.find({ status: 'active' }).populate('product'); | ||
|
||
res.status(200).json({ featuredProducts }); | ||
} catch (error) { | ||
console.error('Error fetching featured products:', error.message); | ||
res.status(500).json({ error: 'Failed to fetch featured products' }); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
const Promotion = require("../../model/rent/promotion"); | ||
|
||
|
||
// Create a new promotion | ||
exports.createPromotion = async (req, res) => { | ||
try { | ||
const { code, description, discountPercentage, startDate, endDate, applicableProducts, status } = req.body; | ||
|
||
// Create the promotion object | ||
const newPromotion = new Promotion({ | ||
code, | ||
description, | ||
discountPercentage, | ||
startDate, | ||
endDate, | ||
applicableProducts, | ||
status | ||
}); | ||
|
||
// Save the promotion to the database | ||
await newPromotion.save(); | ||
res.status(201).json({ message: 'Promotion created successfully', promotion: newPromotion }); | ||
} catch (error) { | ||
console.error('Error creating promotion:', error.message); | ||
res.status(500).json({ error: 'Failed to create promotion' }); | ||
} | ||
}; | ||
|
||
// Get all active promotions | ||
exports.getActivePromotions = async (req, res) => { | ||
try { | ||
const promotions = await Promotion.find({ status: 'active' }); | ||
|
||
res.status(200).json({ promotions }); | ||
} catch (error) { | ||
console.error('Error fetching promotions:', error.message); | ||
res.status(500).json({ error: 'Failed to fetch active promotions' }); | ||
} | ||
}; | ||
|
||
// Deactivate a promotion | ||
exports.deactivatePromotion = async (req, res) => { | ||
try { | ||
const { promotionId } = req.params; | ||
const promotion = await Promotion.findById(promotionId); | ||
|
||
if (!promotion) { | ||
return res.status(404).json({ error: 'Promotion not found' }); | ||
} | ||
|
||
promotion.status = 'inactive'; | ||
await promotion.save(); | ||
|
||
res.status(200).json({ message: 'Promotion deactivated successfully' }); | ||
} catch (error) { | ||
console.error('Error deactivating promotion:', error.message); | ||
res.status(500).json({ error: 'Failed to deactivate promotion' }); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
const mongoose = require('mongoose'); | ||
|
||
const featuredProductSchema = new mongoose.Schema( | ||
{ | ||
product: { type: mongoose.Schema.Types.ObjectId, ref: 'RentProduct', required: true }, | ||
description: { type: String }, | ||
startDate: { type: Date, required: true }, | ||
endDate: { type: Date, required: true }, | ||
status: { type: String, enum: ['active', 'inactive'], default: 'active' } | ||
}, | ||
{ timestamps: true } | ||
); | ||
|
||
const FeaturedProduct = mongoose.model('FeaturedProduct', featuredProductSchema); | ||
|
||
module.exports = FeaturedProduct; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
const mongoose = require("mongoose"); | ||
|
||
const promotionSchema = new mongoose.Schema( | ||
{ | ||
code: { type: String, required: true, unique: true }, | ||
description: { type: String, required: true }, | ||
discountPercentage: { type: Number, required: true, min: 0, max: 100 }, | ||
startDate: { type: Date, required: true }, | ||
endDate: { type: Date, required: true }, | ||
applicableProducts: [ | ||
{ type: mongoose.Schema.Types.ObjectId, ref: "RentProduct" }, | ||
], // Specific products or categories for the promo | ||
status: { type: String, enum: ["active", "inactive"], default: "active" }, | ||
}, | ||
{ timestamps: true } | ||
); | ||
|
||
const Promotion = mongoose.model("Promotion", promotionSchema); | ||
|
||
module.exports = Promotion; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
const mongoose = require('mongoose'); | ||
|
||
const seasonalPricingSchema = new mongoose.Schema( | ||
{ | ||
product: { type: mongoose.Schema.Types.ObjectId, ref: 'RentProduct', required: true }, | ||
season: { type: String, required: true, enum: ['summer', 'winter', 'fall', 'spring'] }, | ||
discountPercentage: { type: Number, required: true, min: 0, max: 100 }, | ||
startDate: { type: Date, required: true }, | ||
endDate: { type: Date, required: true }, | ||
}, | ||
{ timestamps: true } | ||
); | ||
|
||
const SeasonalPricing = mongoose.model('SeasonalPricing', seasonalPricingSchema); | ||
|
||
module.exports = SeasonalPricing; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
const express = require('express'); | ||
const { featureProduct, getFeaturedProducts } = require('../../controllers/rent/featuredProductController'); | ||
const router = express.Router(); | ||
|
||
|
||
// Admin route to feature a product | ||
router.post('rent/feature',featureProduct); | ||
|
||
// Get all featured products | ||
router.get('rent/featured',getFeaturedProducts); | ||
|
||
module.exports = router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
const express = require('express'); | ||
const { createPromotion, getActivePromotions, deactivatePromotion } = require('../../controllers/rent/promotionController'); | ||
const router = express.Router(); | ||
|
||
|
||
// Admin routes to manage promotions | ||
router.post('/rent/create', createPromotion); // Create new promotion | ||
router.get('/rent/active', getActivePromotions); // Get active promotions | ||
router.put('/rent/deactivate/:promotionId', deactivatePromotion); // Deactivate a promotion | ||
|
||
module.exports = router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
const express = require('express'); | ||
const { applySeasonalPricing } = require('../../controllers/rent/SeasonalPricingController'); | ||
const router = express.Router(); | ||
|
||
|
||
|
||
// Admin route to apply seasonal pricing | ||
router.post('/apply', applySeasonalPricing); | ||
|
||
module.exports = router; |