Skip to content

Commit

Permalink
Merge pull request #1006 from IkkiOcean/second
Browse files Browse the repository at this point in the history
Add Backend Support for Managing Malfunctioning Products with Enhanced Validation and Error Handling
  • Loading branch information
manikumarreddyu authored Nov 10, 2024
2 parents 86830a3 + c19c891 commit 28f24f9
Show file tree
Hide file tree
Showing 3 changed files with 283 additions and 0 deletions.
202 changes: 202 additions & 0 deletions backend/controllers/shop/sub-controllers/damagecontroller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
const MalfunctioningProduct = require("../models/MalfunctioningProduct");
const mongoose = require("mongoose");

// Create malfunctioning product
exports.createMalfunctioningProduct = async (req, res) => {
try {
const { productId, description, actionsTaken, createdBy } = req.body;

// Validate if productId exists and is valid
if (!mongoose.Types.ObjectId.isValid(productId)) {
return res.status(400).json({ message: "Invalid product ID" });
}

// Check if the user exists
const userExists = await User.findById(createdBy);
if (!userExists) {
return res.status(404).json({ message: "User not found" });
}

const newProduct = new MalfunctioningProduct({
productId,
description,
actionsTaken,
createdBy,
});

await newProduct.save();
res.status(201).json(newProduct);
} catch (error) {
console.error(error);
res
.status(400)
.json({
message: "Error creating malfunctioning product",
error: error.message,
});
}
};

// Get malfunctioning product by ID
exports.getMalfunctioningProduct = async (req, res) => {
try {
const productId = req.params.id;

// Validate if productId exists and is valid
if (!mongoose.Types.ObjectId.isValid(productId)) {
return res.status(400).json({ message: "Invalid product ID format" });
}

const product = await MalfunctioningProduct.findById(productId).populate(
"productId"
);

// Handle case where product is not found
if (!product) {
return res
.status(404)
.json({ message: "Malfunctioning product not found" });
}

res.status(200).json(product);
} catch (error) {
console.error(error);
res
.status(500)
.json({
message: "Error retrieving malfunctioning product",
error: error.message,
});
}
};

// Update malfunctioning product
exports.updateMalfunctioningProduct = async (req, res) => {
try {
const updatedData = req.body;
const productId = req.params.id;

// Validate if productId exists and is valid
if (!mongoose.Types.ObjectId.isValid(productId)) {
return res.status(400).json({ message: "Invalid product ID format" });
}

// Check if any field is being updated and validate
if (
!updatedData.productId &&
!updatedData.description &&
!updatedData.actionsTaken
) {
return res.status(400).json({ message: "No valid fields to update" });
}

const product = await MalfunctioningProduct.findByIdAndUpdate(
productId,
updatedData,
{ new: true }
);

// Handle case where product is not found
if (!product) {
return res.status(404).json({ message: "Product not found" });
}

res.status(200).json(product);
} catch (error) {
console.error(error);
res
.status(500)
.json({
message: "Error updating malfunctioning product",
error: error.message,
});
}
};

// Delete malfunctioning product
exports.deleteMalfunctioningProduct = async (req, res) => {
try {
const productId = req.params.id;

// Validate if productId exists and is valid
if (!mongoose.Types.ObjectId.isValid(productId)) {
return res.status(400).json({ message: "Invalid product ID format" });
}

const product = await MalfunctioningProduct.findByIdAndDelete(productId);

// Handle case where product is not found
if (!product) {
return res
.status(404)
.json({ message: "Malfunctioning product not found" });
}

res.status(200).json({ message: "Product deleted successfully" });
} catch (error) {
console.error(error);
res
.status(500)
.json({
message: "Error deleting malfunctioning product",
error: error.message,
});
}
};

// Get all malfunctioning products with pagination and search
exports.getAllMalfunctioningProducts = async (req, res) => {
try {
const { page = 1, limit = 10, search = "" } = req.query;
const skip = (page - 1) * limit;

const query = search
? { description: { $regex: search, $options: "i" } }
: {};

const products = await MalfunctioningProduct.find(query)
.skip(skip)
.limit(parseInt(limit))
.populate("productId")
.exec();

const totalCount = await MalfunctioningProduct.countDocuments(query);

res.status(200).json({
totalCount,
totalPages: Math.ceil(totalCount / limit),
currentPage: page,
products,
});
} catch (error) {
console.error(error);
res
.status(500)
.json({
message: "Error fetching malfunctioning products",
error: error.message,
});
}
};

// Get count of malfunctioning products by status
exports.getMalfunctioningProductCountByStatus = async (req, res) => {
try {
const { status } = req.params;

if (!["pending", "in progress", "resolved"].includes(status)) {
return res.status(400).json({ message: "Invalid status" });
}

const count = await MalfunctioningProduct.countDocuments({ status });
res.status(200).json({ count });
} catch (error) {
console.error(error);
res
.status(500)
.json({
message: "Error fetching count by status",
error: error.message,
});
}
};
27 changes: 27 additions & 0 deletions backend/model/shop/sub-model/damagemodal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const mongoose = require('mongoose');

const malfunctioningProductSchema = new mongoose.Schema({
productId: { type: mongoose.Schema.Types.ObjectId, ref: 'Product', required: true },
description: { type: String, required: true },
status: { type: String, enum: ['pending', 'in progress', 'resolved'], default: 'pending' },
reportedDate: { type: Date, default: Date.now },
resolvedDate: { type: Date },
actionsTaken: { type: String },
createdBy: { type: mongoose.Schema.Types.ObjectId, ref: 'User', required: true }
});

module.exports = mongoose.model('MalfunctioningProduct', malfunctioningProductSchema);



const damagedProductSchema = new mongoose.Schema({
productId: { type: mongoose.Schema.Types.ObjectId, ref: 'Product', required: true },
damageType: { type: String, enum: ['crack', 'scratches', 'water damage', 'other'], required: true },
damageDescription: { type: String, required: true },
damageSeverity: { type: String, enum: ['minor', 'moderate', 'severe'], required: true },
reportedDate: { type: Date, default: Date.now },
status: { type: String, enum: ['reported', 'under review', 'resolved'], default: 'reported' },
createdBy: { type: mongoose.Schema.Types.ObjectId, ref: 'User', required: true }
});

module.exports = mongoose.model('DamagedProduct', damagedProductSchema);
54 changes: 54 additions & 0 deletions backend/routes/sub-routes/routesdamage
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
const express = require("express");
const router = express.Router();
const MalfunctioningProductController = require("../controllers/MalfunctioningProductController");
const {
validateMalfunctioningProduct,
validateUpdateMalfunctioningProduct,
} = require("../middlewares/validationMiddleware");
const authenticate = require("../middlewares/authenticationMiddleware");

// Create malfunctioning product
router.post(
"/",
authenticate,
validateMalfunctioningProduct,
MalfunctioningProductController.createMalfunctioningProduct
);

// Get malfunctioning product by ID
router.get(
"/:id",
authenticate,
MalfunctioningProductController.getMalfunctioningProduct
);

// Update malfunctioning product
router.put(
"/:id",
authenticate,
validateUpdateMalfunctioningProduct,
MalfunctioningProductController.updateMalfunctioningProduct
);

// Delete malfunctioning product
router.delete(
"/:id",
authenticate,
MalfunctioningProductController.deleteMalfunctioningProduct
);

// Get all malfunctioning products (with pagination and search)
router.get(
"/",
authenticate,
MalfunctioningProductController.getAllMalfunctioningProducts
);

// Get count of malfunctioning products by status
router.get(
"/count/:status",
authenticate,
MalfunctioningProductController.getMalfunctioningProductCountByStatus
);

module.exports = router;

0 comments on commit 28f24f9

Please sign in to comment.