mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-16 18:56:22 +00:00
Merge pull request #185 from SyncrowIOT/SP-907-be-get-users-by-project
Add endpoint to get users by project
This commit is contained in:
@ -21,6 +21,11 @@ export class ControllerRoute {
|
|||||||
public static readonly DELETE_PROJECT_SUMMARY = 'Delete a project';
|
public static readonly DELETE_PROJECT_SUMMARY = 'Delete a project';
|
||||||
public static readonly DELETE_PROJECT_DESCRIPTION =
|
public static readonly DELETE_PROJECT_DESCRIPTION =
|
||||||
'This endpoint deletes an existing project by its unique identifier (UUID).';
|
'This endpoint deletes an existing project by its unique identifier (UUID).';
|
||||||
|
|
||||||
|
public static readonly GET_USERS_BY_PROJECT_SUMMARY =
|
||||||
|
'Get users by project';
|
||||||
|
public static readonly GET_USERS_BY_PROJECT_DESCRIPTION =
|
||||||
|
'This endpoint retrieves all users associated with a specific project.';
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -733,6 +738,7 @@ export class ControllerRoute {
|
|||||||
|
|
||||||
public static readonly CREATE_USER_INVITATION_DESCRIPTION =
|
public static readonly CREATE_USER_INVITATION_DESCRIPTION =
|
||||||
'This endpoint creates an invitation for a user to assign to role and spaces.';
|
'This endpoint creates an invitation for a user to assign to role and spaces.';
|
||||||
|
|
||||||
public static readonly CHECK_EMAIL_SUMMARY = 'Check email';
|
public static readonly CHECK_EMAIL_SUMMARY = 'Check email';
|
||||||
|
|
||||||
public static readonly CHECK_EMAIL_DESCRIPTION =
|
public static readonly CHECK_EMAIL_DESCRIPTION =
|
||||||
|
@ -86,4 +86,18 @@ export class ProjectController {
|
|||||||
async findOne(@Param() params: GetProjectParam): Promise<BaseResponseDto> {
|
async findOne(@Param() params: GetProjectParam): Promise<BaseResponseDto> {
|
||||||
return this.projectService.getProject(params.projectUuid);
|
return this.projectService.getProject(params.projectUuid);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ApiBearerAuth()
|
||||||
|
@UseGuards(JwtAuthGuard)
|
||||||
|
@ApiOperation({
|
||||||
|
summary: ControllerRoute.PROJECT.ACTIONS.GET_USERS_BY_PROJECT_SUMMARY,
|
||||||
|
description:
|
||||||
|
ControllerRoute.PROJECT.ACTIONS.GET_USERS_BY_PROJECT_DESCRIPTION,
|
||||||
|
})
|
||||||
|
@Get(':projectUuid/users')
|
||||||
|
async findUsersByProject(
|
||||||
|
@Param() params: GetProjectParam,
|
||||||
|
): Promise<BaseResponseDto> {
|
||||||
|
return this.projectService.getUsersByProject(params.projectUuid);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,6 +6,8 @@ import { ProjectRepository } from '@app/common/modules/project/repositiories';
|
|||||||
import { CreateOrphanSpaceHandler } from './handler';
|
import { CreateOrphanSpaceHandler } from './handler';
|
||||||
import { SpaceRepository } from '@app/common/modules/space';
|
import { SpaceRepository } from '@app/common/modules/space';
|
||||||
import { CommunityRepository } from '@app/common/modules/community/repositories';
|
import { CommunityRepository } from '@app/common/modules/community/repositories';
|
||||||
|
import { InviteUserRepository } from '@app/common/modules/Invite-user/repositiories';
|
||||||
|
import { UserRepository } from '@app/common/modules/user/repositories';
|
||||||
|
|
||||||
const CommandHandlers = [CreateOrphanSpaceHandler];
|
const CommandHandlers = [CreateOrphanSpaceHandler];
|
||||||
|
|
||||||
@ -19,6 +21,8 @@ const CommandHandlers = [CreateOrphanSpaceHandler];
|
|||||||
CommunityRepository,
|
CommunityRepository,
|
||||||
ProjectService,
|
ProjectService,
|
||||||
ProjectRepository,
|
ProjectRepository,
|
||||||
|
InviteUserRepository,
|
||||||
|
UserRepository,
|
||||||
],
|
],
|
||||||
exports: [ProjectService, CqrsModule],
|
exports: [ProjectService, CqrsModule],
|
||||||
})
|
})
|
||||||
|
@ -12,11 +12,17 @@ import { ProjectDto } from '@app/common/modules/project/dtos';
|
|||||||
import { PageResponse } from '@app/common/dto/pagination.response.dto';
|
import { PageResponse } from '@app/common/dto/pagination.response.dto';
|
||||||
import { CommandBus } from '@nestjs/cqrs';
|
import { CommandBus } from '@nestjs/cqrs';
|
||||||
import { CreateOrphanSpaceCommand } from '../command/create-orphan-space-command';
|
import { CreateOrphanSpaceCommand } from '../command/create-orphan-space-command';
|
||||||
|
import { InviteUserRepository } from '@app/common/modules/Invite-user/repositiories';
|
||||||
|
import { UserRepository } from '@app/common/modules/user/repositories';
|
||||||
|
import { UserStatusEnum } from '@app/common/constants/user-status.enum';
|
||||||
|
import { RoleType } from '@app/common/constants/role.type.enum';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class ProjectService {
|
export class ProjectService {
|
||||||
constructor(
|
constructor(
|
||||||
private readonly projectRepository: ProjectRepository,
|
private readonly projectRepository: ProjectRepository,
|
||||||
|
private readonly inviteUserRepository: InviteUserRepository,
|
||||||
|
private readonly userRepository: UserRepository,
|
||||||
private commandBus: CommandBus,
|
private commandBus: CommandBus,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
@ -181,6 +187,78 @@ export class ProjectService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async getUsersByProject(uuid: string): Promise<BaseResponseDto> {
|
||||||
|
try {
|
||||||
|
// Fetch invited users
|
||||||
|
const invitedUsers = await this.inviteUserRepository.find({
|
||||||
|
where: { project: { uuid }, isActive: true },
|
||||||
|
select: [
|
||||||
|
'firstName',
|
||||||
|
'lastName',
|
||||||
|
'email',
|
||||||
|
'createdAt',
|
||||||
|
'status',
|
||||||
|
'phoneNumber',
|
||||||
|
'jobTitle',
|
||||||
|
'invitedBy',
|
||||||
|
'isEnabled',
|
||||||
|
],
|
||||||
|
relations: ['roleType'],
|
||||||
|
});
|
||||||
|
|
||||||
|
// Fetch project users
|
||||||
|
const users = await this.userRepository.find({
|
||||||
|
where: { project: { uuid }, isActive: true },
|
||||||
|
select: ['firstName', 'lastName', 'email', 'createdAt'],
|
||||||
|
relations: ['roleType'],
|
||||||
|
});
|
||||||
|
|
||||||
|
// Combine both arrays
|
||||||
|
const allUsers = [...users, ...invitedUsers];
|
||||||
|
|
||||||
|
const normalizedUsers = allUsers.map((user) => {
|
||||||
|
const createdAt = new Date(user.createdAt);
|
||||||
|
const createdDate = createdAt.toLocaleDateString();
|
||||||
|
const createdTime = createdAt.toLocaleTimeString();
|
||||||
|
|
||||||
|
// Normalize user properties
|
||||||
|
const normalizedProps = this.normalizeUserProperties(user);
|
||||||
|
|
||||||
|
// Return the normalized user object
|
||||||
|
return {
|
||||||
|
...user,
|
||||||
|
createdDate,
|
||||||
|
createdTime,
|
||||||
|
...normalizedProps,
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
return new SuccessResponseDto({
|
||||||
|
message: `Users in project with ID ${uuid} retrieved successfully`,
|
||||||
|
data: normalizedUsers,
|
||||||
|
statusCode: HttpStatus.OK,
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
if (error instanceof HttpException) {
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new HttpException(
|
||||||
|
`An error occurred while retrieving users in the project with id ${uuid}`,
|
||||||
|
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
normalizeUserProperties(user: any) {
|
||||||
|
return {
|
||||||
|
status: user.status ?? UserStatusEnum.ACTIVE,
|
||||||
|
invitedBy: user.invitedBy ?? RoleType.SPACE_MEMBER,
|
||||||
|
isEnabled: user.isEnabled ?? true,
|
||||||
|
phoneNumber: user.phoneNumber ?? null,
|
||||||
|
jobTitle: user.jobTitle ?? null,
|
||||||
|
roleType: user.roleType?.type ?? null,
|
||||||
|
};
|
||||||
|
}
|
||||||
async findOne(uuid: string): Promise<ProjectEntity> {
|
async findOne(uuid: string): Promise<ProjectEntity> {
|
||||||
const project = await this.projectRepository.findOne({ where: { uuid } });
|
const project = await this.projectRepository.findOne({ where: { uuid } });
|
||||||
return project;
|
return project;
|
||||||
|
Reference in New Issue
Block a user