62 lines
1.5 KiB
TypeScript
62 lines
1.5 KiB
TypeScript
import {
|
|
BadRequestException,
|
|
Controller,
|
|
Get,
|
|
Param,
|
|
Query,
|
|
UseGuards,
|
|
} from '@nestjs/common';
|
|
import { Type } from 'class-transformer';
|
|
import {
|
|
IsInt,
|
|
IsOptional,
|
|
IsString,
|
|
Max,
|
|
MaxLength,
|
|
Min,
|
|
} from 'class-validator';
|
|
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 { UserSearchService } from './user-search.service';
|
|
|
|
class SearchCommunityUsersQueryDto {
|
|
@IsString()
|
|
@MaxLength(80)
|
|
query!: string;
|
|
|
|
@IsOptional()
|
|
@Type(() => Number)
|
|
@IsInt()
|
|
@Min(1)
|
|
@Max(20)
|
|
limit?: number;
|
|
}
|
|
|
|
@Controller('communities/:communityId/users')
|
|
@Roles('admin', 'owner')
|
|
@UseGuards(RolesGuard)
|
|
export class CommunityUserController {
|
|
constructor(private readonly userSearchService: UserSearchService) {}
|
|
|
|
@Get('search')
|
|
public async searchUsers(
|
|
@CurrentUser() currentUser: UserDocument,
|
|
@Param('communityId', ParseObjectIdPipe)
|
|
communityId: mongoose.Types.ObjectId,
|
|
@Query() query: SearchCommunityUsersQueryDto,
|
|
) {
|
|
if (!currentUser.communityId.equals(communityId)) {
|
|
throw new BadRequestException('Different community.');
|
|
}
|
|
return this.userSearchService.search({
|
|
communityId,
|
|
query: query.query,
|
|
limit: query.limit,
|
|
});
|
|
}
|
|
}
|