Source

utils/investigation-response.utils.js

import { fetchUserProfile } from "./user-profile.utils";
/**
 * Enriches an investigation response DTO with detailed author and editor profile information.
 *
 * Fetches user profiles from the Auth gRPC service (or cache if provided) and populates
 * the `metadata.author` and `metadata.editorsDetailed` fields with user details such as
 * name, email, and avatar.
 * @category Utils
 * @param {IInvestigation} investigation - The investigation object containing metadata.
 * @param {IInvestigationResponseDto} dto - The investigation response DTO to enrich.
 * @param {AuthGrpcService} grpcClient - The Auth gRPC client used to fetch user profiles.
 * @param {Map<string, IUserProfile | Promise<IUserProfile>>} profileCache - Optional cache to store/reuse fetched user profiles.
 * @returns {Promise<void>} - Enriches the `dto` object in-place.
 */
export async function enrichInvestigationResponse(investigation, dto, grpcClient, profileCache) {
    const metadata = dto.metadata;
    const authorId = investigation.metadata?.author?.toString() ?? metadata.author?.id ?? null;
    if (authorId) {
        const profile = await fetchUserProfile(authorId, profileCache, grpcClient);
        metadata.author = {
            id: authorId,
            name: profile.name,
            email: profile.email,
            avatar: profile.avatar,
        };
    }
    else {
        metadata.author = {
            id: null,
            name: null,
            email: null,
            avatar: null,
        };
    }
    if (Array.isArray(investigation.metadata?.editors) && investigation.metadata.editors.length) {
        const editors = await Promise.all(investigation.metadata.editors.map(async (editorId) => {
            const id = editorId?.toString();
            if (!id) {
                return { id: "", name: null, email: null, avatar: null };
            }
            const profile = await fetchUserProfile(id, profileCache, grpcClient);
            return {
                id,
                name: profile.name,
                email: profile.email,
                avatar: profile.avatar,
            };
        }));
        metadata.editorsDetailed = editors.filter((editor) => Boolean(editor.id));
    }
    else {
        metadata.editorsDetailed = [];
    }
}