upd
This commit is contained in:
+3
-3
@@ -21,15 +21,15 @@ connectDB();
|
||||
app.use(json());
|
||||
app.use(cors({ origin: "*" }));
|
||||
|
||||
app.use("/app", authMiddleware, appRouter);
|
||||
app.use("/companies", authMiddleware, companiesRouter);
|
||||
app.use("/users", authMiddleware, usersRouter);
|
||||
app.use("/login", loginRouter);
|
||||
app.use("/registration", registrationRouter);
|
||||
app.use("/actions", actionsRouter);
|
||||
app.use("/builds", buildsRouter);
|
||||
app.use("/scheduled_sessions", scheduledSessionsRouter);
|
||||
app.use("/schedules", schedulesRouter);
|
||||
app.use("/app", authMiddleware, appRouter);
|
||||
app.use("/companies", authMiddleware, companiesRouter);
|
||||
app.use("/users", authMiddleware, usersRouter);
|
||||
|
||||
app.listen(port, () => {
|
||||
console.log(`Server listening on port ${port}`);
|
||||
|
||||
@@ -258,9 +258,6 @@ router.get(
|
||||
async (req, res) => {
|
||||
const { companyId, buildId } = req.params;
|
||||
|
||||
console.log("companyId", companyId);
|
||||
console.log("buildId", buildId);
|
||||
|
||||
try {
|
||||
const lastScheduledSession = await ScheduledSession.findOne({
|
||||
companyId,
|
||||
|
||||
@@ -5,13 +5,11 @@ import Schedule from "../models/Schedule";
|
||||
import {
|
||||
addMinutes,
|
||||
areIntervalsOverlapping,
|
||||
differenceInMinutes,
|
||||
endOfDay,
|
||||
isValid,
|
||||
parseISO,
|
||||
startOfDay,
|
||||
} from "date-fns";
|
||||
import { isValidObjectId } from "mongoose";
|
||||
|
||||
const scheduledSessionsRouter = Router();
|
||||
|
||||
@@ -26,50 +24,33 @@ scheduledSessionsRouter.get("/", async (_req, res) => {
|
||||
|
||||
scheduledSessionsRouter.get("/:id", async (req, res) => {
|
||||
const scheduledSessionId = req.params.id;
|
||||
|
||||
if (!isValidObjectId(scheduledSessionId)) {
|
||||
return res.json({
|
||||
status: "error",
|
||||
message: "Invalid session ID value",
|
||||
});
|
||||
}
|
||||
|
||||
const scheduledSession = await ScheduledSession.findById(req.params.id);
|
||||
const scheduledSession = await ScheduledSession.findById(scheduledSessionId);
|
||||
|
||||
res.json(scheduledSession);
|
||||
});
|
||||
|
||||
scheduledSessionsRouter.get("/builds/:buildId", async (req, res) => {
|
||||
if (!req.params.buildId) {
|
||||
res.json({ error: "Parameter `buildId` is required" });
|
||||
return;
|
||||
scheduledSessionsRouter.get(
|
||||
"/companies/:companyId/builds/:buildId",
|
||||
async (req, res) => {
|
||||
const { companyId, buildId } = req.params;
|
||||
const { date } = req.query;
|
||||
|
||||
try {
|
||||
const scheduledSessions = await ScheduledSession.find({
|
||||
companyId,
|
||||
buildId,
|
||||
startAt: {
|
||||
$gte: startOfDay(parseISO(date as string)),
|
||||
$lt: endOfDay(parseISO(date as string)),
|
||||
},
|
||||
});
|
||||
|
||||
res.json(scheduledSessions);
|
||||
} catch (error) {
|
||||
res.json({ error: (error as Error).message });
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
if (!isValidObjectId(buildId)) {
|
||||
return res.json({
|
||||
status: "error",
|
||||
message: "Invalid build ID value",
|
||||
});
|
||||
}
|
||||
|
||||
const scheduledSessions = await ScheduledSession.find({
|
||||
buildId,
|
||||
startAt: {
|
||||
$gte: startOfDay(parseISO(date)),
|
||||
$lt: endOfDay(parseISO(date)),
|
||||
},
|
||||
});
|
||||
|
||||
res.json(scheduledSessions);
|
||||
});
|
||||
);
|
||||
|
||||
scheduledSessionsRouter.get("/:buildId", async (req, res) => {
|
||||
if (!req.params.buildId) {
|
||||
@@ -98,13 +79,6 @@ scheduledSessionsRouter.get("/:buildId", async (req, res) => {
|
||||
scheduledSessionsRouter.post("/", async (req, res) => {
|
||||
const { buildId, startAt, client, duration } = req.body;
|
||||
|
||||
if (!isValidObjectId(buildId)) {
|
||||
return res.json({
|
||||
status: "error",
|
||||
message: "Invalid build ID value",
|
||||
});
|
||||
}
|
||||
|
||||
if (!buildId || !startAt) {
|
||||
return res.json({
|
||||
status: "error",
|
||||
@@ -183,7 +157,7 @@ scheduledSessionsRouter.post("/", async (req, res) => {
|
||||
const schedule = await Schedule.findOne({
|
||||
buildId,
|
||||
startDate: { $lte: startAtISO },
|
||||
endDate: { $gte: startAtISO },
|
||||
// endDate: { $gte: startAtISO },
|
||||
});
|
||||
|
||||
if (!schedule) {
|
||||
@@ -246,16 +220,13 @@ scheduledSessionsRouter.post("/", async (req, res) => {
|
||||
scheduledSessionsRouter.put("/:id", async (req, res) => {
|
||||
const scheduledSessionId = req.params.id;
|
||||
|
||||
if (!isValidObjectId(scheduledSessionId)) {
|
||||
return res.json({
|
||||
status: "error",
|
||||
message: "Invalid session ID value",
|
||||
});
|
||||
}
|
||||
let {
|
||||
startAt,
|
||||
endAt,
|
||||
activeSessionId,
|
||||
}: { startAt: string; endAt: string; activeSessionId: string } = req.body;
|
||||
|
||||
let { startAt, duration }: { startAt: string; duration: number } = req.body;
|
||||
|
||||
if (!startAt && !duration) {
|
||||
if (!startAt && !endAt) {
|
||||
return res.json({
|
||||
status: "error",
|
||||
message: "Parameter `startAt` or `duration` is required, or both",
|
||||
@@ -269,16 +240,9 @@ scheduledSessionsRouter.put("/:id", async (req, res) => {
|
||||
});
|
||||
}
|
||||
|
||||
function isInteger(num: number) {
|
||||
return (num ^ 0) === num;
|
||||
}
|
||||
|
||||
if (duration && !isInteger(duration)) {
|
||||
return res.json({
|
||||
status: "error",
|
||||
message: "Parameter `duration` is not an integer",
|
||||
});
|
||||
}
|
||||
// function isInteger(num: number) {
|
||||
// return (num ^ 0) === num;
|
||||
// }
|
||||
|
||||
const scheduledSession = await ScheduledSession.findById(scheduledSessionId);
|
||||
|
||||
@@ -289,22 +253,10 @@ scheduledSessionsRouter.put("/:id", async (req, res) => {
|
||||
});
|
||||
}
|
||||
|
||||
if (!duration) {
|
||||
duration = differenceInMinutes(
|
||||
scheduledSession.endAt,
|
||||
scheduledSession.startAt
|
||||
);
|
||||
}
|
||||
|
||||
const endAt = addMinutes(
|
||||
(startAt && parseISO(startAt.toString())) || scheduledSession.startAt,
|
||||
duration
|
||||
);
|
||||
|
||||
try {
|
||||
const scheduledSession = await ScheduledSession.findByIdAndUpdate(
|
||||
scheduledSessionId,
|
||||
{ startAt, endAt },
|
||||
{ startAt, endAt, activeSessionId },
|
||||
{ new: true }
|
||||
);
|
||||
|
||||
@@ -319,13 +271,6 @@ scheduledSessionsRouter.put("/:id", async (req, res) => {
|
||||
scheduledSessionsRouter.delete("/:id", async (req, res) => {
|
||||
const scheduledSessionId = req.params.id;
|
||||
|
||||
if (!isValidObjectId(scheduledSessionId)) {
|
||||
return res.json({
|
||||
status: "error",
|
||||
message: "Invalid session ID value",
|
||||
});
|
||||
}
|
||||
|
||||
try {
|
||||
await ScheduledSession.findByIdAndDelete(scheduledSessionId);
|
||||
|
||||
|
||||
@@ -3,33 +3,36 @@ import Schedule from "../models/Schedule";
|
||||
|
||||
const schedulesRouter = Router();
|
||||
|
||||
schedulesRouter.get("/builds/:buildId", async (req, res) => {
|
||||
const buildId = req.params.buildId;
|
||||
const date = new Date();
|
||||
schedulesRouter.get(
|
||||
"/companies/:companyId/builds/:buildId",
|
||||
async (req, res) => {
|
||||
const { companyId, buildId } = req.params;
|
||||
const date = new Date();
|
||||
|
||||
const schedules = await Schedule.find({
|
||||
buildId,
|
||||
startDate: { $lte: date },
|
||||
endDate: { $gte: date },
|
||||
});
|
||||
|
||||
if (!schedules.length) {
|
||||
res.json({ error: "No data" });
|
||||
return;
|
||||
}
|
||||
|
||||
if (req.query.date) {
|
||||
const schedule = await Schedule.findOne({
|
||||
const schedules = await Schedule.find({
|
||||
companyId,
|
||||
buildId,
|
||||
startDate: { $lte: date },
|
||||
endDate: { $gte: date },
|
||||
});
|
||||
|
||||
res.json(schedule);
|
||||
return;
|
||||
}
|
||||
if (!schedules.length) {
|
||||
res.json({ error: "No data" });
|
||||
return;
|
||||
}
|
||||
|
||||
res.json(schedules);
|
||||
});
|
||||
if (req.query.date) {
|
||||
const schedule = await Schedule.findOne({
|
||||
companyId,
|
||||
buildId,
|
||||
startDate: { $lte: date },
|
||||
});
|
||||
|
||||
res.json(schedule);
|
||||
return;
|
||||
}
|
||||
|
||||
res.json(schedules);
|
||||
}
|
||||
);
|
||||
|
||||
export default schedulesRouter;
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
"noUnusedLocals": true,
|
||||
"noUnusedParameters": true,
|
||||
"noFallthroughCasesInSwitch": true,
|
||||
"skipLibCheck": true
|
||||
"skipLibCheck": true,
|
||||
},
|
||||
"ts-node": {
|
||||
"transpileOnly": true
|
||||
|
||||
Reference in New Issue
Block a user