mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-15 18:27:05 +00:00
Add UserRepository and addUserCommunity endpoint
This commit is contained in:
@ -8,6 +8,8 @@ import { SpaceTypeRepositoryModule } from '@app/common/modules/space-type/space.
|
||||
import { SpaceTypeRepository } from '@app/common/modules/space-type/repositories';
|
||||
import { UserSpaceRepositoryModule } from '@app/common/modules/user-space/user.space.repository.module';
|
||||
import { UserSpaceRepository } from '@app/common/modules/user-space/repositories';
|
||||
import { UserRepositoryModule } from '@app/common/modules/user/user.repository.module';
|
||||
import { UserRepository } from '@app/common/modules/user/repositories';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
@ -15,6 +17,7 @@ import { UserSpaceRepository } from '@app/common/modules/user-space/repositories
|
||||
SpaceRepositoryModule,
|
||||
SpaceTypeRepositoryModule,
|
||||
UserSpaceRepositoryModule,
|
||||
UserRepositoryModule,
|
||||
],
|
||||
controllers: [CommunityController],
|
||||
providers: [
|
||||
@ -22,6 +25,7 @@ import { UserSpaceRepository } from '@app/common/modules/user-space/repositories
|
||||
SpaceRepository,
|
||||
SpaceTypeRepository,
|
||||
UserSpaceRepository,
|
||||
UserRepository,
|
||||
],
|
||||
exports: [CommunityService],
|
||||
})
|
||||
|
@ -13,9 +13,13 @@ import {
|
||||
} from '@nestjs/common';
|
||||
import { ApiTags, ApiBearerAuth } from '@nestjs/swagger';
|
||||
import { JwtAuthGuard } from '../../../libs/common/src/guards/jwt.auth.guard';
|
||||
import { AddCommunityDto } from '../dtos/add.community.dto';
|
||||
import {
|
||||
AddCommunityDto,
|
||||
AddUserCommunityDto,
|
||||
} from '../dtos/add.community.dto';
|
||||
import { GetCommunityChildDto } from '../dtos/get.community.dto';
|
||||
import { UpdateCommunityNameDto } from '../dtos/update.community.dto';
|
||||
import { CheckUserCommunityGuard } from 'src/guards/user.community.guard';
|
||||
|
||||
@ApiTags('Community Module')
|
||||
@Controller({
|
||||
@ -90,7 +94,20 @@ export class CommunityController {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(JwtAuthGuard, CheckUserCommunityGuard)
|
||||
@Post('user')
|
||||
async addUserCommunity(@Body() addUserCommunityDto: AddUserCommunityDto) {
|
||||
try {
|
||||
await this.communityService.addUserCommunity(addUserCommunityDto);
|
||||
return { message: 'user community added successfully' };
|
||||
} catch (error) {
|
||||
throw new HttpException(
|
||||
error.message || 'Internal server error',
|
||||
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
}
|
||||
}
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Put('rename/:communityUuid')
|
||||
|
@ -14,3 +14,22 @@ export class AddCommunityDto {
|
||||
Object.assign(this, dto);
|
||||
}
|
||||
}
|
||||
export class AddUserCommunityDto {
|
||||
@ApiProperty({
|
||||
description: 'communityUuid',
|
||||
required: true,
|
||||
})
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
public communityUuid: string;
|
||||
@ApiProperty({
|
||||
description: 'userUuid',
|
||||
required: true,
|
||||
})
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
public userUuid: string;
|
||||
constructor(dto: Partial<AddUserCommunityDto>) {
|
||||
Object.assign(this, dto);
|
||||
}
|
||||
}
|
||||
|
@ -7,7 +7,7 @@ import {
|
||||
BadRequestException,
|
||||
} from '@nestjs/common';
|
||||
import { SpaceRepository } from '@app/common/modules/space/repositories';
|
||||
import { AddCommunityDto } from '../dtos';
|
||||
import { AddCommunityDto, AddUserCommunityDto } from '../dtos';
|
||||
import {
|
||||
CommunityChildInterface,
|
||||
GetCommunityByUserUuidInterface,
|
||||
@ -188,6 +188,26 @@ export class CommunityService {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async addUserCommunity(addUserCommunityDto: AddUserCommunityDto) {
|
||||
try {
|
||||
await this.userSpaceRepository.save({
|
||||
user: { uuid: addUserCommunityDto.userUuid },
|
||||
space: { uuid: addUserCommunityDto.communityUuid },
|
||||
});
|
||||
} catch (err) {
|
||||
if (err.code === '23505') {
|
||||
throw new HttpException(
|
||||
'User already belongs to this community',
|
||||
HttpStatus.BAD_REQUEST,
|
||||
);
|
||||
}
|
||||
throw new HttpException(
|
||||
err.message || 'Internal Server Error',
|
||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
}
|
||||
}
|
||||
async renameCommunityByUuid(
|
||||
communityUuid: string,
|
||||
updateCommunityDto: UpdateCommunityNameDto,
|
||||
|
Reference in New Issue
Block a user