Skip to content

Commit

Permalink
Merge pull request #1002 from haseebzaki-07/new_branch_7
Browse files Browse the repository at this point in the history
Promotions and Deals Management APIs
  • Loading branch information
manikumarreddyu authored Nov 10, 2024
2 parents 50cf8f3 + 9055587 commit ad4f42b
Show file tree
Hide file tree
Showing 10 changed files with 234 additions and 0 deletions.
35 changes: 35 additions & 0 deletions backend/controllers/rent/SeasonalPricingController.js
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' });
}
};
42 changes: 42 additions & 0 deletions backend/controllers/rent/featuredProductController.js
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' });
}
};
59 changes: 59 additions & 0 deletions backend/controllers/rent/promotionController.js
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' });
}
};
13 changes: 13 additions & 0 deletions backend/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const shopRoutes = require('./routes/shop')
const googleauth = require('./routes/googleauth')
const agriProductRoutes = require('./routes/agriProductRoutes');


const discussionRoutes = require('./routes/discussionRoutes');

const rentProductRoutes = require('./routes/rent/rentProductRoutes');
Expand All @@ -21,8 +22,14 @@ const adminOrderRoutes = require('./routes/rent/adminOrderRoutes');
const adminUserControlRoutes = require('./routes/rent/adminUserControlRoutes');
const analyticsRoutes = require('./routes/rent/analyticsRoutes');
const reviewRoutes = require('./routes/rent/reviewRoutes');

const promotionRoutes = require('./routes/rent/promotionRoutes');
const seasonalPricingRoutes = require('./routes/rent/seasonalPricingRoutes');
const featuredProductRoutes = require('./routes/rent/featuredProductRoutes');

const rewardsRoutes = require('./routes/rent/rewardsRoutes');


const ratingRoutes = require('./routes/rent/ratingRoutes');

const rentOrderRoutes = require('./routes/rent/rentOrderRoutes');
Expand Down Expand Up @@ -62,8 +69,14 @@ app.use('/api', adminOrderRoutes);
app.use('/api/admin', adminUserControlRoutes);
app.use('/api', analyticsRoutes);
app.use('/api', reviewRoutes);

app.use('/api', promotionRoutes);
app.use('/api', seasonalPricingRoutes);
app.use('/api', featuredProductRoutes);

app.use('/api', rewardsRoutes);


app.use('/api', ratingRoutes);

app.use('/api', rentOrderRoutes);
Expand Down
16 changes: 16 additions & 0 deletions backend/model/rent/featuredProduct.js
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;
20 changes: 20 additions & 0 deletions backend/model/rent/promotion.js
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;
16 changes: 16 additions & 0 deletions backend/model/rent/seasonalPricing.js
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;
12 changes: 12 additions & 0 deletions backend/routes/rent/featuredProductRoutes.js
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;
11 changes: 11 additions & 0 deletions backend/routes/rent/promotionRoutes.js
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;
10 changes: 10 additions & 0 deletions backend/routes/rent/seasonalPricingRoutes.js
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;

0 comments on commit ad4f42b

Please sign in to comment.