Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hotfix/dont make request if there is no ids #173

Merged
merged 5 commits into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,18 @@ export class GetProjectsListQry extends Query<Project[], OrganizationWithStatus>

async internalExecute(organizationStatus?: OrganizationWithStatus): Promise<Project[]> {
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)
.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
})
return this.projectsWithUserName.addUserNameToProjects(projects, usersList)
}
return projects
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -22,8 +22,28 @@ describe('GetActivitiesByFiltersQry', () => {
}
})

expect(getUsersListQry.execute).toHaveBeenCalledWith({ ids: [1, 2] })

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() {
Expand Down Expand Up @@ -71,6 +91,7 @@ function setup() {
searchProjectRolesQry,
activitiesWithRoleInformation,
activities,
getUsersListQry,
activitiesUser
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -34,27 +35,36 @@ export class GetActivitiesByFiltersQry extends Query<Activity[], GetActivitiesBy
)
const projectRoleIds = activitiesResponse.map((a) => a.projectRoleId)
const uniqueProjectRoleIds = Array.from(new Set(projectRoleIds))

const userIds = activitiesResponse.map((activity) => activity.userId)
const uniqueUserIds = Array.from(new Set(userIds))

const [projectRolesInformation, usersList] = await Promise.all([
this.searchProjectRolesQry.execute({
ids: uniqueProjectRoleIds,
year: new Date(queryParams.startDate).getFullYear()
}),
this.getUsersListQry.execute({ ids: uniqueUserIds })
])

const activities = this.activitiesWithRoleInformation.addRoleInformationToActivities(
activitiesResponse,
projectRolesInformation
)
const approvalUserIds = activitiesResponse.map((activity) => activity.approval.approvedByUserId)

const withUserName = this.activitiesWithUserName.addUserNameToActivities(activities, usersList)
const uniqueUserIds = Array.from(new Set([...userIds, ...approvalUserIds]))
const uniqueUserIdsWithoutUndefined = uniqueUserIds.filter((id) => id !== undefined) as Id[]

return this.activitiesWithApprovalUserName.addUserNameToActivitiesApproval(
withUserName,
usersList
)
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
)
return this.activitiesWithApprovalUserName.addUserNameToActivitiesApproval(
withUserName,
usersList
)
}
return []
}
}
2 changes: 1 addition & 1 deletion src/test-utils/mothers/activity-mother.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export class ActivityMother {
state: 'ACCEPTED',
canBeApproved: false,
approvalDate: new Date('2023-02-28T00:00:00.000Z'),
approvedByUserId: undefined
approvedByUserId: 2
}
})
]
Expand Down