Refactor and add new user-related endpoints for projects

This commit is contained in:
faris Aljohari
2024-12-30 18:59:42 -06:00
parent 5220722579
commit 7e6e34a3de
6 changed files with 224 additions and 93 deletions

View File

@ -0,0 +1,159 @@
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 { UserStatusEnum } from '@app/common/constants/user-status.enum';
import { RoleType } from '@app/common/constants/role.type.enum';
import { InviteUserRepository } from '@app/common/modules/Invite-user/repositiories';
import { UserRepository } from '@app/common/modules/user/repositories';
@Injectable()
export class ProjectUserService {
constructor(
private readonly inviteUserRepository: InviteUserRepository,
private readonly userRepository: UserRepository,
) {}
async getUsersByProject(uuid: string): Promise<BaseResponseDto> {
try {
// Fetch invited users
const invitedUsers = await this.inviteUserRepository.find({
where: { project: { uuid }, isActive: true },
select: [
'uuid',
'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: ['uuid', '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,
);
}
}
async getUserByUuidInProject(
projectUuid: string,
userUuid: string,
): Promise<BaseResponseDto> {
try {
let user;
user = await this.inviteUserRepository.findOne({
where: {
project: { uuid: projectUuid },
uuid: userUuid,
isActive: true,
},
select: [
'uuid',
'firstName',
'lastName',
'email',
'createdAt',
'status',
'phoneNumber',
'jobTitle',
'invitedBy',
'isEnabled',
],
relations: ['roleType'],
});
if (!user) {
user = await this.userRepository.findOne({
where: {
project: { uuid: projectUuid },
uuid: userUuid,
isActive: true,
},
select: ['uuid', 'firstName', 'lastName', 'email', 'createdAt'],
relations: ['roleType'],
});
}
if (!user) {
throw new HttpException(
`User with ID ${userUuid} not found in project ${projectUuid}`,
HttpStatus.NOT_FOUND,
);
}
const responseData = this.formatUserResponse(user);
return new SuccessResponseDto({
message: `User in project with ID ${projectUuid} retrieved successfully`,
data: responseData,
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,
);
}
}
private formatUserResponse(user: any) {
const createdAt = new Date(user.createdAt);
return {
...user,
createdDate: createdAt.toLocaleDateString(),
createdTime: createdAt.toLocaleTimeString(),
...this.normalizeUserProperties(user),
};
}
private 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,
};
}
}