-
Notifications
You must be signed in to change notification settings - Fork 117
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
277 additions
and
0 deletions.
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
backend/controllers/shop/sub-controllers/analyticcontroller.js
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,51 @@ | ||
const ProductAnalysis = require("../models/ProductAnalysis"); | ||
|
||
exports.createProductAnalysis = async (req, res) => { | ||
try { | ||
const { productId, views, sales, ratings, feedback } = req.body; | ||
const productAnalysis = new ProductAnalysis({ | ||
productId, | ||
views, | ||
sales, | ||
ratings, | ||
feedback, | ||
}); | ||
await productAnalysis.save(); | ||
res.status(201).json(productAnalysis); | ||
} catch (error) { | ||
res | ||
.status(400) | ||
.json({ message: "Error creating product analysis data", error }); | ||
} | ||
}; | ||
|
||
exports.getProductAnalysis = async (req, res) => { | ||
try { | ||
const productAnalysis = await ProductAnalysis.findById(req.params.id); | ||
if (!productAnalysis) | ||
return res.status(404).json({ message: "Product analysis not found" }); | ||
res.status(200).json(productAnalysis); | ||
} catch (error) { | ||
res | ||
.status(400) | ||
.json({ message: "Error retrieving product analysis", error }); | ||
} | ||
}; | ||
|
||
exports.updateProductAnalysis = async (req, res) => { | ||
try { | ||
const updatedData = req.body; | ||
const productAnalysis = await ProductAnalysis.findByIdAndUpdate( | ||
req.params.id, | ||
updatedData, | ||
{ new: true } | ||
); | ||
if (!productAnalysis) | ||
return res.status(404).json({ message: "Product analysis not found" }); | ||
res.status(200).json(productAnalysis); | ||
} catch (error) { | ||
res | ||
.status(400) | ||
.json({ message: "Error updating product analysis data", error }); | ||
} | ||
}; |
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,29 @@ | ||
const mongoose = require('mongoose'); | ||
|
||
const productAnalysisSchema = new mongoose.Schema({ | ||
productId: { | ||
type: mongoose.Schema.Types.ObjectId, | ||
ref: 'Product', | ||
required: true, | ||
}, | ||
views: { | ||
type: Number, | ||
default: 0, | ||
}, | ||
sales: { | ||
type: Number, | ||
default: 0, | ||
}, | ||
ratings: { | ||
type: Number, | ||
default: 0, | ||
}, | ||
feedback: { | ||
type: [String], | ||
default: [], | ||
}, | ||
}); | ||
|
||
const ProductAnalysis = mongoose.model('ProductAnalysis', productAnalysisSchema); | ||
|
||
module.exports = ProductAnalysis; |
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,25 @@ | ||
const mongoose = require('mongoose'); | ||
|
||
const userAnalysisSchema = new mongoose.Schema({ | ||
userId: { | ||
type: mongoose.Schema.Types.ObjectId, | ||
ref: 'User', | ||
required: true, | ||
}, | ||
activityLog: { | ||
type: [String], | ||
default: [], | ||
}, | ||
interactions: { | ||
type: Number, | ||
default: 0, | ||
}, | ||
preferences: { | ||
type: Object, | ||
default: {}, | ||
}, | ||
}); | ||
|
||
const UserAnalysis = mongoose.model('UserAnalysis', userAnalysisSchema); | ||
|
||
module.exports = UserAnalysis; |
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,172 @@ | ||
const UserAnalysis = require("../models/UserAnalysis"); | ||
const mongoose = require("mongoose"); | ||
|
||
// Utility function to validate ObjectId | ||
const isValidObjectId = (id) => mongoose.Types.ObjectId.isValid(id); | ||
|
||
// Create User Analysis | ||
exports.createUserAnalysis = async (req, res) => { | ||
try { | ||
const { userId, activityLog, interactions, preferences } = req.body; | ||
|
||
// Input validation | ||
if ( | ||
!userId || | ||
!activityLog || | ||
!Array.isArray(activityLog) || | ||
!interactions || | ||
!preferences | ||
) { | ||
return res | ||
.status(400) | ||
.json({ | ||
message: | ||
"Missing required fields: userId, activityLog, interactions, preferences", | ||
}); | ||
} | ||
|
||
// Check if the userId is valid | ||
if (!isValidObjectId(userId)) { | ||
return res.status(400).json({ message: "Invalid userId format" }); | ||
} | ||
|
||
// Create new UserAnalysis record | ||
const userAnalysis = new UserAnalysis({ | ||
userId, | ||
activityLog, | ||
interactions, | ||
preferences, | ||
}); | ||
await userAnalysis.save(); | ||
res.status(201).json(userAnalysis); | ||
} catch (error) { | ||
console.error(error); // For debugging purposes | ||
if (error.code === 11000) { | ||
return res | ||
.status(409) | ||
.json({ message: "Duplicate entry detected for user analysis" }); | ||
} | ||
res | ||
.status(500) | ||
.json({ message: "Error creating user analysis data", error }); | ||
} | ||
}; | ||
|
||
// Get User Analysis by ID | ||
exports.getUserAnalysis = async (req, res) => { | ||
try { | ||
const { id } = req.params; | ||
|
||
// Check if the id is a valid ObjectId | ||
if (!isValidObjectId(id)) { | ||
return res | ||
.status(400) | ||
.json({ message: "Invalid UserAnalysis ID format" }); | ||
} | ||
|
||
const userAnalysis = await UserAnalysis.findById(id); | ||
|
||
if (!userAnalysis) { | ||
return res.status(404).json({ message: "User analysis not found" }); | ||
} | ||
|
||
res.status(200).json(userAnalysis); | ||
} catch (error) { | ||
console.error(error); // For debugging purposes | ||
res | ||
.status(500) | ||
.json({ message: "Error retrieving user analysis data", error }); | ||
} | ||
}; | ||
|
||
// Update User Analysis by ID | ||
exports.updateUserAnalysis = async (req, res) => { | ||
try { | ||
const { id } = req.params; | ||
const updatedData = req.body; | ||
|
||
// Check if the id is a valid ObjectId | ||
if (!isValidObjectId(id)) { | ||
return res | ||
.status(400) | ||
.json({ message: "Invalid UserAnalysis ID format" }); | ||
} | ||
|
||
// Input validation: Ensure there is at least one field to update | ||
if (!updatedData || Object.keys(updatedData).length === 0) { | ||
return res.status(400).json({ message: "No data provided to update" }); | ||
} | ||
|
||
// Check for fields that shouldn't be updated or are invalid | ||
if (updatedData.userId && !isValidObjectId(updatedData.userId)) { | ||
return res.status(400).json({ message: "Invalid userId format" }); | ||
} | ||
|
||
const userAnalysis = await UserAnalysis.findByIdAndUpdate(id, updatedData, { | ||
new: true, | ||
}); | ||
|
||
if (!userAnalysis) { | ||
return res.status(404).json({ message: "User analysis not found" }); | ||
} | ||
|
||
res.status(200).json(userAnalysis); | ||
} catch (error) { | ||
console.error(error); // For debugging purposes | ||
if (error.code === 11000) { | ||
return res | ||
.status(409) | ||
.json({ message: "Duplicate entry detected during update" }); | ||
} | ||
res | ||
.status(500) | ||
.json({ message: "Error updating user analysis data", error }); | ||
} | ||
}; | ||
|
||
// Delete User Analysis by ID (Optional - In case we want to support deleting analysis data) | ||
exports.deleteUserAnalysis = async (req, res) => { | ||
try { | ||
const { id } = req.params; | ||
|
||
// Check if the id is a valid ObjectId | ||
if (!isValidObjectId(id)) { | ||
return res | ||
.status(400) | ||
.json({ message: "Invalid UserAnalysis ID format" }); | ||
} | ||
|
||
const userAnalysis = await UserAnalysis.findByIdAndDelete(id); | ||
|
||
if (!userAnalysis) { | ||
return res.status(404).json({ message: "User analysis not found" }); | ||
} | ||
|
||
res.status(200).json({ message: "User analysis deleted successfully" }); | ||
} catch (error) { | ||
console.error(error); // For debugging purposes | ||
res | ||
.status(500) | ||
.json({ message: "Error deleting user analysis data", error }); | ||
} | ||
}; | ||
|
||
// Get all User Analysis records (Optional - If we want to list all data) | ||
exports.getAllUserAnalysis = async (req, res) => { | ||
try { | ||
const userAnalysis = await UserAnalysis.find(); | ||
|
||
if (userAnalysis.length === 0) { | ||
return res | ||
.status(404) | ||
.json({ message: "No user analysis data available" }); | ||
} | ||
|
||
res.status(200).json(userAnalysis); | ||
} catch (error) { | ||
console.error(error); // For debugging purposes | ||
res | ||
.status(500) | ||
.json({ message: "Error retrieving all user analysis data", error }); | ||
} | ||
}; |