updated space endpoint to follow project

This commit is contained in:
hannathkadher
2024-12-09 17:33:19 +04:00
parent 19825540dd
commit cdc95056ae
9 changed files with 87 additions and 39 deletions

View File

@ -140,7 +140,8 @@ export class ControllerRoute {
}; };
static SPACE = class { static SPACE = class {
public static readonly ROUTE = '/communities/:communityUuid/spaces'; public static readonly ROUTE =
'/projects/:projectUuid/communities/:communityUuid/spaces';
static ACTIONS = class { static ACTIONS = class {
public static readonly CREATE_SPACE_SUMMARY = 'Create a new space'; public static readonly CREATE_SPACE_SUMMARY = 'Create a new space';
public static readonly CREATE_SPACE_DESCRIPTION = public static readonly CREATE_SPACE_DESCRIPTION =

View File

@ -12,7 +12,7 @@ import {
UseGuards, UseGuards,
} from '@nestjs/common'; } from '@nestjs/common';
import { JwtAuthGuard } from '@app/common/guards/jwt.auth.guard'; import { JwtAuthGuard } from '@app/common/guards/jwt.auth.guard';
import { AddSpaceDto, CommunitySpaceParam } from '../dtos'; import { AddSpaceDto, CommunitySpaceParam, UpdateSpaceDto } from '../dtos';
import { BaseResponseDto } from '@app/common/dto/base.response.dto'; import { BaseResponseDto } from '@app/common/dto/base.response.dto';
import { GetSpaceParam } from '../dtos/get.space.param'; import { GetSpaceParam } from '../dtos/get.space.param';
@ -37,7 +37,7 @@ export class SpaceController {
): Promise<BaseResponseDto> { ): Promise<BaseResponseDto> {
return await this.spaceService.createSpace( return await this.spaceService.createSpace(
addSpaceDto, addSpaceDto,
communitySpaceParam.communityUuid, communitySpaceParam,
); );
} }
@ -51,11 +51,9 @@ export class SpaceController {
}) })
@Get() @Get()
async getHierarchy( async getHierarchy(
@Param() param: CommunitySpaceParam, @Param() params: CommunitySpaceParam,
): Promise<BaseResponseDto> { ): Promise<BaseResponseDto> {
return this.spaceService.getSpacesHierarchyForCommunity( return this.spaceService.getSpacesHierarchyForCommunity(params);
param.communityUuid,
);
} }
@ApiBearerAuth() @ApiBearerAuth()
@ -66,7 +64,7 @@ export class SpaceController {
}) })
@Delete('/:spaceUuid') @Delete('/:spaceUuid')
async deleteSpace(@Param() params: GetSpaceParam): Promise<BaseResponseDto> { async deleteSpace(@Param() params: GetSpaceParam): Promise<BaseResponseDto> {
return this.spaceService.delete(params.spaceUuid, params.communityUuid); return this.spaceService.delete(params);
} }
@ApiBearerAuth() @ApiBearerAuth()
@ -78,13 +76,9 @@ export class SpaceController {
}) })
async updateSpace( async updateSpace(
@Param() params: GetSpaceParam, @Param() params: GetSpaceParam,
@Body() updateSpaceDto: AddSpaceDto, @Body() updateSpaceDto: UpdateSpaceDto,
): Promise<BaseResponseDto> { ): Promise<BaseResponseDto> {
return this.spaceService.updateSpace( return this.spaceService.updateSpace(params, updateSpaceDto);
params.spaceUuid,
params.communityUuid,
updateSpaceDto,
);
} }
@ApiBearerAuth() @ApiBearerAuth()
@ -95,7 +89,7 @@ export class SpaceController {
}) })
@Get('/:spaceUuid') @Get('/:spaceUuid')
async get(@Param() params: GetSpaceParam): Promise<BaseResponseDto> { async get(@Param() params: GetSpaceParam): Promise<BaseResponseDto> {
return this.spaceService.findOne(params.spaceUuid); return this.spaceService.findOne(params);
} }
@ApiBearerAuth() @ApiBearerAuth()
@ -108,7 +102,7 @@ export class SpaceController {
async getHierarchyUnderSpace( async getHierarchyUnderSpace(
@Param() params: GetSpaceParam, @Param() params: GetSpaceParam,
): Promise<BaseResponseDto> { ): Promise<BaseResponseDto> {
return this.spaceService.getSpacesHierarchyForSpace(params.spaceUuid); return this.spaceService.getSpacesHierarchyForSpace(params);
} }
//should it be post? //should it be post?
@ -123,6 +117,6 @@ export class SpaceController {
async generateSpaceInvitationCode( async generateSpaceInvitationCode(
@Param() params: GetSpaceParam, @Param() params: GetSpaceParam,
): Promise<BaseResponseDto> { ): Promise<BaseResponseDto> {
return this.spaceService.getSpaceInvitationCode(params.spaceUuid); return this.spaceService.getSpaceInvitationCode(params);
} }
} }

View File

@ -30,7 +30,7 @@ export class AddSpaceDto {
parentUuid?: string; parentUuid?: string;
@IsString() @IsString()
@IsNotEmpty() @IsOptional()
public icon: string; public icon: string;
@ApiProperty({ @ApiProperty({
@ -56,6 +56,7 @@ export class AddSpaceDto {
@IsArray() @IsArray()
@ValidateNested({ each: true }) @ValidateNested({ each: true })
@IsOptional()
@Type(() => ProductAssignmentDto) @Type(() => ProductAssignmentDto)
products: ProductAssignmentDto[]; products: ProductAssignmentDto[];
} }

View File

@ -1,7 +1,8 @@
import { ApiProperty } from '@nestjs/swagger'; import { ApiProperty } from '@nestjs/swagger';
import { IsUUID } from 'class-validator'; import { IsUUID } from 'class-validator';
import { ProjectParam } from './project.param.dto';
export class CommunitySpaceParam { export class CommunitySpaceParam extends ProjectParam {
@ApiProperty({ @ApiProperty({
description: 'UUID of the community this space belongs to', description: 'UUID of the community this space belongs to',
example: 'd290f1ee-6c54-4b01-90e6-d701748f0851', example: 'd290f1ee-6c54-4b01-90e6-d701748f0851',

View File

@ -3,3 +3,5 @@ export * from './community-space.param';
export * from './get.space.param'; export * from './get.space.param';
export * from './user-space.param'; export * from './user-space.param';
export * from './subspace'; export * from './subspace';
export * from './project.param.dto';
export * from './update.space.dto';

View File

@ -0,0 +1,11 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsUUID } from 'class-validator';
export class ProjectParam {
@ApiProperty({
description: 'UUID of the project this community belongs to',
example: 'd290f1ee-6c54-4b01-90e6-d701748f0851',
})
@IsUUID()
projectUuid: string;
}

View File

@ -0,0 +1,4 @@
import { PartialType } from '@nestjs/swagger';
import { AddSpaceDto } from './add.space.dto';
export class UpdateSpaceDto extends PartialType(AddSpaceDto) {}

View File

@ -5,7 +5,7 @@ import {
HttpStatus, HttpStatus,
Injectable, Injectable,
} from '@nestjs/common'; } from '@nestjs/common';
import { AddSpaceDto } from '../dtos'; import { AddSpaceDto, CommunitySpaceParam, GetSpaceParam, UpdateSpaceDto } from '../dtos';
import { SuccessResponseDto } from '@app/common/dto/success.response.dto'; import { SuccessResponseDto } from '@app/common/dto/success.response.dto';
import { BaseResponseDto } from '@app/common/dto/base.response.dto'; import { BaseResponseDto } from '@app/common/dto/base.response.dto';
import { CommunityRepository } from '@app/common/modules/community/repositories'; import { CommunityRepository } from '@app/common/modules/community/repositories';
@ -13,6 +13,7 @@ import { SpaceEntity } from '@app/common/modules/space/entities';
import { generateRandomString } from '@app/common/helper/randomString'; import { generateRandomString } from '@app/common/helper/randomString';
import { SpaceLinkService } from './space-link'; import { SpaceLinkService } from './space-link';
import { SpaceProductService } from './space-products'; import { SpaceProductService } from './space-products';
import { ProjectRepository } from '@app/common/modules/project/repositiories';
@Injectable() @Injectable()
export class SpaceService { export class SpaceService {
@ -21,15 +22,19 @@ export class SpaceService {
private readonly communityRepository: CommunityRepository, private readonly communityRepository: CommunityRepository,
private readonly spaceLinkService: SpaceLinkService, private readonly spaceLinkService: SpaceLinkService,
private readonly spaceProductService: SpaceProductService, private readonly spaceProductService: SpaceProductService,
private readonly projectRepository: ProjectRepository,
) {} ) {}
async createSpace( async createSpace(
addSpaceDto: AddSpaceDto, addSpaceDto: AddSpaceDto,
communityId: string, params: CommunitySpaceParam,
): Promise<BaseResponseDto> { ): Promise<BaseResponseDto> {
const { parentUuid, direction, products } = addSpaceDto; const { parentUuid, direction, products } = addSpaceDto;
const { communityUuid, projectUuid } = params;
const community = await this.validateCommunity(communityId); await this.validateProject(projectUuid);
const community = await this.validateCommunity(communityUuid);
const parent = parentUuid ? await this.validateSpace(parentUuid) : null; const parent = parentUuid ? await this.validateSpace(parentUuid) : null;
try { try {
@ -67,9 +72,11 @@ export class SpaceService {
} }
async getSpacesHierarchyForCommunity( async getSpacesHierarchyForCommunity(
communityUuid: string, params: CommunitySpaceParam,
): Promise<BaseResponseDto> { ): Promise<BaseResponseDto> {
const { communityUuid, projectUuid } = params;
await this.validateCommunity(communityUuid); await this.validateCommunity(communityUuid);
await this.validateProject(projectUuid);
try { try {
// Get all spaces related to the community, including the parent-child relations // Get all spaces related to the community, including the parent-child relations
const spaces = await this.spaceRepository.find({ const spaces = await this.spaceRepository.find({
@ -80,7 +87,7 @@ export class SpaceService {
'incomingConnections', 'incomingConnections',
'spaceProducts', 'spaceProducts',
'spaceProducts.product', 'spaceProducts.product',
], // Include parent and children relations ],
}); });
// Organize spaces into a hierarchical structure // Organize spaces into a hierarchical structure
@ -99,9 +106,14 @@ export class SpaceService {
} }
} }
async findOne(spaceUuid: string): Promise<BaseResponseDto> { async findOne(params: GetSpaceParam): Promise<BaseResponseDto> {
const { communityUuid, spaceUuid, projectUuid } = params;
try { try {
const space = await this.validateSpace(spaceUuid); const space = await this.validateCommunityAndSpace(
communityUuid,
spaceUuid,
projectUuid,
);
return new SuccessResponseDto({ return new SuccessResponseDto({
message: `Space with ID ${spaceUuid} successfully fetched`, message: `Space with ID ${spaceUuid} successfully fetched`,
@ -119,15 +131,14 @@ export class SpaceService {
} }
} }
async delete( async delete(params: GetSpaceParam): Promise<BaseResponseDto> {
spaceUuid: string,
communityUuid: string,
): Promise<BaseResponseDto> {
try { try {
const { communityUuid, spaceUuid, projectUuid } = params;
// First, check if the community exists // First, check if the community exists
const space = await this.validateCommunityAndSpace( const space = await this.validateCommunityAndSpace(
communityUuid, communityUuid,
spaceUuid, spaceUuid,
projectUuid,
); );
// Delete the space // Delete the space
@ -149,14 +160,15 @@ export class SpaceService {
} }
async updateSpace( async updateSpace(
spaceUuid: string, params: GetSpaceParam,
communityId: string, updateSpaceDto: UpdateSpaceDto,
updateSpaceDto: AddSpaceDto,
): Promise<BaseResponseDto> { ): Promise<BaseResponseDto> {
const { communityUuid, spaceUuid, projectUuid } = params;
try { try {
const space = await this.validateCommunityAndSpace( const space = await this.validateCommunityAndSpace(
communityId, communityUuid,
spaceUuid, spaceUuid,
projectUuid,
); );
// If a parentId is provided, check if the parent exists // If a parentId is provided, check if the parent exists
@ -193,9 +205,10 @@ export class SpaceService {
} }
async getSpacesHierarchyForSpace( async getSpacesHierarchyForSpace(
spaceUuid: string, params: GetSpaceParam,
): Promise<BaseResponseDto> { ): Promise<BaseResponseDto> {
await this.validateSpace(spaceUuid); const { spaceUuid, communityUuid, projectUuid } = params;
await this.validateCommunityAndSpace(communityUuid, spaceUuid, projectUuid);
try { try {
// Get all spaces that are children of the provided space, including the parent-child relations // Get all spaces that are children of the provided space, including the parent-child relations
@ -220,11 +233,16 @@ export class SpaceService {
} }
} }
async getSpaceInvitationCode(spaceUuid: string): Promise<any> { async getSpaceInvitationCode(params: GetSpaceParam): Promise<any> {
const { communityUuid, spaceUuid, projectUuid } = params;
try { try {
const invitationCode = generateRandomString(6); const invitationCode = generateRandomString(6);
const space = await this.validateSpace(spaceUuid); const space = await this.validateCommunityAndSpace(
communityUuid,
spaceUuid,
projectUuid,
);
space.invitationCode = invitationCode; space.invitationCode = invitationCode;
await this.spaceRepository.save(space); await this.spaceRepository.save(space);
@ -283,7 +301,13 @@ export class SpaceService {
return community; return community;
} }
async validateCommunityAndSpace(communityUuid: string, spaceUuid: string) { async validateCommunityAndSpace(
communityUuid: string,
spaceUuid: string,
projectUuid: string,
) {
await this.validateProject(projectUuid);
const community = await this.validateCommunity(communityUuid); const community = await this.validateCommunity(communityUuid);
if (!community) { if (!community) {
this.throwNotFound('Community', communityUuid); this.throwNotFound('Community', communityUuid);
@ -301,6 +325,14 @@ export class SpaceService {
return space; return space;
} }
private async validateProject(uuid: string) {
const project = await this.projectRepository.findOne({
where: { uuid },
});
if (!project) this.throwNotFound('Project', uuid);
}
private throwNotFound(entity: string, uuid: string) { private throwNotFound(entity: string, uuid: string) {
throw new HttpException( throw new HttpException(
`${entity} with ID ${uuid} not found`, `${entity} with ID ${uuid} not found`,

View File

@ -42,6 +42,7 @@ import { DeviceService } from 'src/device/services';
import { DeviceStatusFirebaseService } from '@app/common/firebase/devices-status/services/devices-status.service'; import { DeviceStatusFirebaseService } from '@app/common/firebase/devices-status/services/devices-status.service';
import { DeviceStatusLogRepository } from '@app/common/modules/device-status-log/repositories'; import { DeviceStatusLogRepository } from '@app/common/modules/device-status-log/repositories';
import { SceneDeviceRepository } from '@app/common/modules/scene-device/repositories'; import { SceneDeviceRepository } from '@app/common/modules/scene-device/repositories';
import { ProjectRepository } from '@app/common/modules/project/repositiories';
@Module({ @Module({
imports: [ConfigModule, SpaceRepositoryModule], imports: [ConfigModule, SpaceRepositoryModule],
@ -79,6 +80,7 @@ import { SceneDeviceRepository } from '@app/common/modules/scene-device/reposito
SceneDeviceRepository, SceneDeviceRepository,
SpaceProductService, SpaceProductService,
SpaceProductRepository, SpaceProductRepository,
ProjectRepository,
], ],
exports: [SpaceService], exports: [SpaceService],
}) })