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 { parseUploadArray } from "../../services/parseImages.js";
import Category from "../../models/category.model.js";
import Content from "../../models/content.model.js";
import ApiResponse from "../../utils/ApiRespinseHandler.js";
import { contentStatus } from "../../validators/constaints.js";
// ============================================================
// PATCH /api/v1/admin/verify-artist/:userId
// Set isVarifiedAsArtist: true for a user (by User _id)
// ============================================================
export const verifyArtist = AsyncHandler(async (req, res) => {
const { userId } = req.params;
if (!mongoose.Types.ObjectId.isValid(userId)) {
throw new ApiError(400, "Invalid user ID");
}
const user = await User.findById(userId);
if (!user) {
throw new ApiError(404, "User not found");
}
user.isVarifiedAsArtist = true;
// Promote role to ARTIST if they are still a basic USER
if (user.role === "USER") {
user.role = "ARTIST";
}
await user.save();
return res.status(200).json(
new ApiResponse(200, "Artist verified successfully", {
_id: user._id,
fullName: user.fullName,
role: user.role,
isVarifiedAsArtist: user.isVarifiedAsArtist
})
);
});
// ============================================================
// POST /api/v1/admin/content
// Create a new content entry (admin only)
// ============================================================
export const createContent = AsyncHandler(async (req, res) => {
const {
catId,
userId,
title,
language,
about,
tags,
IsNew,
isRecommended,
status,
contentPrice,
contentAccessType
} = req.body;
if (!catId) throw new ApiError(400, "Category ID is required");
if (!userId) throw new ApiError(400, "Artist/User ID is required");
if (!contentPrice) throw new ApiError(400, "contentPrice is required");
if (!mongoose.Types.ObjectId.isValid(catId))
throw new ApiError(400, "Invalid Category ID");
if (!mongoose.Types.ObjectId.isValid(userId))
throw new ApiError(400, "Invalid User ID");
const category = await Category.findById(catId);
if (!category) throw new ApiError(404, "Category not found");
// userId here is the User._id of the artist
const artist = await User.findById(userId);
console.log('artist', artist)
if (!artist) throw new ApiError(404, "Artist not found");
const imageUploadData = parseUploadArray(req.files, "images");
const videoUploadData = parseUploadArray(req.files, "videos");
const separateTags = tags
? tags.split(",").map(tag => tag.trim()).filter(Boolean)
: [];
const contentAccessTypeInCaps = contentAccessType?.toUpperCase()
const contentStatusInCaps = status?.toUpperCase()
const content = await Content.create({
catId,
createdBy: req.user._id || req.userId,
contentOf: artist._id,
title,
about,
language: language?.toUpperCase(),
tags: separateTags,
images: imageUploadData,
videos: videoUploadData,
IsNew: IsNew === true || IsNew === "true",
isRecommended: isRecommended === true || isRecommended === "true",
status: contentStatusInCaps,
contentPrice,
contentAccessType: contentAccessTypeInCaps
});
return res.status(201).json(
new ApiResponse(201, "Content created successfully", content)
);
});
// ============================================================
// PATCH /api/v1/admin/content/:id
// Update content by id (admin only)
// ============================================================
export const updateContent = AsyncHandler(async (req, res) => {
const { id } = req.params;
if (!mongoose.Types.ObjectId.isValid(id))
throw new ApiError(400, "Invalid content ID");
const content = await Content.findById(id);
if (!content) throw new ApiError(404, "Content not found");
const {
catId,
title,
about,
language,
tags,
IsNew,
isRecommended,
status,
contentPrice,
contentAccessType
} = req.body;
const contentAccessTypeInCaps = contentAccessType.toUpperCase()
// Validate catId if provided
if (catId) {
if (!mongoose.Types.ObjectId.isValid(catId))
throw new ApiError(400, "Invalid Category ID");
const category = await Category.findById(catId);
if (!category) throw new ApiError(404, "Category not found");
content.catId = catId;
}
if (title !== undefined) content.title = title;
if (about !== undefined) content.about = about;
if (language !== undefined) content.language = language.toUpperCase();
if (status !== undefined) {
if (![contentStatus.active, contentStatus.inactive, contentStatus.draft].includes(status.toUpperCase()))
throw new ApiError(400, "Invalid status value");
content.status = status;
}
if (contentPrice !== undefined) content.contentPrice = contentPrice;
if (contentAccessType !== undefined) content.contentAccessType = contentAccessTypeInCaps
if (IsNew !== undefined)
content.IsNew = IsNew === true || IsNew === "true";
if (isRecommended !== undefined)
content.isRecommended = isRecommended === true || isRecommended === "true";
if (tags !== undefined) {
content.tags = typeof tags === "string"
? tags.split(",").map(t => t.trim()).filter(Boolean)
: tags;
}
// Handle new file uploads (append to existing arrays)
if (req.files) {
const newImages = parseUploadArray(req.files, "images");
const newVideos = parseUploadArray(req.files, "videos");
if (newImages.length > 0) content.images.push(...newImages);
if (newVideos.length > 0) content.videos.push(...newVideos);
}
await content.save();
return res.status(200).json(
new ApiResponse(200, "Content updated successfully", content)
);
});
// ============================================================
// DELETE /api/v1/admin/content/:id
// Delete content by id (admin only)
// ============================================================
export const deleteContent = AsyncHandler(async (req, res) => {
const { id } = req.params;
if (!mongoose.Types.ObjectId.isValid(id))
throw new ApiError(400, "Invalid content ID");
const content = await Content.findByIdAndDelete(id);
if (!content) throw new ApiError(404, "Content not found");
return res.status(200).json(
new ApiResponse(200, "Content deleted successfully", { _id: id })
);
});