|
| 1 | +import { Context } from 'koa'; |
| 2 | +import { UID, Schema } from '@strapi/strapi'; |
| 3 | +import { errors } from '@strapi/utils'; |
| 4 | +import { isContentTypeEnabled } from '../util/enabledContentTypes'; |
| 5 | +import { getPluginService } from '../util/getPluginService'; |
| 6 | + |
| 7 | +interface DocumentEntry { |
| 8 | + id: number; |
| 9 | + documentId: string; |
| 10 | + [key: string]: unknown; // Dynamic fields like title, etc. |
| 11 | +} |
| 12 | + |
| 13 | +interface SearchResult extends DocumentEntry { |
| 14 | + contentType: string; |
| 15 | +} |
| 16 | + |
| 17 | +/** |
| 18 | + * Search controller |
| 19 | + */ |
| 20 | +export default { |
| 21 | + search: async (ctx: Context & { params: { id: number } }) => { |
| 22 | + const { q } = ctx.query; |
| 23 | + const results: SearchResult[] = []; |
| 24 | + |
| 25 | + const qStr = typeof q === 'string' ? q.trim() : ''; |
| 26 | + if (!qStr) { |
| 27 | + throw new errors.ValidationError('Missing or invalid query parameter "?q=" (must be a non-empty string)'); |
| 28 | + } |
| 29 | + |
| 30 | + await Promise.all( |
| 31 | + Object.entries(strapi.contentTypes).map( |
| 32 | + async ([uid, config]: [UID.ContentType, Schema.ContentType]) => { |
| 33 | + const hasWT = isContentTypeEnabled(config); |
| 34 | + if (!hasWT) return; |
| 35 | + |
| 36 | + const mainField = await getPluginService('get-main-field').getMainField(uid); |
| 37 | + if (!mainField) return; |
| 38 | + |
| 39 | + const fieldsArr: string[] = ['documentId', ...(mainField ? [mainField] : [])]; |
| 40 | + const entries = (await strapi.documents(uid).findMany({ |
| 41 | + filters: { |
| 42 | + [mainField]: { $containsi: qStr }, |
| 43 | + }, |
| 44 | + // @ts-expect-error |
| 45 | + fields: fieldsArr, |
| 46 | + })); |
| 47 | + |
| 48 | + if (!entries || entries.length === 0) return; |
| 49 | + |
| 50 | + const entriesWithContentType: SearchResult[] = entries.map((entry: DocumentEntry) => ({ |
| 51 | + ...entry, |
| 52 | + contentType: uid, |
| 53 | + })); |
| 54 | + |
| 55 | + results.push(...entriesWithContentType); |
| 56 | + }, |
| 57 | + ), |
| 58 | + ); |
| 59 | + |
| 60 | + ctx.body = results; |
| 61 | + }, |
| 62 | + reverseSearch: async (ctx: Context & { params: { contentType: string; documentId: string } }) => { |
| 63 | + const { contentType, documentId } = ctx.params; |
| 64 | + |
| 65 | + if (typeof contentType !== 'string' || !(contentType in strapi.contentTypes)) { |
| 66 | + throw new errors.ValidationError(`Unknown or invalid content type: ${contentType}`); |
| 67 | + } |
| 68 | + |
| 69 | + const mainField = await getPluginService('get-main-field').getMainField( |
| 70 | + contentType as UID.ContentType, |
| 71 | + ); |
| 72 | + // eslint-disable-next-line max-len |
| 73 | + const fieldsArr: string[] = ['id', 'documentId', ...(mainField ? [mainField] : [])]; |
| 74 | + |
| 75 | + const entry = (await strapi |
| 76 | + .documents(contentType as UID.ContentType) |
| 77 | + .findOne({ |
| 78 | + documentId, |
| 79 | + // @ts-expect-error |
| 80 | + fields: fieldsArr, |
| 81 | + })); |
| 82 | + |
| 83 | + if (!entry) { |
| 84 | + throw new errors.NotFoundError('Entry not found'); |
| 85 | + } |
| 86 | + |
| 87 | + ctx.body = { |
| 88 | + id: entry.id, |
| 89 | + documentId: entry.documentId, |
| 90 | + // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment |
| 91 | + ...(mainField ? { [mainField]: entry[mainField] } : {}), |
| 92 | + }; |
| 93 | + }, |
| 94 | +}; |
0 commit comments