Skip to content

Commit

Permalink
feat(assignments-service): Find courses by members
Browse files Browse the repository at this point in the history
  • Loading branch information
Clashsoft committed Oct 27, 2023
1 parent f9309c8 commit 5d19a14
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions services/apps/assignments/src/course/course.controller.ts
Original file line number Diff line number Diff line change
@@ -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.';

Expand All @@ -14,6 +16,7 @@ const forbiddenResponse = 'Not owner.';
export class CourseController {
constructor(
private readonly courseService: CourseService,
private readonly memberService: MemberService,
) {
}

Expand All @@ -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<Course[]> {
return this.courseService.findAll({createdBy});
const filter: FilterQuery<Course> = {};
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')
Expand Down

0 comments on commit 5d19a14

Please sign in to comment.