mirror of
https://github.com/SyncrowIOT/backend.git
synced 2026-10-25 08:17:54 +00:00
83 lines
2.3 KiB
TypeScript
83 lines
2.3 KiB
TypeScript
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
|
|
import { GetSpaceParam } from '../dtos';
|
|
import { BaseResponseDto } from '@app/common/dto/base.response.dto';
|
|
import { SceneService } from '../../scene/services';
|
|
import { SuccessResponseDto } from '@app/common/dto/success.response.dto';
|
|
import { GetSceneDto } from '../../scene/dtos';
|
|
import { ValidationService } from './space-validation.service';
|
|
import { QueryRunner } from 'typeorm';
|
|
import { SceneEntity } from '@app/common/modules/scene/entities';
|
|
import { SpaceEntity } from '@app/common/modules/space/entities/space.entity';
|
|
|
|
@Injectable()
|
|
export class SpaceSceneService {
|
|
constructor(
|
|
private readonly sceneSevice: SceneService,
|
|
private readonly validationService: ValidationService,
|
|
) {}
|
|
|
|
async getScenes(
|
|
params: GetSpaceParam,
|
|
getSceneDto: GetSceneDto,
|
|
): Promise<BaseResponseDto> {
|
|
try {
|
|
const { spaceUuid, communityUuid, projectUuid } = params;
|
|
|
|
await this.validationService.checkCommunityAndProjectSpaceExistence(
|
|
communityUuid,
|
|
projectUuid,
|
|
spaceUuid,
|
|
);
|
|
|
|
const scenes = await this.sceneSevice.findScenesBySpace(
|
|
spaceUuid,
|
|
getSceneDto,
|
|
);
|
|
|
|
return new SuccessResponseDto({
|
|
message: `Scenes retrieved successfully for space ${spaceUuid}`,
|
|
data: scenes,
|
|
});
|
|
} catch (error) {
|
|
console.error('Error retrieving scenes:', error);
|
|
|
|
if (error instanceof HttpException) {
|
|
throw error;
|
|
} else {
|
|
throw new HttpException(
|
|
'An error occurred while retrieving scenes',
|
|
HttpStatus.INTERNAL_SERVER_ERROR,
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
async deleteScenes(
|
|
space: SpaceEntity,
|
|
queryRunner: QueryRunner,
|
|
): Promise<void> {
|
|
try {
|
|
const scenes = await queryRunner.manager.find(SceneEntity, {
|
|
where: { space },
|
|
});
|
|
|
|
if (scenes.length === 0) {
|
|
return;
|
|
}
|
|
|
|
const sceneUuids = scenes.map((scene) => scene.uuid);
|
|
|
|
await Promise.all(
|
|
sceneUuids.map((uuid) =>
|
|
this.sceneSevice.deleteScene({ sceneUuid: uuid }),
|
|
),
|
|
);
|
|
} catch (error) {
|
|
throw new HttpException(
|
|
`Failed to delete scenes: ${error.message}`,
|
|
HttpStatus.INTERNAL_SERVER_ERROR,
|
|
);
|
|
}
|
|
}
|
|
}
|