fix: map blog authors dynamically from Strapi admin users and authorName attributes
This commit is contained in:
+18
-5
@@ -81,11 +81,24 @@ export async function getStrapiBlogs() {
|
|||||||
date: dateStr,
|
date: dateStr,
|
||||||
image: coverUrl,
|
image: coverUrl,
|
||||||
slug: b.slug,
|
slug: b.slug,
|
||||||
author: (typeof b.authorName === 'string' && b.authorName.trim())
|
author: (() => {
|
||||||
? b.authorName.trim()
|
if (typeof b.authorName === 'string' && b.authorName.trim()) return b.authorName.trim();
|
||||||
: (typeof b.author === 'string' && b.author.trim()
|
if (typeof b.author === 'string' && b.author.trim()) return b.author.trim();
|
||||||
? b.author.trim()
|
const authorObj = b.author || b.createdBy;
|
||||||
: (b.author?.firstname ? `${b.author.firstname} ${b.author.lastname || ''}`.trim() : (b.author?.username || 'Cavin Editorial Team'))),
|
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',
|
authorDesignation: b.authorDesignation || 'Chief AI Architect & Head of Data Engineering',
|
||||||
editor: (typeof b.editorName === 'string' && b.editorName.trim())
|
editor: (typeof b.editorName === 'string' && b.editorName.trim())
|
||||||
? b.editorName.trim()
|
? b.editorName.trim()
|
||||||
|
|||||||
@@ -1,3 +1,92 @@
|
|||||||
import { factories } from '@strapi/strapi';
|
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<number, string>();
|
||||||
|
const userDesigMap = new Map<number, string>();
|
||||||
|
|
||||||
|
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<number, string>();
|
||||||
|
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<number, string>();
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
|||||||
Reference in New Issue
Block a user