add validation checks

This commit is contained in:
Mhd Zayd Skaff
2025-07-09 16:49:00 +03:00
parent 09322c5b80
commit 388087d0ab
2 changed files with 31 additions and 3 deletions

View File

@ -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()

View File

@ -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(