Skip to content

Commit 98f99a1

Browse files
committed
Merge branch 'issue/privy-wagmi' of https://github.com/VoiceDeck/app into issue/privy-wagmi
2 parents a1e3e4b + c78e20e commit 98f99a1

File tree

3 files changed

+61
-11
lines changed

3 files changed

+61
-11
lines changed

app/api/reports/update/route.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
import { fetchNewReports } from "@/lib/impact-reports";
1+
import { fetchNewReports, updateCMSContents } from "@/lib/impact-reports";
22
import { NextResponse } from "next/server";
33

44
export async function GET() {
55
try {
6+
console.log("api/reports/update has called");
67
await fetchNewReports();
8+
await updateCMSContents();
79
return NextResponse.json({ status: 200 });
810
} catch (error) {
911
let errorMessage = "An unknown error occurred";

lib/directus.ts

+35
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,9 @@ export const getCMSReports = async (): Promise<CMSContent[]> => {
169169
filter: {
170170
status: {
171171
_eq: 'published',
172+
},
173+
Minted: {
174+
_eq: true,
172175
}
173176
},
174177
})
@@ -184,6 +187,38 @@ export const getCMSReports = async (): Promise<CMSContent[]> => {
184187
}
185188
};
186189

190+
/**
191+
* Update the latest contents of the CMS `reports` collection.
192+
* @returns A promise that resolves to an array of CMS contents.
193+
* @throws Will throw an error if the CMS contents cannot be fetched.
194+
*/
195+
export const updateCMSReports = async (): Promise<CMSContent[]> => {
196+
const client = getDirectusClient();
197+
198+
try {
199+
console.log("[Directus] Fetching CMS contents from remote");
200+
const response = await client.request(
201+
readItems('reports', {
202+
filter: {
203+
status: {
204+
_eq: 'published',
205+
},
206+
Minted: {
207+
_eq: true,
208+
}
209+
},
210+
})
211+
);
212+
CMSReports = response as CMSContent[];
213+
console.log("[Directus] fetched CMS contents: ", CMSReports.length);
214+
215+
return CMSReports;
216+
} catch (error) {
217+
console.error(`[Directus] Failed to fetch CMS contents: ${error}`);
218+
throw new Error(`[Directus] Failed to fetch CMS contents: ${error}`);
219+
}
220+
};
221+
187222
/**
188223
* Retrieves the total funded amount for a given hypercert by its ID.
189224
* It fetches all contributions related to the hypercert and sums up their amounts.

lib/impact-reports.ts

+23-10
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
import { Mutex } from "async-mutex";
77

88
import type { Report } from "@/types";
9-
import { getCMSReports, getFundedAmountByHCId } from "./directus";
9+
import { getCMSReports, getFundedAmountByHCId, updateCMSReports } from "./directus";
1010
import { getOrders } from "./marketplace";
1111
import { delay } from "./utils";
1212
import { getHypercertsByOwner } from "@/hypercerts/getHypercertsByOwner";
@@ -236,24 +236,17 @@ const updateReports = async (): Promise<Report[]> => {
236236
);
237237
}
238238

239-
const fromCMS = await getCMSReports();
239+
const fromCMS = await updateCMSReports();
240240

241-
const existingReportIds = reports
242-
? reports.map((report) => report.hypercertId)
243-
: [];
244241
const reportsToUpdatePromises = claims
245242
.filter(
246243
(claim) =>
247-
claim.hypercert_id && !existingReportIds.includes(claim.hypercert_id),
244+
claim.hypercert_id,
248245
)
249246
.map(async (claim, index) => {
250247
console.log(`[server] Processing claim with ID: ${claim.hypercert_id}.`);
251248

252-
// a delay to spread out the requests
253-
await delay(index * 20);
254-
255249
// step 2: get offchain data from CMS
256-
257250
const cmsReport = fromCMS.find(
258251
(cmsReport) => cmsReport.title === claim?.metadata?.name,
259252
);
@@ -331,3 +324,23 @@ export const updateFundedAmount = async (
331324
release();
332325
}
333326
};
327+
328+
export const updateCMSContents = async () => {
329+
const _reports = await updateReports();
330+
331+
const orders = await getOrders(_reports);
332+
333+
const orderMap = new Map(orders.map(order => [order?.hypercertId, order]));
334+
335+
reports = _reports.map(report => {
336+
const order = orderMap.get(report.hypercertId);
337+
if (order) {
338+
report.order = order;
339+
} else if (report.fundedSoFar < report.totalCost) {
340+
console.warn(`[server] No order found for hypercert ${report.hypercertId}`);
341+
}
342+
return report;
343+
});
344+
345+
console.log(`[server] total fetched reports: ${reports.length}`);
346+
};

0 commit comments

Comments
 (0)