mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-11-26 21:14:54 +00:00
102 lines
3.1 KiB
TypeScript
102 lines
3.1 KiB
TypeScript
import { BaseResponseDto } from '@app/common/dto/base.response.dto';
|
|
import { SuccessResponseDto } from '@app/common/dto/success.response.dto';
|
|
import { SpaceRepository } from '@app/common/modules/space/repositories';
|
|
import {
|
|
UserRepository,
|
|
UserSpaceRepository,
|
|
} from '@app/common/modules/user/repositories';
|
|
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
|
|
import { SpaceService } from './space.service';
|
|
import { UserSpaceParam } from '../dtos';
|
|
|
|
@Injectable()
|
|
export class SpaceUserService {
|
|
constructor(
|
|
private readonly spaceRepository: SpaceRepository,
|
|
private readonly userRepository: UserRepository,
|
|
private readonly userSpaceRepository: UserSpaceRepository,
|
|
private readonly spaceService: SpaceService,
|
|
) {}
|
|
async associateUserToSpace(params: UserSpaceParam): Promise<BaseResponseDto> {
|
|
const { communityUuid, spaceUuid, userUuid, projectUuid } = params;
|
|
// Find the user by ID
|
|
const user = await this.userRepository.findOne({
|
|
where: { uuid: userUuid },
|
|
});
|
|
if (!user) {
|
|
throw new HttpException(
|
|
`User with ID ${userUuid} not found`,
|
|
HttpStatus.NOT_FOUND,
|
|
);
|
|
}
|
|
|
|
// Find the space by ID
|
|
const space = await this.spaceService.validateCommunityAndSpace(
|
|
communityUuid,
|
|
spaceUuid,
|
|
projectUuid,
|
|
);
|
|
|
|
// Check if the association already exists
|
|
const existingAssociation = await this.userSpaceRepository.findOne({
|
|
where: { user: { uuid: userUuid }, space: { uuid: spaceUuid } },
|
|
});
|
|
if (existingAssociation) {
|
|
throw new HttpException(
|
|
`User is already associated with the space`,
|
|
HttpStatus.CONFLICT,
|
|
);
|
|
}
|
|
|
|
const userSpace = this.userSpaceRepository.create({ user, space });
|
|
|
|
await this.userSpaceRepository.save(userSpace);
|
|
return new SuccessResponseDto({
|
|
data: userSpace,
|
|
message: `Space ${spaceUuid} has been successfully associated t user ${userUuid}`,
|
|
});
|
|
}
|
|
|
|
async disassociateUserFromSpace(
|
|
params: UserSpaceParam,
|
|
): Promise<BaseResponseDto> {
|
|
const { userUuid, spaceUuid, communityUuid, projectUuid } = params;
|
|
// Find the user by ID
|
|
const user = await this.userRepository.findOne({
|
|
where: { uuid: userUuid },
|
|
});
|
|
if (!user) {
|
|
throw new HttpException(
|
|
`User with ID ${userUuid} not found`,
|
|
HttpStatus.NOT_FOUND,
|
|
);
|
|
}
|
|
|
|
// Find the space by ID
|
|
await this.spaceService.validateCommunityAndSpace(
|
|
communityUuid,
|
|
spaceUuid,
|
|
projectUuid,
|
|
);
|
|
|
|
// Find the existing association
|
|
const existingAssociation = await this.userSpaceRepository.findOne({
|
|
where: { user: { uuid: userUuid }, space: { uuid: spaceUuid } },
|
|
});
|
|
|
|
if (!existingAssociation) {
|
|
throw new HttpException(
|
|
`No association found between user ${userUuid} and space ${spaceUuid}`,
|
|
HttpStatus.NOT_FOUND,
|
|
);
|
|
}
|
|
|
|
// Remove the association
|
|
await this.userSpaceRepository.remove(existingAssociation);
|
|
|
|
return new SuccessResponseDto({
|
|
message: `User ${userUuid} has been successfully disassociated from space ${spaceUuid}`,
|
|
});
|
|
}
|
|
}
|