Skip to content

Commit c3e369c

Browse files
authored
Merge pull request #2113 from Giveth/staging
Fix performance issues
2 parents e7adcf7 + 548e0db commit c3e369c

6 files changed

Lines changed: 280 additions & 162 deletions

File tree

src/entities/project.ts

Lines changed: 30 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -462,8 +462,11 @@ export class Project extends BaseEntity {
462462
projectType: string = 'project';
463463

464464
// Many-to-many relationship with causes through CauseProject junction table
465+
// @Field(_type => [CauseProject], { nullable: true })
466+
// @OneToMany(_type => CauseProject, causeProject => causeProject.project)
467+
// causeProjects?: CauseProject[];
468+
465469
@Field(_type => [CauseProject], { nullable: true })
466-
@OneToMany(_type => CauseProject, causeProject => causeProject.project)
467470
causeProjects?: CauseProject[];
468471

469472
@Field(_type => [Project], { nullable: true })
@@ -580,10 +583,10 @@ export class Project extends BaseEntity {
580583
}
581584
}
582585

583-
@Field(_type => [CauseProject], { nullable: true })
584-
async loadCauseProjects(): Promise<CauseProject[] | []> {
585-
return [];
586-
}
586+
// @Field(_type => [CauseProject], { nullable: true })
587+
// async loadCauseProjects(): Promise<CauseProject[] | []> {
588+
// return [];
589+
// }
587590

588591
@BeforeUpdate()
589592
async updateProjectDescriptionSummary() {
@@ -817,9 +820,9 @@ export class Cause extends Project {
817820
totalDonated?: number;
818821

819822
// Many-to-many relationship with projects through CauseProject junction table
820-
@Field(_type => [CauseProject], { nullable: true })
821-
@OneToMany(_type => CauseProject, causeProject => causeProject.cause)
822-
causeProjects?: CauseProject[];
823+
// @Field(_type => [CauseProject], { nullable: true })
824+
// @OneToMany(_type => CauseProject, causeProject => causeProject.cause)
825+
// causeProjects?: CauseProject[];
823826

824827
@Field(_type => Int, { nullable: true })
825828
@Column({ type: 'integer', default: 0, nullable: true })
@@ -831,45 +834,35 @@ export class Cause extends Project {
831834
projectType: string = 'cause';
832835

833836
@Field(_type => [CauseProject], { nullable: true })
834-
async loadCauseProjects(): Promise<CauseProject[] | []> {
837+
causeProjects?: CauseProject[];
838+
839+
// Internal method for loading cause projects with filtering
840+
async loadCauseProjects(userRemoved?: boolean): Promise<CauseProject[] | []> {
835841
if (this.projectType.toLowerCase() === 'project') {
836842
return [];
837843
}
838844

839-
const causeProjects = await CauseProject.createQueryBuilder('causeProject')
845+
const baseQuery = CauseProject.createQueryBuilder('causeProject')
840846
.leftJoinAndSelect('causeProject.project', 'project')
841847
.leftJoinAndSelect('project.status', 'status')
842848
.leftJoinAndSelect('project.addresses', 'addresses')
843-
.leftJoinAndSelect(
844-
'project.socialProfiles',
845-
'socialProfiles',
846-
'socialProfiles.projectId = project.id',
847-
)
848-
.leftJoinAndSelect(
849-
'project.socialMedia',
850-
'socialMedia',
851-
'socialMedia.projectId = project.id',
852-
)
853-
.leftJoinAndSelect('project.anchorContracts', 'anchor_contract_address')
854-
.leftJoinAndSelect('project.projectPower', 'projectPower')
855849
.leftJoinAndSelect('project.projectInstantPower', 'projectInstantPower')
856-
.leftJoinAndSelect('project.projectFuturePower', 'projectFuturePower')
857-
.leftJoinAndSelect('project.projectUpdates', 'projectUpdates')
858-
.leftJoinAndSelect(
859-
'project.categories',
860-
'categories',
861-
'categories.isActive = :isActive',
862-
{ isActive: true },
863-
)
864-
.leftJoinAndSelect('categories.mainCategory', 'mainCategory')
865850
.leftJoinAndSelect('project.organization', 'organization')
866-
.leftJoinAndSelect('project.qfRounds', 'qfRounds')
867-
.leftJoin('project.adminUser', 'user')
868-
.where('causeProject.causeId = :causeId', { causeId: this.id })
869-
.getMany();
851+
.leftJoinAndSelect('project.adminUser', 'user')
852+
.where('causeProject.causeId = :causeId', { causeId: this.id });
853+
854+
if (userRemoved !== undefined) {
855+
baseQuery.andWhere('causeProject.userRemoved = :userRemoved', {
856+
userRemoved,
857+
});
858+
}
859+
860+
const causeProjects = await baseQuery.getMany();
861+
870862
return causeProjects;
871863
}
872864

865+
// TODO: merge this with causeProjects
873866
// Virtual field to get projects directly
874867
@Field(_type => [Project], { nullable: true })
875868
async projects(): Promise<Project[] | []> {
@@ -880,36 +873,23 @@ export class Cause extends Project {
880873
const causeProjects = await CauseProject.createQueryBuilder('causeProject')
881874
.leftJoinAndSelect('causeProject.project', 'project')
882875
.leftJoinAndSelect('project.status', 'status')
883-
.leftJoinAndSelect('project.addresses', 'addresses')
884-
.leftJoinAndSelect(
885-
'project.socialProfiles',
886-
'socialProfiles',
887-
'socialProfiles.projectId = project.id',
888-
)
889876
.leftJoinAndSelect(
890877
'project.socialMedia',
891878
'socialMedia',
892879
'socialMedia.projectId = project.id',
893880
)
894-
.leftJoinAndSelect('project.anchorContracts', 'anchor_contract_address')
895-
.leftJoinAndSelect('project.projectPower', 'projectPower')
896-
.leftJoinAndSelect('project.projectInstantPower', 'projectInstantPower')
897881
.leftJoinAndSelect(
898882
'project.projectUpdates',
899883
'projectUpdates',
900884
'projectUpdates.projectId = project.id',
901885
)
902-
.leftJoinAndSelect('project.projectFuturePower', 'projectFuturePower')
903886
.leftJoinAndSelect(
904887
'project.categories',
905888
'categories',
906889
'categories.isActive = :isActive',
907890
{ isActive: true },
908891
)
909892
.leftJoinAndSelect('categories.mainCategory', 'mainCategory')
910-
.leftJoinAndSelect('project.organization', 'organization')
911-
.leftJoinAndSelect('project.qfRounds', 'qfRounds')
912-
.leftJoinAndSelect('project.adminUser', 'adminUser')
913893
.where('causeProject.causeId = :causeId', { causeId: this.id })
914894
.getMany();
915895
return causeProjects.map(cp => cp.project);
@@ -924,7 +904,7 @@ export class CauseProject extends BaseEntity {
924904
id: number;
925905

926906
@Field(_type => Cause, { nullable: true })
927-
@ManyToOne(_type => Cause, cause => cause.causeProjects)
907+
@ManyToOne(_type => Cause)
928908
@JoinColumn()
929909
cause: Cause;
930910

@@ -933,7 +913,7 @@ export class CauseProject extends BaseEntity {
933913
causeId: number;
934914

935915
@Field(_type => Project, { nullable: true })
936-
@ManyToOne(_type => Project, project => project.causeProjects)
916+
@ManyToOne(_type => Project)
937917
@JoinColumn()
938918
project: Project;
939919

src/repositories/causeRepository.test.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { User } from '../entities/user';
33
import { Project, Cause, ReviewStatus, ProjStatus } from '../entities/project';
44
import {
55
findCauseById,
6-
findCauseByCauseId,
76
findCausesByOwnerId,
87
findCausesByProjectIds,
98
createCause,
@@ -143,7 +142,7 @@ describe('causeRepository test cases', async () => {
143142

144143
describe('findCauseByCauseId test cases', () => {
145144
it('should find cause by causeId with relations', async () => {
146-
const foundCause = await findCauseByCauseId(testCause.id);
145+
const foundCause = await findCauseById(testCause.id);
147146

148147
assert.isOk(foundCause);
149148
assert.equal(foundCause?.id, testCause.id);
@@ -161,7 +160,7 @@ describe('causeRepository test cases', async () => {
161160
});
162161

163162
it('should return null when cause not found', async () => {
164-
const foundCause = await findCauseByCauseId(999999);
163+
const foundCause = await findCauseById(999999);
165164
assert.isNull(foundCause);
166165
});
167166
});

src/repositories/causeRepository.ts

Lines changed: 69 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {
44
ProjStatus,
55
CauseProject,
66
} from '../entities/project';
7-
import { User, publicSelectionFields } from '../entities/user';
7+
import { User } from '../entities/user';
88
import { Project } from '../entities/project';
99
import { i18n, translationErrorMessagesKeys } from '../utils/errorMessages';
1010
import { ChainType } from '../types/network';
@@ -31,8 +31,6 @@ export enum SortDirection {
3131
export const findCauseById = async (id: number): Promise<Cause | null> => {
3232
return Cause.createQueryBuilder('cause')
3333
.leftJoinAndSelect('cause.adminUser', 'adminUser')
34-
.leftJoinAndSelect('cause.causeProjects', 'causeProjects')
35-
.leftJoinAndSelect('causeProjects.project', 'project')
3634
.leftJoinAndSelect('cause.status', 'status')
3735
.leftJoinAndSelect('cause.categories', 'categories')
3836
.leftJoinAndSelect('categories.mainCategory', 'mainCategory')
@@ -43,23 +41,6 @@ export const findCauseById = async (id: number): Promise<Cause | null> => {
4341
.getOne();
4442
};
4543

46-
export const findCauseByCauseId = async (
47-
causeId: number,
48-
): Promise<Cause | null> => {
49-
return Cause.createQueryBuilder('cause')
50-
.leftJoinAndSelect('cause.adminUser', 'adminUser')
51-
.leftJoinAndSelect('cause.causeProjects', 'causeProjects')
52-
.leftJoinAndSelect('causeProjects.project', 'project')
53-
.leftJoinAndSelect('cause.status', 'status')
54-
.leftJoinAndSelect('cause.categories', 'categories')
55-
.leftJoinAndSelect('categories.mainCategory', 'mainCategory')
56-
.where('cause.id = :causeId', { causeId })
57-
.andWhere('lower(cause.projectType) = lower(:projectType)', {
58-
projectType: 'cause',
59-
})
60-
.getOne();
61-
};
62-
6344
export const findCausesByOwnerId = async (
6445
ownerId: number,
6546
): Promise<Cause[]> => {
@@ -149,27 +130,27 @@ export const createCause = async (
149130
);
150131

151132
// Return the cause with all relations
152-
const result = await Cause.createQueryBuilder('cause')
153-
.leftJoinAndSelect('cause.adminUser', 'adminUser')
154-
.leftJoinAndSelect('cause.status', 'status')
155-
.leftJoinAndSelect('cause.causeProjects', 'causeProjects')
156-
.leftJoinAndSelect('causeProjects.project', 'project')
157-
.leftJoinAndSelect('cause.categories', 'categories')
158-
.leftJoinAndSelect('categories.mainCategory', 'mainCategory')
159-
.where('cause.id = :id', { id: savedCause.id })
160-
.getOne();
161-
162-
if (!result) {
163-
throw new Error('Failed to retrieve created cause');
164-
}
165-
166-
result.causeProjects = await loadCauseProjects(result);
167-
168-
return result;
133+
// const result = await Cause.createQueryBuilder('cause')
134+
// .leftJoinAndSelect('cause.adminUser', 'adminUser')
135+
// .leftJoinAndSelect('cause.status', 'status')
136+
// .leftJoinAndSelect('cause.causeProjects', 'causeProjects')
137+
// .leftJoinAndSelect('causeProjects.project', 'project')
138+
// .leftJoinAndSelect('cause.categories', 'categories')
139+
// .leftJoinAndSelect('categories.mainCategory', 'mainCategory')
140+
// .where('cause.id = :id', { id: savedCause.id })
141+
// .getOne();
142+
143+
// if (!result) {
144+
// throw new Error('Failed to retrieve created cause');
145+
// }
146+
147+
// result.causeProjects = await loadCauseProjects(result);
148+
149+
return savedCause;
169150
};
170151

171152
export const activateCause = async (causeId: number): Promise<Cause> => {
172-
const cause = await findCauseByCauseId(causeId);
153+
const cause = await findCauseById(causeId);
173154
if (!cause) {
174155
throw new Error(i18n.__(translationErrorMessagesKeys.CAUSE_NOT_FOUND));
175156
}
@@ -185,7 +166,7 @@ export const activateCause = async (causeId: number): Promise<Cause> => {
185166
};
186167

187168
export const deactivateCause = async (causeId: number): Promise<Cause> => {
188-
const cause = await findCauseByCauseId(causeId);
169+
const cause = await findCauseById(causeId);
189170
if (!cause) {
190171
throw new Error(i18n.__(translationErrorMessagesKeys.CAUSE_NOT_FOUND));
191172
}
@@ -269,55 +250,55 @@ export const validateTransactionHash = async (
269250
return true;
270251
};
271252

272-
export const loadCauseProjects = async (
273-
cause: Cause,
274-
userRemoved: boolean | undefined = undefined,
275-
): Promise<CauseProject[] | []> => {
276-
if (cause.projectType.toLowerCase() === 'project') {
277-
return [];
278-
}
279-
280-
const baseQuery = await CauseProject.createQueryBuilder('causeProject')
281-
.leftJoinAndSelect('causeProject.project', 'project')
282-
.leftJoinAndSelect('project.status', 'status')
283-
.leftJoinAndSelect('project.addresses', 'addresses')
284-
.leftJoinAndSelect(
285-
'project.socialProfiles',
286-
'socialProfiles',
287-
'socialProfiles.projectId = project.id',
288-
)
289-
.leftJoinAndSelect(
290-
'project.socialMedia',
291-
'socialMedia',
292-
'socialMedia.projectId = project.id',
293-
)
294-
.leftJoinAndSelect('project.anchorContracts', 'anchor_contract_address')
295-
.leftJoinAndSelect('project.projectPower', 'projectPower')
296-
.leftJoinAndSelect('project.projectInstantPower', 'projectInstantPower')
297-
.leftJoinAndSelect('project.projectFuturePower', 'projectFuturePower')
298-
.leftJoinAndSelect('project.projectUpdates', 'projectUpdates')
299-
.leftJoinAndSelect(
300-
'project.categories',
301-
'categories',
302-
'categories.isActive = :isActive',
303-
{ isActive: true },
304-
)
305-
.leftJoinAndSelect('categories.mainCategory', 'mainCategory')
306-
.leftJoinAndSelect('project.organization', 'organization')
307-
.leftJoinAndSelect('project.qfRounds', 'qfRounds')
308-
.leftJoin('project.adminUser', 'user')
309-
.addSelect(publicSelectionFields)
310-
.where('causeProject.causeId = :causeId', { causeId: cause.id });
311-
312-
if (userRemoved !== undefined) {
313-
baseQuery.andWhere('causeProject.userRemoved = :userRemoved', {
314-
userRemoved,
315-
});
316-
}
317-
318-
const causeProjects = await baseQuery.getMany();
319-
return causeProjects;
320-
};
253+
// export const loadCauseProjects = async (
254+
// cause: Cause,
255+
// userRemoved: boolean | undefined = undefined,
256+
// ): Promise<CauseProject[] | []> => {
257+
// if (cause.projectType.toLowerCase() === 'project') {
258+
// return [];
259+
// }
260+
261+
// const baseQuery = await CauseProject.createQueryBuilder('causeProject')
262+
// .leftJoinAndSelect('causeProject.project', 'project')
263+
// .leftJoinAndSelect('project.status', 'status')
264+
// .leftJoinAndSelect('project.addresses', 'addresses')
265+
// .leftJoinAndSelect(
266+
// 'project.socialProfiles',
267+
// 'socialProfiles',
268+
// 'socialProfiles.projectId = project.id',
269+
// )
270+
// .leftJoinAndSelect(
271+
// 'project.socialMedia',
272+
// 'socialMedia',
273+
// 'socialMedia.projectId = project.id',
274+
// )
275+
// .leftJoinAndSelect('project.anchorContracts', 'anchor_contract_address')
276+
// .leftJoinAndSelect('project.projectPower', 'projectPower')
277+
// .leftJoinAndSelect('project.projectInstantPower', 'projectInstantPower')
278+
// .leftJoinAndSelect('project.projectFuturePower', 'projectFuturePower')
279+
// .leftJoinAndSelect('project.projectUpdates', 'projectUpdates')
280+
// .leftJoinAndSelect(
281+
// 'project.categories',
282+
// 'categories',
283+
// 'categories.isActive = :isActive',
284+
// { isActive: true },
285+
// )
286+
// .leftJoinAndSelect('categories.mainCategory', 'mainCategory')
287+
// .leftJoinAndSelect('project.organization', 'organization')
288+
// .leftJoinAndSelect('project.qfRounds', 'qfRounds')
289+
// .leftJoin('project.adminUser', 'user')
290+
// .addSelect(publicSelectionFields)
291+
// .where('causeProject.causeId = :causeId', { causeId: cause.id });
292+
293+
// if (userRemoved !== undefined) {
294+
// baseQuery.andWhere('causeProject.userRemoved = :userRemoved', {
295+
// userRemoved,
296+
// });
297+
// }
298+
299+
// const causeProjects = await baseQuery.getMany();
300+
// return causeProjects;
301+
// };
321302

322303
export const findAllCauses = async (
323304
limit?: number,
@@ -342,11 +323,7 @@ export const findAllCauses = async (
342323
{ isActive: true },
343324
)
344325
.leftJoinAndSelect('projectCategories.mainCategory', 'projectMainCategory')
345-
.leftJoinAndSelect('project.addresses', 'addresses')
346326
.leftJoinAndSelect('project.socialMedia', 'socialMedia')
347-
.leftJoinAndSelect('project.projectPower', 'projectPower')
348-
.leftJoinAndSelect('project.projectInstantPower', 'projectInstantPower')
349-
.leftJoinAndSelect('project.projectFuturePower', 'projectFuturePower')
350327
.where('lower(cause.projectType) = lower(:projectType)', {
351328
projectType: 'cause',
352329
});

0 commit comments

Comments
 (0)