Skip to content

Commit

Permalink
Merge pull request #110 from Giveth/feat/handle-retro-list
Browse files Browse the repository at this point in the history
Handle importing project from retro list
  • Loading branch information
MohammadPCh authored Aug 28, 2024
2 parents 7209daa + ab484b1 commit 968cb33
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/features/import-projects/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@ import { fetchAndProcessGivethProjects } from "./giveth/index";
import { fetchAndProcessRpgf3Projects } from "./rpgf";
import { fetchAndProcessGitcoinProjects } from "./gitcoin";
import { fetchRFProjectsByRound } from "./rf";
import { fetchAndProcessRlProjects } from "./retroList";

export const task = async () => {
console.log("Importing Projects", new Date());
await fetchAndProcessGivethProjects();
await fetchAndProcessGitcoinProjects();
// fetchAndProcessRpgf3Projects();
await fetchRFProjectsByRound(4);
await fetchRFProjectsByRound(5);
// await fetchRFProjectsByRound(5); //TODO: It will fill on 20th Sep
await fetchAndProcessRlProjects(5);
};

export const importProjects = async () => {
Expand Down
20 changes: 20 additions & 0 deletions src/features/import-projects/retroList/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { SourceConfig } from "../types";
import { RoundNumber } from "./type";

// Define RL_API_URLS with specific round number keys
export const RL_API_URLS: Record<RoundNumber, string> = {
4: process.env.RL4_API_URL || "https://round4-api-eas.retrolist.app/projects",
5:
process.env.RL5_API_URL ||
"https://round5-api-eas.retrolist.app/5/projects",
};

export const rlSourceConfig: SourceConfig = {
source: "rf",
idField: "id",
titleField: "name",
descriptionField: "description",
imageField: "bannerImageUrl",
urlField: "url",
rfRoundField: "rfRound",
};
5 changes: 5 additions & 0 deletions src/features/import-projects/retroList/helper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { type RlProjectInfo } from "./type";

export const generateRlUrl = (project: RlProjectInfo) => {
return `/project/${project.id}`;
};
22 changes: 22 additions & 0 deletions src/features/import-projects/retroList/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { updateOrCreateProject } from "../helpers";
import { rlSourceConfig } from "./constants";
import { generateRlUrl } from "./helper";
import { fetchRlProjects } from "./service";

export const fetchAndProcessRlProjects = async (round: number) => {
try {
const data = await fetchRlProjects(round);
if (!data) return;
for (const project of data) {
const processedProject = {
...project,
url: generateRlUrl(project),
rfRound: round,
};

await updateOrCreateProject(processedProject, rlSourceConfig);
}
} catch (error: any) {
console.log("error on fetchAndProcessRlProjects", error.message);
}
};
39 changes: 39 additions & 0 deletions src/features/import-projects/retroList/service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Assuming RL_API_URLS and RlProjectInfo are defined as mentioned
import { RL_API_URLS } from "./constants";
import { RlProjectInfo, RoundNumber } from "./type";

export const fetchRlProjects = async (
round: number
): Promise<RlProjectInfo[] | null> => {
console.log(`Fetching RL projects for round ${round}`);
try {
// Check if the round exists in RL_API_URLS
if (!(round in RL_API_URLS)) {
console.log(`Invalid round number: ${round}`);
return null;
}

// Use type assertion to narrow down the type of 'round' to RoundNumber
const url = RL_API_URLS[round as RoundNumber];
const res = await fetch(url, {
method: "GET",
headers: {
"Content-Type": "application/json",
},
});

// Check if the response is ok (status code in the range 200-299)
if (!res.ok) {
console.log(`Failed to fetch data: ${res.status} ${res.statusText}`);
return null;
}

const data: RlProjectInfo[] = await res.json();
console.log(`Fetched ${data.length} projects for round ${round}`);

return data;
} catch (error: any) {
console.log("Error on fetching fetchRlProjects", error.message);
return null;
}
};
18 changes: 18 additions & 0 deletions src/features/import-projects/retroList/type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export type RoundNumber = 4 | 5;

export type RlProjectInfo = {
id: string;
name: string;
displayName: string;
description: string;
bio: string;
address: string;
bannerImageUrl: string;
profileImageUrl: string;
impactCategory: string[];
primaryCategory: string;
recategorization: string;
prelimResult: string;
reportReason: string;
includedInBallots: number;
};

0 comments on commit 968cb33

Please sign in to comment.