mirror of
https://github.com/SyncrowIOT/backend.git
synced 2026-10-23 09:27:55 +00:00
Merge pull request #196 from SyncrowIOT/SP-905-activate-invitation-code
Sp 905 activate invitation code
This commit is contained in:
@@ -760,6 +760,18 @@ export class ControllerRoute {
|
||||
public static readonly CREATE_USER_INVITATION_DESCRIPTION =
|
||||
'This endpoint creates an invitation for a user to assign to role and spaces.';
|
||||
|
||||
public static readonly UPDATE_USER_INVITATION_SUMMARY =
|
||||
'Update user invitation';
|
||||
|
||||
public static readonly UPDATE_USER_INVITATION_DESCRIPTION =
|
||||
'This endpoint updates an invitation for a user to assign to role and spaces.';
|
||||
|
||||
public static readonly ACTIVATION_CODE_SUMMARY =
|
||||
'Activate Invitation Code';
|
||||
|
||||
public static readonly ACTIVATION_CODE_DESCRIPTION =
|
||||
'This endpoint activate invitation code';
|
||||
|
||||
public static readonly CHECK_EMAIL_SUMMARY = 'Check email';
|
||||
|
||||
public static readonly CHECK_EMAIL_DESCRIPTION =
|
||||
|
||||
@@ -56,12 +56,6 @@ export class InviteUserEntity extends AbstractEntity<InviteUserDto> {
|
||||
})
|
||||
public phoneNumber: string;
|
||||
|
||||
@Column({
|
||||
nullable: false,
|
||||
default: true,
|
||||
})
|
||||
public isEnabled: boolean;
|
||||
|
||||
@Column({
|
||||
nullable: false,
|
||||
default: true,
|
||||
|
||||
@@ -1,5 +1,13 @@
|
||||
import { InviteUserService } from '../services/invite-user.service';
|
||||
import { Body, Controller, Post, Req, UseGuards } from '@nestjs/common';
|
||||
import {
|
||||
Body,
|
||||
Controller,
|
||||
Param,
|
||||
Post,
|
||||
Put,
|
||||
Req,
|
||||
UseGuards,
|
||||
} from '@nestjs/common';
|
||||
import { ApiTags, ApiBearerAuth, ApiOperation } from '@nestjs/swagger';
|
||||
import { AddUserInvitationDto } from '../dtos/add.invite-user.dto';
|
||||
import { ControllerRoute } from '@app/common/constants/controller-route';
|
||||
@@ -8,6 +16,8 @@ import { PermissionsGuard } from 'src/guards/permissions.guard';
|
||||
import { Permissions } from 'src/decorators/permissions.decorator';
|
||||
import { JwtAuthGuard } from '@app/common/guards/jwt.auth.guard';
|
||||
import { CheckEmailDto } from '../dtos/check-email.dto';
|
||||
import { ActivateCodeDto } from '../dtos/active-code.dto';
|
||||
import { UpdateUserInvitationDto } from '../dtos/update.invite-user.dto';
|
||||
|
||||
@ApiTags('Invite User Module')
|
||||
@Controller({
|
||||
@@ -51,4 +61,36 @@ export class InviteUserController {
|
||||
addUserInvitationDto,
|
||||
);
|
||||
}
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Post('activation')
|
||||
@ApiOperation({
|
||||
summary: ControllerRoute.INVITE_USER.ACTIONS.ACTIVATION_CODE_SUMMARY,
|
||||
description:
|
||||
ControllerRoute.INVITE_USER.ACTIONS.ACTIVATION_CODE_DESCRIPTION,
|
||||
})
|
||||
async activationCodeController(
|
||||
@Body() activateCodeDto: ActivateCodeDto,
|
||||
): Promise<BaseResponseDto> {
|
||||
return await this.inviteUserService.activationCodeController(
|
||||
activateCodeDto,
|
||||
);
|
||||
}
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Put(':invitedUserUuid')
|
||||
@ApiOperation({
|
||||
summary: ControllerRoute.INVITE_USER.ACTIONS.UPDATE_USER_INVITATION_SUMMARY,
|
||||
description:
|
||||
ControllerRoute.INVITE_USER.ACTIONS.UPDATE_USER_INVITATION_DESCRIPTION,
|
||||
})
|
||||
async updateUserInvitation(
|
||||
@Param('invitedUserUuid') invitedUserUuid: string,
|
||||
@Body() updateUserInvitationDto: UpdateUserInvitationDto,
|
||||
): Promise<BaseResponseDto> {
|
||||
return await this.inviteUserService.updateUserInvitation(
|
||||
updateUserInvitationDto,
|
||||
invitedUserUuid,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
import { ApiProperty } from '@nestjs/swagger';
|
||||
import { IsString, IsNotEmpty } from 'class-validator';
|
||||
|
||||
export class ActivateCodeDto {
|
||||
@ApiProperty({
|
||||
description: 'The activation code of the user',
|
||||
example: '7CvRcA',
|
||||
required: true,
|
||||
})
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
activationCode: string;
|
||||
|
||||
@ApiProperty({
|
||||
description: 'The UUID of the user',
|
||||
example: 'd7a44e8a-32d5-4f39-ae2e-013f1245aead',
|
||||
required: true,
|
||||
})
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
userUuid: string;
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
import { ApiProperty } from '@nestjs/swagger';
|
||||
import {
|
||||
ArrayMinSize,
|
||||
IsArray,
|
||||
IsNotEmpty,
|
||||
IsOptional,
|
||||
IsString,
|
||||
} from 'class-validator';
|
||||
|
||||
export class UpdateUserInvitationDto {
|
||||
@ApiProperty({
|
||||
description: 'The first name of the user',
|
||||
example: 'John',
|
||||
required: true,
|
||||
})
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
public firstName: string;
|
||||
|
||||
@ApiProperty({
|
||||
description: 'The last name of the user',
|
||||
example: 'Doe',
|
||||
required: true,
|
||||
})
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
public lastName: string;
|
||||
|
||||
@ApiProperty({
|
||||
description: 'The job title of the user',
|
||||
example: 'Software Engineer',
|
||||
required: false,
|
||||
})
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
public jobTitle?: string;
|
||||
|
||||
@ApiProperty({
|
||||
description: 'The phone number of the user',
|
||||
example: '+1234567890',
|
||||
required: false,
|
||||
})
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
public phoneNumber?: string;
|
||||
|
||||
@ApiProperty({
|
||||
description: 'The role uuid of the user',
|
||||
example: 'd290f1ee-6c54-4b01-90e6-d701748f0851',
|
||||
required: true,
|
||||
})
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
public roleUuid: string;
|
||||
@ApiProperty({
|
||||
description: 'The project uuid of the user',
|
||||
example: 'd290f1ee-6c54-4b01-90e6-d701748f0851',
|
||||
required: true,
|
||||
})
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
public projectUuid: string;
|
||||
@ApiProperty({
|
||||
description: 'The array of space UUIDs (at least one required)',
|
||||
example: ['b5f3c9d2-58b7-4377-b3f7-60acb711d5d9'],
|
||||
required: true,
|
||||
})
|
||||
@IsArray()
|
||||
@ArrayMinSize(1)
|
||||
public spaceUuids: string[];
|
||||
constructor(dto: Partial<UpdateUserInvitationDto>) {
|
||||
Object.assign(this, dto);
|
||||
}
|
||||
}
|
||||
@@ -3,13 +3,28 @@ import { InviteUserService } from './services/invite-user.service';
|
||||
import { InviteUserController } from './controllers/invite-user.controller';
|
||||
import { ConfigModule } from '@nestjs/config';
|
||||
|
||||
import { UserRepository } from '@app/common/modules/user/repositories';
|
||||
import {
|
||||
UserRepository,
|
||||
UserSpaceRepository,
|
||||
} from '@app/common/modules/user/repositories';
|
||||
import { InviteUserRepositoryModule } from '@app/common/modules/Invite-user/Invite-user.repository.module';
|
||||
import {
|
||||
InviteUserRepository,
|
||||
InviteUserSpaceRepository,
|
||||
} from '@app/common/modules/Invite-user/repositiories';
|
||||
import { EmailService } from '@app/common/util/email.service';
|
||||
import { SpaceUserService, ValidationService } from 'src/space/services';
|
||||
import { CommunityService } from 'src/community/services';
|
||||
import { SpaceRepository } from '@app/common/modules/space';
|
||||
import { SpaceModelRepository } from '@app/common/modules/space-model';
|
||||
import { CommunityRepository } from '@app/common/modules/community/repositories';
|
||||
import { ProjectRepository } from '@app/common/modules/project/repositiories';
|
||||
import { TuyaService } from '@app/common/integrations/tuya/services/tuya.service';
|
||||
import { UserSpaceService } from 'src/users/services';
|
||||
import { UserDevicePermissionService } from 'src/user-device-permission/services';
|
||||
import { DeviceUserPermissionRepository } from '@app/common/modules/device/repositories';
|
||||
import { PermissionTypeRepository } from '@app/common/modules/permission/repositories';
|
||||
import { ProjectUserService } from 'src/project/services/project-user.service';
|
||||
|
||||
@Module({
|
||||
imports: [ConfigModule, InviteUserRepositoryModule],
|
||||
@@ -20,6 +35,20 @@ import { EmailService } from '@app/common/util/email.service';
|
||||
UserRepository,
|
||||
EmailService,
|
||||
InviteUserSpaceRepository,
|
||||
SpaceUserService,
|
||||
ValidationService,
|
||||
UserSpaceRepository,
|
||||
CommunityService,
|
||||
SpaceRepository,
|
||||
SpaceModelRepository,
|
||||
CommunityRepository,
|
||||
ProjectRepository,
|
||||
TuyaService,
|
||||
UserSpaceService,
|
||||
UserDevicePermissionService,
|
||||
DeviceUserPermissionRepository,
|
||||
PermissionTypeRepository,
|
||||
ProjectUserService,
|
||||
],
|
||||
exports: [InviteUserService],
|
||||
})
|
||||
|
||||
@@ -1,10 +1,15 @@
|
||||
import { Injectable, HttpException, HttpStatus } from '@nestjs/common';
|
||||
import {
|
||||
Injectable,
|
||||
HttpException,
|
||||
HttpStatus,
|
||||
BadRequestException,
|
||||
} from '@nestjs/common';
|
||||
import { AddUserInvitationDto } from '../dtos';
|
||||
import { BaseResponseDto } from '@app/common/dto/base.response.dto';
|
||||
import { UserStatusEnum } from '@app/common/constants/user-status.enum';
|
||||
import { SuccessResponseDto } from '@app/common/dto/success.response.dto';
|
||||
import { generateRandomString } from '@app/common/helper/randomString';
|
||||
import { In, IsNull, Not } from 'typeorm';
|
||||
import { In, IsNull, Not, QueryRunner } from 'typeorm';
|
||||
import { DataSource } from 'typeorm';
|
||||
import { UserEntity } from '@app/common/modules/user/entities';
|
||||
import { RoleType } from '@app/common/constants/role.type.enum';
|
||||
@@ -15,7 +20,11 @@ import {
|
||||
import { CheckEmailDto } from '../dtos/check-email.dto';
|
||||
import { UserRepository } from '@app/common/modules/user/repositories';
|
||||
import { EmailService } from '@app/common/util/email.service';
|
||||
import { SpaceEntity } from '@app/common/modules/space';
|
||||
import { SpaceEntity, SpaceRepository } from '@app/common/modules/space';
|
||||
import { ActivateCodeDto } from '../dtos/active-code.dto';
|
||||
import { UserSpaceService } from 'src/users/services';
|
||||
import { SpaceUserService } from 'src/space/services';
|
||||
import { UpdateUserInvitationDto } from '../dtos/update.invite-user.dto';
|
||||
|
||||
@Injectable()
|
||||
export class InviteUserService {
|
||||
@@ -24,6 +33,9 @@ export class InviteUserService {
|
||||
private readonly userRepository: UserRepository,
|
||||
private readonly emailService: EmailService,
|
||||
private readonly inviteUserSpaceRepository: InviteUserSpaceRepository,
|
||||
private readonly userSpaceService: UserSpaceService,
|
||||
private readonly spaceUserService: SpaceUserService,
|
||||
private readonly spaceRepository: SpaceRepository,
|
||||
private readonly dataSource: DataSource,
|
||||
) {}
|
||||
|
||||
@@ -171,4 +183,296 @@ export class InviteUserService {
|
||||
);
|
||||
}
|
||||
}
|
||||
async activationCodeController(
|
||||
dto: ActivateCodeDto,
|
||||
): Promise<BaseResponseDto> {
|
||||
try {
|
||||
const { activationCode, userUuid } = dto;
|
||||
const user = await this.userRepository.findOne({
|
||||
where: { uuid: userUuid, isActive: true, isUserVerified: true },
|
||||
});
|
||||
|
||||
if (!user) {
|
||||
throw new HttpException('User not found', HttpStatus.NOT_FOUND);
|
||||
}
|
||||
const { email } = user;
|
||||
const invitedUser = await this.inviteUserRepository.findOne({
|
||||
where: {
|
||||
email,
|
||||
invitationCode: activationCode,
|
||||
status: UserStatusEnum.INVITED,
|
||||
isActive: true,
|
||||
},
|
||||
relations: ['project', 'spaces.space.community'],
|
||||
});
|
||||
|
||||
if (!invitedUser) {
|
||||
throw new HttpException(
|
||||
'Invalid activation code',
|
||||
HttpStatus.BAD_REQUEST,
|
||||
);
|
||||
}
|
||||
|
||||
for (const invitedSpace of invitedUser.spaces) {
|
||||
try {
|
||||
const deviceUUIDs =
|
||||
await this.userSpaceService.getDeviceUUIDsForSpace(
|
||||
invitedSpace.space.uuid,
|
||||
);
|
||||
|
||||
await this.userSpaceService.addUserPermissionsToDevices(
|
||||
userUuid,
|
||||
deviceUUIDs,
|
||||
);
|
||||
|
||||
await this.spaceUserService.associateUserToSpace({
|
||||
communityUuid: invitedSpace.space.community.uuid,
|
||||
spaceUuid: invitedSpace.space.uuid,
|
||||
userUuid: user.uuid,
|
||||
projectUuid: invitedUser.project.uuid,
|
||||
});
|
||||
} catch (spaceError) {
|
||||
console.error(
|
||||
`Error processing space ${invitedSpace.space.uuid}:`,
|
||||
spaceError,
|
||||
);
|
||||
// Skip to the next space
|
||||
continue;
|
||||
}
|
||||
}
|
||||
await this.inviteUserRepository.update(
|
||||
{ uuid: invitedUser.uuid },
|
||||
{ status: UserStatusEnum.ACTIVE },
|
||||
);
|
||||
await this.userRepository.update(
|
||||
{ uuid: userUuid },
|
||||
{
|
||||
project: { uuid: invitedUser.project.uuid },
|
||||
inviteUser: { uuid: invitedUser.uuid },
|
||||
},
|
||||
);
|
||||
return new SuccessResponseDto({
|
||||
statusCode: HttpStatus.OK,
|
||||
success: true,
|
||||
message: 'The code has been successfully activated',
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Error activating the code:', error);
|
||||
throw new HttpException(
|
||||
error.message ||
|
||||
'An unexpected error occurred while activating the code',
|
||||
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
}
|
||||
}
|
||||
async updateUserInvitation(
|
||||
dto: UpdateUserInvitationDto,
|
||||
invitedUserUuid: string,
|
||||
): Promise<BaseResponseDto> {
|
||||
const { projectUuid } = dto;
|
||||
|
||||
const queryRunner = this.dataSource.createQueryRunner();
|
||||
await queryRunner.startTransaction();
|
||||
|
||||
try {
|
||||
// Fetch the user's existing data in the project
|
||||
const userOldData = await this.inviteUserRepository.findOne({
|
||||
where: { uuid: invitedUserUuid, project: { uuid: projectUuid } },
|
||||
});
|
||||
|
||||
if (!userOldData) {
|
||||
throw new HttpException('User not found', HttpStatus.NOT_FOUND);
|
||||
}
|
||||
|
||||
// Perform update actions if status is 'INVITED'
|
||||
if (
|
||||
userOldData.status === UserStatusEnum.INVITED ||
|
||||
userOldData.status === UserStatusEnum.DISABLED
|
||||
) {
|
||||
await this.updateWhenUserIsInviteOrDisable(
|
||||
queryRunner,
|
||||
dto,
|
||||
invitedUserUuid,
|
||||
);
|
||||
} else if (userOldData.status === UserStatusEnum.ACTIVE) {
|
||||
await this.updateWhenUserIsActive(queryRunner, dto, invitedUserUuid);
|
||||
}
|
||||
|
||||
await queryRunner.commitTransaction();
|
||||
|
||||
return new SuccessResponseDto({
|
||||
statusCode: HttpStatus.OK,
|
||||
success: true,
|
||||
message: 'User invitation updated successfully',
|
||||
});
|
||||
} catch (error) {
|
||||
await queryRunner.rollbackTransaction();
|
||||
|
||||
// Throw an appropriate HTTP exception
|
||||
throw error instanceof HttpException
|
||||
? error
|
||||
: new HttpException(
|
||||
'An unexpected error occurred while updating the user',
|
||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
} finally {
|
||||
await queryRunner.release();
|
||||
}
|
||||
}
|
||||
|
||||
private async updateWhenUserIsInviteOrDisable(
|
||||
queryRunner: QueryRunner,
|
||||
dto: UpdateUserInvitationDto,
|
||||
invitedUserUuid: string,
|
||||
): Promise<void> {
|
||||
const { firstName, lastName, jobTitle, phoneNumber, roleUuid, spaceUuids } =
|
||||
dto;
|
||||
|
||||
// Update user invitation details
|
||||
await queryRunner.manager.update(
|
||||
this.inviteUserRepository.target,
|
||||
{ uuid: invitedUserUuid },
|
||||
{
|
||||
firstName,
|
||||
lastName,
|
||||
jobTitle,
|
||||
phoneNumber,
|
||||
roleType: { uuid: roleUuid },
|
||||
},
|
||||
);
|
||||
|
||||
// Remove old space associations
|
||||
await queryRunner.manager.delete(this.inviteUserSpaceRepository.target, {
|
||||
inviteUser: { uuid: invitedUserUuid },
|
||||
});
|
||||
|
||||
// Save new space associations
|
||||
const spaceData = spaceUuids.map((spaceUuid) => ({
|
||||
inviteUser: { uuid: invitedUserUuid },
|
||||
space: { uuid: spaceUuid },
|
||||
}));
|
||||
await queryRunner.manager.save(
|
||||
this.inviteUserSpaceRepository.target,
|
||||
spaceData,
|
||||
);
|
||||
}
|
||||
private async updateWhenUserIsActive(
|
||||
queryRunner: QueryRunner,
|
||||
dto: UpdateUserInvitationDto,
|
||||
invitedUserUuid: string,
|
||||
): Promise<void> {
|
||||
const {
|
||||
firstName,
|
||||
lastName,
|
||||
jobTitle,
|
||||
phoneNumber,
|
||||
roleUuid,
|
||||
spaceUuids,
|
||||
projectUuid,
|
||||
} = dto;
|
||||
|
||||
const user = await this.userRepository.findOne({
|
||||
where: { inviteUser: { uuid: invitedUserUuid } },
|
||||
relations: ['userSpaces.space', 'userSpaces.space.community'],
|
||||
});
|
||||
|
||||
if (!user) {
|
||||
throw new HttpException(
|
||||
`User with invitedUserUuid ${invitedUserUuid} not found`,
|
||||
HttpStatus.NOT_FOUND,
|
||||
);
|
||||
}
|
||||
|
||||
// Update user details
|
||||
await queryRunner.manager.update(
|
||||
this.inviteUserRepository.target,
|
||||
{ uuid: invitedUserUuid },
|
||||
{
|
||||
firstName,
|
||||
lastName,
|
||||
jobTitle,
|
||||
phoneNumber,
|
||||
roleType: { uuid: roleUuid },
|
||||
},
|
||||
);
|
||||
|
||||
// Disassociate the user from all current spaces
|
||||
const disassociatePromises = user.userSpaces.map((userSpace) =>
|
||||
this.spaceUserService
|
||||
.disassociateUserFromSpace({
|
||||
communityUuid: userSpace.space.community.uuid,
|
||||
spaceUuid: userSpace.space.uuid,
|
||||
userUuid: user.uuid,
|
||||
projectUuid,
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error(
|
||||
`Failed to disassociate user from space ${userSpace.space.uuid}:`,
|
||||
error,
|
||||
);
|
||||
throw error;
|
||||
}),
|
||||
);
|
||||
|
||||
await Promise.allSettled(disassociatePromises);
|
||||
|
||||
// Process new spaces
|
||||
const associatePromises = spaceUuids.map(async (spaceUuid) => {
|
||||
try {
|
||||
// Fetch space details
|
||||
const spaceDetails = await this.getSpaceByUuid(spaceUuid);
|
||||
|
||||
// Fetch device UUIDs for the space
|
||||
const deviceUUIDs =
|
||||
await this.userSpaceService.getDeviceUUIDsForSpace(spaceUuid);
|
||||
|
||||
// Grant permissions to the user for all devices in the space
|
||||
await this.userSpaceService.addUserPermissionsToDevices(
|
||||
user.uuid,
|
||||
deviceUUIDs,
|
||||
);
|
||||
|
||||
// Associate the user with the new space
|
||||
await this.spaceUserService.associateUserToSpace({
|
||||
communityUuid: spaceDetails.communityUuid,
|
||||
spaceUuid: spaceUuid,
|
||||
userUuid: user.uuid,
|
||||
projectUuid,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error(`Failed to process space ${spaceUuid}:`, error);
|
||||
throw error;
|
||||
}
|
||||
});
|
||||
|
||||
await Promise.all(associatePromises);
|
||||
}
|
||||
|
||||
async getSpaceByUuid(spaceUuid: string) {
|
||||
try {
|
||||
const space = await this.spaceRepository.findOne({
|
||||
where: {
|
||||
uuid: spaceUuid,
|
||||
},
|
||||
relations: ['community'],
|
||||
});
|
||||
if (!space) {
|
||||
throw new BadRequestException(`Invalid space UUID ${spaceUuid}`);
|
||||
}
|
||||
return {
|
||||
uuid: space.uuid,
|
||||
createdAt: space.createdAt,
|
||||
updatedAt: space.updatedAt,
|
||||
name: space.spaceName,
|
||||
spaceTuyaUuid: space.community.externalId,
|
||||
communityUuid: space.community.uuid,
|
||||
};
|
||||
} catch (err) {
|
||||
if (err instanceof BadRequestException) {
|
||||
throw err; // Re-throw BadRequestException
|
||||
} else {
|
||||
throw new HttpException('Space not found', HttpStatus.NOT_FOUND);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -35,14 +35,14 @@ export class ProjectUserController {
|
||||
description:
|
||||
ControllerRoute.PROJECT.ACTIONS.GET_USERS_BY_PROJECT_DESCRIPTION,
|
||||
})
|
||||
@Get(':userUuid')
|
||||
@Get(':invitedUserUuid')
|
||||
async findUserByUuidInProject(
|
||||
@Param() params: GetProjectParam,
|
||||
@Param('userUuid') userUuid: string,
|
||||
@Param('invitedUserUuid') invitedUserUuid: string,
|
||||
): Promise<BaseResponseDto> {
|
||||
return this.projectUserService.getUserByUuidInProject(
|
||||
params.projectUuid,
|
||||
userUuid,
|
||||
invitedUserUuid,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,9 +7,9 @@ import { CreateOrphanSpaceHandler } from './handler';
|
||||
import { SpaceRepository } from '@app/common/modules/space';
|
||||
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';
|
||||
import { ProjectUserController } from './controllers/project-user.controller';
|
||||
import { ProjectUserService } from './services/project-user.service';
|
||||
import { UserSpaceRepository } from '@app/common/modules/user/repositories';
|
||||
|
||||
const CommandHandlers = [CreateOrphanSpaceHandler];
|
||||
|
||||
@@ -25,7 +25,7 @@ const CommandHandlers = [CreateOrphanSpaceHandler];
|
||||
ProjectUserService,
|
||||
ProjectRepository,
|
||||
InviteUserRepository,
|
||||
UserRepository,
|
||||
UserSpaceRepository,
|
||||
],
|
||||
exports: [ProjectService, CqrsModule],
|
||||
})
|
||||
|
||||
@@ -1,19 +1,17 @@
|
||||
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';
|
||||
import { ProjectService } from './project.service';
|
||||
import { UserStatusEnum } from '@app/common/constants/user-status.enum';
|
||||
import { UserSpaceRepository } from '@app/common/modules/user/repositories';
|
||||
|
||||
@Injectable()
|
||||
export class ProjectUserService {
|
||||
constructor(
|
||||
private readonly inviteUserRepository: InviteUserRepository,
|
||||
private readonly userRepository: UserRepository,
|
||||
private readonly projectService: ProjectService,
|
||||
private readonly userSpaceRepository: UserSpaceRepository,
|
||||
) {}
|
||||
|
||||
async getUsersByProject(uuid: string): Promise<BaseResponseDto> {
|
||||
@@ -25,8 +23,7 @@ export class ProjectUserService {
|
||||
HttpStatus.NOT_FOUND,
|
||||
);
|
||||
}
|
||||
// Fetch invited users
|
||||
const invitedUsers = await this.inviteUserRepository.find({
|
||||
const allUsers = await this.inviteUserRepository.find({
|
||||
where: { project: { uuid }, isActive: true },
|
||||
select: [
|
||||
'uuid',
|
||||
@@ -38,35 +35,20 @@ export class ProjectUserService {
|
||||
'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,
|
||||
roleType: user.roleType.type,
|
||||
createdDate,
|
||||
createdTime,
|
||||
...normalizedProps,
|
||||
};
|
||||
});
|
||||
|
||||
@@ -88,14 +70,14 @@ export class ProjectUserService {
|
||||
}
|
||||
async getUserByUuidInProject(
|
||||
projectUuid: string,
|
||||
userUuid: string,
|
||||
invitedUserUuid: string,
|
||||
): Promise<BaseResponseDto> {
|
||||
try {
|
||||
let user;
|
||||
user = await this.inviteUserRepository.findOne({
|
||||
let userSpaces;
|
||||
const user = await this.inviteUserRepository.findOne({
|
||||
where: {
|
||||
project: { uuid: projectUuid },
|
||||
uuid: userUuid,
|
||||
uuid: invitedUserUuid,
|
||||
isActive: true,
|
||||
},
|
||||
select: [
|
||||
@@ -108,31 +90,37 @@ export class ProjectUserService {
|
||||
'phoneNumber',
|
||||
'jobTitle',
|
||||
'invitedBy',
|
||||
'isEnabled',
|
||||
],
|
||||
relations: ['roleType', 'spaces.space'],
|
||||
});
|
||||
if (!user) {
|
||||
user = await this.userRepository.findOne({
|
||||
where: {
|
||||
project: { uuid: projectUuid },
|
||||
uuid: userUuid,
|
||||
isActive: true,
|
||||
},
|
||||
select: ['uuid', 'firstName', 'lastName', 'email', 'createdAt'],
|
||||
relations: ['roleType', 'userSpaces.space'],
|
||||
});
|
||||
}
|
||||
|
||||
if (!user) {
|
||||
throw new HttpException(
|
||||
`User with ID ${userUuid} not found in project ${projectUuid}`,
|
||||
`User with ID ${invitedUserUuid} not found in project ${projectUuid}`,
|
||||
HttpStatus.NOT_FOUND,
|
||||
);
|
||||
}
|
||||
const responseData = this.formatUserResponse(user);
|
||||
if (user.status === UserStatusEnum.ACTIVE) {
|
||||
const spaces = await this.userSpaceRepository.find({
|
||||
where: { user: { inviteUser: { uuid: invitedUserUuid } } },
|
||||
relations: ['space'],
|
||||
});
|
||||
userSpaces = spaces.map((space) => space.space);
|
||||
} else {
|
||||
userSpaces = user.spaces.map((space) => space.space);
|
||||
}
|
||||
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: responseData,
|
||||
data: {
|
||||
...user,
|
||||
roleType: user.roleType.type,
|
||||
createdDate,
|
||||
createdTime,
|
||||
spaces: userSpaces,
|
||||
},
|
||||
statusCode: HttpStatus.OK,
|
||||
});
|
||||
} catch (error) {
|
||||
@@ -146,29 +134,4 @@ export class ProjectUserService {
|
||||
);
|
||||
}
|
||||
}
|
||||
private formatUserResponse(user: any) {
|
||||
const createdAt = new Date(user.createdAt);
|
||||
return {
|
||||
uuid: user.uuid,
|
||||
firstName: user.firstName,
|
||||
lastName: user.lastName,
|
||||
email: user.email,
|
||||
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,
|
||||
spaces:
|
||||
user?.spaces?.map((space: any) => space?.space) ??
|
||||
user?.userSpaces?.map((space: any) => space?.space),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -51,6 +51,7 @@ export class ValidationService {
|
||||
'subspaces.tags',
|
||||
'subspaces.devices',
|
||||
'devices',
|
||||
'devices.productDevice',
|
||||
],
|
||||
});
|
||||
|
||||
|
||||
@@ -117,9 +117,7 @@ export class UserSpaceService {
|
||||
);
|
||||
}
|
||||
|
||||
private async getDeviceUUIDsForSpace(
|
||||
unitUuid: string,
|
||||
): Promise<{ uuid: string }[]> {
|
||||
async getDeviceUUIDsForSpace(unitUuid: string): Promise<{ uuid: string }[]> {
|
||||
const devices = await this.spaceRepository.find({
|
||||
where: { uuid: unitUuid },
|
||||
relations: ['devices', 'devices.productDevice'],
|
||||
@@ -130,7 +128,7 @@ export class UserSpaceService {
|
||||
return allDevices.map((device) => ({ uuid: device.uuid }));
|
||||
}
|
||||
|
||||
private async addUserPermissionsToDevices(
|
||||
async addUserPermissionsToDevices(
|
||||
userUuid: string,
|
||||
deviceUUIDs: { uuid: string }[],
|
||||
): Promise<void> {
|
||||
|
||||
Reference in New Issue
Block a user