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

Data pipeline #37

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 55 additions & 6 deletions src/lib/firebase/database/surveys.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,75 @@
import { Survey } from '@/types/survey-types';
import { GoogleForm, Survey } from '@/types/survey-types';
import { forms } from '../../googleAuthorization';
import { db } from "../firebaseConfig";
import { collection, doc, getDoc, getDocs, setDoc } from 'firebase/firestore';
import { createWatch } from './watches';
import { Watch } from '@/types/watch-types';

export async function createSurvey(body: {title: string, documentTitle: string}) {
let form = null;
let form: Survey | null = null;
try {
form = await forms.forms.create({
let googleForm: GoogleForm = (await forms.forms.create({
requestBody: {
info: {
title: body.title,
documentTitle: body.documentTitle,
},
},
});
})).data as unknown as GoogleForm;

const schemaWatch = await createWatch(googleForm.formId || '', "SCHEMA");
const responsesWatch = await createWatch(googleForm.formId || '', "RESPONSES");

googleForm = (await forms.forms.batchUpdate({
formId: googleForm.formId,
requestBody: {
includeFormInResponse: true,
requests: [
{
createItem: {
item: {
itemId: "00000000",
title: "Swaliga User ID",
description:
"Make sure to copy this ID directly from your student dashboard",
questionItem: {
question: {
required: true,
textQuestion: {
paragraph: false,
},
},
},
},
location: {
index: 0,
},
},
},
],
writeControl: {
targetRevisionId: googleForm.revisionId,
},
},
})).data.form as unknown as GoogleForm;

form = {
...googleForm,
assignedUsers: [],
responseIds: [],
schemaWatch: schemaWatch as unknown as Watch,
responsesWatch: responsesWatch as unknown as Watch,
swaligaIdQuestionId: (googleForm.items[0] as any).questionItem.question.questionId,
};
} catch (err) {
console.log(err);
throw Error('unable to create google form');
}

try {
await setDoc(doc(db, "surveys", form.data.formId || ''), form.data);
return form.data;
console.log('form', form);
await setDoc(doc(db, "surveys", form!.formId || ''), form);
return form;
} catch (err) {
throw Error('cannot add survey to firestore');
}
Expand Down
1 change: 0 additions & 1 deletion src/lib/firebase/database/watches.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ export async function createWatch(formId: string, eventType: string) {
}
);

console.log("Response", response.data);
return response.data;
} catch (error) {
console.log("Error with creating watch using given information", error);
Expand Down
17 changes: 10 additions & 7 deletions src/lib/pubsub.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { PubSub } from "@google-cloud/pubsub";
import { getSurveyByID, updateSurvey } from "./firebase/database/surveys";
import { forms } from "./googleAuthorization";
import { GoogleFormResponse, Response } from "@/types/survey-types";
import { GoogleFormResponse, Response, Survey } from "@/types/survey-types";
import { createResponse } from "./firebase/database/response";

export const pubSubClient = new PubSub();
Expand All @@ -16,7 +16,7 @@ export function listenForMessages() {

if (message.attributes.eventType === "RESPONSES") {
// TODO: update userId input using currently logged in user
newResponseHandler("1111111111", message.attributes.formId);
newResponseHandler(message.attributes.formId);
} else {
updateSurvey(message.attributes.formId);
}
Expand All @@ -28,7 +28,7 @@ export function listenForMessages() {
console.log('Listening for PubSub messages');
}

async function newResponseHandler(userId: string, formId: string) {
async function newResponseHandler(formId: string) {
// Retrieve form responses or empty array
const googleResponses = await forms.forms.responses.list({ formId });
const googleResponseData =
Expand All @@ -42,13 +42,16 @@ async function newResponseHandler(userId: string, formId: string) {
for (const response of googleResponseData) {
// Shouldn't be a runtime error hopefully(?)
// But maybe could have some error checking this seems suspicious idk
const updatedResponse: Response = { ...response, formId, userId };
const responseId = response.responseId;

// Add response to firestore if it doesn't exist
if (!existingResponseIds?.includes(responseId)) {
if (!existingResponseIds?.includes(response.responseId)) {
const survey: Survey = await getSurveyByID(formId) as Survey;
const formTitle = survey.info.title;
const userId = (response.answers[survey.swaligaIdQuestionId] as any).textAnswers.answers[0].value;

const updatedResponse: Response = { ...response, formId, userId, formTitle };
await createResponse(updatedResponse);
console.log(`Added response with ID: ${responseId}`);
console.log(`Added response with ID: ${response.responseId}`);
}
}
}
1 change: 1 addition & 0 deletions src/types/survey-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export interface Survey extends GoogleForm {
responseIds: string[];
schemaWatch: Watch;
responsesWatch: Watch;
swaligaIdQuestionId: string;
}

export interface GoogleFormResponse {
Expand Down
2 changes: 1 addition & 1 deletion src/types/watch-types.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export interface Watch {
eventType: EventType;
createTime: string;
expireTime: string;
errorType: ErrorType;
errorType?: ErrorType;
state: State;
}

Expand Down
Loading