From 8787fc33b5ab8443faee207e5dab51bdd6e5fe45 Mon Sep 17 00:00:00 2001 From: Manumartin95 Date: Wed, 20 Sep 2023 09:03:46 +0200 Subject: [PATCH 1/5] feat: dont make request if there is no ids --- .../project/application/get-projects-list-qry.ts | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/features/administration/features/project/application/get-projects-list-qry.ts b/src/features/administration/features/project/application/get-projects-list-qry.ts index adaee759..e80db09a 100644 --- a/src/features/administration/features/project/application/get-projects-list-qry.ts +++ b/src/features/administration/features/project/application/get-projects-list-qry.ts @@ -22,9 +22,16 @@ export class GetProjectsListQry extends Query async internalExecute(organizationStatus?: OrganizationWithStatus): Promise { const projects = await this.projectRepository.getProjects(organizationStatus) - const usersList = await this.getUsersListQry.execute({ - ids: projects.map((project) => project.blockedByUser).filter((id) => id !== null) as Id[] - }) - return this.projectsWithUserName.addUserNameToProjects(projects, usersList) + + const blockerUserIds = projects.map((project) => project.blockedByUser) + + if (blockerUserIds.length > 0) { + const uniqueBlockerUserIds = Array.from(new Set(blockerUserIds)) + const usersList = await this.getUsersListQry.execute({ + ids: uniqueBlockerUserIds.filter((id) => id !== null) as Id[] + }) + return this.projectsWithUserName.addUserNameToProjects(projects, usersList) + } + return projects } } From 39fdbcf42131917b4069859ab70e6c8df6ce3579 Mon Sep 17 00:00:00 2001 From: Manumartin95 Date: Wed, 20 Sep 2023 09:35:29 +0200 Subject: [PATCH 2/5] feat: include approval user ids to set of ids --- .../activity/application/get-activities-by-filters-qry.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/features/binnacle/features/activity/application/get-activities-by-filters-qry.ts b/src/features/binnacle/features/activity/application/get-activities-by-filters-qry.ts index cdab9c4d..06604e19 100644 --- a/src/features/binnacle/features/activity/application/get-activities-by-filters-qry.ts +++ b/src/features/binnacle/features/activity/application/get-activities-by-filters-qry.ts @@ -9,6 +9,7 @@ import { ActivitiesWithRoleInformation } from '../domain/services/activities-wit import { ActivitiesWithUserName } from '../domain/services/activities-with-user-name' import { ActivitiesWithApprovalUserName } from '../domain/services/activities-with-approval-user-name' import { GetActivitiesQueryParams } from '../domain/get-activities-query-params' +import { Id } from '../../../../../shared/types/id' interface GetActivitiesByStateParams { queryParams: GetActivitiesQueryParams @@ -35,7 +36,9 @@ export class GetActivitiesByFiltersQry extends Query a.projectRoleId) const uniqueProjectRoleIds = Array.from(new Set(projectRoleIds)) const userIds = activitiesResponse.map((activity) => activity.userId) - const uniqueUserIds = Array.from(new Set(userIds)) + const approvalUserIds = activitiesResponse.map((activity) => activity.approval.approvedByUserId) + + const uniqueUserIds = Array.from(new Set([...userIds, ...approvalUserIds])) as Id[] const [projectRolesInformation, usersList] = await Promise.all([ this.searchProjectRolesQry.execute({ From b764a5e38c7c0c4ba2713c36b8b9379804e745c5 Mon Sep 17 00:00:00 2001 From: Manumartin95 Date: Wed, 20 Sep 2023 10:03:46 +0200 Subject: [PATCH 3/5] feat: modify get activity by filters to dont send undefined in request --- .../application/get-activities-by-filters-qry.test.ts | 5 ++++- .../activity/application/get-activities-by-filters-qry.ts | 6 ++++-- src/test-utils/mothers/activity-mother.ts | 2 +- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/features/binnacle/features/activity/application/get-activities-by-filters-qry.test.ts b/src/features/binnacle/features/activity/application/get-activities-by-filters-qry.test.ts index e8b42495..56b0dc9c 100644 --- a/src/features/binnacle/features/activity/application/get-activities-by-filters-qry.test.ts +++ b/src/features/binnacle/features/activity/application/get-activities-by-filters-qry.test.ts @@ -12,7 +12,7 @@ import { GetActivitiesByFiltersQry } from './get-activities-by-filters-qry' describe('GetActivitiesByFiltersQry', () => { it('should return pending activities', async () => { - const { getPendingActivitiesQry, activitiesUser } = setup() + const { getPendingActivitiesQry, activitiesUser, getUsersListQry } = setup() const result = await getPendingActivitiesQry.internalExecute({ queryParams: { @@ -22,6 +22,8 @@ describe('GetActivitiesByFiltersQry', () => { } }) + expect(getUsersListQry.execute).toHaveBeenCalledWith({ ids: [1, 2] }) + expect(result).toEqual(activitiesUser) }) }) @@ -71,6 +73,7 @@ function setup() { searchProjectRolesQry, activitiesWithRoleInformation, activities, + getUsersListQry, activitiesUser } } diff --git a/src/features/binnacle/features/activity/application/get-activities-by-filters-qry.ts b/src/features/binnacle/features/activity/application/get-activities-by-filters-qry.ts index 06604e19..a8af06ec 100644 --- a/src/features/binnacle/features/activity/application/get-activities-by-filters-qry.ts +++ b/src/features/binnacle/features/activity/application/get-activities-by-filters-qry.ts @@ -35,17 +35,19 @@ export class GetActivitiesByFiltersQry extends Query a.projectRoleId) const uniqueProjectRoleIds = Array.from(new Set(projectRoleIds)) + const userIds = activitiesResponse.map((activity) => activity.userId) const approvalUserIds = activitiesResponse.map((activity) => activity.approval.approvedByUserId) - const uniqueUserIds = Array.from(new Set([...userIds, ...approvalUserIds])) as Id[] + const uniqueUserIds = Array.from(new Set([...userIds, ...approvalUserIds])) + const uniqueUserIdsWithoutUndefined = uniqueUserIds.filter((id) => id !== undefined) as Id[] const [projectRolesInformation, usersList] = await Promise.all([ this.searchProjectRolesQry.execute({ ids: uniqueProjectRoleIds, year: new Date(queryParams.startDate).getFullYear() }), - this.getUsersListQry.execute({ ids: uniqueUserIds }) + this.getUsersListQry.execute({ ids: uniqueUserIdsWithoutUndefined }) ]) const activities = this.activitiesWithRoleInformation.addRoleInformationToActivities( diff --git a/src/test-utils/mothers/activity-mother.ts b/src/test-utils/mothers/activity-mother.ts index fc0b7d3c..cabce1bd 100644 --- a/src/test-utils/mothers/activity-mother.ts +++ b/src/test-utils/mothers/activity-mother.ts @@ -39,7 +39,7 @@ export class ActivityMother { state: 'ACCEPTED', canBeApproved: false, approvalDate: new Date('2023-02-28T00:00:00.000Z'), - approvedByUserId: undefined + approvedByUserId: 2 } }) ] From 52cbfa9eefa2f6cddfded7c8de1edc32c9c12a96 Mon Sep 17 00:00:00 2001 From: Manumartin95 Date: Wed, 20 Sep 2023 10:09:26 +0200 Subject: [PATCH 4/5] test: check that users query is not called when there is no user ids --- .../application/get-projects-list-qry.test.ts | 17 +++++++++++++++++ .../application/get-projects-list-qry.ts | 6 ++++-- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/features/administration/features/project/application/get-projects-list-qry.test.ts b/src/features/administration/features/project/application/get-projects-list-qry.test.ts index dcaac761..2fcf6bf3 100644 --- a/src/features/administration/features/project/application/get-projects-list-qry.test.ts +++ b/src/features/administration/features/project/application/get-projects-list-qry.test.ts @@ -22,6 +22,23 @@ describe('GetProjectsListQry', () => { expect(projectRepository.getProjects).toBeCalledWith(organizationWithStatus) expect(getUsersListQry.execute).toHaveBeenCalledWith({ ids: [2, 1] }) }) + + it('should not make user request if there is no projects', async () => { + const { getProjectsListQry, projectRepository, getUsersListQry } = setup() + const organizationWithStatus = { + organizationId: 1, + open: true + } + + projectRepository.getProjects.mockResolvedValue([ + ProjectMother.projectsFilteredByOrganizationDateIso()[2] + ]) + + await getProjectsListQry.internalExecute(organizationWithStatus) + + expect(projectRepository.getProjects).toBeCalledWith(organizationWithStatus) + expect(getUsersListQry.execute).not.toHaveBeenCalled() + }) }) function setup() { diff --git a/src/features/administration/features/project/application/get-projects-list-qry.ts b/src/features/administration/features/project/application/get-projects-list-qry.ts index e80db09a..e5ba4c17 100644 --- a/src/features/administration/features/project/application/get-projects-list-qry.ts +++ b/src/features/administration/features/project/application/get-projects-list-qry.ts @@ -23,12 +23,14 @@ export class GetProjectsListQry extends Query async internalExecute(organizationStatus?: OrganizationWithStatus): Promise { const projects = await this.projectRepository.getProjects(organizationStatus) - const blockerUserIds = projects.map((project) => project.blockedByUser) + const blockerUserIds = projects + .map((project) => project.blockedByUser) + .filter((id) => id !== null) as Id[] if (blockerUserIds.length > 0) { const uniqueBlockerUserIds = Array.from(new Set(blockerUserIds)) const usersList = await this.getUsersListQry.execute({ - ids: uniqueBlockerUserIds.filter((id) => id !== null) as Id[] + ids: uniqueBlockerUserIds }) return this.projectsWithUserName.addUserNameToProjects(projects, usersList) } From 9f3e46bcc1331a7c00a0a2a6952ccf92a394f56a Mon Sep 17 00:00:00 2001 From: Manumartin95 Date: Wed, 20 Sep 2023 10:57:08 +0200 Subject: [PATCH 5/5] refactor: add condition to not make user o project role request if there is no activities --- .../get-activities-by-filters-qry.test.ts | 18 +++++++++ .../get-activities-by-filters-qry.ts | 39 +++++++++++-------- 2 files changed, 40 insertions(+), 17 deletions(-) diff --git a/src/features/binnacle/features/activity/application/get-activities-by-filters-qry.test.ts b/src/features/binnacle/features/activity/application/get-activities-by-filters-qry.test.ts index 56b0dc9c..a226fb6c 100644 --- a/src/features/binnacle/features/activity/application/get-activities-by-filters-qry.test.ts +++ b/src/features/binnacle/features/activity/application/get-activities-by-filters-qry.test.ts @@ -26,6 +26,24 @@ describe('GetActivitiesByFiltersQry', () => { expect(result).toEqual(activitiesUser) }) + + it('should return empty array and not make more requests if there is no activities', async () => { + const { getPendingActivitiesQry, getUsersListQry, activityRepository } = setup() + + activityRepository.getActivitiesBasedOnFilters.mockResolvedValue([]) + + const result = await getPendingActivitiesQry.internalExecute({ + queryParams: { + approvalState: 'PENDING', + startDate: '2023-01-01', + endDate: '2023-12-31' + } + }) + + expect(getUsersListQry.execute).not.toHaveBeenCalled() + + expect(result).toEqual([]) + }) }) function setup() { diff --git a/src/features/binnacle/features/activity/application/get-activities-by-filters-qry.ts b/src/features/binnacle/features/activity/application/get-activities-by-filters-qry.ts index a8af06ec..bc0fb811 100644 --- a/src/features/binnacle/features/activity/application/get-activities-by-filters-qry.ts +++ b/src/features/binnacle/features/activity/application/get-activities-by-filters-qry.ts @@ -42,24 +42,29 @@ export class GetActivitiesByFiltersQry extends Query id !== undefined) as Id[] - const [projectRolesInformation, usersList] = await Promise.all([ - this.searchProjectRolesQry.execute({ - ids: uniqueProjectRoleIds, - year: new Date(queryParams.startDate).getFullYear() - }), - this.getUsersListQry.execute({ ids: uniqueUserIdsWithoutUndefined }) - ]) + if (activitiesResponse.length > 0) { + const [projectRolesInformation, usersList] = await Promise.all([ + this.searchProjectRolesQry.execute({ + ids: uniqueProjectRoleIds, + year: new Date(queryParams.startDate).getFullYear() + }), + this.getUsersListQry.execute({ ids: uniqueUserIdsWithoutUndefined }) + ]) - const activities = this.activitiesWithRoleInformation.addRoleInformationToActivities( - activitiesResponse, - projectRolesInformation - ) - - const withUserName = this.activitiesWithUserName.addUserNameToActivities(activities, usersList) + const activities = this.activitiesWithRoleInformation.addRoleInformationToActivities( + activitiesResponse, + projectRolesInformation + ) - return this.activitiesWithApprovalUserName.addUserNameToActivitiesApproval( - withUserName, - usersList - ) + const withUserName = this.activitiesWithUserName.addUserNameToActivities( + activities, + usersList + ) + return this.activitiesWithApprovalUserName.addUserNameToActivitiesApproval( + withUserName, + usersList + ) + } + return [] } }