mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-10 07:07:21 +00:00
add validation checks
This commit is contained in:
@ -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()
|
||||
|
@ -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(
|
||||
|
Reference in New Issue
Block a user