Skip to content

Commit

Permalink
fix: api route convention error
Browse files Browse the repository at this point in the history
  • Loading branch information
rfviolato committed Jan 21, 2024
1 parent a4ac0d3 commit eb42807
Show file tree
Hide file tree
Showing 12 changed files with 136 additions and 142 deletions.
42 changes: 42 additions & 0 deletions src/app/api/log-event/route.ts
Original file line number Diff line number Diff line change
@@ -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 };
22 changes: 22 additions & 0 deletions src/app/api/log-home-arrive/route.ts
Original file line number Diff line number Diff line change
@@ -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 };
22 changes: 22 additions & 0 deletions src/app/api/log-home-leave/route.ts
Original file line number Diff line number Diff line change
@@ -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 };
22 changes: 22 additions & 0 deletions src/app/api/log-work-arrive/route.ts
Original file line number Diff line number Diff line change
@@ -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 };
20 changes: 20 additions & 0 deletions src/app/api/log-work-leave/route.ts
Original file line number Diff line number Diff line change
@@ -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 };
8 changes: 4 additions & 4 deletions src/app/constants.ts
Original file line number Diff line number Diff line change
@@ -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";
7 changes: 4 additions & 3 deletions src/app/gql/lib/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ const MONGODB_URL = `mongodb+srv://${username}:${password}@cluster0-uk48d.mongod

export const createDbClient = async (): Promise<IDB> => {
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");
Expand Down
23 changes: 0 additions & 23 deletions src/app/log-event/route.ts

This file was deleted.

43 changes: 0 additions & 43 deletions src/app/log-home-arrive/route.ts

This file was deleted.

23 changes: 0 additions & 23 deletions src/app/log-home-leave/route.ts

This file was deleted.

23 changes: 0 additions & 23 deletions src/app/log-work-arrive/route.ts

This file was deleted.

23 changes: 0 additions & 23 deletions src/app/log-work-leave/route.ts

This file was deleted.

0 comments on commit eb42807

Please sign in to comment.