Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix a few issues #106

Merged
merged 3 commits into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/directus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ export const getFundedAmountByHCId = async (
fields: ["amount"],
filter: {
hypercert_id: {
_eq: hypercertId,
_eq: hypercertId.toLowerCase(),
},
},
})
Expand Down
62 changes: 12 additions & 50 deletions lib/impact-reports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
} from "@hypercerts-org/sdk";
import { Mutex } from "async-mutex";

import type { Claim, Report } from "@/types";
import type { Report } from "@/types";
import { getCMSReports, getFundedAmountByHCId } from "./directus";
import { getOrders } from "./marketplace";
import { delay } from "./utils";
Expand Down Expand Up @@ -63,22 +63,19 @@ export const fetchReports = async (): Promise<Report[]> => {

// TODO: remove this when we don't need dummy order
if (process.env.DEPLOY_ENV === "production") {
reports = _reports.map((report) => {
for (const order of orders) {
if (order && order.hypercertId === report.hypercertId) {
report.order = order;
break;
}
}
const orderMap = new Map(orders.map(order => [order?.hypercertId, order]));

if (!report.order && report.fundedSoFar < report.totalCost) {
// warn if there is no order for a report that is not fully funded
console.warn(
`[server] No order found for hypercert ${report.hypercertId}`,
);

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;
});

} else {
reports = _reports.map((report) => {
for (const order of orders) {
Expand Down Expand Up @@ -272,18 +269,13 @@ const updateReports = async (): Promise<Report[]> => {
// a delay to spread out the requests
await delay(index * 20);

// step 1: get metadata from IPFS
// const metadata = await getHypercertMetadata(
// claim.uri as string,
// getHypercertClient().storage,
// );

// step 2: get offchain data from CMS

const cmsReport = fromCMS.find(
(cmsReport) => cmsReport.title === claim?.metadata?.name,
);
if (!cmsReport) {
// TODO: change this to just logging
throw new Error(
`[server] CMS content for report titled '${claim?.metadata?.name}' not found.`,
);
Expand All @@ -295,8 +287,6 @@ const updateReports = async (): Promise<Report[]> => {
summary: claim?.metadata?.description,
image: claim?.metadata?.image || null,
originalReportUrl: claim?.metadata?.external_url,
// @ts-ignore
state: claim?.metadata?.properties,
category: claim?.metadata?.work_scope?.[0],
workTimeframe: `${claim.metadata?.work_timeframe_from} - ${claim.metadata?.work_timeframe_to}`,
impactScope: claim?.metadata?.impact_scope?.[0],
Expand All @@ -309,6 +299,7 @@ const updateReports = async (): Promise<Report[]> => {
dateCreated: cmsReport.date_created,
slug: cmsReport.slug,
story: cmsReport.story,
state: cmsReport.states[0],
bcRatio: cmsReport.bc_ratio,
villagesImpacted: cmsReport.villages_impacted,
peopleImpacted: cmsReport.people_impacted,
Expand Down Expand Up @@ -337,35 +328,6 @@ const updateReports = async (): Promise<Report[]> => {
return reports as Report[];
};

/**
* Retrieves the metadata for a given claim URI from IPFS.
* @param claimUri - The IPFS URI of the claim for which metadata is to be fetched.
* @param storage - An instance of HypercertsStorage to retrieve metadata from IPFS.
* @returns A promise that resolves to the metadata of the claim.
* @throws Will throw an error if the metadata cannot be fetched.
*/
// ! Deprecated
// export const getHypercertMetadata = async (
// claimUri: string,
// storage: HypercertsStorage,
// ): Promise<HypercertMetadata> => {
// let metadata: HypercertMetadata | null;

// try {
// const response = await storage.getMetadata(claimUri);
// metadata = response;

// return metadata;
// } catch (error) {
// console.error(
// `[Hypercerts] Failed to fetch metadata of ${claimUri}: ${error}`,
// );
// throw new Error(
// `[Hypercerts] Failed to fetch metadata of ${claimUri}: ${error}`,
// );
// }
// };

// update the fundedSoFar field of the report
export const updateFundedAmount = async (
hypercertId: string,
Expand Down
2 changes: 1 addition & 1 deletion next.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const nextConfig = {
images: {
remotePatterns: [
{
hostname: process.env.NEXT_PUBLIC_DEPLOY_ENV === "production" ? "directus.voicedeck.org" : "directus.vd-dev.org",
hostname: "directus.voicedeck.org",
protocol: "https",
}
]
Expand Down
2 changes: 1 addition & 1 deletion types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,6 @@ export interface CMSContent {
summary: string | null;
image: string | null;
original_report_url: string | null;
states: string[] | null;
category: string | null;
work_timeframe: string | null;
impact_scope: string | null;
Expand All @@ -161,6 +160,7 @@ export interface CMSContent {
status: string;
date_created: string | null;
slug: string;
states: string[];
story: string | null;
bc_ratio: number | null;
villages_impacted: number | null;
Expand Down
Loading