diff --git a/src/space/controllers/space.controller.ts b/src/space/controllers/space.controller.ts index 20660ca..776c222 100644 --- a/src/space/controllers/space.controller.ts +++ b/src/space/controllers/space.controller.ts @@ -1,11 +1,13 @@ import { ControllerRoute } from '@app/common/constants/controller-route'; import { BaseResponseDto } from '@app/common/dto/base.response.dto'; +import { SuccessResponseDto } from '@app/common/dto/success.response.dto'; import { Body, Controller, Delete, Get, Param, + ParseUUIDPipe, Post, Put, Query, @@ -81,9 +83,13 @@ export class SpaceController { async updateSpacesOrder( @Body() orderSpacesDto: OrderSpacesDto, @Param() communitySpaceParam: CommunitySpaceParam, - @Param('parentSpaceUuid') parentSpaceUuid: string, + @Param('parentSpaceUuid', ParseUUIDPipe) parentSpaceUuid: string, ) { - return this.spaceService.updateSpacesOrder(parentSpaceUuid, orderSpacesDto); + await this.spaceService.updateSpacesOrder(parentSpaceUuid, orderSpacesDto); + return new SuccessResponseDto({ + statusCode: 200, + message: 'Spaces order updated successfully', + }); } @ApiBearerAuth() diff --git a/src/space/services/space.service.ts b/src/space/services/space.service.ts index 5ea39cd..126a3ec 100644 --- a/src/space/services/space.service.ts +++ b/src/space/services/space.service.ts @@ -360,6 +360,28 @@ export class SpaceService { parentSpaceUuid: string, { spacesUuids }: OrderSpacesDto, ) { + const parentSpace = await this.spaceRepository.findOne({ + where: { uuid: parentSpaceUuid, disabled: false }, + relations: ['children'], + }); + if (!parentSpace) { + throw new HttpException( + `Parent space with ID ${parentSpaceUuid} not found`, + HttpStatus.NOT_FOUND, + ); + } + // ensure that all sent spaces belong to the parent space + const missingSpaces = spacesUuids.filter( + (uuid) => !parentSpace.children.some((child) => child.uuid === uuid), + ); + if (missingSpaces.length > 0) { + throw new HttpException( + `Some spaces with IDs ${missingSpaces.join( + ', ', + )} do not belong to the parent space with ID ${parentSpaceUuid}`, + HttpStatus.BAD_REQUEST, + ); + } try { await this.spaceRepository.update( { uuid: In(spacesUuids), parent: { uuid: parentSpaceUuid } }, @@ -372,7 +394,7 @@ export class SpaceService { ' END', }, ); - return true; + return; } catch (error) { console.error('Error updating spaces order:', error); throw new HttpException(