Untitled

 avatar
unknown
plain_text
5 months ago
6.0 kB
5
Indexable
async addLog(log: DeviceLogDocument) {
        const logTime = new Date(log.attendanceAt);
        console.log("logTime", logTime);

        const userCourse = await this.userCourseService.findOnebyQuery({
            rollNumber: Number(log.deviceUserId),
        });
        if (userCourse) {
            const startDate = moment(logTime)
                .subtract(1, "days")
                .format("YYYY-MM-DD");
            const logDate = moment(logTime).format("YYYY-MM-DD");

            const isLogPresent = await this.attendanceModel.find({
                userId: userCourse.user,
                courseId: userCourse.course,
                $and: [
                    {
                        attendanceDate: {
                            $gte: new Date(`${startDate}T18:30:00.000Z`),
                        },
                    },
                    {
                        attendanceDate: {
                            $lte: new Date(`${logDate}T18:29:59.999Z`),
                        },
                    },
                ],
            });
            const oldLog = size(isLogPresent) && isLogPresent[0];
            //calculate late miutes
            //@todo
            const course = await this.userCourseService.findcourse(
                userCourse.course
            );
            const batches = course.batches;
            const userBatch = find(
                batches,
                (b) => String(b._id) === String(userCourse.batch)
            ) as BatchEntity;
            const batchAttendanceTime = userBatch?.attendanceMessageTime;
            const batchEndTime = userBatch?.attendanceEndTime;
            let isLate;

            if (batchAttendanceTime) {
                const timeOfLog = moment(log.attendanceAt).format("HH:mm");
                // const batchEndTime = userBatch.attendanceEndTime
                if (timeOfLog > batchAttendanceTime) {
                    isLate = this.differenceBetweenTimes(
                        batchAttendanceTime,
                        timeOfLog
                    );
                }
            }
            //update continue absent count
            let res = await this.absentCountService.update({
                userId: `${userCourse.user}`,
                courseId: `${userCourse.course}`,
                present: true,
                date: logDate,
                isReturnOldDoc: true,
            });
            const backDateAbsentCount = res.totalDays ?? 0;
            const backDateAbsentDates = res.absentDates ?? [];

            if (oldLog && oldLog.mark === "PRESENT") {
                return oldLog;
            } else if (oldLog && oldLog.mark === "ABSENT") {
                console.log("absent -> present ", isLate);

                const meta = {
                    previousMark: oldLog.mark,
                    previousLogTime: oldLog.attendanceDate,
                };
                oldLog.mark = "PRESENT";
                oldLog.attendanceDate = logTime;
                oldLog.deviceLogId = log._id;
                oldLog.meta = meta;
                if (backDateAbsentCount) {
                    oldLog.backDateAbsentCount = backDateAbsentCount;
                    oldLog.backDateAbsentDates = backDateAbsentDates;
                }

                if (isLate) {
                    oldLog.lateMinutes = isLate;
                }

                await oldLog.save();
                if (
                    moment(logTime).add(6, "hours").isAfter(moment()) &&
                    userBatch
                ) {
                    await this.sendPresentCallMessage(
                        oldLog.userId,
                        oldLog._id,
                        userBatch,
                        logTime,
                        userCourse?._id
                    );
                }

                //if present then remove from absent list
                const job = {
                    name: "call-list-create",
                    data: {
                        date: moment(logTime).format("YYYY-MM-DD"),
                        userId: `${userCourse.user}`,
                        course,
                        batchId: `${userCourse.batch}`,
                        isPresent: 1,
                    },
                } as Job;

                this.absentCallListQueue.add(job.name, job.data, {
                    attempts: 1,
                });

                return oldLog;
            } else {
                const toadd = {
                    userId: userCourse.user,
                    courseId: userCourse.course,
                    attendanceDate: logTime,
                    deviceLogId: log._id,
                    batchId: userCourse.batch,
                    userCourseId: userCourse._id,
                    mark: "PRESENT",
                } as AttendanceDocument;

                if (isLate) {
                    toadd.lateMinutes = isLate;
                }
                if (backDateAbsentCount) {
                    toadd.backDateAbsentCount = backDateAbsentCount;
                    toadd.backDateAbsentDates = backDateAbsentDates;
                }
                const alog = await this.create(toadd);
                await this.userCourseService.update({id:userCourse._id,attendanceDate:logTime})
            
                if (
                    moment(logTime).add(6, "hours").isAfter(moment()) &&
                    userBatch
                ) {
                    console.log("ccc");
                    await this.sendPresentCallMessage(
                        alog.userId,
                        alog._id,
                        userBatch,
                        logTime,
                        userCourse?._id
                    );
                }
                return alog;
            }
        }
        return true;
    }
Editor is loading...
Leave a Comment