Skip to content

Commit 63a3ed6

Browse files
committed
Before deploy
1 parent dee0d1a commit 63a3ed6

File tree

9 files changed

+629
-18
lines changed

9 files changed

+629
-18
lines changed

package-lock.json

+532
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"dev": "next dev",
77
"build": "next build",
88
"start": "next start",
9-
"lint": "next lint"
9+
"lint": "next lint -- --fix"
1010
},
1111
"dependencies": {
1212
"@hookform/resolvers": "^3.1.0",
@@ -20,6 +20,7 @@
2020
"dayjs": "^1.11.7",
2121
"eslint": "8.39.0",
2222
"eslint-config-next": "13.3.1",
23+
"googleapis": "^118.0.0",
2324
"next": "13.3.1",
2425
"next-auth": "^4.22.1",
2526
"nookies": "^2.5.2",

src/lib/google.ts

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import dayjs from 'dayjs'
2+
import { prisma } from './prisma'
3+
import { google } from 'googleapis'
4+
5+
export async function getGoogleOAuthToken(userId: string) {
6+
const account = await prisma.account.findFirstOrThrow({
7+
where: {
8+
provider: 'google',
9+
user_id: userId,
10+
},
11+
})
12+
const auth = new google.auth.OAuth2(
13+
process.env.GOOGLE_CLIENT_ID,
14+
process.env.GOOGLE_CLIENT_SECRETS,
15+
)
16+
17+
auth.setCredentials({
18+
access_token: account.access_token,
19+
refresh_token: account.refresh_token,
20+
expiry_date: account.expires_at ? account.expires_at * 1000 : null,
21+
})
22+
23+
if (!account.expires_at) {
24+
return auth
25+
}
26+
27+
const isTokenExpired = dayjs(account.expires_at * 1000).isBefore(new Date())
28+
29+
if (isTokenExpired) {
30+
const { credentials } = await auth.refreshAccessToken()
31+
const {
32+
access_token,
33+
expiry_date,
34+
id_token,
35+
refresh_token,
36+
scope,
37+
token_type,
38+
} = credentials
39+
40+
await prisma.account.update({
41+
where: {
42+
id: account.id,
43+
},
44+
data: {
45+
access_token,
46+
expires_at: expiry_date ? Math.floor(expiry_date / 1000) : null,
47+
id_token,
48+
refresh_token,
49+
scope,
50+
token_type,
51+
},
52+
})
53+
}
54+
return auth
55+
}

src/pages/_document.page.tsx

-13
This file was deleted.

src/pages/api/_document.page.tsx

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/* eslint-disable @next/next/no-page-custom-font */
2+
/* eslint-disable @next/next/no-document-import-in-page */
13
import { getCssText } from '@ignite-ui/react'
24
import { Head, Html, Main, NextScript } from 'next/document'
35

src/pages/api/auth/[...nextauth].api.ts

+3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ export function buildNextAuthOptions(
1616
clientSecret: process.env.GOOGLE_CLIENT_SECRET ?? '',
1717
authorization: {
1818
params: {
19+
prompt: 'consent',
20+
access_type: 'offline',
21+
response_type: 'code',
1922
scope:
2023
'https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/calendar',
2124
},

src/pages/api/users/[username]/availability.api.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export default async function handle(
5050

5151
const possibleTimes = Array.from({ length: endHour - startHour }).map(
5252
(_, i) => {
53-
return startHour + 1
53+
return startHour + i
5454
},
5555
)
5656

src/pages/api/users/[username]/schedule.api.ts

+32-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { prisma } from '@/lib/prisma'
22
import { z } from 'zod'
33
import { NextApiRequest, NextApiResponse } from 'next'
44
import dayjs from 'dayjs'
5+
import { google } from 'googleapis'
6+
import { getGoogleOAuthToken } from '@/lib/google'
57

68
export default async function handle(
79
req: NextApiRequest,
@@ -55,7 +57,7 @@ export default async function handle(
5557
})
5658
}
5759

58-
await prisma.scheduling.create({
60+
const scheduling = await prisma.scheduling.create({
5961
data: {
6062
name,
6163
email,
@@ -65,5 +67,34 @@ export default async function handle(
6567
},
6668
})
6769

70+
const calendar = google.calendar({
71+
version: 'v3',
72+
auth: await getGoogleOAuthToken(user.id),
73+
})
74+
75+
await calendar.events.insert({
76+
calendarId: 'primary',
77+
conferenceDataVersion: 1,
78+
requestBody: {
79+
summary: `IgniteCall: ${name}`,
80+
description: observations,
81+
start: {
82+
dateTime: schedulingDate.format(),
83+
},
84+
end: {
85+
dateTime: schedulingDate.add(1, 'hour').format(),
86+
},
87+
attendees: [{ email, displayName: name }],
88+
conferenceData: {
89+
createRequest: {
90+
requestId: scheduling.id,
91+
conferenceSolutionKey: {
92+
type: 'hangoutsMeet',
93+
},
94+
},
95+
},
96+
},
97+
})
98+
6899
return res.status(201).end()
69100
}

src/pages/schedule/[username]/ScheduleForm/ConfirmStep/index.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export function ConfirmStep({
4141
async function handleConfirmScheduling(data: ConfirmFormData) {
4242
const { name, email, observations } = data
4343

44-
await api.post(`/user/${username}/schedule`, {
44+
await api.post(`/users/${username}/schedule`, {
4545
name,
4646
email,
4747
observations,
@@ -77,7 +77,7 @@ export function ConfirmStep({
7777
)}
7878
</label>
7979
<label>
80-
<Text size={'sm'}>Nome completo</Text>
80+
<Text size={'sm'}>Email</Text>
8181
<TextInput
8282
type="email"
8383
placeholder="[email protected]"

0 commit comments

Comments
 (0)