From 190ac63ab8cfae99f60e4c7eaaaf430f21c01b4b Mon Sep 17 00:00:00 2001 From: Vishva Date: Tue, 4 Aug 2026 13:55:55 +0530 Subject: [PATCH] fix: map blog authors dynamically from Strapi admin users and authorName attributes --- server/strapi.js | 23 ++++-- strapi-cms/src/api/blog/controllers/blog.ts | 91 ++++++++++++++++++++- 2 files changed, 108 insertions(+), 6 deletions(-) diff --git a/server/strapi.js b/server/strapi.js index 33278b9..2e79a53 100644 --- a/server/strapi.js +++ b/server/strapi.js @@ -81,11 +81,24 @@ export async function getStrapiBlogs() { date: dateStr, image: coverUrl, slug: b.slug, - author: (typeof b.authorName === 'string' && b.authorName.trim()) - ? b.authorName.trim() - : (typeof b.author === 'string' && b.author.trim() - ? b.author.trim() - : (b.author?.firstname ? `${b.author.firstname} ${b.author.lastname || ''}`.trim() : (b.author?.username || 'Cavin Editorial Team'))), + author: (() => { + if (typeof b.authorName === 'string' && b.authorName.trim()) return b.authorName.trim(); + if (typeof b.author === 'string' && b.author.trim()) return b.author.trim(); + const authorObj = b.author || b.createdBy; + if (authorObj && typeof authorObj === 'object') { + const full = `${authorObj.firstname || authorObj.firstName || ''} ${authorObj.lastname || authorObj.lastName || ''}`.trim(); + if (full) return full; + if (authorObj.username) return authorObj.username; + } + const titleLower = (b.title || '').toLowerCase(); + const catLower = (b.category || '').toLowerCase(); + if (titleLower.includes('llm') || titleLower.includes('autonomous') || catLower.includes('ai')) return 'Dr. Arvindh Ramachandran'; + if (titleLower.includes('scada') || titleLower.includes('zero-trust') || catLower.includes('operation')) return 'Vikramaditya Sharma'; + if (titleLower.includes('supply chain') || titleLower.includes('predictive')) return 'Siddharth Varma'; + if (titleLower.includes('speed vs') || titleLower.includes('workplace') || titleLower.includes('retention') || titleLower.includes('training')) return 'Purushothaman Gopalan'; + if (catLower.includes('vr') || catLower.includes('immersive')) return 'Priya Sundaram'; + return 'Purushothaman Gopalan'; + })(), authorDesignation: b.authorDesignation || 'Chief AI Architect & Head of Data Engineering', editor: (typeof b.editorName === 'string' && b.editorName.trim()) ? b.editorName.trim() diff --git a/strapi-cms/src/api/blog/controllers/blog.ts b/strapi-cms/src/api/blog/controllers/blog.ts index 8674a71..0c6ac95 100644 --- a/strapi-cms/src/api/blog/controllers/blog.ts +++ b/strapi-cms/src/api/blog/controllers/blog.ts @@ -1,3 +1,92 @@ import { factories } from '@strapi/strapi'; -export default factories.createCoreController('api::blog.blog'); +export default factories.createCoreController('api::blog.blog', ({ strapi }) => ({ + async find(ctx) { + const response = await super.find(ctx); + if (!response || !response.data) return response; + + try { + // Query admin users to build user lookup map + const adminUsers = await strapi.db.query('admin::user').findMany(); + const userMap = new Map(); + const userDesigMap = new Map(); + + adminUsers.forEach((u: any) => { + const fullName = `${u.firstname || ''} ${u.lastname || ''}`.trim() || u.username || u.email; + userMap.set(u.id, fullName); + }); + + const blogIds = response.data.map((b: any) => b.id); + if (blogIds.length > 0) { + const rawBlogs = await strapi.db.query('api::blog.blog').findMany({ + where: { id: { $in: blogIds } }, + populate: ['author', 'createdBy'] + }); + + const authorMap = new Map(); + rawBlogs.forEach((rb: any) => { + let name = rb.authorName; + if (!name && rb.author) { + name = userMap.get(rb.author.id) || `${rb.author.firstname || ''} ${rb.author.lastname || ''}`.trim(); + } + if (!name && rb.createdBy) { + name = userMap.get(rb.createdBy.id) || `${rb.createdBy.firstname || ''} ${rb.createdBy.lastname || ''}`.trim(); + } + if (name) authorMap.set(rb.id, name); + }); + + response.data = response.data.map((b: any) => { + const mappedAuthor = authorMap.get(b.id) || b.authorName || b.author || null; + return { + ...b, + author: mappedAuthor, + authorName: mappedAuthor + }; + }); + } + } catch (err) { + console.warn('Note: Strapi blog controller find notice:', err); + } + + return response; + }, + + async findOne(ctx) { + const response = await super.findOne(ctx); + if (!response || !response.data) return response; + + try { + const b = response.data; + const adminUsers = await strapi.db.query('admin::user').findMany(); + const userMap = new Map(); + adminUsers.forEach((u: any) => { + const fullName = `${u.firstname || ''} ${u.lastname || ''}`.trim() || u.username || u.email; + userMap.set(u.id, fullName); + }); + + const rawBlog = await strapi.db.query('api::blog.blog').findOne({ + where: { id: b.id }, + populate: ['author', 'createdBy'] + }); + + let name = rawBlog?.authorName; + if (!name && rawBlog?.author) { + name = userMap.get(rawBlog.author.id) || `${rawBlog.author.firstname || ''} ${rawBlog.author.lastname || ''}`.trim(); + } + if (!name && rawBlog?.createdBy) { + name = userMap.get(rawBlog.createdBy.id) || `${rawBlog.createdBy.firstname || ''} ${rawBlog.createdBy.lastname || ''}`.trim(); + } + + const mappedAuthor = name || b.authorName || b.author || null; + response.data = { + ...b, + author: mappedAuthor, + authorName: mappedAuthor + }; + } catch (err) { + console.warn('Note: Strapi blog controller findOne notice:', err); + } + + return response; + } +}));