90 lines
2.8 KiB
TypeScript
90 lines
2.8 KiB
TypeScript
import {
|
|
Body,
|
|
Controller,
|
|
ForbiddenException,
|
|
Get,
|
|
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);
|
|
}
|
|
|
|
@Get(':communityId')
|
|
public async getCommunity(
|
|
@Param('communityId', ParseObjectIdPipe)
|
|
communityId: mongoose.Types.ObjectId,
|
|
) {
|
|
return this.communityService.getById(communityId);
|
|
}
|
|
|
|
@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.');
|
|
}
|
|
}
|
|
}
|