-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
commit to establish the first working layer of google calendar api
- Loading branch information
1 parent
28d015c
commit 18726e8
Showing
9 changed files
with
550 additions
and
163 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 16 additions & 51 deletions
67
src/components/events/calendar/AddToCalendarAuthorizedButton.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
|
||
import { z } from 'zod'; | ||
import { createTRPCRouter, publicProcedure } from '../trpc'; | ||
import { and, eq } from 'drizzle-orm'; | ||
import { accounts } from '@src/server/db/schema/users'; | ||
|
||
|
||
const byUserID = z.object({ | ||
userId: z.string(), | ||
provider: z.string() | ||
}) | ||
|
||
export const accountRouter = createTRPCRouter({ | ||
getToken: publicProcedure | ||
.input(byUserID) | ||
.query( async({input, ctx}) => { | ||
|
||
const { userId, provider } = input; | ||
|
||
try { | ||
const account = await ctx.db.query.accounts.findFirst({ | ||
where: and( | ||
eq( accounts.provider, provider ), | ||
eq( accounts.userId, userId ), | ||
) | ||
}) | ||
|
||
if ( ! account ) { | ||
console.error("Unable to find account in the database!") | ||
throw new Error("Unable to find Acccount in database!") | ||
} | ||
if ( ! account.access_token ) { | ||
console.log("Access token for user is empty!") | ||
account.access_token = "" | ||
} | ||
|
||
const token = { | ||
access_token: account.access_token, | ||
refresh_token: account.refresh_token, | ||
id_token: account.id_token, | ||
} | ||
|
||
return token | ||
} catch ( error ) { | ||
console.error("Error when trying to grab account from database: ", error) | ||
throw error | ||
} | ||
}), | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
|
||
import { z } from 'zod'; | ||
import { createTRPCRouter, publicProcedure } from '../trpc'; | ||
import { google } from 'googleapis'; | ||
import { type OAuth2Client } from 'google-auth-library'; | ||
import { env } from '@src/env.mjs'; | ||
|
||
const byEventDetails = z.object({ | ||
eventName: z.string(), | ||
startTime: z.date(), | ||
endTime: z.date(), | ||
location: z.string().nullable(), | ||
description: z.string(), | ||
tokens: z.object({ | ||
access_token: z.string(), | ||
refresh_token: z.string().nullable(), | ||
id_token: z.string().nullable(), | ||
}) | ||
|
||
}) | ||
|
||
export const calendarRouter = createTRPCRouter({ | ||
addEvent: publicProcedure | ||
.input(byEventDetails) | ||
.mutation( ( {input} ) => { | ||
console.log(input) | ||
const { eventName, startTime, endTime, location, tokens, description } = input; | ||
|
||
const oauth2Client : OAuth2Client = new google.auth.OAuth2({ | ||
clientId: env.GOOGLE_CLIENT_ID , | ||
clientSecret: env.GOOGLE_CLIENT_SECRET, | ||
redirectUri: env.NEXTAUTH_URL, | ||
}) | ||
|
||
|
||
oauth2Client.setCredentials( {access_token: tokens.access_token, refresh_token: tokens.refresh_token, id_token: tokens.id_token }); | ||
|
||
const calendar = google.calendar({ version: 'v3', auth: oauth2Client }); | ||
|
||
try { | ||
const response = calendar.events.insert({ | ||
calendarId: 'primary', | ||
requestBody: { | ||
summary: eventName, | ||
description: description, | ||
location: location, | ||
start: { | ||
dateTime: startTime | ||
}, | ||
end: { | ||
dateTime: endTime | ||
}, | ||
} | ||
}) | ||
|
||
return response | ||
} catch( error) { | ||
console.error("Recieved an error when trying to add event to calendar: ", e) | ||
throw error | ||
} | ||
|
||
|
||
|
||
}), | ||
}); |
Oops, something went wrong.