From 9044bfac84a3c32c40de46c1dcfe421ced23c47f Mon Sep 17 00:00:00 2001 From: mohitb35 <44917347+mohitb35@users.noreply.github.com> Date: Wed, 7 Aug 2024 18:28:01 +0530 Subject: [PATCH] feat: remove references to treecounter/treecounter groups from profile apis - updates prisma schema - renames fetchGroupTreecounterData >> fetchProfileGroupData and updates queries - updates APIs to use fetchProfileGroupData --- prisma/schema.prisma | 1 - .../common/Layout/MyForestContextV2.tsx | 2 -- src/features/common/types/myForestv2.d.ts | 2 +- .../procedures/myForestV2/contributions.ts | 17 +++++-------- .../procedures/myForestV2/leaderboard.ts | 17 +++++-------- src/server/utils/fetchGroupTreecounterData.ts | 17 ------------- src/server/utils/fetchProfile.ts | 1 - src/server/utils/fetchProfileGroupData.ts | 25 +++++++++++++++++++ 8 files changed, 38 insertions(+), 44 deletions(-) delete mode 100644 src/server/utils/fetchGroupTreecounterData.ts create mode 100644 src/server/utils/fetchProfileGroupData.ts diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 0878231070..43fa32f5f5 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -61,7 +61,6 @@ model project { model profile { id Int @id @default(autoincrement()) - treecounterId Int @map("treecounter_id") guid String @db.VarChar(32) name String? @db.VarChar(255) deleted_at DateTime? @map("deleted_at") @db.DateTime(0) diff --git a/src/features/common/Layout/MyForestContextV2.tsx b/src/features/common/Layout/MyForestContextV2.tsx index 03a448d6d8..4e3e8b3783 100644 --- a/src/features/common/Layout/MyForestContextV2.tsx +++ b/src/features/common/Layout/MyForestContextV2.tsx @@ -81,7 +81,6 @@ export const MyForestProviderV2: FC = ({ children }) => { const _contributions = trpc.myForestV2.contributions.useQuery( { profileId: `${userInfo?.profileId}`, - slug: `${userInfo?.slug}`, }, { enabled: !!userInfo?.profileId && !!userInfo?.slug, @@ -93,7 +92,6 @@ export const MyForestProviderV2: FC = ({ children }) => { const _leaderboard = trpc.myForestV2.leaderboard.useQuery( { profileId: `${userInfo?.profileId}`, - slug: `${userInfo?.slug}`, }, { enabled: !!userInfo?.profileId && !!userInfo?.slug, diff --git a/src/features/common/types/myForestv2.d.ts b/src/features/common/types/myForestv2.d.ts index 6e0a4405cc..ddb984b14d 100644 --- a/src/features/common/types/myForestv2.d.ts +++ b/src/features/common/types/myForestv2.d.ts @@ -113,7 +113,7 @@ export type MyForestProject = Omit & { allowDonations: boolean; }; -export type GroupTreecounterQueryResult = { +export type ProfileGroupQueryResult = { profileId: number; }; diff --git a/src/server/procedures/myForestV2/contributions.ts b/src/server/procedures/myForestV2/contributions.ts index 12a1f8f83e..e6b134b965 100644 --- a/src/server/procedures/myForestV2/contributions.ts +++ b/src/server/procedures/myForestV2/contributions.ts @@ -18,7 +18,7 @@ import { } from '../../../features/common/types/myForestv2'; import getPointCoordinates from '../../../utils/getPointCoordinates'; import { fetchProfile } from '../../utils/fetchProfile'; -import { fetchGroupTreecounterData } from '../../utils/fetchGroupTreecounterData'; +import { fetchProfileGroupData } from '../../utils/fetchProfileGroupData'; function initializeStats(): ContributionStats { return { @@ -379,10 +379,9 @@ export const contributionsProcedure = procedure .input( z.object({ profileId: z.string(), - slug: z.string(), }) ) - .query(async ({ input: { profileId, slug } }) => { + .query(async ({ input: { profileId } }) => { console.log(new Date().toLocaleString(), 'starting contributionsProcedure'); // Initialize return values @@ -406,15 +405,11 @@ export const contributionsProcedure = procedure }); } - // Check if the profile is associated with a group treecounter, and fetch all profile ids for that group (parent and children) - // slug and treecounterId are used to identify the group treecounter, if there is a mismatch, it will be treated as a normal profile - const groupTreecounterData = await fetchGroupTreecounterData( - slug, - profile.treecounterId - ); + // Check if the profile is associated with a profile group, and fetch all profile ids for that group (parent and children) + const profileGroupData = await fetchProfileGroupData(profile.id); const profileIds = - groupTreecounterData.length > 0 - ? groupTreecounterData.map(({ profileId }) => profileId) + profileGroupData.length > 0 + ? profileGroupData.map(({ profileId }) => profileId) : [profile.id]; // Fetch eligible projects diff --git a/src/server/procedures/myForestV2/leaderboard.ts b/src/server/procedures/myForestV2/leaderboard.ts index 147860a76a..d97b08d34c 100644 --- a/src/server/procedures/myForestV2/leaderboard.ts +++ b/src/server/procedures/myForestV2/leaderboard.ts @@ -5,7 +5,7 @@ import prisma from '../../../../prisma/client'; import { TRPCError } from '@trpc/server'; import { Prisma } from '@prisma/client'; import { fetchProfile } from '../../utils/fetchProfile'; -import { fetchGroupTreecounterData } from '../../utils/fetchGroupTreecounterData'; +import { fetchProfileGroupData } from '../../utils/fetchProfileGroupData'; async function fetchMostRecentGifts(profileIds: number[]) { return await prisma.$queryRaw< @@ -58,10 +58,9 @@ export const leaderboardProcedure = procedure .input( z.object({ profileId: z.string(), - slug: z.string(), }) ) - .query(async ({ input: { profileId, slug } }) => { + .query(async ({ input: { profileId } }) => { // Initialize return values const leaderboard: Leaderboard = { mostRecent: [], @@ -78,15 +77,11 @@ export const leaderboardProcedure = procedure }); } - // Check if the profile is associated with a group treecounter, and fetch all profile ids for that group (parent and children) - // slug and treecounterId are used to identify the group treecounter, if there is a mismatch, it will be treated as a normal profile - const groupTreecounterData = await fetchGroupTreecounterData( - slug, - profile.treecounterId - ); + // Check if the profile is associated with a profile group, and fetch all profile ids for that group (parent and children) + const profileGroupData = await fetchProfileGroupData(profile.id); const profileIds = - groupTreecounterData.length > 0 - ? groupTreecounterData.map(({ profileId }) => profileId) + profileGroupData.length > 0 + ? profileGroupData.map(({ profileId }) => profileId) : [profile.id]; // Fetch the most recent gifts for the profile(s) diff --git a/src/server/utils/fetchGroupTreecounterData.ts b/src/server/utils/fetchGroupTreecounterData.ts deleted file mode 100644 index 0369a54d55..0000000000 --- a/src/server/utils/fetchGroupTreecounterData.ts +++ /dev/null @@ -1,17 +0,0 @@ -import prisma from '../../../prisma/client'; -import { GroupTreecounterQueryResult } from '../../features/common/types/myForestv2'; - -export async function fetchGroupTreecounterData( - slug: string, - parentTreecounterId: number -) { - const data = await prisma.$queryRaw` - SELECT p.id as profileId - FROM profile p - INNER JOIN treecounter t ON p.treecounter_id = t.id - INNER JOIN treecounter_group child ON child.treecounter_id = t.id - INNER JOIN treecounter_group parent ON child.root_id = parent.id - WHERE parent.slug = ${slug} AND parent.treecounter_id = ${parentTreecounterId}; - `; - return data; -} diff --git a/src/server/utils/fetchProfile.ts b/src/server/utils/fetchProfile.ts index 13ce224e5e..d1bc7ba6b9 100644 --- a/src/server/utils/fetchProfile.ts +++ b/src/server/utils/fetchProfile.ts @@ -5,7 +5,6 @@ export async function fetchProfile(profileId: string) { select: { id: true, guid: true, - treecounterId: true, }, where: { guid: profileId, diff --git a/src/server/utils/fetchProfileGroupData.ts b/src/server/utils/fetchProfileGroupData.ts new file mode 100644 index 0000000000..5d0c36e8e8 --- /dev/null +++ b/src/server/utils/fetchProfileGroupData.ts @@ -0,0 +1,25 @@ +import prisma from '../../../prisma/client'; +import { ProfileGroupQueryResult } from '../../features/common/types/myForestv2'; + +export async function fetchProfileGroupData(profileId: number) { + const data = await prisma.$queryRaw` + SELECT pg.profile_id as profileId + FROM profile_group AS pg + WHERE pg.lft BETWEEN ( + SELECT root.lft + FROM profile_group AS root + WHERE root.profile_id = ${profileId} + ) AND ( + SELECT root.rgt + FROM profile_group AS root + WHERE root.profile_id = ${profileId} + ) + AND pg.root_id = ( + SELECT root.root_id + FROM profile_group AS root + WHERE root.profile_id = ${profileId} + ) + AND pg.profile_id IS NOT NULL; + `; + return data; +}