Untitled
unknown
plain_text
17 days ago
18 kB
2
Indexable
Never
import { BadRequestException, Injectable } from "@nestjs/common"; import { InjectModel } from "@nestjs/mongoose"; import { PaginateModel } from "mongoose"; import { MemberChapterLectureDocument, MemberChapterLectureEntity, } from "./schema/member-chapter-lecture.schema"; import { DB_CONNECTIONS_NAMES } from "../common/constants/db-connections"; import { UpdateMemberChapterLectureDto } from "./dto/member-chapter-lecture.dto"; import { BatchChapterService } from "./batch-chapter.service"; import { BatchChapterStatus } from "./enum/batch-chapter-status.enum"; import moment from "moment"; import mongoose from "mongoose"; import { mongoDateFilter } from "../../utils/ChangeDateFormat"; @Injectable() export class MemberChapterLectureService { constructor( @InjectModel( MemberChapterLectureEntity.name, DB_CONNECTIONS_NAMES.ADMISSIONS_DB ) private memberchapterlectureModel: PaginateModel<MemberChapterLectureDocument>, private readonly batchChapterService: BatchChapterService ) {} async create(data: UpdateMemberChapterLectureDto) { try { const { batchChapterId, lectureId, bufferLectureId, timeTableId } = data; if (!batchChapterId) { throw new BadRequestException("batchChapterId is missing !"); } if (!lectureId && !bufferLectureId) { throw new BadRequestException( "lectureId or bufferLectureId required" ); } const findOne: any = await this.batchChapterService.findOne( batchChapterId, "chapterLectureId" ); if (!findOne) { throw new BadRequestException("batch Chapter not found !"); } let memberChapterLecture = await this.memberchapterlectureModel.findOneAndUpdate( { timeTableId }, { batchChapterId, timeTableId, status: data.status, remark: data.remark, date: data.date, ...(lectureId ? { lectureId } : { lectureId: null }), ...(bufferLectureId ? { bufferLectureId } : { bufferLectureId: null }), }, { new: true, upsert: true } ); if (timeTableId) { let payload: any = {}; let totalLecturesCount = findOne.chapterLectureId?.totalLectures ?? 0; let completedLecturesCount = await this.memberchapterlectureModel.countDocuments({ batchChapterId, lectureId: { $exists: true }, }); const notDoneLectures = totalLecturesCount - completedLecturesCount; // if (notDoneLectures === 0) { // payload = { // id: findOne._id, // status: BatchChapterStatus.COMPLETED // } // } else if ( notDoneLectures !== totalLecturesCount && findOne?.status !== BatchChapterStatus.IN_PROGRESS ) { payload = { id: findOne._id, status: BatchChapterStatus.IN_PROGRESS, }; } if (Object.keys(payload).length > 0) { await this.batchChapterService.update(payload); } } return memberChapterLecture; } catch (e) { throw new BadRequestException(e); } } async findOne(id: string) { return await this.memberchapterlectureModel.findOne({ _id: id }); } async findOneByQuery(query: any) { const memberchapterlecture = await this.memberchapterlectureModel.findOne(query); return memberchapterlecture; } async getAll(data: any) { let filter: any = {}; if (data.batchChapterId) { filter["batchChapterId"] = data.batchChapterId; } if (data.startDate) { filter["date"] = mongoDateFilter(data.startDate, data.endDate); } const memberchapterlectures = data.paginate ? await this.memberchapterlectureModel.paginate(filter, { page: data.page || 1, limit: data.limit || 10, lean: true, }) : await this.memberchapterlectureModel.find(filter).lean(true); return memberchapterlectures; } async findByTimeTableId(data: any): Promise<any> { const { timeTableId } = data; if (timeTableId) { let res: any = await this.memberchapterlectureModel .findOne({ timeTableId }) .populate({ path: "batchChapterId", select: "_id chapterId" }) .lean(); if (res) { let chapterLecture: any = await this.batchChapterService.findOne( res.batchChapterId?._id, "chapterLectureId chapterId" ); if (chapterLecture) { let lectureData = ( chapterLecture.chapterLectureId?.lectures ?? [] ).find((l: any) => String(l._id) === res.lectureId); let bufferLectureData = ( chapterLecture.chapterLectureId?.bufferLectures ?? [] ).find((l: any) => String(l._id) === res.bufferLectureId); return { ...res, chapterName: chapterLecture.chapterId?.title ?? "", lectureDescription: lectureData?.description ? lectureData.description : bufferLectureData?.description ?? "", lectureName: lectureData?.name ? lectureData.name : bufferLectureData?.name, }; } } } } async getMemberReport(data: any): Promise<any> { let batchChapter = await this.batchChapterService.findOneByQuery(data); if (batchChapter) { return await this.getAll({ batchChapterId: batchChapter._id }); } return []; } async getStaffWiseReport(data: any): Promise<any> { const { startDate, endDate, memberId, batchId, courseId } = data; let filter: any = {}; const start = new Date( moment(startDate).subtract(1, "day").format("YYYY-MM-DD") + "T18:30:00.000Z" ); const end = new Date( moment(endDate || startDate).format("YYYY-MM-DD") + "T18:30:00.000Z" ); if (data.startDate && end) { filter["date"] = { $gte: start, $lt: end, }; } const pipeline: any[] = [ { $match: filter, }, { $lookup: { from: "batch-chapters", localField: "batchChapterId", foreignField: "_id", as: "batchChapter", }, }, { $unwind: { path: "$batchChapter", }, }, { $lookup: { from: "members", localField: "batchChapter.memberId", foreignField: "_id", as: "member", }, }, ]; if (memberId) { pipeline.push({ $match: { "member._id": new mongoose.Types.ObjectId(memberId), }, }); } pipeline.push({ $unwind: { path: "$member", }, }); if (batchId) { pipeline.push({ $match: { "batchChapter.batchId": new mongoose.Types.ObjectId( batchId ), }, }); } pipeline.push( { $lookup: { from: "basechapters", localField: "batchChapter.chapterId", foreignField: "_id", as: "chapter", }, }, { $unwind: { path: "$chapter", }, }, { $lookup: { from: "chapter-lecture", localField: "batchChapter.chapterLectureId", foreignField: "_id", as: "chapterLecture", }, }, { $unwind: { path: "$chapterLecture", }, }, { $lookup: { from: "coursechaptertemplates", localField: "batchChapter.courseChapterTemplateId", foreignField: "_id", as: "courseChapterTemplate", }, } ); if (courseId) { pipeline.push({ $match: { "courseChapterTemplate.courseId": new mongoose.Types.ObjectId(courseId), }, }); } pipeline.push( { $unwind: { path: "$courseChapterTemplate", }, }, { $addFields: { lectureName: { $cond: { if: { $gt: ["$lectureId", null], }, then: { $arrayElemAt: [ { $map: { input: { $filter: { input: "$chapterLecture.lectures", as: "lecture", cond: { $eq: [ "$$lecture._id", { $toObjectId: "$lectureId", }, ], }, }, }, as: "matchedLecture", in: "$$matchedLecture.name", }, }, 0, ], }, else: { $arrayElemAt: [ { $map: { input: { $filter: { input: "$chapterLecture.bufferLectures", as: "bufferLecture", cond: { $eq: [ "$$bufferLecture._id", { $toObjectId: "$bufferLectureId", }, ], }, }, }, as: "matchedBufferLecture", in: "$$matchedBufferLecture.name", }, }, 0, ], }, }, }, lectureDescription: { $cond: { if: { $gt: ["$lectureId", null], }, then: { $arrayElemAt: [ { $map: { input: { $filter: { input: "$chapterLecture.lectures", as: "lecture", cond: { $eq: [ "$$lecture._id", { $toObjectId: "$lectureId", }, ], }, }, }, as: "matchedLecture", in: "$$matchedLecture.description", }, }, 0, ], }, else: { $arrayElemAt: [ { $map: { input: { $filter: { input: "$chapterLecture.bufferLectures", as: "bufferLecture", cond: { $eq: [ "$$bufferLecture._id", { $toObjectId: "$bufferLectureId", }, ], }, }, }, as: "matchedBufferLecture", in: "$$matchedBufferLecture.name", }, }, 0, ], }, }, }, }, }, { $addFields: { formattedCreatedAt: { $dateToString: { format: "%d/%m/%Y", date: "$createdAt", }, }, }, }, { $project: { name: "$member.name", code: "$member.code", chapter: "$chapter.title", chapterCode: "$chapter.code", remark: "$remark", lecture: "$lectureName", lectureDescription: "$lectureDescription", date: "$formattedCreatedAt", batchId: "$batchChapter.batchId", courseId: "$courseChapterTemplate.courseId", subjectId: "$courseChapterTemplate.subjectId", }, } ); const result = await this.memberchapterlectureModel.aggregate(pipeline); return result; } }
Leave a Comment