upd
This commit is contained in:
@@ -1,12 +1,98 @@
|
||||
import { Router } from "express";
|
||||
import ScheduledSession from "../models/ScheduledSession.js";
|
||||
import Build from "../models/Build.js";
|
||||
import Schedule from "../models/Schedule.js";
|
||||
import { addMinutes, endOfDay, parseISO, startOfDay } from "date-fns";
|
||||
|
||||
const scheduledSessionsRouter = Router();
|
||||
|
||||
scheduledSessionsRouter.get("/", async (_req, res) => {
|
||||
await ScheduledSession.find();
|
||||
scheduledSessionsRouter.get("/:buildId", async (req, res) => {
|
||||
if (!req.params.buildId) {
|
||||
res.json({ error: "Parameter `buildId` is required!" });
|
||||
return;
|
||||
}
|
||||
|
||||
res.json({ ok: 1 });
|
||||
if (!req.query.date) {
|
||||
res.json({ error: "Query parameter `date` is required!" });
|
||||
return;
|
||||
}
|
||||
|
||||
const buildId = req.params.buildId;
|
||||
const date = req.query.date as string;
|
||||
const scheduledSessions = await ScheduledSession.find({
|
||||
buildId,
|
||||
startAt: {
|
||||
$gte: startOfDay(parseISO(date)),
|
||||
$lt: endOfDay(parseISO(date)),
|
||||
},
|
||||
});
|
||||
|
||||
res.json(scheduledSessions);
|
||||
});
|
||||
|
||||
scheduledSessionsRouter.post("/", async (req, res) => {
|
||||
if (!req.body.companyId) {
|
||||
res.json({ error: "Parameter `companyId` is required!" });
|
||||
return;
|
||||
}
|
||||
|
||||
if (!req.body.buildId) {
|
||||
res.json({ error: "Parameter `buildId` is required!" });
|
||||
return;
|
||||
}
|
||||
|
||||
if (!req.body.startAt) {
|
||||
res.json({ error: "Parameter `startAt` is required!" });
|
||||
return;
|
||||
}
|
||||
|
||||
if (!req.body.client) {
|
||||
res.json({ error: "Parameter `client` is required!" });
|
||||
return;
|
||||
}
|
||||
|
||||
const { companyId, buildId, startAt, client } = req.body;
|
||||
|
||||
const build = await Build.findById(buildId);
|
||||
const schedule = await Schedule.findOne({ companyId, buildId });
|
||||
const scheduledSessions = await ScheduledSession.find({
|
||||
companyId,
|
||||
buildId,
|
||||
startAt,
|
||||
});
|
||||
|
||||
if (!build) {
|
||||
res.json({ error: "`Build` not found in the database!" });
|
||||
return;
|
||||
}
|
||||
|
||||
if (scheduledSessions.length === build.sessionLimit) {
|
||||
res.json({
|
||||
error: "No slots available!",
|
||||
scheduledSessionsLength: scheduledSessions.length,
|
||||
buildSessionLimit: build.sessionLimit,
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
if (!schedule) {
|
||||
res.json({ error: "`Schedule` not found in the database!" });
|
||||
return;
|
||||
}
|
||||
|
||||
const endAt = addMinutes(parseISO(startAt), schedule.sessionDuration);
|
||||
|
||||
const scheduleSession = await ScheduledSession.create({
|
||||
companyId,
|
||||
buildId,
|
||||
startAt,
|
||||
endAt,
|
||||
clientName: client.name,
|
||||
clientEmail: client.email,
|
||||
clientPhone: client.phone,
|
||||
});
|
||||
|
||||
res.json({ ok: 1, scheduleSession });
|
||||
});
|
||||
|
||||
export default scheduledSessionsRouter;
|
||||
|
||||
Reference in New Issue
Block a user