Skip to content

Commit

Permalink
Merge pull request #2166 from Plant-for-the-Planet-org/feature/remove…
Browse files Browse the repository at this point in the history
…-treecounter-references-in-apis

Remove references to treecounter/treecounter groups from profile apis
  • Loading branch information
mariahosfeld authored Aug 12, 2024
2 parents 5381424 + b7c8f50 commit 0527e28
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 44 deletions.
1 change: 0 additions & 1 deletion prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 0 additions & 2 deletions src/features/common/Layout/MyForestContextV2.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/features/common/types/myForestv2.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ export type MyForestProject = Omit<ProjectQueryResult, 'allowDonations'> & {
allowDonations: boolean;
};

export type GroupTreecounterQueryResult = {
export type ProfileGroupQueryResult = {
profileId: number;
};

Expand Down
17 changes: 6 additions & 11 deletions src/server/procedures/myForestV2/contributions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
17 changes: 6 additions & 11 deletions src/server/procedures/myForestV2/leaderboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<
Expand Down Expand Up @@ -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: [],
Expand All @@ -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)
Expand Down
17 changes: 0 additions & 17 deletions src/server/utils/fetchGroupTreecounterData.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/server/utils/fetchProfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ export async function fetchProfile(profileId: string) {
select: {
id: true,
guid: true,
treecounterId: true,
},
where: {
guid: profileId,
Expand Down
25 changes: 25 additions & 0 deletions src/server/utils/fetchProfileGroupData.ts
Original file line number Diff line number Diff line change
@@ -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<ProfileGroupQueryResult[]>`
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;
}

0 comments on commit 0527e28

Please sign in to comment.