Skip to content

Commit

Permalink
Merge pull request #21 from Hack4Impact-UMD/forms-crud-ops
Browse files Browse the repository at this point in the history
CRUD operations for Google Forms Watches and associated API endpoints (create and renew)
  • Loading branch information
nkanchinadam authored Apr 16, 2024
2 parents 1346c27 + 428a408 commit ca2208c
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/app/api/watches/renew/route.ts
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 });
}
}
28 changes: 28 additions & 0 deletions src/app/api/watches/route.ts
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 });
}
}
43 changes: 43 additions & 0 deletions src/lib/firebase/database/watches.ts
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;
}
}

0 comments on commit ca2208c

Please sign in to comment.