From eb42807b93bebc627738261e09c17610aa1c7e6d Mon Sep 17 00:00:00 2001 From: Rafael Violato Date: Thu, 18 Jan 2024 20:43:29 +0100 Subject: [PATCH] fix: api route convention error --- src/app/api/log-event/route.ts | 42 +++++++++++++++++++++++++++ src/app/api/log-home-arrive/route.ts | 22 ++++++++++++++ src/app/api/log-home-leave/route.ts | 22 ++++++++++++++ src/app/api/log-work-arrive/route.ts | 22 ++++++++++++++ src/app/api/log-work-leave/route.ts | 20 +++++++++++++ src/app/constants.ts | 8 +++--- src/app/gql/lib/db.ts | 7 +++-- src/app/log-event/route.ts | 23 --------------- src/app/log-home-arrive/route.ts | 43 ---------------------------- src/app/log-home-leave/route.ts | 23 --------------- src/app/log-work-arrive/route.ts | 23 --------------- src/app/log-work-leave/route.ts | 23 --------------- 12 files changed, 136 insertions(+), 142 deletions(-) create mode 100644 src/app/api/log-event/route.ts create mode 100644 src/app/api/log-home-arrive/route.ts create mode 100644 src/app/api/log-home-leave/route.ts create mode 100644 src/app/api/log-work-arrive/route.ts create mode 100644 src/app/api/log-work-leave/route.ts delete mode 100644 src/app/log-event/route.ts delete mode 100644 src/app/log-home-arrive/route.ts delete mode 100644 src/app/log-home-leave/route.ts delete mode 100644 src/app/log-work-arrive/route.ts delete mode 100644 src/app/log-work-leave/route.ts diff --git a/src/app/api/log-event/route.ts b/src/app/api/log-event/route.ts new file mode 100644 index 0000000..e0e0524 --- /dev/null +++ b/src/app/api/log-event/route.ts @@ -0,0 +1,42 @@ +import moment from "moment"; +import { createDbClient } from "../../gql/lib/db"; +import { DAY_FORMAT } from "../../gql/constants"; +import { NextRequest, NextResponse } from "next/server"; + +export const dynamic = "force-dynamic"; + +async function logEventHandler(request: NextRequest) { + const { date, event } = await request.json(); + const momentDate = moment(date).utc(); + + if (!event) { + return NextResponse.json({ error: "Date is missing" }, { status: 400 }); + } + + if (!date) { + return NextResponse.json({ error: "Event is missing" }, { status: 400 }); + } + + try { + const db = await createDbClient(); + const day = momentDate.format(DAY_FORMAT); + + await db.workTimetable.updateOne( + { day: { $eq: day } }, + { + $push: { events: event }, + $setOnInsert: { + date: new Date(momentDate.toISOString()), + day, + }, + }, + { upsert: true } + ); + + return NextResponse.json({}, { status: 200 }); + } catch (exception) { + return NextResponse.json({ error: exception }, { status: 500 }); + } +} + +export { logEventHandler as POST }; diff --git a/src/app/api/log-home-arrive/route.ts b/src/app/api/log-home-arrive/route.ts new file mode 100644 index 0000000..952cb3e --- /dev/null +++ b/src/app/api/log-home-arrive/route.ts @@ -0,0 +1,22 @@ +import { logTime } from "../../gql/utils/log-time"; +import { NextRequest, NextResponse } from "next/server"; + +export const dynamic = "force-dynamic"; + +async function logHomeArriveHandler(request: NextRequest) { + const { date } = await request.json(); + + if (!date) { + return NextResponse.json({ error: "Date is missing" }, { status: 400 }); + } + + try { + await logTime(date, "homeArriveTime"); + + return NextResponse.json({}, { status: 200 }); + } catch (exception) { + return NextResponse.json({ error: exception }, { status: 500 }); + } +} + +export { logHomeArriveHandler as POST }; diff --git a/src/app/api/log-home-leave/route.ts b/src/app/api/log-home-leave/route.ts new file mode 100644 index 0000000..764aa72 --- /dev/null +++ b/src/app/api/log-home-leave/route.ts @@ -0,0 +1,22 @@ +import { logTime } from "../../gql/utils/log-time"; +import { NextRequest, NextResponse } from "next/server"; + +export const dynamic = "force-dynamic"; + +async function logHomeLeaveHandler(request: NextRequest) { + const { date } = await request.json(); + + if (!date) { + return NextResponse.json({ error: "Date is missing" }, { status: 400 }); + } + + try { + await logTime(date, "homeLeaveTime"); + + return NextResponse.json({}, { status: 200 }); + } catch (exception) { + return NextResponse.json({ error: exception }, { status: 500 }); + } +} + +export { logHomeLeaveHandler as POST }; diff --git a/src/app/api/log-work-arrive/route.ts b/src/app/api/log-work-arrive/route.ts new file mode 100644 index 0000000..6e06ddc --- /dev/null +++ b/src/app/api/log-work-arrive/route.ts @@ -0,0 +1,22 @@ +import { logTime } from "../../gql/utils/log-time"; +import { NextRequest, NextResponse } from "next/server"; + +export const dynamic = "force-dynamic"; + +async function logWorkArriveHandler(request: NextRequest) { + const { date } = await request.json(); + + if (!date) { + return NextResponse.json({ error: "Date is missing" }, { status: 400 }); + } + + try { + await logTime(date, "workArriveTime"); + + return NextResponse.json({}, { status: 200 }); + } catch (exception) { + return NextResponse.json({ error: exception }, { status: 500 }); + } +} + +export { logWorkArriveHandler as POST }; diff --git a/src/app/api/log-work-leave/route.ts b/src/app/api/log-work-leave/route.ts new file mode 100644 index 0000000..57e7c05 --- /dev/null +++ b/src/app/api/log-work-leave/route.ts @@ -0,0 +1,20 @@ +import { logTime } from "../../gql/utils/log-time"; +import { NextRequest, NextResponse } from "next/server"; + +async function logWorkLeaveHandler(request: NextRequest) { + const { date } = await request.json(); + + if (!date) { + return NextResponse.json({ error: "Date is missing" }, { status: 400 }); + } + + try { + await logTime(date, "workLeaveTime"); + + return NextResponse.json({}, { status: 200 }); + } catch (exception) { + return NextResponse.json({ error: exception }, { status: 500 }); + } +} + +export { logWorkLeaveHandler as POST }; diff --git a/src/app/constants.ts b/src/app/constants.ts index 4bbdf55..da823e9 100644 --- a/src/app/constants.ts +++ b/src/app/constants.ts @@ -1,4 +1,4 @@ -export const IS_DEV = process.env.NODE_ENV === 'development'; -export const DEVELOPMENT_DAY = '2019-12-12'; -export const DATE_FORMAT = 'YYYY-MM-DD'; -export const MONTH_DATE_FORMAT = 'YYYY-MM'; +export const IS_DEV = process.env.NODE_ENV === "development"; +export const DEVELOPMENT_DAY = "2019-11-14"; +export const DATE_FORMAT = "YYYY-MM-DD"; +export const MONTH_DATE_FORMAT = "YYYY-MM"; diff --git a/src/app/gql/lib/db.ts b/src/app/gql/lib/db.ts index 47b8442..ade4513 100644 --- a/src/app/gql/lib/db.ts +++ b/src/app/gql/lib/db.ts @@ -7,9 +7,10 @@ const MONGODB_URL = `mongodb+srv://${username}:${password}@cluster0-uk48d.mongod export const createDbClient = async (): Promise => { try { - const client = await MongoClient.connect(MONGODB_URL, { - // useUnifiedTopology: true, - }); + const client = await MongoClient.connect(MONGODB_URL); + + console.log({ username }); + console.log({ password: password?.slice(0, 3) }); const db = client.db(process.env.DB_NAME); const workTimetable = db.collection("workTimetable"); diff --git a/src/app/log-event/route.ts b/src/app/log-event/route.ts deleted file mode 100644 index 0bd7faa..0000000 --- a/src/app/log-event/route.ts +++ /dev/null @@ -1,23 +0,0 @@ -import type { NextApiRequest, NextApiResponse } from "next"; -import { logTime } from "../gql/utils/log-time"; - -export default async function handler( - request: NextApiRequest, - response: NextApiResponse -) { - const { - body: { date }, - } = request; - - if (!date) { - return response.status(400).send({ error: "Date is missing" }); - } - - try { - await logTime(date, "homeArriveTime"); - - return response.status(200).end(); - } catch (exception) { - return response.status(500).send({ error: exception }); - } -} diff --git a/src/app/log-home-arrive/route.ts b/src/app/log-home-arrive/route.ts deleted file mode 100644 index b79c6dc..0000000 --- a/src/app/log-home-arrive/route.ts +++ /dev/null @@ -1,43 +0,0 @@ -import type { NextApiRequest, NextApiResponse } from "next"; -import moment from "moment"; -import { createDbClient } from "../gql/lib/db"; -import { DAY_FORMAT } from "../gql/constants"; - -export default async function handler( - request: NextApiRequest, - response: NextApiResponse -) { - const { - body: { event, date }, - } = request; - const momentDate = moment(date).utc(); - - if (!event) { - return response.status(400).send({ error: "Date is missing" }); - } - - if (!date) { - return response.status(400).send({ error: "Event is missing" }); - } - - try { - const db = await createDbClient(); - const day = momentDate.format(DAY_FORMAT); - - await db.workTimetable.updateOne( - { day: { $eq: day } }, - { - $push: { events: event }, - $setOnInsert: { - date: new Date(momentDate.toISOString()), - day, - }, - }, - { upsert: true } - ); - - return response.status(200).end(); - } catch (exception) { - return response.status(500).send({ error: exception }); - } -} diff --git a/src/app/log-home-leave/route.ts b/src/app/log-home-leave/route.ts deleted file mode 100644 index fd78f4a..0000000 --- a/src/app/log-home-leave/route.ts +++ /dev/null @@ -1,23 +0,0 @@ -import type { NextApiRequest, NextApiResponse } from "next"; -import { logTime } from "../gql/utils/log-time"; - -export default async function handler( - request: NextApiRequest, - response: NextApiResponse -) { - const { - body: { date }, - } = request; - - if (!date) { - return response.status(400).send({ error: "Date is missing" }); - } - - try { - await logTime(date, "homeLeaveTime"); - - return response.status(200).end(); - } catch (exception) { - return response.status(500).send({ error: exception }); - } -} diff --git a/src/app/log-work-arrive/route.ts b/src/app/log-work-arrive/route.ts deleted file mode 100644 index 1bd9222..0000000 --- a/src/app/log-work-arrive/route.ts +++ /dev/null @@ -1,23 +0,0 @@ -import type { NextApiRequest, NextApiResponse } from "next"; -import { logTime } from "../gql/utils/log-time"; - -export default async function handler( - request: NextApiRequest, - response: NextApiResponse -) { - const { - body: { date }, - } = request; - - if (!date) { - return response.status(400).send({ error: "Date is missing" }); - } - - try { - await logTime(date, "workArriveTime"); - - return response.status(200).end(); - } catch (exception) { - return response.status(500).send({ error: exception }); - } -} diff --git a/src/app/log-work-leave/route.ts b/src/app/log-work-leave/route.ts deleted file mode 100644 index 2b3246b..0000000 --- a/src/app/log-work-leave/route.ts +++ /dev/null @@ -1,23 +0,0 @@ -import type { NextApiRequest, NextApiResponse } from "next"; -import { logTime } from "../gql/utils/log-time"; - -export default async function handler( - request: NextApiRequest, - response: NextApiResponse -) { - const { - body: { date }, - } = request; - - if (!date) { - return response.status(400).send({ error: "Date is missing" }); - } - - try { - await logTime(date, "workLeaveTime"); - - return response.status(200).end(); - } catch (exception) { - return response.status(500).send({ error: exception }); - } -}