feat(api): add community endpoints
This commit is contained in:
1 parent
caf517123b
commit
6cd213a462
5 files changed
+366
No files matched your search
@@ -0,0 +1,107 @@
|
||||
import {
|
||||
Body,
|
||||
Controller,
|
||||
BadRequestException,
|
||||
ForbiddenException,
|
||||
Get,
|
||||
Param,
|
||||
Patch,
|
||||
Post,
|
||||
Req,
|
||||
UseGuards,
|
||||
} from '@nestjs/common';
|
||||
import type { Request } from 'express';
|
||||
import mongoose from 'mongoose';
|
||||
import { AllowPublicAccess } from '../auth/allow-public-access.decorator';
|
||||
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 '../auth/auth-request';
|
||||
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()
|
||||
@AllowPublicAccess()
|
||||
public async createCommunity(
|
||||
@Body() body: CreateCommunityDto,
|
||||
@Req() req: Request,
|
||||
) {
|
||||
if (!req.identity && !body.owner) {
|
||||
throw new BadRequestException(
|
||||
'owner is required when not authenticated.',
|
||||
);
|
||||
}
|
||||
return this.communityService.createCommunityWithOwner({
|
||||
...body,
|
||||
identityId: req.identity?._id,
|
||||
});
|
||||
}
|
||||
|
||||
@Get(':communityId')
|
||||
@AllowPublicAccess()
|
||||
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.');
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user