import { HttpException, HttpStatus, Injectable } from '@nestjs/common'; import { BaseResponseDto } from '@app/common/dto/base.response.dto'; import { SuccessResponseDto } from '@app/common/dto/success.response.dto'; import { InviteUserRepository } from '@app/common/modules/Invite-user/repositiories'; import { ProjectService } from './project.service'; import { UserSpaceRepository } from '@app/common/modules/user/repositories'; @Injectable() export class ProjectUserService { constructor( private readonly inviteUserRepository: InviteUserRepository, private readonly projectService: ProjectService, private readonly userSpaceRepository: UserSpaceRepository, ) {} async getUsersByProject(uuid: string): Promise { try { const project = await this.projectService.getProject(uuid); if (!project) { throw new HttpException( `Project with ID ${uuid} not found`, HttpStatus.NOT_FOUND, ); } const allUsers = await this.inviteUserRepository.find({ where: { project: { uuid }, isActive: true }, select: [ 'uuid', 'firstName', 'lastName', 'email', 'createdAt', 'status', 'phoneNumber', 'jobTitle', 'invitedBy', 'isEnabled', ], relations: ['roleType'], order: { createdAt: 'ASC', }, }); const normalizedUsers = allUsers.map((user) => { const createdAt = new Date(user.createdAt); const createdDate = createdAt.toLocaleDateString(); const createdTime = createdAt.toLocaleTimeString(); return { ...user, roleType: user.roleType.type, createdDate, createdTime, }; }); 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, ); } } async getUserByUuidInProject( projectUuid: string, invitedUserUuid: string, ): Promise { try { const user = await this.inviteUserRepository.findOne({ where: { project: { uuid: projectUuid }, uuid: invitedUserUuid, isActive: true, }, select: [ 'uuid', 'firstName', 'lastName', 'email', 'createdAt', 'status', 'phoneNumber', 'jobTitle', 'invitedBy', 'isEnabled', ], relations: ['roleType', 'spaces.space', 'spaces.space.community'], }); if (!user) { throw new HttpException( `User with ID ${invitedUserUuid} not found in project ${projectUuid}`, HttpStatus.NOT_FOUND, ); } const createdAt = new Date(user.createdAt); const createdDate = createdAt.toLocaleDateString(); const createdTime = createdAt.toLocaleTimeString(); return new SuccessResponseDto({ message: `User in project with ID ${projectUuid} retrieved successfully`, data: { ...user, roleType: user.roleType.type, createdDate, createdTime, spaces: user.spaces.map(({ space }) => { const { community, ...spaceWithoutCommunity } = space; return { ...spaceWithoutCommunity, communityUuid: community.uuid, communityName: community.name, }; }), }, statusCode: HttpStatus.OK, }); } catch (error) { if (error instanceof HttpException) { throw error; } throw new HttpException( `An error occurred while retrieving user in the project with id ${projectUuid}`, HttpStatus.INTERNAL_SERVER_ERROR, ); } } }