GIF89a
import mongoose from "mongoose";
import User from "../../models/user.model.js";
import ApiError from "../../utils/ApiErrorHandler.js";
import AsyncHandler from "../../utils/AsyncHandler.js";
import Content from "../../models/content.model.js";
import ApiResponse from "../../utils/ApiRespinseHandler.js";
import { ContentPurchase } from "../../models/contentPurchase.model.js";
import { roleContaints } from "../../validators/constaints.js";
//import { ContentPurchase } from "../../models/ContentPurchase.model.js";
// GET /api/v1/content
// Query params: ?catId= | ?role= | ?contentOf= | ?id=
export const getContents = AsyncHandler(async (req, res) => {
const { catId, role, contentOf, id } = req.query;
// Single content by id
if (id) {
if (!mongoose.Types.ObjectId.isValid(id)) {
throw new ApiError(400, "Invalid content ID");
}
const content = await Content.findById(id)
.populate({ path: "catId", select: "name" })
.populate({ path: "contentOf", select: "fullName avatar role" })
.populate({ path: "createdBy", select: "fullName" });
if (!content) throw new ApiError(404, "Content not found");
return res.status(200).json(
new ApiResponse(200, "Content fetched successfully", content)
);
}
const queryObj = {};
// Filter by category
if (catId) {
if (!mongoose.Types.ObjectId.isValid(catId)) {
throw new ApiError(400, "Invalid category ID");
}
queryObj.catId = catId;
}
// Filter by specific artist (User _id)
if (contentOf) {
if (!mongoose.Types.ObjectId.isValid(contentOf)) {
throw new ApiError(400, "Invalid artist user ID");
}
queryObj.contentOf = contentOf;
}
// Filter by role — find all users with that role first, then filter content
if (role) {
const roleInUpperCase = role.toUpperCase();
const usersWithRole = await User.find({ role: roleInUpperCase }).select("_id");
const userIds = usersWithRole.map(u => u._id);
queryObj.contentOf = { $in: userIds };
}
const contents = await Content.find(queryObj)
.populate({ path: "catId", select: "name" })
.populate({ path: "contentOf", select: "fullName avatar role" })
.populate({ path: "createdBy", select: "fullName" })
.sort({ createdAt: -1 });
return res.status(200).json(
new ApiResponse(200, "Contents fetched successfully", contents)
);
});
export const buyContent = AsyncHandler(async (req, res) => {
const { contentId } = req.params;
if (!contentId)
throw new ApiError(400, "Content ID is required");
if (!mongoose.Types.ObjectId.isValid(contentId))
throw new ApiError(400, "Invalid Content ID");
const content = await Content.findById(contentId);
if (!content)
throw new ApiError(404, "Content not found");
const userId = req.user._id || req.userId;
const alreadyPurchased = await ContentPurchase.findOne({
userId,
contentId
});
if (alreadyPurchased)
throw new ApiError(400, "Content already purchased");
const purchase = await ContentPurchase.create({
userId,
contentId,
amount: content.contentPrice
});
return res.status(200).json(
new ApiResponse(
200,
"Content purchased successfully",
purchase
)
);
});
// export const getPurchasedContents = AsyncHandler(async (req, res) => {
// const userId = req.user._id || req.userId;
// const purchases = await ContentPurchase.find({ userId })
// .populate({
// path: "contentId",
// populate: [
// {
// path: "catId",
// select: "catName"
// },
// {
// path: "contentOf",
// select: "fullName avatar"
// }
// ]
// })
// .sort({ createdAt: -1 });
// return res.status(200).json(
// new ApiResponse(
// 200,
// "Purchased contents fetched successfully",
// purchases
// )
// );
// });
export const getPurchasedContents = AsyncHandler(async (req, res) => {
const userId = req.user._id || req.userId;
const user = await User.findOne({ authId: userId })
if (!user) {
return res.status(404).json(
new ApiResponse(404, "User not found")
);
}
if (user?.role === roleContaints.admin) {
const purchases = await ContentPurchase.find()
.populate({
path: "contentId",
populate: [
{
path: "catId",
select: "catName"
},
{
path: "contentOf",
select: "fullName avatar"
}
]
})
.sort({ createdAt: -1 });
return res.status(200).json(
new ApiResponse(
200,
"Purchased contents fetched successfully",
purchases
)
);
}
const purchases = await ContentPurchase.find({ userId })
.populate({
path: "contentId",
populate: [
{
path: "catId",
select: "catName"
},
{
path: "contentOf",
select: "fullName avatar"
}
]
})
.sort({ createdAt: -1 });
return res.status(200).json(
new ApiResponse(
200,
"Purchased contents fetched successfully",
purchases
)
);
});