From 9c5ab10e463f797081e541053692877168918f1e Mon Sep 17 00:00:00 2001 From: HrithikSampson Date: Wed, 11 Sep 2024 21:20:50 +0530 Subject: [PATCH 1/3] migration: project banners for endaoment projects need to have the correct banners --- ...069430594-add_endaoment_project_banners.ts | 86 +++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 migration/1726069430594-add_endaoment_project_banners.ts diff --git a/migration/1726069430594-add_endaoment_project_banners.ts b/migration/1726069430594-add_endaoment_project_banners.ts new file mode 100644 index 000000000..2b37ebd42 --- /dev/null +++ b/migration/1726069430594-add_endaoment_project_banners.ts @@ -0,0 +1,86 @@ +import { MigrationInterface, QueryRunner } from 'typeorm'; +import { endaomentProjects } from './data/importedEndaomentProjects'; +import { endaomentProjectCategoryMapping } from './data/endaomentProjectCategoryMapping'; +import { NETWORK_IDS } from '../src/provider'; +export class AddEndaomentProjectBanners1726069430594 + implements MigrationInterface +{ + public async up(queryRunner: QueryRunner): Promise { + const mainCategorySlugToBannerMapping = { + 'environment-and-energy': 'nature', + 'health-and-wellness': 'health-wellness', + 'art-and-culture': 'art-culture', + nature: 'nature', + community: 'community', + finance: 'finance', + education: 'education', + equality: 'equality', + other: '1', + 'economic-and-infrastructure': 'economic-infrastructure', + ngo: 'non-profit', + technology: 'technology', + }; + const subCategoryToCategory = await queryRunner.query( + 'SELECT category.value, main_category.slug from category LEFT JOIN main_category on category."mainCategoryId" = main_category.id;', + ); + + const imageCategoryMapping = subCategoryToCategory.reduce(function ( + categoryImageKeyPair, + category: { value: string; slug: string }, + ) { + const bannerLink = mainCategorySlugToBannerMapping[category.slug] || '1'; + categoryImageKeyPair[category.value] = bannerLink; + return categoryImageKeyPair; + }, {}); + + for (const project of endaomentProjects) { + const mainnetAddress = project.mainnetAddress; + const projectAddresses = await queryRunner.query( + `SELECT * FROM project_address WHERE LOWER(address) = $1 AND "networkId" = $2 LIMIT 1`, + [mainnetAddress!.toLowerCase(), NETWORK_IDS.MAIN_NET], + ); + + const projectAddress = await projectAddresses?.[0]; + + if (!projectAddress) { + // eslint-disable-next-line no-console + console.log(`Could not find project address for ${mainnetAddress}`); + continue; + } + + // Insert the project-category relationship in a single query + const getCategoryNames = (nteeCode: string): string[] => { + const mapping = endaomentProjectCategoryMapping.find( + category => category.nteeCode === nteeCode, + ); + return mapping + ? [ + mapping.category1, + mapping.category2, + mapping.category3 || '', + mapping.category4 || '', + ].filter(Boolean) + : []; + }; + if (!project.nteeCode) { + // eslint-disable-next-line no-console + console.log(`Could not find nteeCode for ${mainnetAddress}`); + continue; + } + const categoryNames = getCategoryNames(String(project.nteeCode)); + const bannerImage = `/images/defaultProjectImages/${imageCategoryMapping[categoryNames[1]] || '1'}.png`; + await queryRunner.query(`UPDATE project SET image = $1 WHERE id = $2`, [ + bannerImage, + projectAddress.projectId, + ]); + // eslint-disable-next-line no-console + console.log( + `Updated project ${projectAddress.projectId} with image ${bannerImage}`, + ); + } + } + + public async down(queryRunner: QueryRunner): Promise { + // No down migration + } +} From 1954f6ec990235d8f7400ecc7a6aa76ff51ae8e9 Mon Sep 17 00:00:00 2001 From: HrithikSampson Date: Wed, 11 Sep 2024 21:47:22 +0530 Subject: [PATCH 2/3] chore: underscore before unused variable in add_endaoment_project_banners --- migration/1726069430594-add_endaoment_project_banners.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/migration/1726069430594-add_endaoment_project_banners.ts b/migration/1726069430594-add_endaoment_project_banners.ts index 2b37ebd42..1b7f2089a 100644 --- a/migration/1726069430594-add_endaoment_project_banners.ts +++ b/migration/1726069430594-add_endaoment_project_banners.ts @@ -80,7 +80,7 @@ export class AddEndaomentProjectBanners1726069430594 } } - public async down(queryRunner: QueryRunner): Promise { + public async down(_queryRunner: QueryRunner): Promise { // No down migration } } From ee8617cd96b3ebf61bed12ab68df51906bb734c9 Mon Sep 17 00:00:00 2001 From: HrithikSampson Date: Tue, 17 Sep 2024 10:31:07 +0530 Subject: [PATCH 3/3] add environment and energy image mapping --- migration/1726069430594-add_endaoment_project_banners.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/migration/1726069430594-add_endaoment_project_banners.ts b/migration/1726069430594-add_endaoment_project_banners.ts index 1b7f2089a..7118dc76a 100644 --- a/migration/1726069430594-add_endaoment_project_banners.ts +++ b/migration/1726069430594-add_endaoment_project_banners.ts @@ -7,7 +7,7 @@ export class AddEndaomentProjectBanners1726069430594 { public async up(queryRunner: QueryRunner): Promise { const mainCategorySlugToBannerMapping = { - 'environment-and-energy': 'nature', + 'environment-and-energy': 'environment-energy', 'health-and-wellness': 'health-wellness', 'art-and-culture': 'art-culture', nature: 'nature',