mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-16 02:36:19 +00:00
Add endpoint to rename community by UUID
This commit is contained in:
@ -7,6 +7,7 @@ import {
|
||||
HttpStatus,
|
||||
Param,
|
||||
Post,
|
||||
Put,
|
||||
Query,
|
||||
UseGuards,
|
||||
} from '@nestjs/common';
|
||||
@ -14,6 +15,7 @@ import { ApiTags, ApiBearerAuth } from '@nestjs/swagger';
|
||||
import { JwtAuthGuard } from '../../../libs/common/src/guards/jwt.auth.guard';
|
||||
import { AddCommunityDto } from '../dtos/add.community.dto';
|
||||
import { GetCommunityChildDto } from '../dtos/get.community.dto';
|
||||
import { UpdateCommunityNameDto } from '../dtos/update.community.dto';
|
||||
|
||||
@ApiTags('Community Module')
|
||||
@Controller({
|
||||
@ -82,4 +84,29 @@ export class CommunityController {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Put('rename/:communityUuid')
|
||||
async renameCommunityByUuid(
|
||||
@Param('communityUuid') communityUuid: string,
|
||||
@Body() updateCommunityDto: UpdateCommunityNameDto,
|
||||
) {
|
||||
try {
|
||||
const community = await this.communityService.renameCommunityByUuid(
|
||||
communityUuid,
|
||||
updateCommunityDto,
|
||||
);
|
||||
return community;
|
||||
} catch (error) {
|
||||
if (error.status === 404) {
|
||||
throw new HttpException('Community not found', HttpStatus.NOT_FOUND);
|
||||
} else {
|
||||
throw new HttpException(
|
||||
error.message || 'Internal server error',
|
||||
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
16
src/community/dtos/update.community.dto.ts
Normal file
16
src/community/dtos/update.community.dto.ts
Normal file
@ -0,0 +1,16 @@
|
||||
import { ApiProperty } from '@nestjs/swagger';
|
||||
import { IsNotEmpty, IsString } from 'class-validator';
|
||||
|
||||
export class UpdateCommunityNameDto {
|
||||
@ApiProperty({
|
||||
description: 'communityName',
|
||||
required: true,
|
||||
})
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
public communityName: string;
|
||||
|
||||
constructor(dto: Partial<UpdateCommunityNameDto>) {
|
||||
Object.assign(this, dto);
|
||||
}
|
||||
}
|
@ -13,3 +13,8 @@ export interface CommunityChildInterface {
|
||||
totalCount?: number;
|
||||
children?: CommunityChildInterface[];
|
||||
}
|
||||
export interface RenameCommunityByUuidInterface {
|
||||
uuid: string;
|
||||
name: string;
|
||||
type: string;
|
||||
}
|
||||
|
@ -1,13 +1,20 @@
|
||||
import { GetCommunityChildDto } from './../dtos/get.community.dto';
|
||||
import { SpaceTypeRepository } from './../../../libs/common/src/modules/space-type/repositories/space.type.repository';
|
||||
import { Injectable, HttpException, HttpStatus } from '@nestjs/common';
|
||||
import {
|
||||
Injectable,
|
||||
HttpException,
|
||||
HttpStatus,
|
||||
BadRequestException,
|
||||
} from '@nestjs/common';
|
||||
import { SpaceRepository } from '@app/common/modules/space/repositories';
|
||||
import { AddCommunityDto } from '../dtos';
|
||||
import {
|
||||
CommunityChildInterface,
|
||||
GetCommunityByUuidInterface,
|
||||
RenameCommunityByUuidInterface,
|
||||
} from '../interface/community.interface';
|
||||
import { SpaceEntity } from '@app/common/modules/space/entities';
|
||||
import { UpdateCommunityNameDto } from '../dtos/update.community.dto';
|
||||
|
||||
@Injectable()
|
||||
export class CommunityService {
|
||||
@ -131,4 +138,46 @@ export class CommunityService {
|
||||
|
||||
return childHierarchies;
|
||||
}
|
||||
async renameCommunityByUuid(
|
||||
communityUuid: string,
|
||||
updateCommunityDto: UpdateCommunityNameDto,
|
||||
): Promise<RenameCommunityByUuidInterface> {
|
||||
try {
|
||||
const community = await this.spaceRepository.findOneOrFail({
|
||||
where: { uuid: communityUuid },
|
||||
relations: ['spaceType'],
|
||||
});
|
||||
|
||||
if (
|
||||
!community ||
|
||||
!community.spaceType ||
|
||||
community.spaceType.type !== 'community'
|
||||
) {
|
||||
throw new BadRequestException('Invalid community UUID');
|
||||
}
|
||||
|
||||
await this.spaceRepository.update(
|
||||
{ uuid: communityUuid },
|
||||
{ spaceName: updateCommunityDto.communityName },
|
||||
);
|
||||
|
||||
// Fetch the updated community
|
||||
const updatedCommunity = await this.spaceRepository.findOneOrFail({
|
||||
where: { uuid: communityUuid },
|
||||
relations: ['spaceType'],
|
||||
});
|
||||
|
||||
return {
|
||||
uuid: updatedCommunity.uuid,
|
||||
name: updatedCommunity.spaceName,
|
||||
type: updatedCommunity.spaceType.type,
|
||||
};
|
||||
} catch (err) {
|
||||
if (err instanceof BadRequestException) {
|
||||
throw err; // Re-throw BadRequestException
|
||||
} else {
|
||||
throw new HttpException('Community not found', HttpStatus.NOT_FOUND);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user