.
This commit is contained in:
1 parent
a197185ad0
commit
30dfb7ce58
27 files changed
+1584
-45
No files matched your search
@@ -0,0 +1,80 @@
|
||||
import {
|
||||
Body,
|
||||
Controller,
|
||||
ForbiddenException,
|
||||
Param,
|
||||
Patch,
|
||||
Post,
|
||||
UseGuards,
|
||||
} from '@nestjs/common';
|
||||
import mongoose from 'mongoose';
|
||||
import { CurrentUser } from '../auth/current-user.decorator';
|
||||
import { Roles } from '../auth/roles.decorator';
|
||||
import { RolesGuard } from '../auth/roles.guard';
|
||||
import { ParseObjectIdPipe } from '../common/parse-object-id.pipe';
|
||||
import type { UserDocument } from '../global/database/schema';
|
||||
import { CommunityService } from './community.service';
|
||||
import { CreateCommunityDto } from './dto/create-community.dto';
|
||||
import { TransferOwnershipDto } from './dto/transfer-ownership.dto';
|
||||
|
||||
@Controller('communities')
|
||||
export class CommunityController {
|
||||
constructor(private readonly communityService: CommunityService) {}
|
||||
|
||||
@Post()
|
||||
public async createCommunity(@Body() body: CreateCommunityDto) {
|
||||
return this.communityService.createCommunityWithOwner(body);
|
||||
}
|
||||
|
||||
@Patch(':communityId/members/:userId/promote')
|
||||
@Roles('owner')
|
||||
@UseGuards(RolesGuard)
|
||||
public async promoteToAdmin(
|
||||
@CurrentUser() currentUser: UserDocument,
|
||||
@Param('communityId', ParseObjectIdPipe)
|
||||
communityId: mongoose.Types.ObjectId,
|
||||
@Param('userId', ParseObjectIdPipe) targetUserId: mongoose.Types.ObjectId,
|
||||
) {
|
||||
this.assertOwnerOfCommunity(currentUser, communityId);
|
||||
return this.communityService.promoteToAdmin({ communityId, targetUserId });
|
||||
}
|
||||
|
||||
@Patch(':communityId/members/:userId/demote')
|
||||
@Roles('owner')
|
||||
@UseGuards(RolesGuard)
|
||||
public async demoteToMember(
|
||||
@CurrentUser() currentUser: UserDocument,
|
||||
@Param('communityId', ParseObjectIdPipe)
|
||||
communityId: mongoose.Types.ObjectId,
|
||||
@Param('userId', ParseObjectIdPipe) targetUserId: mongoose.Types.ObjectId,
|
||||
) {
|
||||
this.assertOwnerOfCommunity(currentUser, communityId);
|
||||
return this.communityService.demoteToMember({ communityId, targetUserId });
|
||||
}
|
||||
|
||||
@Patch(':communityId/transfer-ownership')
|
||||
@Roles('owner')
|
||||
@UseGuards(RolesGuard)
|
||||
public async transferOwnership(
|
||||
@CurrentUser() currentUser: UserDocument,
|
||||
@Param('communityId', ParseObjectIdPipe)
|
||||
communityId: mongoose.Types.ObjectId,
|
||||
@Body() body: TransferOwnershipDto,
|
||||
) {
|
||||
this.assertOwnerOfCommunity(currentUser, communityId);
|
||||
return this.communityService.transferOwnership({
|
||||
communityId,
|
||||
currentOwnerUserId: currentUser._id,
|
||||
newOwnerUserId: new mongoose.Types.ObjectId(body.newOwnerUserId),
|
||||
});
|
||||
}
|
||||
|
||||
private assertOwnerOfCommunity(
|
||||
user: UserDocument,
|
||||
communityId: mongoose.Types.ObjectId,
|
||||
): void {
|
||||
if (!user.communityId.equals(communityId)) {
|
||||
throw new ForbiddenException('You do not belong to this community.');
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user