mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-11-26 10:04:54 +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 { SpaceTypeRepository } from '@app/common/modules/space-type/repositories';
|
||||||
import { UserSpaceRepositoryModule } from '@app/common/modules/user-space/user.space.repository.module';
|
import { UserSpaceRepositoryModule } from '@app/common/modules/user-space/user.space.repository.module';
|
||||||
import { UserSpaceRepository } from '@app/common/modules/user-space/repositories';
|
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({
|
@Module({
|
||||||
imports: [
|
imports: [
|
||||||
@ -15,6 +17,7 @@ import { UserSpaceRepository } from '@app/common/modules/user-space/repositories
|
|||||||
SpaceRepositoryModule,
|
SpaceRepositoryModule,
|
||||||
SpaceTypeRepositoryModule,
|
SpaceTypeRepositoryModule,
|
||||||
UserSpaceRepositoryModule,
|
UserSpaceRepositoryModule,
|
||||||
|
UserRepositoryModule,
|
||||||
],
|
],
|
||||||
controllers: [CommunityController],
|
controllers: [CommunityController],
|
||||||
providers: [
|
providers: [
|
||||||
@ -22,6 +25,7 @@ import { UserSpaceRepository } from '@app/common/modules/user-space/repositories
|
|||||||
SpaceRepository,
|
SpaceRepository,
|
||||||
SpaceTypeRepository,
|
SpaceTypeRepository,
|
||||||
UserSpaceRepository,
|
UserSpaceRepository,
|
||||||
|
UserRepository,
|
||||||
],
|
],
|
||||||
exports: [CommunityService],
|
exports: [CommunityService],
|
||||||
})
|
})
|
||||||
|
|||||||
@ -13,9 +13,13 @@ import {
|
|||||||
} from '@nestjs/common';
|
} from '@nestjs/common';
|
||||||
import { ApiTags, ApiBearerAuth } from '@nestjs/swagger';
|
import { ApiTags, ApiBearerAuth } from '@nestjs/swagger';
|
||||||
import { JwtAuthGuard } from '../../../libs/common/src/guards/jwt.auth.guard';
|
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 { GetCommunityChildDto } from '../dtos/get.community.dto';
|
||||||
import { UpdateCommunityNameDto } from '../dtos/update.community.dto';
|
import { UpdateCommunityNameDto } from '../dtos/update.community.dto';
|
||||||
|
import { CheckUserCommunityGuard } from 'src/guards/user.community.guard';
|
||||||
|
|
||||||
@ApiTags('Community Module')
|
@ApiTags('Community Module')
|
||||||
@Controller({
|
@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()
|
@ApiBearerAuth()
|
||||||
@UseGuards(JwtAuthGuard)
|
@UseGuards(JwtAuthGuard)
|
||||||
@Put('rename/:communityUuid')
|
@Put('rename/:communityUuid')
|
||||||
|
|||||||
@ -14,3 +14,22 @@ export class AddCommunityDto {
|
|||||||
Object.assign(this, dto);
|
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,
|
BadRequestException,
|
||||||
} from '@nestjs/common';
|
} from '@nestjs/common';
|
||||||
import { SpaceRepository } from '@app/common/modules/space/repositories';
|
import { SpaceRepository } from '@app/common/modules/space/repositories';
|
||||||
import { AddCommunityDto } from '../dtos';
|
import { AddCommunityDto, AddUserCommunityDto } from '../dtos';
|
||||||
import {
|
import {
|
||||||
CommunityChildInterface,
|
CommunityChildInterface,
|
||||||
GetCommunityByUserUuidInterface,
|
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(
|
async renameCommunityByUuid(
|
||||||
communityUuid: string,
|
communityUuid: string,
|
||||||
updateCommunityDto: UpdateCommunityNameDto,
|
updateCommunityDto: UpdateCommunityNameDto,
|
||||||
|
|||||||
Reference in New Issue
Block a user