diff --git a/client/public/images/avatar.jpg b/client/public/images/avatar.jpg deleted file mode 100644 index 8c8cfa8..0000000 Binary files a/client/public/images/avatar.jpg and /dev/null differ diff --git a/client/public/images/no-avatar.png b/client/public/images/no-avatar.png new file mode 100644 index 0000000..07e6f5a Binary files /dev/null and b/client/public/images/no-avatar.png differ diff --git a/client/src/pages/DashboardPage.tsx b/client/src/pages/DashboardPage.tsx index dc51032..86bca1e 100644 --- a/client/src/pages/DashboardPage.tsx +++ b/client/src/pages/DashboardPage.tsx @@ -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 diff --git a/server/src/config/db.ts b/server/src/config/db.ts index 81b9319..06cc0c9 100644 --- a/server/src/config/db.ts +++ b/server/src/config/db.ts @@ -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) { diff --git a/server/src/index.ts b/server/src/index.ts index 002f2a9..db6464d 100644 --- a/server/src/index.ts +++ b/server/src/index.ts @@ -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}`); diff --git a/server/src/models/Build.ts b/server/src/models/Build.ts index 0c00d9f..3b031c2 100644 --- a/server/src/models/Build.ts +++ b/server/src/models/Build.ts @@ -11,6 +11,11 @@ const buildSchema = new Schema( required: true, unique: true, }, + build: { + type: String, + required: true, + unique: true, + }, sessionLimit: { type: Number, required: true, diff --git a/server/src/routes/scheduledSessions.ts b/server/src/routes/scheduledSessions.ts index 58b9f47..cd79b8e 100644 --- a/server/src/routes/scheduledSessions.ts +++ b/server/src/routes/scheduledSessions.ts @@ -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;