import type { DataProvider, Identifier } from "ra-core"; import { COMPANY_CREATED, COMPANY_NOTE_CREATED, CONTACT_CREATED, CONTACT_NOTE_CREATED, DEAL_CREATED, DEAL_NOTE_CREATED, } from "../../consts"; import type { Activity, Company, CompanyNote, Contact, ContactNote, Deal, DealNote, } from "../../types"; // PERFORMANCE FIX: Reduced from 250 to 50 records per query // This changes from fetching 1,250 records (5 queries × 250) to just 250 records (5 queries × 50) // Dashboard only shows 10-20 activities, so 50 per query is more than enough // TODO: Replace with a server-side view or edge function for even better performance export async function getActivityLog( dataProvider: DataProvider, companyId?: Identifier, salesId?: Identifier, ) { const companyFilter = {} as any; if (companyId) { companyFilter.id = companyId; } else if (salesId) { companyFilter["sales_id@in"] = `(${salesId})`; } const filter = {} as any; if (companyId) { filter.company_id = companyId; } else if (salesId) { filter["sales_id@in"] = `(${salesId})`; } const [newCompanies, newContactsAndNotes, newDealsAndNotes, newCompanyNotes] = await Promise.all([ getNewCompanies(dataProvider, companyFilter), getNewContactsAndNotes(dataProvider, filter), getNewDealsAndNotes(dataProvider, filter), getNewCompanyNotes(dataProvider, filter), ]); return ( [ ...newCompanies, ...newContactsAndNotes, ...newDealsAndNotes, ...newCompanyNotes, ] // sort by date desc .sort((a, b) => a.date && b.date ? a.date.localeCompare(b.date) * -1 : 0, ) // limit to 50 activities (reduced from 250 for performance) .slice(0, 50) ); } const getNewCompanies = async ( dataProvider: DataProvider, filter: any, ): Promise => { const { data: companies } = await dataProvider.getList("companies", { filter, pagination: { page: 1, perPage: 50 }, // Reduced from 250 sort: { field: "created_at", order: "DESC" }, }); return companies.map((company) => ({ id: `company.${company.id}.created`, type: COMPANY_CREATED, company_id: company.id, company, sales_id: company.sales_id, date: company.created_at, })); }; async function getNewContactsAndNotes( dataProvider: DataProvider, filter: any, ): Promise { const { data: contacts } = await dataProvider.getList("contacts", { filter, pagination: { page: 1, perPage: 50 }, // Reduced from 250 sort: { field: "first_seen", order: "DESC" }, }); const recentContactNotesFilter = {} as any; if (filter.sales_id) { recentContactNotesFilter.sales_id = filter.sales_id; } if (filter.company_id) { // No company_id field in contactNote, filtering by related contacts instead. // This filter is only valid if a company has less than 50 contacts. const contactIds = contacts.map((contact) => contact.id).join(","); recentContactNotesFilter["contact_id@in"] = `(${contactIds})`; } const { data: contactNotes } = await dataProvider.getList( "contactNotes", { filter: recentContactNotesFilter, pagination: { page: 1, perPage: 50 }, // Reduced from 250 sort: { field: "date", order: "DESC" }, }, ); const newContacts = contacts.map((contact) => ({ id: `contact.${contact.id}.created`, type: CONTACT_CREATED, company_id: contact.company_id, sales_id: contact.sales_id, contact, date: contact.first_seen, })); const newContactNotes = contactNotes.map((contactNote) => ({ id: `contactNote.${contactNote.id}.created`, type: CONTACT_NOTE_CREATED, sales_id: contactNote.sales_id, contactNote, date: contactNote.date, })); return [...newContacts, ...newContactNotes]; } async function getNewDealsAndNotes( dataProvider: DataProvider, filter: any, ): Promise { const { data: deals } = await dataProvider.getList("deals", { filter, pagination: { page: 1, perPage: 50 }, // Reduced from 250 sort: { field: "created_at", order: "DESC" }, }); const recentDealNotesFilter = {} as any; if (filter.sales_id) { recentDealNotesFilter.sales_id = filter.sales_id; } if (filter.company_id) { // No company_id field in dealNote, filtering by related deals instead. // This filter is only valid if a deal has less than 50 notes. const dealIds = deals.map((deal) => deal.id).join(","); recentDealNotesFilter["deal_id@in"] = `(${dealIds})`; } const { data: dealNotes } = await dataProvider.getList( "dealNotes", { filter: recentDealNotesFilter, pagination: { page: 1, perPage: 50 }, // Reduced from 250 sort: { field: "date", order: "DESC" }, }, ); const newDeals = deals.map((deal) => ({ id: `deal.${deal.id}.created`, type: DEAL_CREATED, company_id: deal.company_id, sales_id: deal.sales_id, deal, date: deal.created_at, })); const newDealNotes = dealNotes.map((dealNote) => ({ id: `dealNote.${dealNote.id}.created`, type: DEAL_NOTE_CREATED, sales_id: dealNote.sales_id, dealNote, date: dealNote.date, })); return [...newDeals, ...newDealNotes]; } async function getNewCompanyNotes( dataProvider: DataProvider, filter: any, ): Promise { const recentCompanyNotesFilter = {} as any; if (filter.sales_id) { recentCompanyNotesFilter.sales_id = filter.sales_id; } if (filter.company_id) { recentCompanyNotesFilter.company_id = filter.company_id; } const { data: companyNotes } = await dataProvider.getList( "companyNotes", { filter: recentCompanyNotesFilter, pagination: { page: 1, perPage: 50 }, // Reduced from 250 sort: { field: "date", order: "DESC" }, }, ); const newCompanyNotes = companyNotes.map((companyNote) => ({ id: `companyNote.${companyNote.id}.created`, type: COMPANY_NOTE_CREATED, sales_id: companyNote.sales_id, companyNote, date: companyNote.date, })); return newCompanyNotes; }