upd
This commit is contained in:
Binary file not shown.
|
Before Width: | Height: | Size: 2.6 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 585 B |
@@ -78,8 +78,6 @@ function DashboardPage() {
|
||||
);
|
||||
|
||||
if (foundSchedule) {
|
||||
console.log("foundSchedule", foundSchedule);
|
||||
|
||||
const startDateTime = parse(
|
||||
foundSchedule.startTime,
|
||||
"HH:mm",
|
||||
@@ -138,8 +136,6 @@ function DashboardPage() {
|
||||
}
|
||||
|
||||
async function getManagers() {
|
||||
console.log("getManagers");
|
||||
|
||||
if (!company || !selectedBuild) {
|
||||
return;
|
||||
}
|
||||
@@ -236,8 +232,6 @@ function DashboardPage() {
|
||||
)
|
||||
.json();
|
||||
|
||||
console.log(scheduledSessions, result);
|
||||
|
||||
setScheduledSessions(
|
||||
scheduledSessions.map((scheduledSession) =>
|
||||
scheduledSession.id === result.id ? result : scheduledSession
|
||||
|
||||
@@ -2,7 +2,7 @@ import { connect } from "mongoose";
|
||||
|
||||
async function connectDB() {
|
||||
try {
|
||||
await connect(process.env.MONGO_URI!, { dbName: "test2" });
|
||||
await connect(process.env.MONGO_URI!, { dbName: "crm_stream" });
|
||||
console.log("MongoDB connected...");
|
||||
} catch (error) {
|
||||
if (error instanceof Error) {
|
||||
|
||||
+1
-1
@@ -23,11 +23,11 @@ app.use(cors());
|
||||
app.use("/login", loginRouter);
|
||||
app.use("/registration", registrationRouter);
|
||||
app.use("/actions", actionsRouter);
|
||||
app.use("/scheduled_sessions", scheduledSessionsRouter);
|
||||
app.use("/app", authMiddleware, appRouter);
|
||||
app.use("/companies", authMiddleware, companiesRouter);
|
||||
app.use("/users", authMiddleware, usersRouter);
|
||||
app.use("/builds", authMiddleware, buildsRouter);
|
||||
app.use("/scheduled_sessions", authMiddleware, scheduledSessionsRouter);
|
||||
|
||||
app.listen(port, () => {
|
||||
console.log(`Server listening on port ${port}`);
|
||||
|
||||
@@ -11,6 +11,11 @@ const buildSchema = new Schema(
|
||||
required: true,
|
||||
unique: true,
|
||||
},
|
||||
build: {
|
||||
type: String,
|
||||
required: true,
|
||||
unique: true,
|
||||
},
|
||||
sessionLimit: {
|
||||
type: Number,
|
||||
required: true,
|
||||
|
||||
@@ -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