From 6b51d0335f716d42c31396b13b6fd0466b52c9ef Mon Sep 17 00:00:00 2001 From: Cherik Date: Sun, 2 Jun 2024 16:09:47 +0330 Subject: [PATCH 01/22] move fetchAndProcessGivethProjects to index --- .../import-projects/giveth/helpers.ts | 24 +++---------------- src/features/import-projects/giveth/index.ts | 22 +++++++++++++++++ 2 files changed, 25 insertions(+), 21 deletions(-) create mode 100644 src/features/import-projects/giveth/index.ts diff --git a/src/features/import-projects/giveth/helpers.ts b/src/features/import-projects/giveth/helpers.ts index c4f1def..0341099 100644 --- a/src/features/import-projects/giveth/helpers.ts +++ b/src/features/import-projects/giveth/helpers.ts @@ -62,7 +62,9 @@ const updateOrCreateProject = async ( } }; -const processProjectsBatch = async (projectsBatch: GivethProjectInfo[]) => { +export const processProjectsBatch = async ( + projectsBatch: GivethProjectInfo[] +) => { const dataSource = await getDataSource(); if (!dataSource) return; for (const project of projectsBatch) { @@ -70,23 +72,3 @@ const processProjectsBatch = async (projectsBatch: GivethProjectInfo[]) => { await updateOrCreateProject(dataSource, project); } }; - -export const fetchAndProcessGivethProjects = async () => { - try { - let hasMoreProjects = true; - let skip = 0; - const limit = 10; - - while (hasMoreProjects) { - const projectsBatch = await fetchGivethProjectsBatch(limit, skip); - if (projectsBatch.length > 0) { - await processProjectsBatch(projectsBatch); - skip += limit; - } else { - hasMoreProjects = false; - } - } - } catch (error: any) { - console.log("error on fetching giveth projects", error.message); - } -}; diff --git a/src/features/import-projects/giveth/index.ts b/src/features/import-projects/giveth/index.ts new file mode 100644 index 0000000..87a2558 --- /dev/null +++ b/src/features/import-projects/giveth/index.ts @@ -0,0 +1,22 @@ +import { processProjectsBatch } from "./helpers"; +import { fetchGivethProjectsBatch } from "./service"; + +export const fetchAndProcessGivethProjects = async () => { + try { + let hasMoreProjects = true; + let skip = 0; + const limit = 10; + + while (hasMoreProjects) { + const projectsBatch = await fetchGivethProjectsBatch(limit, skip); + if (projectsBatch.length > 0) { + await processProjectsBatch(projectsBatch); + skip += limit; + } else { + hasMoreProjects = false; + } + } + } catch (error: any) { + console.log("error on fetching giveth projects", error.message); + } +}; From 4db94c6f1def7cf88dda27a8db021564482878ad Mon Sep 17 00:00:00 2001 From: Cherik Date: Sun, 2 Jun 2024 16:10:11 +0330 Subject: [PATCH 02/22] add RPGF3_API_URL --- .env.template | 3 ++- src/features/import-projects/rpgf/constants.ts | 2 ++ 2 files changed, 4 insertions(+), 1 deletion(-) create mode 100644 src/features/import-projects/rpgf/constants.ts diff --git a/.env.template b/.env.template index eb10b9e..7957e45 100644 --- a/.env.template +++ b/.env.template @@ -21,4 +21,5 @@ PROJECT_VERIFY_ATTESTATION_SCHEMA=0x3D5854AF182F27966DEA837C446A051B3509224DDC03 PROJECT_GIVBACK_ELIGIBLE_ATTESTATION_SCHEMA=0x0000000000000000000000000000000000000000000000000000000000000000 #APIs -GIVETH_API_URL=Https://somewhere.giveth.io/XXXXX +GIVETH_API_URL=https://somewhere.giveth.io/XXXXX +RPGF3_API_URL=https://somewhere.RPGF3.io/XXXXX \ No newline at end of file diff --git a/src/features/import-projects/rpgf/constants.ts b/src/features/import-projects/rpgf/constants.ts new file mode 100644 index 0000000..4b46259 --- /dev/null +++ b/src/features/import-projects/rpgf/constants.ts @@ -0,0 +1,2 @@ +export const RPGF3_API_URL = + process.env.RPGF3_API_URL || "https://backend.pairwise.vote/mock/projects"; From 79fd50c5b21fe1db9eada556cb9130fb1a2a995c Mon Sep 17 00:00:00 2001 From: Cherik Date: Sun, 2 Jun 2024 16:10:19 +0330 Subject: [PATCH 03/22] add Rpgf3ProjectInfo --- src/features/import-projects/rpgf/type.ts | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 src/features/import-projects/rpgf/type.ts diff --git a/src/features/import-projects/rpgf/type.ts b/src/features/import-projects/rpgf/type.ts new file mode 100644 index 0000000..6944940 --- /dev/null +++ b/src/features/import-projects/rpgf/type.ts @@ -0,0 +1,14 @@ +export type Rpgf3ProjectInfo = { + id: string; + name: string; + poll_id: string; + url: string; + impactDescription: string; + contributionDescription: string; + RPGF3Id: string; + parentId: string; + image: string; + metadataUrl: string; + created_at: string; + type: string; +}; From c26e331afdeeeebb5742df360cfbad0db6a5b2c3 Mon Sep 17 00:00:00 2001 From: Cherik Date: Sun, 2 Jun 2024 16:10:24 +0330 Subject: [PATCH 04/22] add fetchRpgf3Projects --- src/features/import-projects/rpgf/service.ts | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 src/features/import-projects/rpgf/service.ts diff --git a/src/features/import-projects/rpgf/service.ts b/src/features/import-projects/rpgf/service.ts new file mode 100644 index 0000000..b1d23ac --- /dev/null +++ b/src/features/import-projects/rpgf/service.ts @@ -0,0 +1,15 @@ +import { RPGF3_API_URL } from "./constants"; +import { Rpgf3ProjectInfo } from "./type"; + +export const fetchRpgf3Projects = async () => { + []; + const res = await fetch(RPGF3_API_URL, { + method: "GET", + headers: { + "Content-Type": "application/json", + }, + }); + let data: Rpgf3ProjectInfo[] = await res.json(); + + return data; +}; From c4a3ba3bdfa4feff0a4f6b33b01fd35f08ad9be0 Mon Sep 17 00:00:00 2001 From: Cherik Date: Sun, 2 Jun 2024 16:30:42 +0330 Subject: [PATCH 05/22] add updateOrCreateProject --- src/features/import-projects/rpgf/helpers.ts | 64 ++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 src/features/import-projects/rpgf/helpers.ts diff --git a/src/features/import-projects/rpgf/helpers.ts b/src/features/import-projects/rpgf/helpers.ts new file mode 100644 index 0000000..69c29d3 --- /dev/null +++ b/src/features/import-projects/rpgf/helpers.ts @@ -0,0 +1,64 @@ +import { DataSource } from "typeorm"; +import { Project } from "../../../model"; +import { Rpgf3ProjectInfo } from "./type"; + +export const updateOrCreateProject = async ( + dataSource: DataSource, + project: Rpgf3ProjectInfo +) => { + const existingProject = await dataSource + .getRepository(Project) + .createQueryBuilder("project") + .where("project.id = :id", { id: `rpgf3-${project.id}` }) + .getOne(); + + if (existingProject) { + const isUpdated = + existingProject.title !== project.name || + existingProject.description !== project.impactDescription || + existingProject.slug !== "" || + existingProject.image !== project.image; + + if (isUpdated) { + const updatedProject = new Project({ + ...existingProject, + title: project.name, + description: project.impactDescription, + image: project.image, + slug: "", + lastUpdatedTimestamp: new Date(), + }); + + await dataSource + .createQueryBuilder() + .update(Project) + .set(updatedProject) + .where("id = :id", { id: `rpgf3-${project.id}` }) + .execute(); + + console.log(`Updated project:rpgf3-${project.id}`); + } + } else { + const newProject = new Project({ + id: `rpgf3-${project.id}`, + title: project.name, + description: project.impactDescription, + image: project.image, + slug: "", + projectId: project.id, + source: "rpgf3", + totalVouches: 0, + totalFlags: 0, + totalAttests: 0, + lastUpdatedTimestamp: new Date(), + }); + + await dataSource + .createQueryBuilder() + .insert() + .into(Project) + .values([newProject]) + .execute(); + } + console.log(`Created project: rpgf3-${project.id}`); +}; From 5b1dda73e40b1401d26d02c5ed64e4a08aafe95a Mon Sep 17 00:00:00 2001 From: Cherik Date: Sun, 2 Jun 2024 16:30:49 +0330 Subject: [PATCH 06/22] add fetchAndProcessRpgf3Projects --- src/features/import-projects/rpgf/index.ts | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 src/features/import-projects/rpgf/index.ts diff --git a/src/features/import-projects/rpgf/index.ts b/src/features/import-projects/rpgf/index.ts new file mode 100644 index 0000000..b9ecdbe --- /dev/null +++ b/src/features/import-projects/rpgf/index.ts @@ -0,0 +1,14 @@ +import { getDataSource } from "../../../helpers/db"; +import { fetchRpgf3Projects } from "./service"; +import { updateOrCreateProject } from "./helpers"; + +export const fetchAndProcessRpgf3Projects = async () => { + const data = await fetchRpgf3Projects(); + if (!data) return; + const dataSource = await getDataSource(); + if (!dataSource) return; + for (const project of data) { + if (project.type.toLowerCase() !== "project") continue; + await updateOrCreateProject(dataSource, project); + } +}; From 9d29155f0bf6d53edfff80652735163f8ba1d08c Mon Sep 17 00:00:00 2001 From: Cherik Date: Sun, 2 Jun 2024 16:46:34 +0330 Subject: [PATCH 07/22] add SourceConfig --- src/features/import-projects/types.ts | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 src/features/import-projects/types.ts diff --git a/src/features/import-projects/types.ts b/src/features/import-projects/types.ts new file mode 100644 index 0000000..33351d4 --- /dev/null +++ b/src/features/import-projects/types.ts @@ -0,0 +1,8 @@ +interface SourceConfig { + source: string; + idField: string; + titleField: string; + descriptionField: string; + slugField: string; + imageField: string; +} From 918194365847ef8f6c01f5a6d9271cc9b7a9ef45 Mon Sep 17 00:00:00 2001 From: Cherik Date: Sun, 2 Jun 2024 16:47:05 +0330 Subject: [PATCH 08/22] add general updateOrCreateProject --- src/features/import-projects/helpers.ts | 70 +++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 src/features/import-projects/helpers.ts diff --git a/src/features/import-projects/helpers.ts b/src/features/import-projects/helpers.ts new file mode 100644 index 0000000..0310ed6 --- /dev/null +++ b/src/features/import-projects/helpers.ts @@ -0,0 +1,70 @@ +import { type DataSource } from "typeorm"; +import { Project } from "../../model"; + +export const updateOrCreateProject = async ( + dataSource: DataSource, + project: any, + sourConfig: SourceConfig +) => { + const { + source, + idField, + titleField, + descriptionField, + slugField, + imageField, + } = sourConfig; + + const existingProject = await dataSource + .getRepository(Project) + .createQueryBuilder("project") + .where("project.id = :id", { id: `${source}-${project[idField]}` }) + .getOne(); + + if (existingProject) { + const isUpdated = + existingProject.title !== project[titleField] || + existingProject.description !== project[descriptionField] || + existingProject.slug !== project[slugField] || + existingProject.image !== project[imageField]; + + if (isUpdated) { + const updatedProject = new Project({ + ...existingProject, + title: project[titleField], + description: project[descriptionField], + image: project[imageField], + slug: project[slugField], + lastUpdatedTimestamp: new Date(), + }); + + await dataSource + .createQueryBuilder() + .update(Project) + .set(updatedProject) + .where("id = :id", { id: `${source}-${project[idField]}` }) + .execute(); + } + } else { + const newProject = new Project({ + id: `${source}-${project[idField]}`, + title: project[titleField], + description: project[descriptionField], + image: project[imageField], + slug: project[slugField], + projectId: project[idField], + source: source, + totalVouches: 0, + totalFlags: 0, + totalAttests: 0, + lastUpdatedTimestamp: new Date(), + }); + + await dataSource + .createQueryBuilder() + .insert() + .into(Project) + .values([newProject]) + .execute(); + } +}; From 9fc46d6d439be81e18efbc710d356a7ec8c7c2d0 Mon Sep 17 00:00:00 2001 From: Cherik Date: Sun, 2 Jun 2024 16:52:55 +0330 Subject: [PATCH 09/22] delete old updateOrCreateProject --- .../import-projects/giveth/helpers.ts | 65 +------------------ src/features/import-projects/rpgf/helpers.ts | 64 ------------------ 2 files changed, 3 insertions(+), 126 deletions(-) delete mode 100644 src/features/import-projects/rpgf/helpers.ts diff --git a/src/features/import-projects/giveth/helpers.ts b/src/features/import-projects/giveth/helpers.ts index 0341099..9fe43c2 100644 --- a/src/features/import-projects/giveth/helpers.ts +++ b/src/features/import-projects/giveth/helpers.ts @@ -1,66 +1,7 @@ -import { DataSource } from "typeorm"; import { getDataSource } from "../../../helpers/db"; -import { Project } from "../../../model"; -import { fetchGivethProjectsBatch } from "./service"; import { GivethProjectInfo } from "./type"; - -const updateOrCreateProject = async ( - dataSource: DataSource, - project: GivethProjectInfo -) => { - const existingProject = await dataSource - .getRepository(Project) - .createQueryBuilder("project") - .where("project.id = :id", { id: `giveth-${project.id}` }) - .getOne(); - - if (existingProject) { - const isUpdated = - existingProject.title !== project.title || - existingProject.description !== project.descriptionSummary || - existingProject.slug !== project.slug || - existingProject.image !== project.image; - - if (isUpdated) { - const updatedProject = new Project({ - ...existingProject, - title: project.title, - description: project.descriptionSummary, - image: project.image, - slug: project.slug, - lastUpdatedTimestamp: new Date(), - }); - - await dataSource - .createQueryBuilder() - .update(Project) - .set(updatedProject) - .where("id = :id", { id: `giveth-${project.id}` }) - .execute(); - } - } else { - const newProject = new Project({ - id: `giveth-${project.id}`, - title: project.title, - description: project.descriptionSummary, - image: project.image, - slug: project.slug, - projectId: project.id, - source: "giveth", - totalVouches: 0, - totalFlags: 0, - totalAttests: 0, - lastUpdatedTimestamp: new Date(), - }); - - await dataSource - .createQueryBuilder() - .insert() - .into(Project) - .values([newProject]) - .execute(); - } -}; +import { updateOrCreateProject } from "../helpers"; +import { givethSourceConfig } from "./constants"; export const processProjectsBatch = async ( projectsBatch: GivethProjectInfo[] @@ -69,6 +10,6 @@ export const processProjectsBatch = async ( if (!dataSource) return; for (const project of projectsBatch) { console.log("Processing Project: Giveth", project.id); - await updateOrCreateProject(dataSource, project); + await updateOrCreateProject(dataSource, project, givethSourceConfig); } }; diff --git a/src/features/import-projects/rpgf/helpers.ts b/src/features/import-projects/rpgf/helpers.ts deleted file mode 100644 index 69c29d3..0000000 --- a/src/features/import-projects/rpgf/helpers.ts +++ /dev/null @@ -1,64 +0,0 @@ -import { DataSource } from "typeorm"; -import { Project } from "../../../model"; -import { Rpgf3ProjectInfo } from "./type"; - -export const updateOrCreateProject = async ( - dataSource: DataSource, - project: Rpgf3ProjectInfo -) => { - const existingProject = await dataSource - .getRepository(Project) - .createQueryBuilder("project") - .where("project.id = :id", { id: `rpgf3-${project.id}` }) - .getOne(); - - if (existingProject) { - const isUpdated = - existingProject.title !== project.name || - existingProject.description !== project.impactDescription || - existingProject.slug !== "" || - existingProject.image !== project.image; - - if (isUpdated) { - const updatedProject = new Project({ - ...existingProject, - title: project.name, - description: project.impactDescription, - image: project.image, - slug: "", - lastUpdatedTimestamp: new Date(), - }); - - await dataSource - .createQueryBuilder() - .update(Project) - .set(updatedProject) - .where("id = :id", { id: `rpgf3-${project.id}` }) - .execute(); - - console.log(`Updated project:rpgf3-${project.id}`); - } - } else { - const newProject = new Project({ - id: `rpgf3-${project.id}`, - title: project.name, - description: project.impactDescription, - image: project.image, - slug: "", - projectId: project.id, - source: "rpgf3", - totalVouches: 0, - totalFlags: 0, - totalAttests: 0, - lastUpdatedTimestamp: new Date(), - }); - - await dataSource - .createQueryBuilder() - .insert() - .into(Project) - .values([newProject]) - .execute(); - } - console.log(`Created project: rpgf3-${project.id}`); -}; From aefebdab7071f9f5a299c6482d2a3167392cdf93 Mon Sep 17 00:00:00 2001 From: Cherik Date: Sun, 2 Jun 2024 16:53:04 +0330 Subject: [PATCH 10/22] add givethSourceConfig --- src/features/import-projects/giveth/constants.ts | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/features/import-projects/giveth/constants.ts b/src/features/import-projects/giveth/constants.ts index dca2815..2164c79 100644 --- a/src/features/import-projects/giveth/constants.ts +++ b/src/features/import-projects/giveth/constants.ts @@ -1,2 +1,11 @@ export const GIVETH_API_URL = process.env.GIVETH_API_URL || "https://mainnet.serve.giveth.io/graphql"; + +export const givethSourceConfig = { + source: "giveth", + idField: "id", + titleField: "title", + descriptionField: "descriptionSummary", + slugField: "slug", + imageField: "image", +}; From d52feb85fa1218378cbe121bd3fda81bac9f2d18 Mon Sep 17 00:00:00 2001 From: Cherik Date: Sun, 2 Jun 2024 16:53:13 +0330 Subject: [PATCH 11/22] add rpgf3SourceConfig --- src/features/import-projects/rpgf/constants.ts | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/features/import-projects/rpgf/constants.ts b/src/features/import-projects/rpgf/constants.ts index 4b46259..bc6a8bb 100644 --- a/src/features/import-projects/rpgf/constants.ts +++ b/src/features/import-projects/rpgf/constants.ts @@ -1,2 +1,11 @@ export const RPGF3_API_URL = process.env.RPGF3_API_URL || "https://backend.pairwise.vote/mock/projects"; + +export const rpgf3SourceConfig = { + source: "rpgf3", + idField: "RPGF3Id", + titleField: "name", + descriptionField: "impactDescription", + slugField: "url", + imageField: "image", +}; From 786ad26135741efd4bc174bcabdff11dca66f0df Mon Sep 17 00:00:00 2001 From: Cherik Date: Sun, 2 Jun 2024 17:01:27 +0330 Subject: [PATCH 12/22] update fetchAndProcessRpgf3Projects --- src/features/import-projects/rpgf/index.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/features/import-projects/rpgf/index.ts b/src/features/import-projects/rpgf/index.ts index b9ecdbe..939ba01 100644 --- a/src/features/import-projects/rpgf/index.ts +++ b/src/features/import-projects/rpgf/index.ts @@ -1,6 +1,7 @@ import { getDataSource } from "../../../helpers/db"; +import { updateOrCreateProject } from "../helpers"; +import { rpgf3SourceConfig } from "./constants"; import { fetchRpgf3Projects } from "./service"; -import { updateOrCreateProject } from "./helpers"; export const fetchAndProcessRpgf3Projects = async () => { const data = await fetchRpgf3Projects(); @@ -9,6 +10,6 @@ export const fetchAndProcessRpgf3Projects = async () => { if (!dataSource) return; for (const project of data) { if (project.type.toLowerCase() !== "project") continue; - await updateOrCreateProject(dataSource, project); + await updateOrCreateProject(dataSource, project, rpgf3SourceConfig); } }; From d7391c4cf10b37e5cbc488174b1ffdb4d5a5ec2d Mon Sep 17 00:00:00 2001 From: Cherik Date: Sun, 2 Jun 2024 17:04:10 +0330 Subject: [PATCH 13/22] add RPGF3 --- src/features/import-projects/index.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/features/import-projects/index.ts b/src/features/import-projects/index.ts index 3a059c0..7c03411 100644 --- a/src/features/import-projects/index.ts +++ b/src/features/import-projects/index.ts @@ -1,10 +1,12 @@ import cron from "node-cron"; -import { fetchAndProcessGivethProjects } from "./giveth/helpers"; import { IMPORT_PROJECT_CRON_SCHEDULE } from "../../constants"; +import { fetchAndProcessGivethProjects } from "./giveth/index"; +import { fetchAndProcessRpgf3Projects } from "./rpgf"; export const task = async () => { console.log("Importing Projects", new Date()); await fetchAndProcessGivethProjects(); + await fetchAndProcessRpgf3Projects(); }; export const importProjects = async () => { From 0b498db1edba0b0eb2f634b75c73e9b9a969703e Mon Sep 17 00:00:00 2001 From: Cherik Date: Sun, 2 Jun 2024 17:05:31 +0330 Subject: [PATCH 14/22] add types --- src/features/import-projects/giveth/constants.ts | 2 +- src/features/import-projects/rpgf/constants.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/features/import-projects/giveth/constants.ts b/src/features/import-projects/giveth/constants.ts index 2164c79..a8dafc1 100644 --- a/src/features/import-projects/giveth/constants.ts +++ b/src/features/import-projects/giveth/constants.ts @@ -1,7 +1,7 @@ export const GIVETH_API_URL = process.env.GIVETH_API_URL || "https://mainnet.serve.giveth.io/graphql"; -export const givethSourceConfig = { +export const givethSourceConfig: SourceConfig = { source: "giveth", idField: "id", titleField: "title", diff --git a/src/features/import-projects/rpgf/constants.ts b/src/features/import-projects/rpgf/constants.ts index bc6a8bb..e912b1c 100644 --- a/src/features/import-projects/rpgf/constants.ts +++ b/src/features/import-projects/rpgf/constants.ts @@ -1,7 +1,7 @@ export const RPGF3_API_URL = process.env.RPGF3_API_URL || "https://backend.pairwise.vote/mock/projects"; -export const rpgf3SourceConfig = { +export const rpgf3SourceConfig: SourceConfig = { source: "rpgf3", idField: "RPGF3Id", titleField: "name", From 1f33fb72472197f3ab2f26651ccd2ea032658776 Mon Sep 17 00:00:00 2001 From: Amin Latifi Date: Sun, 2 Jun 2024 15:52:57 +0000 Subject: [PATCH 15/22] Update src/features/import-projects/rpgf/service.ts Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> --- src/features/import-projects/rpgf/service.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/features/import-projects/rpgf/service.ts b/src/features/import-projects/rpgf/service.ts index b1d23ac..99c0e1b 100644 --- a/src/features/import-projects/rpgf/service.ts +++ b/src/features/import-projects/rpgf/service.ts @@ -9,7 +9,7 @@ export const fetchRpgf3Projects = async () => { "Content-Type": "application/json", }, }); - let data: Rpgf3ProjectInfo[] = await res.json(); + const data: Rpgf3ProjectInfo[] = await res.json(); return data; }; From 9fc7906f6771da5f1d738c2a88ef2a72ad401d7f Mon Sep 17 00:00:00 2001 From: Cherik Date: Mon, 3 Jun 2024 10:49:15 +0330 Subject: [PATCH 16/22] fix import --- src/features/import-projects/giveth/helpers.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/features/import-projects/giveth/helpers.ts b/src/features/import-projects/giveth/helpers.ts index 9fe43c2..34ccc16 100644 --- a/src/features/import-projects/giveth/helpers.ts +++ b/src/features/import-projects/giveth/helpers.ts @@ -1,5 +1,5 @@ import { getDataSource } from "../../../helpers/db"; -import { GivethProjectInfo } from "./type"; +import { type GivethProjectInfo } from "./type"; import { updateOrCreateProject } from "../helpers"; import { givethSourceConfig } from "./constants"; From 12b45a9629d4cc48738fa628f6a0041eff88a8d5 Mon Sep 17 00:00:00 2001 From: Cherik Date: Mon, 3 Jun 2024 10:49:27 +0330 Subject: [PATCH 17/22] wrap with try catch --- src/features/import-projects/rpgf/service.ts | 22 ++++++++++++-------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/features/import-projects/rpgf/service.ts b/src/features/import-projects/rpgf/service.ts index 99c0e1b..11df8cf 100644 --- a/src/features/import-projects/rpgf/service.ts +++ b/src/features/import-projects/rpgf/service.ts @@ -2,14 +2,18 @@ import { RPGF3_API_URL } from "./constants"; import { Rpgf3ProjectInfo } from "./type"; export const fetchRpgf3Projects = async () => { - []; - const res = await fetch(RPGF3_API_URL, { - method: "GET", - headers: { - "Content-Type": "application/json", - }, - }); - const data: Rpgf3ProjectInfo[] = await res.json(); + try { + const res = await fetch(RPGF3_API_URL, { + method: "GET", + headers: { + "Content-Type": "application/json", + }, + }); + const data: Rpgf3ProjectInfo[] = await res.json(); - return data; + return data; + } catch (error: any) { + console.log("error on fetching fetchRpgf3Projects", error.message); + return null; + } }; From a9ee7449c31cd99fed2088d68a6a2cb2216a90a6 Mon Sep 17 00:00:00 2001 From: Cherik Date: Mon, 3 Jun 2024 10:50:47 +0330 Subject: [PATCH 18/22] wrap fetchAndProcessRpgf3Projects with try catch --- src/features/import-projects/rpgf/index.ts | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/features/import-projects/rpgf/index.ts b/src/features/import-projects/rpgf/index.ts index 939ba01..2c2eb6f 100644 --- a/src/features/import-projects/rpgf/index.ts +++ b/src/features/import-projects/rpgf/index.ts @@ -4,12 +4,16 @@ import { rpgf3SourceConfig } from "./constants"; import { fetchRpgf3Projects } from "./service"; export const fetchAndProcessRpgf3Projects = async () => { - const data = await fetchRpgf3Projects(); - if (!data) return; - const dataSource = await getDataSource(); - if (!dataSource) return; - for (const project of data) { - if (project.type.toLowerCase() !== "project") continue; - await updateOrCreateProject(dataSource, project, rpgf3SourceConfig); + try { + const data = await fetchRpgf3Projects(); + if (!data) return; + const dataSource = await getDataSource(); + if (!dataSource) return; + for (const project of data) { + if (project.type.toLowerCase() !== "project") continue; + await updateOrCreateProject(dataSource, project, rpgf3SourceConfig); + } + } catch (error: any) { + console.log("error on fetching RPGF3 projects", error.message); } }; From 730dc209e81a7c7ab617ffb494d6643b82001ede Mon Sep 17 00:00:00 2001 From: Cherik Date: Mon, 3 Jun 2024 10:59:59 +0330 Subject: [PATCH 19/22] move getDataSource to the updateOrCreateProject --- src/features/import-projects/giveth/helpers.ts | 5 +---- src/features/import-projects/helpers.ts | 17 +++++++++++++---- src/features/import-projects/rpgf/index.ts | 4 +--- 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/src/features/import-projects/giveth/helpers.ts b/src/features/import-projects/giveth/helpers.ts index 34ccc16..4044dce 100644 --- a/src/features/import-projects/giveth/helpers.ts +++ b/src/features/import-projects/giveth/helpers.ts @@ -6,10 +6,7 @@ import { givethSourceConfig } from "./constants"; export const processProjectsBatch = async ( projectsBatch: GivethProjectInfo[] ) => { - const dataSource = await getDataSource(); - if (!dataSource) return; for (const project of projectsBatch) { - console.log("Processing Project: Giveth", project.id); - await updateOrCreateProject(dataSource, project, givethSourceConfig); + await updateOrCreateProject(project, givethSourceConfig); } }; diff --git a/src/features/import-projects/helpers.ts b/src/features/import-projects/helpers.ts index 0310ed6..4b030c4 100644 --- a/src/features/import-projects/helpers.ts +++ b/src/features/import-projects/helpers.ts @@ -1,8 +1,8 @@ import { type DataSource } from "typeorm"; import { Project } from "../../model"; +import { getDataSource } from "../../helpers/db"; export const updateOrCreateProject = async ( - dataSource: DataSource, project: any, sourConfig: SourceConfig ) => { @@ -15,10 +15,15 @@ export const updateOrCreateProject = async ( imageField, } = sourConfig; + const id = `${source}-${project[idField]}`; + + const dataSource = await getDataSource(); + if (!dataSource) return; + const existingProject = await dataSource .getRepository(Project) .createQueryBuilder("project") - .where("project.id = :id", { id: `${source}-${project[idField]}` }) + .where("project.id = :id", { id }) .getOne(); if (existingProject) { @@ -42,12 +47,14 @@ export const updateOrCreateProject = async ( .createQueryBuilder() .update(Project) .set(updatedProject) - .where("id = :id", { id: `${source}-${project[idField]}` }) + .where("id = :id", { id }) .execute(); + + console.log(new Date(), "Project Updated: ", id); } } else { const newProject = new Project({ - id: `${source}-${project[idField]}`, + id, title: project[titleField], description: project[descriptionField], image: project[imageField], @@ -66,5 +73,7 @@ export const updateOrCreateProject = async ( .into(Project) .values([newProject]) .execute(); + + console.log(new Date(), "Project Created: ", id); } }; diff --git a/src/features/import-projects/rpgf/index.ts b/src/features/import-projects/rpgf/index.ts index 2c2eb6f..cfe79cb 100644 --- a/src/features/import-projects/rpgf/index.ts +++ b/src/features/import-projects/rpgf/index.ts @@ -7,11 +7,9 @@ export const fetchAndProcessRpgf3Projects = async () => { try { const data = await fetchRpgf3Projects(); if (!data) return; - const dataSource = await getDataSource(); - if (!dataSource) return; for (const project of data) { if (project.type.toLowerCase() !== "project") continue; - await updateOrCreateProject(dataSource, project, rpgf3SourceConfig); + await updateOrCreateProject(project, rpgf3SourceConfig); } } catch (error: any) { console.log("error on fetching RPGF3 projects", error.message); From cbd2ffdac279649be4431599ff141f51d065390a Mon Sep 17 00:00:00 2001 From: Cherik Date: Mon, 3 Jun 2024 11:03:18 +0330 Subject: [PATCH 20/22] add more logs --- src/features/import-projects/giveth/index.ts | 2 +- .../import-projects/giveth/service.ts | 49 ++++++++++--------- src/features/import-projects/index.ts | 2 +- src/features/import-projects/rpgf/index.ts | 2 +- 4 files changed, 30 insertions(+), 25 deletions(-) diff --git a/src/features/import-projects/giveth/index.ts b/src/features/import-projects/giveth/index.ts index 87a2558..5fcecfe 100644 --- a/src/features/import-projects/giveth/index.ts +++ b/src/features/import-projects/giveth/index.ts @@ -17,6 +17,6 @@ export const fetchAndProcessGivethProjects = async () => { } } } catch (error: any) { - console.log("error on fetching giveth projects", error.message); + console.log("error on fetchAndProcessGivethProjects", error.message); } }; diff --git a/src/features/import-projects/giveth/service.ts b/src/features/import-projects/giveth/service.ts index 4854729..9869b33 100644 --- a/src/features/import-projects/giveth/service.ts +++ b/src/features/import-projects/giveth/service.ts @@ -3,31 +3,36 @@ import { GIVETH_API_URL } from "./constants"; import { GivethProjectInfo } from "./type"; export const fetchGivethProjectsBatch = async (limit: number, skip: number) => { - const res = await graphQLRequest( - GIVETH_API_URL, - `query ($limit: Int, $skip: Int, $sortingBy: SortingField) { - allProjects( - limit: $limit - skip: $skip - sortingBy: $sortingBy - ) { - projects { - id - title - image - slug - descriptionSummary + try { + const res = await graphQLRequest( + GIVETH_API_URL, + `query ($limit: Int, $skip: Int, $sortingBy: SortingField) { + allProjects( + limit: $limit + skip: $skip + sortingBy: $sortingBy + ) { + projects { + id + title + image + slug + descriptionSummary + } } + }`, + { + limit, + skip, + sortingBy: "Newest", } - }`, - { - limit, - skip, - sortingBy: "Newest", - } - ); + ); - return res.data.allProjects.projects; + return res.data.allProjects.projects; + } catch (error: any) { + console.log("error on fetchGivethProjectsBatch", error.message); + return []; + } }; export const fetchGivethProjects = async () => { diff --git a/src/features/import-projects/index.ts b/src/features/import-projects/index.ts index 7c03411..768c10e 100644 --- a/src/features/import-projects/index.ts +++ b/src/features/import-projects/index.ts @@ -19,6 +19,6 @@ export const importProjects = async () => { timezone: "UTC", }); } catch (error) { - console.log("Error", error); + console.log("Error on scheduling importing project:", error); } }; diff --git a/src/features/import-projects/rpgf/index.ts b/src/features/import-projects/rpgf/index.ts index cfe79cb..4e58cd1 100644 --- a/src/features/import-projects/rpgf/index.ts +++ b/src/features/import-projects/rpgf/index.ts @@ -12,6 +12,6 @@ export const fetchAndProcessRpgf3Projects = async () => { await updateOrCreateProject(project, rpgf3SourceConfig); } } catch (error: any) { - console.log("error on fetching RPGF3 projects", error.message); + console.log("error on fetchAndProcessRpgf3Projects", error.message); } }; From 1a634218d4076072222ac78cd280ad46a5f14cbc Mon Sep 17 00:00:00 2001 From: Cherik Date: Mon, 3 Jun 2024 11:56:12 +0330 Subject: [PATCH 21/22] add more logs --- src/features/import-projects/helpers.ts | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/features/import-projects/helpers.ts b/src/features/import-projects/helpers.ts index 4b030c4..d9f1685 100644 --- a/src/features/import-projects/helpers.ts +++ b/src/features/import-projects/helpers.ts @@ -18,7 +18,12 @@ export const updateOrCreateProject = async ( const id = `${source}-${project[idField]}`; const dataSource = await getDataSource(); - if (!dataSource) return; + if (!dataSource) { + console.log( + `[${new Date().toISOString()}] - ERROR: Failed to UPSERT project. Data source not found. Project ID: ${id}` + ); + return; + } const existingProject = await dataSource .getRepository(Project) @@ -50,7 +55,9 @@ export const updateOrCreateProject = async ( .where("id = :id", { id }) .execute(); - console.log(new Date(), "Project Updated: ", id); + console.log( + `[${new Date().toISOString()}] - INFO: Project Updated. Project ID: ${id}` + ); } } else { const newProject = new Project({ @@ -74,6 +81,8 @@ export const updateOrCreateProject = async ( .values([newProject]) .execute(); - console.log(new Date(), "Project Created: ", id); + console.log( + `[${new Date().toISOString()}] - INFO: Project Created. Project ID: ${id}` + ); } }; From 35d2e6ecb28eabd110467723fd00613e9e0c068b Mon Sep 17 00:00:00 2001 From: Cherik Date: Mon, 3 Jun 2024 11:59:03 +0330 Subject: [PATCH 22/22] fetch projects parallel --- src/features/import-projects/index.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/features/import-projects/index.ts b/src/features/import-projects/index.ts index 768c10e..ad428b6 100644 --- a/src/features/import-projects/index.ts +++ b/src/features/import-projects/index.ts @@ -5,8 +5,8 @@ import { fetchAndProcessRpgf3Projects } from "./rpgf"; export const task = async () => { console.log("Importing Projects", new Date()); - await fetchAndProcessGivethProjects(); - await fetchAndProcessRpgf3Projects(); + fetchAndProcessGivethProjects(); + fetchAndProcessRpgf3Projects(); }; export const importProjects = async () => {