Skip to content

Commit

Permalink
featured products, seasonal pricing, promotions APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
haseebzaki-07 committed Nov 10, 2024
1 parent 0b5d598 commit 7b9c595
Show file tree
Hide file tree
Showing 13 changed files with 451 additions and 2 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' });
}
};
7 changes: 7 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,6 +22,9 @@ 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 ratingRoutes = require('./routes/rent/ratingRoutes');

Expand Down Expand Up @@ -61,6 +65,9 @@ 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', ratingRoutes);

Expand Down
Empty file added backend/logs/error.log
Empty file.
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;
Loading

0 comments on commit 7b9c595

Please sign in to comment.