|
1 | 1 | import { Request, Response } from 'express'; |
2 | 2 | import { asyncHandler } from '../utils/asyncHandler.js'; |
3 | | -import { sendError } from '../utils/apiResponses.js'; |
| 3 | +import { sendSuccess, sendBadRequest, sendNotFound } from '../utils/apiResponses.js'; |
| 4 | +import Resource from '../models/resourceModel.js'; |
| 5 | +import { validateResource } from '../schemas/resourceSchema.js'; |
| 6 | +import { deleteFile } from '../middleware/uploadMiddleware.js'; |
4 | 7 |
|
5 | | -// Stub CRUD handlers for Resources |
| 8 | +// Get all resources with optional search |
6 | 9 | export const getResources = asyncHandler(async (req: Request, res: Response) => { |
7 | | - return sendError(res, 501, 'Not implemented'); |
8 | | -}); |
9 | | - |
10 | | -export const getResourceById = asyncHandler(async (req: Request, res: Response) => { |
11 | | - return sendError(res, 501, 'Not implemented'); |
12 | | -}); |
| 10 | + const { search } = req.query; |
| 11 | + |
| 12 | + const query: any = {}; |
| 13 | + |
| 14 | + // Apply search filter across multiple fields |
| 15 | + if (search && typeof search === 'string') { |
| 16 | + const searchTerm = search.trim(); |
13 | 17 |
|
14 | | -export const getResourcesByLocation = asyncHandler(async (req: Request, res: Response) => { |
15 | | - return sendError(res, 501, 'Not implemented'); |
| 18 | + query.$or = [ |
| 19 | + { Key: { $regex: searchTerm, $options: 'i' } }, |
| 20 | + { Name: { $regex: searchTerm, $options: 'i' } }, |
| 21 | + { Header: { $regex: searchTerm, $options: 'i' } }, |
| 22 | + { ShortDescription: { $regex: searchTerm, $options: 'i' } }, |
| 23 | + { Body: { $regex: searchTerm, $options: 'i' } }, |
| 24 | + { 'LinkList.Name': { $regex: searchTerm, $options: 'i' } }, |
| 25 | + { 'LinkList.Description': { $regex: searchTerm, $options: 'i' } }, |
| 26 | + { 'LinkList.Links.Title': { $regex: searchTerm, $options: 'i' } }, |
| 27 | + { 'LinkList.Links.Link': { $regex: searchTerm, $options: 'i' } } |
| 28 | + ]; |
| 29 | + } |
| 30 | + |
| 31 | + const resources = await Resource.find(query) |
| 32 | + .sort({ DocumentModifiedDate: -1 }) |
| 33 | + .lean(); |
| 34 | + |
| 35 | + return sendSuccess(res, resources); |
16 | 36 | }); |
17 | 37 |
|
18 | | -export const createResource = asyncHandler(async (req: Request, res: Response) => { |
19 | | - return sendError(res, 501, 'Not implemented'); |
| 38 | +// Get single resource by key |
| 39 | +export const getResourceByKey = asyncHandler(async (req: Request, res: Response) => { |
| 40 | + const { key } = req.params; |
| 41 | + |
| 42 | + const resource = await Resource.findOne({ Key: key }).lean(); |
| 43 | + |
| 44 | + if (!resource) { |
| 45 | + return sendNotFound(res, 'Resource not found'); |
| 46 | + } |
| 47 | + |
| 48 | + return sendSuccess(res, resource); |
20 | 49 | }); |
21 | 50 |
|
| 51 | +// Update existing resource |
22 | 52 | export const updateResource = asyncHandler(async (req: Request, res: Response) => { |
23 | | - return sendError(res, 501, 'Not implemented'); |
24 | | -}); |
| 53 | + const { key } = req.params; |
| 54 | + |
| 55 | + // Find existing resource |
| 56 | + const existingResource = await Resource.findOne({ Key: key }); |
| 57 | + |
| 58 | + if (!existingResource) { |
| 59 | + return sendNotFound(res, 'Resource not found'); |
| 60 | + } |
| 61 | + |
| 62 | + // Process file replacements - extract old file URLs for cleanup |
| 63 | + const oldFileUrls: string[] = []; |
| 64 | + |
| 65 | + // Collect all existing file URLs from the resource |
| 66 | + if (existingResource.LinkList) { |
| 67 | + existingResource.LinkList.forEach((linkList) => { |
| 68 | + if (linkList.Links && Array.isArray(linkList.Links)) { |
| 69 | + linkList.Links.forEach((item) => { |
| 70 | + if (item.Link && item.Link.startsWith('http')) { |
| 71 | + oldFileUrls.push(item.Link); |
| 72 | + } |
| 73 | + }); |
| 74 | + } |
| 75 | + }); |
| 76 | + } |
| 77 | + |
| 78 | + // Process form data and merge uploaded files |
| 79 | + const processedData = processResourceFormData(req.body); |
| 80 | + |
| 81 | + // Validate the processed data |
| 82 | + const validation = validateResource(processedData); |
| 83 | + |
| 84 | + if (!validation.success) { |
| 85 | + const errorMessages = validation.errors?.map(err => err.message).join(', ') || 'Validation failed'; |
| 86 | + return sendBadRequest(res, `Validation failed: ${errorMessages}`); |
| 87 | + } |
25 | 88 |
|
26 | | -export const deleteResource = asyncHandler(async (req: Request, res: Response) => { |
27 | | - return sendError(res, 501, 'Not implemented'); |
| 89 | + if (!validation.data) { |
| 90 | + return sendBadRequest(res, 'Validation data is missing'); |
| 91 | + } |
| 92 | + |
| 93 | + // Update resource |
| 94 | + const updateData = { |
| 95 | + ...validation.data, |
| 96 | + DocumentModifiedDate: new Date(), |
| 97 | + }; |
| 98 | + |
| 99 | + const updatedResource = await Resource.findOneAndUpdate( |
| 100 | + { Key: key }, |
| 101 | + updateData, |
| 102 | + { new: true, runValidators: true } |
| 103 | + ); |
| 104 | + |
| 105 | + if (!updatedResource) { |
| 106 | + return sendNotFound(res, 'Resource not found'); |
| 107 | + } |
| 108 | + |
| 109 | + // Clean up old files that are no longer in the updated resource (non-blocking) |
| 110 | + if (oldFileUrls.length > 0 && updatedResource.LinkList) { |
| 111 | + const newFileUrls = new Set<string>(); |
| 112 | + updatedResource.LinkList.forEach((linkList) => { |
| 113 | + if (linkList.Links && Array.isArray(linkList.Links)) { |
| 114 | + linkList.Links.forEach((item) => { |
| 115 | + if (item.Link && item.Link.startsWith('http')) { |
| 116 | + newFileUrls.add(item.Link); |
| 117 | + } |
| 118 | + }); |
| 119 | + } |
| 120 | + }); |
| 121 | + |
| 122 | + // Only delete files that are no longer referenced |
| 123 | + const filesToDelete = oldFileUrls.filter(url => !newFileUrls.has(url)); |
| 124 | + if (filesToDelete.length > 0) { |
| 125 | + Promise.all(filesToDelete.map(url => deleteFile(url))) |
| 126 | + .catch(error => console.error('Error cleaning up old files:', error)); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + return sendSuccess(res, updatedResource); |
28 | 131 | }); |
| 132 | + |
| 133 | +// Helper function to process form data and merge uploaded files |
| 134 | +function processResourceFormData(body: any): any { |
| 135 | + const data = { ...body }; |
| 136 | + |
| 137 | + // Parse LinkList if it's a string |
| 138 | + if (typeof data.LinkList === 'string') { |
| 139 | + try { |
| 140 | + data.LinkList = JSON.parse(data.LinkList); |
| 141 | + } catch (error) { |
| 142 | + console.error('Error parsing LinkList:', error); |
| 143 | + data.LinkList = []; |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + // Process LinkList to merge uploaded file URLs |
| 148 | + if (data.LinkList && Array.isArray(data.LinkList)) { |
| 149 | + data.LinkList = data.LinkList.map((linkList: any, listIndex: number) => { |
| 150 | + if (linkList.Links && Array.isArray(linkList.Links)) { |
| 151 | + linkList.Links = linkList.Links.map((item: any, itemIndex: number) => { |
| 152 | + const fieldName = `newfile_LinkList_${listIndex}_Links_${itemIndex}`; |
| 153 | + const uploadedFileUrl = data[fieldName]; |
| 154 | + |
| 155 | + // If file was uploaded for this item, use the uploaded URL |
| 156 | + if (uploadedFileUrl) { |
| 157 | + return { |
| 158 | + ...item, |
| 159 | + Link: uploadedFileUrl |
| 160 | + }; |
| 161 | + } |
| 162 | + |
| 163 | + return item; |
| 164 | + }); |
| 165 | + } |
| 166 | + |
| 167 | + return linkList; |
| 168 | + }); |
| 169 | + } |
| 170 | + |
| 171 | + // Remove file field entries from data (they're already merged into LinkList) |
| 172 | + Object.keys(data).forEach(key => { |
| 173 | + if (key.startsWith('newfile_LinkList_')) { |
| 174 | + delete data[key]; |
| 175 | + } |
| 176 | + }); |
| 177 | + |
| 178 | + return data; |
| 179 | +} |
0 commit comments