const { authenticate } = require('@feathersjs/authentication');

const lessonContentService = {
	find(params) {
		const userId = params.account.userId;
		if (!userId) {
			throw new BadRequest('requires a user in the query')
		}
		return LessonModel.aggregate([
			{ $lookup: {
				from: 'courses',
				localField: 'courseId',
				foreignField: '_id',
				as: 'course'
			}},
			{ $match: { $or: [
				{ 'course.userIds': userId},
				{ 'course.teacherIds': userId },
				{ 'course.substitutionIds': userId }
			]} },
			{ $unwind: '$contents' },
			{ $match: { 'contents.component': params.query.type } },
			{ $match: { $or: [
				{ 'contents.user': { $in: [params.query.user] } },
				{ $or: [
					{'contents.hidden': { $exists: false } },
					{'contents.hidden': false }
				]},
			] } },
			{ $project: { _id: '$contents._id', content: '$contents.content' } },
		]).exec();
	},
}

const lessonContentServiceHooks = {
	before: {
		all: [authenticate('jwt')],
		find: [],
	},
	after: {
		all: [],
		find: [],
	},
};

module.exports = {
	lessonContentService,
	lessonContentServiceHooks,
};

@@ -56,16 +56,61 @@
 ation');
+%0Aconst %7B LessonModel %7D = require('../model');
 %0A%0Aconst 

fix imports