diff --git a/app/api/reports/update/route.ts b/app/api/reports/update/route.ts index 3fbfe165..4e814a34 100644 --- a/app/api/reports/update/route.ts +++ b/app/api/reports/update/route.ts @@ -1,9 +1,10 @@ -import { fetchNewReports } from "@/lib/impact-reports"; +import { fetchNewReports, updateCMSContents } from "@/lib/impact-reports"; import { NextResponse } from "next/server"; export async function GET() { try { await fetchNewReports(); + await updateCMSContents(); return NextResponse.json({ status: 200 }); } catch (error) { let errorMessage = "An unknown error occurred"; diff --git a/lib/directus.ts b/lib/directus.ts index dbcd84c9..ff2d998f 100644 --- a/lib/directus.ts +++ b/lib/directus.ts @@ -183,6 +183,35 @@ export const getCMSReports = async (): Promise => { } }; +/** + * Update the latest contents of the CMS `reports` collection. + * @returns A promise that resolves to an array of CMS contents. + * @throws Will throw an error if the CMS contents cannot be fetched. + */ +export const updateCMSReports = async (): Promise => { + const client = getDirectusClient(); + + try { + console.log("[Directus] Fetching CMS contents from remote"); + const response = await client.request( + readItems('reports', { + filter: { + status: { + _eq: 'published', + } + }, + }) + ); + CMSReports = response as CMSContent[]; + console.log("[Directus] fetched CMS contents: ", CMSReports.length); + + return CMSReports; + } catch (error) { + console.error(`[Directus] Failed to fetch CMS contents: ${error}`); + throw new Error(`[Directus] Failed to fetch CMS contents: ${error}`); + } +}; + /** * Retrieves the total funded amount for a given hypercert by its ID. * It fetches all contributions related to the hypercert and sums up their amounts. diff --git a/lib/impact-reports.ts b/lib/impact-reports.ts index a766882a..d5120f36 100644 --- a/lib/impact-reports.ts +++ b/lib/impact-reports.ts @@ -6,7 +6,7 @@ import { import { Mutex } from "async-mutex"; import type { Report } from "@/types"; -import { getCMSReports, getFundedAmountByHCId } from "./directus"; +import { getCMSReports, getFundedAmountByHCId, updateCMSReports } from "./directus"; import { getOrders } from "./marketplace"; import { delay } from "./utils"; import { getHypercertsByOwner } from "@/hypercerts/getHypercertsByOwner"; @@ -236,24 +236,17 @@ const updateReports = async (): Promise => { ); } - const fromCMS = await getCMSReports(); + const fromCMS = await updateCMSReports(); - const existingReportIds = reports - ? reports.map((report) => report.hypercertId) - : []; const reportsToUpdatePromises = claims .filter( (claim) => - claim.hypercert_id && !existingReportIds.includes(claim.hypercert_id), + claim.hypercert_id, ) .map(async (claim, index) => { console.log(`[server] Processing claim with ID: ${claim.hypercert_id}.`); - // a delay to spread out the requests - await delay(index * 20); - // step 2: get offchain data from CMS - const cmsReport = fromCMS.find( (cmsReport) => cmsReport.title === claim?.metadata?.name, ); @@ -331,3 +324,23 @@ export const updateFundedAmount = async ( release(); } }; + +export const updateCMSContents = async () => { + const _reports = await updateReports(); + + const orders = await getOrders(_reports); + + const orderMap = new Map(orders.map(order => [order?.hypercertId, order])); + + reports = _reports.map(report => { + const order = orderMap.get(report.hypercertId); + if (order) { + report.order = order; + } else if (report.fundedSoFar < report.totalCost) { + console.warn(`[server] No order found for hypercert ${report.hypercertId}`); + } + return report; + }); + + console.log(`[server] total fetched reports: ${reports.length}`); +};