import { InjectRepository } from '@nestjs/typeorm'; import { MediaDataService } from 'src/module/meta/service/media-data.service'; import { ActivityLog } from '../entity/activity-log.entity'; import { Repository } from 'typeorm'; import * as moment from 'moment'; import axios from 'axios'; import { ConfigService } from '@nestjs/config'; export const ACTIVITY_CATEGORIES = { ASSIGN: 'ASSIGN', INTERACTION: 'INTERACTION', FORM: 'FORM', STAGE: 'STAGE', ASSESSMENT: 'ASSESSMENT', MEETING: 'MEETING', PROCESS: 'PROCESS', STATUS: 'STATUS', TASK: 'TASK', LEAD: 'LEAD', STAGEGROUP: 'STAGE_GROUP', } as const; export type ActivityCategoryType = keyof typeof ACTIVITY_CATEGORIES; export class ActivityLogRepository { constructor( @InjectRepository(ActivityLog) private readonly activityLogRepository: Repository, private readonly mediaDataService: MediaDataService, private readonly configService: ConfigService, ) {} async getAllActivityLog( mapped_entity_type: string, mapped_entity_id: number | string, category: string | undefined, loggedInUser, ) { const { organization_id, id: loggedInUserId } = loggedInUser; const query = this.activityLogRepository .createQueryBuilder('log') .where('log.mapped_entity_type = :mapped_entity_type', { mapped_entity_type, }) .andWhere('log.organization_id = :organization_id', { organization_id }) .andWhere('log.mapped_entity_id = :mapped_entity_id', { mapped_entity_id, }); if (category) { query.andWhere('log.category = :category', { category }); } query.orderBy('log.created_date', 'DESC'); const rows = await query.getRawAndEntities(); if (rows?.entities) { let user: any = null; for (const param of rows?.entities as any) { try { const baseUrl = this.configService.get('REDIRECT_BE_URL'); // Prepare the query string const queryParams = new URLSearchParams({ loggedInUser: JSON.stringify(loggedInUser), }).toString(); // Make the GET request with query parameters const response = await axios.get( `${baseUrl}/users/profile-image-url/${param?.created_by}?entity_type=USR&${queryParams}`, { headers: { 'Content-Type': 'application/json', }, }, ); user = response.data; param.user_name = user.name; param.profile_image = user.profile_image; } catch (error) { console.error('⚠️ Internal Entity API call failed:', error.message); } } } const result = await Promise.all( rows.entities.map(async (entity, i) => { const profileImageId = rows.raw[i].profile_image; let profile: any = null; // if (profileImageId) { // profile = await this.mediaDataService.getMediaDownloadUrl( // profileImageId, // loggedInUser, // ); // } const formattedLabel = entity.category .toLowerCase() // stage_group .replace(/_/g, ' ') // stage group .replace(/\b\w/g, (char) => char.toUpperCase()); // Stage Group return { ...entity, category: formattedLabel, created_date: entity.created_date ? moment(entity.created_date).local().format('DD-MMM, hh:mm A') : null, }; }), ); return result; } async getAllActivityCategory( mapped_entity_id: number | string, mapped_entity_type: string, loggedInUser: any, ) { const { organization_id } = loggedInUser; const result = await this.activityLogRepository .createQueryBuilder('log') .select('DISTINCT UPPER(log.category)', 'category') .where('log.mapped_entity_type = :mapped_entity_type', { mapped_entity_type, }) .andWhere('log.organization_id = :organization_id', { organization_id }) .andWhere('log.mapped_entity_id = :mapped_entity_id', { mapped_entity_id, }) .getRawMany(); return result.map((row) => { const formattedLabel = row.category .toLowerCase() // stage_group .replace(/_/g, ' ') // stage group .replace(/\b\w/g, (char) => char.toUpperCase()); // Stage Group return { label: formattedLabel, value: row.category, }; }); } }