Skip to content

Commit

Permalink
Merge pull request #172 from autentia/feature/show_inactive_users_in_…
Browse files Browse the repository at this point in the history
…activity_pending_of_approval

Feature/show inactive users in activity pending of approval
  • Loading branch information
Manumartin95 authored Sep 18, 2023
2 parents 0b167c7 + 6b18aa7 commit 9db66e5
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,24 @@ import { mock } from 'jest-mock-extended'
import { ProjectRepository } from '../domain/project-repository'
import { ProjectsWithUserName } from '../domain/services/projects-with-user-name'
import { GetProjectsListQry } from './get-projects-list-qry'
import { ProjectMother } from '../domain/tests/project-mother'

describe('GetProjectsListQry', () => {
it('should get the project list', async () => {
const { getProjectsListQry, projectRepository } = setup()
const { getProjectsListQry, projectRepository, getUsersListQry } = setup()
const organizationWithStatus = {
organizationId: 1,
open: true
}

projectRepository.getProjects.mockResolvedValue(
ProjectMother.projectsFilteredByOrganizationDateIso()
)

await getProjectsListQry.internalExecute(organizationWithStatus)

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { OrganizationWithStatus } from '../domain/organization-status'
import { Project } from '../domain/project'
import type { ProjectRepository } from '../domain/project-repository'
import { ProjectsWithUserName } from '../domain/services/projects-with-user-name'
import { Id } from '../../../../../shared/types/id'

@UseCaseKey('GetProjectsListQry')
@InvalidateCache
Expand All @@ -21,7 +22,9 @@ 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()
const usersList = await this.getUsersListQry.execute({
ids: projects.map((project) => project.blockedByUser).filter((id) => id !== null) as Id[]
})
return this.projectsWithUserName.addUserNameToProjects(projects, usersList)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,15 @@ 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()
this.getUsersListQry.execute({ ids: uniqueUserIds })
])

const activities = this.activitiesWithRoleInformation.addRoleInformationToActivities(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ interface Props {
export const ActivityUserFilter: FC<Props> = (props) => {
const { t } = useTranslation()
const { control } = useForm()
const { isLoading, result: users } = useExecuteUseCaseOnMount(GetUsersListQry)
const { isLoading, result: users } = useExecuteUseCaseOnMount(GetUsersListQry, { active: true })

const handleChange = (user: UserInfo) => {
props.onChange(user)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ describe('GetUsersList', () => {
it('should get the users list using the repository', async () => {
const { userRepository, getUsersListQry } = setup()

await getUsersListQry.internalExecute()
await getUsersListQry.internalExecute({})

expect(userRepository.getUsers).toBeCalled()
})
Expand Down
12 changes: 9 additions & 3 deletions src/features/shared/user/application/get-users-list-qry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,21 @@ import { USER_REPOSITORY } from '../../../../shared/di/container-tokens'
import { inject, singleton } from 'tsyringe'
import type { UserRepository } from '../domain/user-repository'
import { UserInfo } from '../domain/user-info'
import { Id } from '../../../../shared/types/id'

interface GetUsersParams {
ids?: Id[]
active?: boolean
}

@UseCaseKey('GetUsersListQry')
@singleton()
export class GetUsersListQry extends Query<UserInfo[]> {
export class GetUsersListQry extends Query<UserInfo[], GetUsersParams> {
constructor(@inject(USER_REPOSITORY) private userRepository: UserRepository) {
super()
}

internalExecute(): Promise<UserInfo[]> {
return this.userRepository.getUsers()
internalExecute(params: GetUsersParams): Promise<UserInfo[]> {
return this.userRepository.getUsers(params?.ids, params?.active)
}
}
4 changes: 3 additions & 1 deletion src/features/shared/user/domain/user-repository.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { User } from './user'
import { UserInfo } from './user-info'
import { Id } from '../../../../shared/types/id'

export interface UserRepository {
getUser(): Promise<User>
getUsers(): Promise<UserInfo[]>

getUsers(ids?: Id[], active?: boolean): Promise<UserInfo[]>
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,14 @@ describe('UserRepository', () => {

httpClient.get.mockResolvedValue(UserMother.userList())

const result = await userRepository.getUsers()
const result = await userRepository.getUsers([1, 2], true)

expect(httpClient.get).toHaveBeenCalledWith('/api/user')
expect(httpClient.get).toHaveBeenCalledWith('/api/user', {
params: {
active: true,
ids: [1, 2]
}
})
expect(result).toEqual(UserMother.userList())
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { AnonymousUserError } from '../domain/anonymous-user-error'
import { UserRepository } from '../domain/user-repository'
import { User } from '../domain/user'
import { UserInfo } from '../domain/user-info'
import { Id } from '../../../../shared/types/id'

@singleton()
export class HttpUserRepository implements UserRepository {
Expand All @@ -24,9 +25,11 @@ export class HttpUserRepository implements UserRepository {
}
}

async getUsers(): Promise<UserInfo[]> {
async getUsers(ids?: Id[], active?: boolean): Promise<UserInfo[]> {
try {
return await this.httpClient.get<UserInfo[]>(HttpUserRepository.usersPath)
return await this.httpClient.get<UserInfo[]>(HttpUserRepository.usersPath, {
params: { ids, active }
})
} catch (error) {
if (error.response?.status === 404 || error.response?.status === 401) {
throw new AnonymousUserError()
Expand Down

0 comments on commit 9db66e5

Please sign in to comment.