|
| 1 | +import { NextFunction, Request, Response } from 'express'; |
| 2 | +import { RouteDefinition } from 'types/route-definition'; |
| 3 | +import BaseController from '../../abstractions/base.controller'; |
| 4 | +import { Record, Resource } from '@models'; |
| 5 | +import { Types } from 'mongoose'; |
| 6 | + |
| 7 | +/** |
| 8 | + * File controller |
| 9 | + */ |
| 10 | +export default class FileController extends BaseController { |
| 11 | + public basePath = 'file'; |
| 12 | + |
| 13 | + /** @returns List of available routes */ |
| 14 | + public routes(): RouteDefinition[] { |
| 15 | + return [ |
| 16 | + { |
| 17 | + path: '/item/:itemId/associated-record', |
| 18 | + method: 'get', |
| 19 | + handler: this.findAssociatedRecordId.bind(this), |
| 20 | + }, |
| 21 | + ]; |
| 22 | + } |
| 23 | + |
| 24 | + /** |
| 25 | + * Find the associated record ID for a given drive and item. |
| 26 | + * |
| 27 | + * @param req Express request |
| 28 | + * @param res Express response |
| 29 | + * @param next Express next function |
| 30 | + * @returns Promise<void> - Sends the associated record ID in the response or an error if not found. |
| 31 | + */ |
| 32 | + public async findAssociatedRecordId( |
| 33 | + req: Request, |
| 34 | + res: Response, |
| 35 | + next: NextFunction |
| 36 | + ): Promise<void> { |
| 37 | + try { |
| 38 | + const { itemId } = req.params; |
| 39 | + const { resourceId } = req.query; |
| 40 | + if (!resourceId && typeof resourceId !== 'string') { |
| 41 | + res.status(400).send({ error: 'Resource ID is required' }); |
| 42 | + return; |
| 43 | + } |
| 44 | + if (!itemId) { |
| 45 | + res.status(400).send({ error: 'Item ID is required' }); |
| 46 | + return; |
| 47 | + } |
| 48 | + |
| 49 | + const [resource] = await Resource.aggregate([ |
| 50 | + { $match: { _id: new Types.ObjectId(String(resourceId)) } }, |
| 51 | + { |
| 52 | + $project: { |
| 53 | + fileFields: { |
| 54 | + $filter: { |
| 55 | + input: '$fields', |
| 56 | + cond: { $eq: ['$$this.type', 'file'] }, |
| 57 | + }, |
| 58 | + }, |
| 59 | + }, |
| 60 | + }, |
| 61 | + ]); |
| 62 | + |
| 63 | + if (!resource) { |
| 64 | + res.status(404).send({ error: 'Resource not found' }); |
| 65 | + return; |
| 66 | + } |
| 67 | + |
| 68 | + // Now use the file fields to build the query for Record |
| 69 | + const fileFieldQueries = resource.fileFields.map((field) => ({ |
| 70 | + [`data.${field.name}`]: { |
| 71 | + $elemMatch: { |
| 72 | + 'content.itemId': { $regex: new RegExp(`^${itemId}$`, 'i') }, |
| 73 | + // 'content.driveId': { $regex: new RegExp(`^${driveId}$`, 'i') }, |
| 74 | + }, |
| 75 | + }, |
| 76 | + })); |
| 77 | + |
| 78 | + const associatedRecord = await Record.findOne({ |
| 79 | + resource: new Types.ObjectId(String(resourceId)), |
| 80 | + archived: { $ne: true }, |
| 81 | + $or: fileFieldQueries, |
| 82 | + }).select('_id'); |
| 83 | + |
| 84 | + if (!associatedRecord) { |
| 85 | + res.status(404).send({ error: 'Record not found' }); |
| 86 | + return; |
| 87 | + } |
| 88 | + |
| 89 | + res.locals.data = associatedRecord._id; |
| 90 | + this.send(res); |
| 91 | + } catch (err) { |
| 92 | + next(err); |
| 93 | + } |
| 94 | + } |
| 95 | +} |
0 commit comments