Files
2025-10-26 22:53:17 +08:00

128 lines
3.9 KiB
TypeScript

import express from 'express';
import type { Request, Response } from 'express';
import type { UploadedFile } from 'express-fileupload';
import path from 'path';
import fs from 'fs';
import axios from 'axios';
// import model
import { GuruModel } from '../models/guru_model'; // import GuruModel
// Function to calculate time conditions
interface TimeCheckResult {
isBefore: boolean;
isWithin: boolean;
isAfter: boolean;
secondsLeft?: number; // Optional, only present if isWithin is true
}
function checkTimeRange(
jam_mulai: string,
jam_selesai: string,
tanggal_kerja: Date
): TimeCheckResult {
const now = new Date(); // Current date and time: 01:19 AM WITA on October 08, 2025
const currentDateTime = new Date(now);
// Create start and end Date objects
const startDateTime = new Date(tanggal_kerja);
[startDateTime.setHours(Number(jam_mulai.split(':')[0])), startDateTime.setMinutes(Number(jam_mulai.split(':')[1]))];
const endDateTime = new Date(tanggal_kerja);
[endDateTime.setHours(Number(jam_selesai.split(':')[0])), endDateTime.setMinutes(Number(jam_selesai.split(':')[1]))];
// Check conditions
const isBefore = currentDateTime < startDateTime;
const isWithin = currentDateTime >= startDateTime && currentDateTime <= endDateTime;
const isAfter = currentDateTime > endDateTime;
// Calculate seconds left if within range
let secondsLeft: number | undefined;
if (isWithin) {
const timeDiff = endDateTime.getTime() - currentDateTime.getTime();
secondsLeft = Math.max(0, Math.floor(timeDiff / 1000)); // Ensure non-negative
}
return {
isBefore,
isWithin,
isAfter,
...(secondsLeft !== undefined && { secondsLeft }) // Only include secondsLeft if defined
};
}
const router = express.Router();
router.get('/', (req: Request, res: Response) => {
res.sendFile(path.join(__dirname, '../ui/guru_pages/index.html'));
});
router.post('/check', async (req: Request, res: Response) => {
const data = req.body;
// console.log(data);
try {
// find on SiswaModel by data because the data container all the data
const siswa = await GuruModel.findOne({ nig: data.nig, password: data.password, _id: data._id, no_telpon: data.no_telpon, jk: data.jk });
console.log("ada data");
return res.status(200).json("data found");
} catch (error) {
console.log(error);
return res.status(400).json("data not found");
}
// res.status(200).json("data not found");
})
router.get('/quiz', async (req: Request, res: Response) => {
res.sendFile(path.join(__dirname, '../ui/guru_pages/quiz.html'));
})
router.get('/tambah_soal', async (req: Request, res: Response) => {
res.sendFile(path.join(__dirname, '../ui/guru_pages/tambah_soal.html'));
})
router.get('/check_status_soal/:soal', async (req: Request, res: Response) => {
const soal = req.params.soal;
if (soal == "soal1") {
const result = checkTimeRange("8:50", "10:10", new Date("10-15-2025"));
if (result.isBefore)
return res.status(200).json({ label: "default", status: "Belum Waktunya", bool: true });
if (result.isAfter)
return res.status(200).json({ label: "default", status: "Sudah Waktunya", bool: true });
}
})
router.get('/soal1', async (req: Request, res: Response) => {
res.sendFile(path.join(__dirname, '../ui/guru_pages/soal1.html'));
})
router.post('/tambah_data_guru', async (req: Request, res: Response) => {
const data = {
"nig": "-",
"nama": "-",
"jk": "Perempuan",
"no_telpon": "-",
"password": "12345678",
"listmatapelajaran": [
{
"matpel": "Informatika"
},
{
"matpel": "Bahasa Indonesia"
}
]
}
const result = await GuruModel.create(data);
console.log(result);
res.status(200).json(result);
});
export default router;