mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-16 02:36:19 +00:00
add validation checks
This commit is contained in:
@ -1,11 +1,13 @@
|
|||||||
import { ControllerRoute } from '@app/common/constants/controller-route';
|
import { ControllerRoute } from '@app/common/constants/controller-route';
|
||||||
import { BaseResponseDto } from '@app/common/dto/base.response.dto';
|
import { BaseResponseDto } from '@app/common/dto/base.response.dto';
|
||||||
|
import { SuccessResponseDto } from '@app/common/dto/success.response.dto';
|
||||||
import {
|
import {
|
||||||
Body,
|
Body,
|
||||||
Controller,
|
Controller,
|
||||||
Delete,
|
Delete,
|
||||||
Get,
|
Get,
|
||||||
Param,
|
Param,
|
||||||
|
ParseUUIDPipe,
|
||||||
Post,
|
Post,
|
||||||
Put,
|
Put,
|
||||||
Query,
|
Query,
|
||||||
@ -81,9 +83,13 @@ export class SpaceController {
|
|||||||
async updateSpacesOrder(
|
async updateSpacesOrder(
|
||||||
@Body() orderSpacesDto: OrderSpacesDto,
|
@Body() orderSpacesDto: OrderSpacesDto,
|
||||||
@Param() communitySpaceParam: CommunitySpaceParam,
|
@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()
|
@ApiBearerAuth()
|
||||||
|
@ -360,6 +360,28 @@ export class SpaceService {
|
|||||||
parentSpaceUuid: string,
|
parentSpaceUuid: string,
|
||||||
{ spacesUuids }: OrderSpacesDto,
|
{ 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 {
|
try {
|
||||||
await this.spaceRepository.update(
|
await this.spaceRepository.update(
|
||||||
{ uuid: In(spacesUuids), parent: { uuid: parentSpaceUuid } },
|
{ uuid: In(spacesUuids), parent: { uuid: parentSpaceUuid } },
|
||||||
@ -372,7 +394,7 @@ export class SpaceService {
|
|||||||
' END',
|
' END',
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
return true;
|
return;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error updating spaces order:', error);
|
console.error('Error updating spaces order:', error);
|
||||||
throw new HttpException(
|
throw new HttpException(
|
||||||
|
Reference in New Issue
Block a user