Skip to content

Commit

Permalink
Merge pull request #10 from smovidya/feat/calendar-pond
Browse files Browse the repository at this point in the history
feat: calendar
  • Loading branch information
Phongsakorn0 authored Aug 19, 2024
2 parents ea7b8da + df8694a commit 3e028c3
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 1 deletion.
Binary file modified bun.lockb
Binary file not shown.
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ services:
mysql:
image: mysql:8.0
ports:
- 3309:3306
- 3305:3306
volumes:
- ~/apps/mysql:/var/lib/mysql
environment:
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"compressorjs": "^1.2.1",
"cookies-next": "^4.2.1",
"date-fns": "^3.6.0",
"googleapis": "^140.0.1",
"i": "^0.3.7",
"jose": "^5.6.3",
"jsonwebtoken": "^9.0.2",
Expand Down
20 changes: 20 additions & 0 deletions src/app/_components/parcels/Parcel.user.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,26 @@ const ParcelUser = ({ parcel, project_id, student_id }: BlogProps) => {
endDate: _date?.to ?? new Date(),
project_id: data.project_id,
});
//TODO: Code here
if (_date?.from !== undefined && _date?.to !== undefined) {
const event = {
summary: student_id,
description:
parcel?.title + " " + data.description + " จำนวน " + data.amount,
start: {
date: _date?.from.toISOString().split("T")[0],
timezone: "Asia/Bangkok",
},
end: {
date: _date?.to.toISOString().split("T")[0],
timezone: "Asia/Bangkok",
},
};
await fetch("/api/calendar", {
method: "POST",
body: JSON.stringify(event),
});
}
}

return (
Expand Down
37 changes: 37 additions & 0 deletions src/app/api/calendar/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { type NextRequest, NextResponse } from "next/server";
import { calendar_v3, google } from "googleapis";

const auth = new google.auth.GoogleAuth({
credentials: {
client_email: "[email protected]",
private_key:
"-----BEGIN PRIVATE KEY-----\nMIIEvgIBADANBgkqhki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iL+VOYHx4TAYkAdkDHJpANh\nWO2IeHHMCwKBgAwcuNnfd5OWUc5O6EPYaF0QZif5DqAsGG8XXnu7UkP0fkBUgCpZ\nDFym6oEZxnx5bY6POk5LMgtgFQwMZWA8dOIrmisrw7rWKbC+9t4Pj+YHylevyZEj\ne5NeN+sNW2ly+xAx2hePo81lbgEJ7ior7nVkMEKdEdfqMSyaUEwZ1Wo/AoGBAJ6x\nNeKmiWdjoZp7JNzSaG1jPqkQ7nHGL/RA0wF0tBI/BE7K0Zbm/Jd8KVZOb6rwlC9p\ndYKwaE00uRTXlmAimD46L4JKuRTOhAbZZxc574pZvV4fKDClip21Ove0Wl1h68CL\n4qbLQKebtwcKh21KsOsx79R9WraJ/RO6wB8QMjyZAoGBAO5NxOehcwmRzP0k4z9z\nXtWL8Ej9kvxdWGg1Jf4THZke6i8gLoY5t9Qqm7js4WrTwI2WO2BD/7YJji5RGGW9\nyc814RCDms2SqWdmVr6d8OdVqJOVv7nGPW7p0FLk5CFci5FoIz/GBFmKBrr2mD2r\nuvVe61diKxJl+ue04y+ikDnj\n-----END PRIVATE KEY-----\n",
},
scopes: "https://www.googleapis.com/auth/calendar",
});

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const getGoogleCalendar = (client: any) =>
google.calendar({
version: "v3",
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
auth: client,
});

export async function POST(req: NextRequest) {
try {
const body = (await req.json()) as calendar_v3.Schema$Event | undefined;
const client = await auth.getClient();
const calendar = getGoogleCalendar(client);
console.log("Connected to Google client 🚀");
const data = calendar.events.insert({
auth: auth,
calendarId: "[email protected]",
requestBody: body,
});
return NextResponse.json({ calendar: data });
} catch (error) {
console.error("Error connecting to Google client: ", error);
return NextResponse.error();
}
}
1 change: 1 addition & 0 deletions src/lib/function.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { SignJWT, jwtVerify } from "jose";
import { encryptionKey, LINE_TOKEN, STUDENT_ID } from "./constant";
import { type Student } from "@prisma/client";
// import { google } from 'googleapis';

/**
* Encrypts data into a JWT token.
Expand Down
17 changes: 17 additions & 0 deletions src/server/api/routers/parcel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ export const parcelRouter = createTRPCRouter({
if (input.amount > amount) {
throw new Error("Requested amount exceeds available amount");
}
console.log("LOG", input);
await tx.parcel_Project.create({
data: {
student_id: input.student_id,
Expand Down Expand Up @@ -182,6 +183,22 @@ export const parcelRouter = createTRPCRouter({
input.startDate?.toDateString() ?? "",
);
}
//TODO: Code here
const event = {
summary: student?.name,
description: parcel?.title,
start: input.startDate,
end: input.endDate,
};
await fetch("https://www.googleapis.com/calendar/v3/calendars", {
method: "POST",
headers: {
Authorization: "Bearer",
},
body: JSON.stringify(event),
}).then((data) => {
console.log(data);
});
});
}),

Expand Down

0 comments on commit 3e028c3

Please sign in to comment.