From 5d19a14d8aa531c5dcd32ea230d7e6fb5dd687b8 Mon Sep 17 00:00:00 2001 From: Adrian Kunz Date: Fri, 27 Oct 2023 20:49:25 +0200 Subject: [PATCH] feat(assignments-service): Find courses by members --- .../assignments/src/course/course.controller.ts | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/services/apps/assignments/src/course/course.controller.ts b/services/apps/assignments/src/course/course.controller.ts index 77f31d310..779ce5448 100644 --- a/services/apps/assignments/src/course/course.controller.ts +++ b/services/apps/assignments/src/course/course.controller.ts @@ -1,11 +1,13 @@ import {Auth, AuthUser, UserToken} from '@app/keycloak-auth'; import {NotFound} from '@mean-stream/nestx'; -import {Body, Controller, Delete, Get, Param, Patch, Post, Query} from '@nestjs/common'; +import {Body, Controller, Delete, Get, Param, ParseArrayPipe, Patch, Post, Query} from '@nestjs/common'; import {ApiCreatedResponse, ApiOkResponse, ApiOperation, ApiTags} from '@nestjs/swagger'; import {CourseStudent, CreateCourseDto, UpdateCourseDto} from './course.dto'; import {Course} from './course.schema'; import {CourseService} from './course.service'; import {CourseAuth} from "../course-member/course-auth.decorator"; +import {FilterQuery} from "mongoose"; +import {MemberService} from "@app/member"; const forbiddenResponse = 'Not owner.'; @@ -14,6 +16,7 @@ const forbiddenResponse = 'Not owner.'; export class CourseController { constructor( private readonly courseService: CourseService, + private readonly memberService: MemberService, ) { } @@ -31,8 +34,17 @@ export class CourseController { @ApiOkResponse({type: [Course]}) async findAll( @Query('createdBy') createdBy?: string, + @Query('members', new ParseArrayPipe({optional: true})) memberIds?: string[], ): Promise { - return this.courseService.findAll({createdBy}); + const filter: FilterQuery = {}; + if (createdBy) { + (filter.$or ||= []).push({createdBy}); + } + if (memberIds) { + const members = await this.memberService.findAll({user: {$in: memberIds}}); + (filter.$or ||= []).push({_id: {$in: members.map(m => m.parent)}}); + } + return this.courseService.findAll(filter); } @Get(':id')