Skip to content

Commit

Permalink
fix: resolved conflicts and env
Browse files Browse the repository at this point in the history
  • Loading branch information
Bunyawat Naunnak committed Aug 19, 2024
2 parents dce5727 + 3e028c3 commit 8b9fe7c
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 7 deletions.
Binary file added bun.lockb
Binary file not shown.
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
20 changes: 13 additions & 7 deletions src/app/api/calendar/route.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { type NextRequest, NextResponse } from "next/server";
import { google } from "googleapis";
import { type calendar_v3, google } from "googleapis";
import { GOOGLE_CLIENT_EMAIL, GOOGLE_PRIVATE_KEY } from "~/lib/constant";

const auth = new google.auth.GoogleAuth({
credentials: {
client_email: "calendar-pussadusmocu@cryptic-ground-432818-s9.iam.gserviceaccount.com",
private_key: "-----BEGIN PRIVATE KEY-----\nMIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDPRkCI7VenvJ7G\njw+q3tmKC7xo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oe3NOoCeyRyqmgBZ049FlVORGqfMOeNssE14tJlYGA/uR\nfsF9PGlr+YMuDjH2v6Uv6cpQAFctstkBNE1NUANscYt+ErylcNNUijP/pQbmL2YF\neIY68tw9PheVLtTF5avl4Fpjni9uLhBqDsP5SiQ5XG/cXj1aTrT6UHvuNwKBgGuL\nj2f+n/v+Znia8IR1Fkz4ViBwbJsLoNydmZpqyavBvOmvd21dkhwDa7SBm6G5QrRw\nSi4it25DGlBehMC3771n0QwzRemXsD+1njR54bslfhXizFjTv6wrY1smO3xfs8Ey\nOTxWGOxG9IhcsgnL9cn0IOJODCxrNMVC+0tY67IJAoGBALb9QOJH5Kf9HTrKcuWv\nEG/OfryxxZZCdDdt7MCc65AB46OpJ3yP4f4LOBp/x3HCDd878xFak8s+AWZ8NCsq\nrUfj4Cexb6TLn5njuoficEjH8A72rFcmlOkJLS+0pXlxPOKnZTTD0A3vhAO9CLST\ndEr6F676WUlzS56HnrAZC4gO\n-----END PRIVATE KEY-----\n",
client_email: GOOGLE_CLIENT_EMAIL,
private_key: GOOGLE_PRIVATE_KEY,
},
scopes: "https://www.googleapis.com/auth/calendar",
});
Expand All @@ -17,15 +18,20 @@ const getGoogleCalendar = (client: any) =>
auth: client,
});

export async function GET(_req: NextRequest) {
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 = await calendar.calendarList.list();
return NextResponse.json({ calendar: data.data});
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();
}
}
}
2 changes: 2 additions & 0 deletions src/lib/constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,3 +267,5 @@ export const DeeAppSecret = process.env.DEE_APP_SECRET ?? "";
export const encryptionKey = process.env.ENCRYPTION_KEY ?? "";
export const algorithm = process.env.ALGORITHM ?? "";
export const LINE_TOKEN = process.env.LINE_TOKEN ?? "";
export const GOOGLE_PRIVATE_KEY = process.env.GOOGLE_PRIVATE_KEY ?? "";
export const GOOGLE_CLIENT_EMAIL = process.env.GOOGLE_CLIENT_EMAIL ?? "";
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 8b9fe7c

Please sign in to comment.