subspace endpoints

This commit is contained in:
hannathkadher
2024-10-29 13:13:44 +04:00
parent 74428b408e
commit eb77f8ab99
13 changed files with 275 additions and 26 deletions
+1 -1
View File
@@ -1,3 +1,3 @@
export * from './space.service';
export * from './space-user.service';
export * from './subspace.service';
export * from './subspace';
+24 -19
View File
@@ -107,23 +107,23 @@ export class SpaceService {
}
}
async findOne(spaceId: string): Promise<BaseResponseDto> {
async findOne(spaceUuid: string): Promise<BaseResponseDto> {
try {
const space = await this.spaceRepository.findOne({
where: {
uuid: spaceId,
uuid: spaceUuid,
},
});
// If space is not found, throw a NotFoundException
if (!space) {
throw new HttpException(
`Space with UUID ${spaceId} not found`,
`Space with UUID ${spaceUuid} not found`,
HttpStatus.NOT_FOUND,
);
}
return new SuccessResponseDto({
message: `Space with ID ${spaceId} successfully fetched`,
message: `Space with ID ${spaceUuid} successfully fetched`,
data: space,
});
} catch (error) {
@@ -138,28 +138,31 @@ export class SpaceService {
}
}
async delete(spaceId: string, communityId: string): Promise<BaseResponseDto> {
async delete(
spaceUuid: string,
communityUuid: string,
): Promise<BaseResponseDto> {
try {
// First, check if the community exists
const community = await this.communityRepository.findOne({
where: { uuid: communityId },
where: { uuid: communityUuid },
});
if (!community) {
throw new HttpException(
`Community with ID ${communityId} not found`,
`Community with ID ${communityUuid} not found`,
HttpStatus.NOT_FOUND,
);
}
// Check if the space exists
const space = await this.spaceRepository.findOne({
where: { uuid: spaceId, community: { uuid: communityId } },
where: { uuid: spaceUuid, community: { uuid: communityUuid } },
});
if (!space) {
throw new HttpException(
`Space with ID ${spaceId} not found`,
`Space with ID ${spaceUuid} not found`,
HttpStatus.NOT_FOUND,
);
}
@@ -168,7 +171,7 @@ export class SpaceService {
await this.spaceRepository.remove(space);
return new SuccessResponseDto({
message: `Space with ID ${spaceId} successfully deleted`,
message: `Space with ID ${spaceUuid} successfully deleted`,
statusCode: HttpStatus.OK,
});
} catch (error) {
@@ -183,7 +186,7 @@ export class SpaceService {
}
async updateSpace(
spaceId: string,
spaceUuid: string,
communityId: string,
updateSpaceDto: AddSpaceDto,
): Promise<BaseResponseDto> {
@@ -202,12 +205,12 @@ export class SpaceService {
// Check if the space exists
const space = await this.spaceRepository.findOne({
where: { uuid: spaceId, community: { uuid: communityId } },
where: { uuid: spaceUuid, community: { uuid: communityId } },
});
if (!space) {
throw new HttpException(
`Space with ID ${spaceId} not found`,
`Space with ID ${spaceUuid} not found`,
HttpStatus.NOT_FOUND,
);
}
@@ -241,7 +244,7 @@ export class SpaceService {
await this.spaceRepository.save(space);
return new SuccessResponseDto({
message: `Space with ID ${spaceId} successfully updated`,
message: `Space with ID ${spaceUuid} successfully updated`,
data: space,
statusCode: HttpStatus.OK,
});
@@ -256,15 +259,17 @@ export class SpaceService {
}
}
async getSpacesHierarchyForSpace(spaceId: string): Promise<BaseResponseDto> {
async getSpacesHierarchyForSpace(
spaceUuid: string,
): Promise<BaseResponseDto> {
const space = await this.spaceRepository.findOne({
where: { uuid: spaceId },
where: { uuid: spaceUuid },
});
// If the space doesn't exist, throw a 404 error
if (!space) {
throw new HttpException(
`Space with ID ${spaceId} not found`,
`Space with ID ${spaceUuid} not found`,
HttpStatus.NOT_FOUND,
);
}
@@ -272,7 +277,7 @@ export class SpaceService {
try {
// Get all spaces that are children of the provided space, including the parent-child relations
const spaces = await this.spaceRepository.find({
where: { parent: { uuid: spaceId } },
where: { parent: { uuid: spaceUuid } },
relations: ['parent', 'children'], // Include parent and children relations
});
@@ -280,7 +285,7 @@ export class SpaceService {
const spaceHierarchy = this.buildSpaceHierarchy(spaces);
return new SuccessResponseDto({
message: `Spaces under space ${spaceId} successfully fetched in hierarchy`,
message: `Spaces under space ${spaceUuid} successfully fetched in hierarchy`,
data: spaceHierarchy,
statusCode: HttpStatus.OK,
});
+2
View File
@@ -0,0 +1,2 @@
export * from './subspace.service';
export * from './subspace-device.service';
@@ -0,0 +1,128 @@
import { BaseResponseDto } from '@app/common/dto/base.response.dto';
import { SuccessResponseDto } from '@app/common/dto/success.response.dto';
import { DeviceRepository } from '@app/common/modules/device/repositories';
import {
SpaceRepository,
SubspaceRepository,
} from '@app/common/modules/space/repositories';
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
import { DeviceSubSpaceParam, GetSubSpaceParam } from '../../dtos';
import { CommunityRepository } from '@app/common/modules/community/repositories';
@Injectable()
export class SubspaceDeviceService {
constructor(
private readonly spaceRepository: SpaceRepository,
private readonly communityRepository: CommunityRepository,
private readonly subspaceRepository: SubspaceRepository,
private readonly deviceRepository: DeviceRepository,
) {}
async listDevicesInSubspace(
params: GetSubSpaceParam,
): Promise<BaseResponseDto> {
const { subSpaceUuid, spaceUuid, communityUuid } = params;
await this.validateCommunityAndSpace(communityUuid, spaceUuid);
const subspace = await this.findSubspaceWithDevices(subSpaceUuid);
return new SuccessResponseDto({
data: subspace.devices,
message: 'Successfully retrieved list of devices',
});
}
async associateDeviceToSubspace(params: DeviceSubSpaceParam) {
const { subSpaceUuid, deviceUuid, spaceUuid, communityUuid } = params;
await this.validateCommunityAndSpace(communityUuid, spaceUuid);
const subspace = await this.findSubspace(subSpaceUuid);
const device = await this.findDevice(deviceUuid);
device.subspace = subspace;
await this.deviceRepository.save(device);
return new SuccessResponseDto({
data: device,
message: 'Successfully associated device to subspace',
});
}
async disassociateDeviceFromSubspace(params: DeviceSubSpaceParam) {
const { subSpaceUuid, deviceUuid, spaceUuid, communityUuid } = params;
await this.validateCommunityAndSpace(communityUuid, spaceUuid);
await this.findSubspace(subSpaceUuid);
const device = await this.findDevice(deviceUuid);
device.subspace = null;
await this.deviceRepository.save(device);
return new SuccessResponseDto({
data: device,
message: 'Successfully dissociated device from subspace',
});
}
// Helper method to validate community and space
private async validateCommunityAndSpace(
communityUuid: string,
spaceUuid: string,
) {
const community = await this.communityRepository.findOne({
where: { uuid: communityUuid },
});
if (!community) {
this.throwNotFound('Community', communityUuid);
}
const space = await this.spaceRepository.findOne({
where: { uuid: spaceUuid, community: { uuid: communityUuid } },
});
if (!space) {
this.throwNotFound('Space', spaceUuid);
}
return space;
}
// Helper method to find subspace with devices relation
private async findSubspaceWithDevices(subSpaceUuid: string) {
const subspace = await this.subspaceRepository.findOne({
where: { uuid: subSpaceUuid },
relations: ['devices'],
});
if (!subspace) {
this.throwNotFound('Subspace', subSpaceUuid);
}
return subspace;
}
private async findSubspace(subSpaceUuid: string) {
const subspace = await this.subspaceRepository.findOne({
where: { uuid: subSpaceUuid },
});
if (!subspace) {
this.throwNotFound('Subspace', subSpaceUuid);
}
return subspace;
}
private async findDevice(deviceUuid: string) {
const device = await this.deviceRepository.findOne({
where: { uuid: deviceUuid },
});
if (!device) {
this.throwNotFound('Device', deviceUuid);
}
return device;
}
private throwNotFound(entity: string, uuid: string) {
throw new HttpException(
`${entity} with ID ${uuid} not found`,
HttpStatus.NOT_FOUND,
);
}
}
@@ -5,7 +5,7 @@ import {
SubspaceRepository,
} from '@app/common/modules/space/repositories';
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
import { AddSubspaceDto, GetSpaceParam, GetSubSpaceParam } from '../dtos';
import { AddSubspaceDto, GetSpaceParam, GetSubSpaceParam } from '../../dtos';
import { SuccessResponseDto } from '@app/common/dto/success.response.dto';
import {
TypeORMCustomModel,