Skip to content

Commit

Permalink
Merge pull request #1813 from Giveth/banner-image-add-endaoment-project
Browse files Browse the repository at this point in the history
Banner image add endaoment project
  • Loading branch information
HrithikSampson authored Sep 25, 2024
2 parents 3ee817a + f49fe56 commit ecf4e19
Showing 1 changed file with 86 additions and 0 deletions.
86 changes: 86 additions & 0 deletions migration/1726069430594-add_endaoment_project_banners.ts
Original file line number Diff line number Diff line change
@@ -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<void> {
const mainCategorySlugToBannerMapping = {
'environment-and-energy': 'environment-energy',
'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<void> {
// No down migration
}
}

0 comments on commit ecf4e19

Please sign in to comment.