Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CRUD operations for Google Forms Watches and associated API endpoints (create and renew) #21

Merged
merged 9 commits into from
Apr 16, 2024
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;
}
}
Loading