mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-10 15:17:41 +00:00
validation checks
This commit is contained in:
@ -5,6 +5,7 @@ import {
|
||||
IsNotEmpty,
|
||||
IsOptional,
|
||||
IsString,
|
||||
IsUUID,
|
||||
} from 'class-validator';
|
||||
|
||||
export class AddUserInvitationDto {
|
||||
@ -67,7 +68,7 @@ export class AddUserInvitationDto {
|
||||
example: 'd290f1ee-6c54-4b01-90e6-d701748f0851',
|
||||
required: true,
|
||||
})
|
||||
@IsString()
|
||||
@IsUUID('4')
|
||||
@IsNotEmpty()
|
||||
public roleUuid: string;
|
||||
@ApiProperty({
|
||||
@ -75,15 +76,17 @@ export class AddUserInvitationDto {
|
||||
example: 'd290f1ee-6c54-4b01-90e6-d701748f0851',
|
||||
required: true,
|
||||
})
|
||||
@IsString()
|
||||
@IsUUID('4')
|
||||
@IsNotEmpty()
|
||||
public projectUuid: string;
|
||||
|
||||
@ApiProperty({
|
||||
description: 'The array of space UUIDs (at least one required)',
|
||||
example: ['b5f3c9d2-58b7-4377-b3f7-60acb711d5d9'],
|
||||
required: true,
|
||||
})
|
||||
@IsArray()
|
||||
@IsUUID('4', { each: true })
|
||||
@ArrayMinSize(1)
|
||||
public spaceUuids: string[];
|
||||
constructor(dto: Partial<AddUserInvitationDto>) {
|
||||
|
@ -8,6 +8,8 @@ import {
|
||||
InviteUserRepository,
|
||||
InviteUserSpaceRepository,
|
||||
} from '@app/common/modules/Invite-user/repositiories';
|
||||
import { ProjectEntity } from '@app/common/modules/project/entities';
|
||||
import { RoleTypeEntity } from '@app/common/modules/role-type/entities';
|
||||
import { RoleTypeRepository } from '@app/common/modules/role-type/repositories';
|
||||
import { SpaceRepository } from '@app/common/modules/space';
|
||||
import { SpaceEntity } from '@app/common/modules/space/entities/space.entity';
|
||||
@ -91,6 +93,8 @@ export class InviteUserService {
|
||||
);
|
||||
}
|
||||
|
||||
await this.checkRole(roleUuid, queryRunner);
|
||||
await this.checkProject(projectUuid, queryRunner);
|
||||
// Validate spaces
|
||||
const validSpaces = await this.validateSpaces(
|
||||
spaceUuids,
|
||||
@ -213,6 +217,61 @@ export class InviteUserService {
|
||||
}
|
||||
}
|
||||
|
||||
private async checkRole(
|
||||
roleUuid: string,
|
||||
queryRunner: QueryRunner,
|
||||
): Promise<BaseResponseDto> {
|
||||
try {
|
||||
const role = await queryRunner.manager.findOne(RoleTypeEntity, {
|
||||
where: { uuid: roleUuid },
|
||||
});
|
||||
|
||||
if (!role) {
|
||||
throw new HttpException('Role not found', HttpStatus.NOT_FOUND);
|
||||
}
|
||||
|
||||
return new SuccessResponseDto({
|
||||
statusCode: HttpStatus.OK,
|
||||
success: true,
|
||||
message: 'Valid role',
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Error checking role:', error);
|
||||
throw new HttpException(
|
||||
error.message || 'An unexpected error occurred while checking the role',
|
||||
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private async checkProject(
|
||||
projectUuid: string,
|
||||
queryRunner: QueryRunner,
|
||||
): Promise<BaseResponseDto> {
|
||||
try {
|
||||
const project = await queryRunner.manager.findOne(ProjectEntity, {
|
||||
where: { uuid: projectUuid },
|
||||
});
|
||||
|
||||
if (!project) {
|
||||
throw new HttpException('Project not found', HttpStatus.NOT_FOUND);
|
||||
}
|
||||
|
||||
return new SuccessResponseDto({
|
||||
statusCode: HttpStatus.OK,
|
||||
success: true,
|
||||
message: 'Valid project',
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Error checking project:', error);
|
||||
throw new HttpException(
|
||||
error.message ||
|
||||
'An unexpected error occurred while checking the project',
|
||||
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private validateUserOrInvite(user: any, userType: string): void {
|
||||
if (user) {
|
||||
if (!user.isActive) {
|
||||
|
Reference in New Issue
Block a user