-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from Hack4Impact-UMD/forms-crud-ops
CRUD operations for Google Forms Watches and associated API endpoints (create and renew)
- Loading branch information
Showing
3 changed files
with
99 additions
and
0 deletions.
There are no files selected for viewing
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,28 @@ | ||
import { renewWatch } from "@/lib/firebase/database/watches"; | ||
import { NextRequest, NextResponse } from 'next/server'; | ||
|
||
export async function POST(req: NextRequest) { | ||
try { | ||
if (!req.body) { | ||
return NextResponse.json({ error: 'Missing Request Body' }, { status: 400 }); | ||
} | ||
|
||
const data = await req.json(); | ||
const formId: string = data.formId as string; | ||
const watchId: string = data.watchId as string; | ||
|
||
if (!formId || !watchId) { | ||
return NextResponse.json({ error: 'Invalid formId or watchId in Request Body' }, { status: 400 }); | ||
} | ||
|
||
try { | ||
const response = await renewWatch(formId, watchId); | ||
return NextResponse.json({ message: response }, { status: 200 }); | ||
} catch { | ||
return NextResponse.json({ error: 'Error with Renewing the Watch' }, { status: 500 }); | ||
} | ||
|
||
} catch (error) { | ||
return NextResponse.json({ error: 'Failed to Load Data' }, { status: 500 }); | ||
} | ||
} |
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,28 @@ | ||
import { createWatch } from "@/lib/firebase/database/watches"; | ||
import { NextRequest, NextResponse } from 'next/server'; | ||
|
||
export async function POST(req: NextRequest) { | ||
try { | ||
if (!req.body) { | ||
return NextResponse.json({ error: 'Missing Request Body' }, { status: 400 }); | ||
} | ||
|
||
const data = await req.json(); | ||
const formId: string = data.formId as string; | ||
const eventType: string = data.eventType as string; | ||
|
||
if (!formId || !eventType) { | ||
return NextResponse.json({ error: 'Invalid formId or eventType in Request Body' }, { status: 400 }); | ||
} | ||
|
||
try { | ||
const response = await createWatch(formId, eventType); | ||
return NextResponse.json({ message: response }, { status: 200 }); | ||
} catch { | ||
return NextResponse.json({ error: 'Error with Creating the Watch' }, { status: 500 }); | ||
} | ||
|
||
} catch (error) { | ||
return NextResponse.json({ error: 'Failed to Load Data' }, { status: 500 }); | ||
} | ||
} |
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,43 @@ | ||
import { forms } from "@/lib/googleAuthorization"; | ||
|
||
export async function createWatch(formId: string, eventType: string) { | ||
try { | ||
const response = await forms.forms.watches.create( | ||
{ | ||
"formId": formId, | ||
"requestBody": { | ||
"watch": { | ||
"eventType": eventType, | ||
"target": { | ||
"topic": { | ||
"topicName": "projects/swaliga-foundation/topics/forms" | ||
} | ||
} | ||
} | ||
} | ||
} | ||
); | ||
|
||
console.log("Response", response.data); | ||
return response.data; | ||
} catch (error) { | ||
console.log("Error with creating watch using given information", error); | ||
throw error; | ||
} | ||
} | ||
|
||
export async function renewWatch(formId : string, watchId : string) { | ||
try { | ||
const response = await forms.forms.watches.renew( | ||
{ | ||
"formId": formId, | ||
"watchId": watchId | ||
} | ||
); | ||
console.log("Response", response.data); | ||
return response.data; | ||
} catch (error) { | ||
console.log("Error with renewing watch using given formId and watchId", error); | ||
throw error; | ||
} | ||
} |