Skip to content

Commit

Permalink
feat: reflect latest CMS contents
Browse files Browse the repository at this point in the history
  • Loading branch information
baumstern committed Nov 11, 2024
1 parent 9359227 commit 0ae6550
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 11 deletions.
3 changes: 2 additions & 1 deletion app/api/reports/update/route.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down
29 changes: 29 additions & 0 deletions lib/directus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,35 @@ export const getCMSReports = async (): Promise<CMSContent[]> => {
}
};

/**
* 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<CMSContent[]> => {
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.
Expand Down
33 changes: 23 additions & 10 deletions lib/impact-reports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -236,24 +236,17 @@ const updateReports = async (): Promise<Report[]> => {
);
}

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,
);
Expand Down Expand Up @@ -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}`);
};

0 comments on commit 0ae6550

Please sign in to comment.