Skip to content
Merged
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
16 changes: 8 additions & 8 deletions functions/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,20 @@ module.exports = {
"/lib/**/*", // Ignore built files.
"/generated/**/*", // Ignore generated files.
],
plugins: [
"@typescript-eslint",
"import",
],
plugins: ["@typescript-eslint", "import"],
rules: {
"quotes": ["error", "double", {"allowTemplateLiterals": true}],
indent: ["error", 2],
quotes: ["error", "double", { allowTemplateLiterals: true }],
"import/no-unresolved": 0,
"indent": ["error", 2],
"no-restricted-globals": ["error", "name", "length"],
"prefer-arrow-callback": "error",
"valid-jsdoc": "off",
"max-len": "off",
"new-cap": "off",
"linebreak-style": ["error", process.platform === "win32" ? "windows" : "unix"],
"@typescript-eslint/no-explicit-any": "off"
"linebreak-style": [
"error",
process.platform === "win32" ? "windows" : "unix",
],
"@typescript-eslint/no-explicit-any": "off",
},
};
154 changes: 78 additions & 76 deletions functions/package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion functions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"cors": "^2.8.5",
"dotenv": "^16.4.7",
"express": "^4.21.2",
"firebase-admin": "^13.0.2",
"firebase-admin": "^12.7.0",
"firebase-functions": "^6.3.2",
"jsonwebtoken": "^9.0.2",
"lodash": "^4.17.21",
Expand Down
5 changes: 3 additions & 2 deletions functions/src/config/firebase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import * as dotenv from "dotenv";
dotenv.config();

admin.initializeApp({
projectId: process.env.PROJECT_ID,
credential: admin.credential.cert(serviceAccount as admin.ServiceAccount),
storageBucket: "garuda-hacks-6-0.firebasestorage.app",
storageBucket: process.env.STORAGE_BUCKET,
});

const db = admin.firestore();
Expand All @@ -16,7 +17,7 @@ const auth = admin.auth();
* Populate Firestore with fake data if running in emulator
* This is useful for testing the API locally
* Comment out this block if you don't want to use fake data
*/
*/
// import { FakeDataPopulator } from "../utils/fake_data_populator";
// if (process.env.FIRESTORE_EMULATOR_HOST !== undefined) {
// const populator = new FakeDataPopulator(db);
Expand Down
11 changes: 7 additions & 4 deletions functions/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { onRequest } from "firebase-functions/v2/https";
import app from "./server";

export const api = onRequest({
cors: true,
maxInstances: 10
}, app);
export const api = onRequest(
{
cors: true,
maxInstances: 10,
},
app
);
11 changes: 6 additions & 5 deletions functions/src/routes/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import {
getApplicationQuestion,
getApplicationQuestions,
patchApplication,
uploadFile, setApplicationStatusToSubmitted
uploadFile,
setApplicationStatusToSubmitted,
} from "../controllers/application_controller";

const router = express.Router();
Expand All @@ -13,10 +14,10 @@ router.patch("/", patchApplication);

router.post("/file-upload", uploadFile);

router.get("/questions", getApplicationQuestions)
router.get("/question", getApplicationQuestion)
router.get("/questions", getApplicationQuestions);
router.get("/question", getApplicationQuestion);

router.post("/status", setApplicationStatusToSubmitted)
router.get("/status", getApplicationStatus)
router.post("/status", setApplicationStatusToSubmitted);
router.get("/status", getApplicationStatus);

export default router;
26 changes: 16 additions & 10 deletions functions/src/server.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import express, {NextFunction, Request, Response} from "express";
import cors, {CorsOptions} from "cors";
import express, { NextFunction, Request, Response } from "express";
import cors, { CorsOptions } from "cors";
import routes from "./routes";
import cookieParser from "cookie-parser";
import * as functions from "firebase-functions";
import {csrfProtection} from "./middlewares/csrf_middleware";
import {validateSessionCookie} from "./middlewares/auth_middleware";
import { csrfProtection } from "./middlewares/csrf_middleware";
import { validateSessionCookie } from "./middlewares/auth_middleware";

const app = express();

Expand All @@ -17,20 +17,20 @@ const corsOptions: CorsOptions = {
"https://www.garudahacks.com",
],
credentials: true,
allowedHeaders: ["Content-Type", "Authorization", "X-XSRF-TOKEN"]
}
allowedHeaders: ["Content-Type", "Authorization", "X-XSRF-TOKEN"],
};

// Middleware
app.options("*", cors(corsOptions)); // preflight
app.use(cors(corsOptions));
app.use(cookieParser())
app.use(cookieParser());
app.use(express.json());

// Auth validation
app.use(validateSessionCookie);

// CSRF protection as we use session cookie for authentication
app.use(csrfProtection)
app.use(csrfProtection);

// Logging
app.use((req: Request, res: Response, next: NextFunction) => {
Expand All @@ -41,7 +41,7 @@ app.use((req: Request, res: Response, next: NextFunction) => {
cookies: req.cookies,
authorizationHeader: req.headers.authorization || "Not Present",
sessionCookie: req.cookies.__session || "Not Present",
body: undefined
body: undefined,
};

const contentType = req.headers["content-type"] || "";
Expand All @@ -50,7 +50,13 @@ app.use((req: Request, res: Response, next: NextFunction) => {
}

const timestamp = new Date().toISOString();
functions.logger.info(`[${timestamp}] Incoming Request Details: ${JSON.stringify(logData, null, 2)}`);
functions.logger.info(
`[${timestamp}] Incoming Request Details: ${JSON.stringify(
logData,
null,
2
)}`
);
next();
});

Expand Down
54 changes: 27 additions & 27 deletions functions/src/utils/fake_data_populator.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import {firestore} from "firebase-admin";
import {FieldValue} from "firebase-admin/firestore";
import {faker} from "@faker-js/faker";
import {APPLICATION_STATES, Question, QUESTION_TYPE} from "../types/application_types";
import { firestore } from "firebase-admin";
import { FieldValue } from "firebase-admin/firestore";
import { faker } from "@faker-js/faker";
import {
APPLICATION_STATES,
Question,
QUESTION_TYPE,
} from "../types/application_types";

/**
* Logs a message with a specific prefix.
Expand Down Expand Up @@ -60,7 +64,7 @@ export class FakeDataPopulator {
date_of_birth: faker.date.past(),
education: "High School",
school: faker.company.name(),
grade: faker.number.int({min: 9, max: 12}),
grade: faker.number.int({ min: 9, max: 12 }),
year: faker.date.future().getFullYear(),
gender_identity: "Man",
status: "not applicable",
Expand Down Expand Up @@ -91,9 +95,9 @@ export class FakeDataPopulator {
text: "Name",
type: QUESTION_TYPE.STRING,
validation: {
required: true
}
}
required: true,
},
};
await this.createQuestionDocument(q);

// number example
Expand All @@ -106,8 +110,8 @@ export class FakeDataPopulator {
required: true,
minValue: 16,
maxValue: 45,
}
}
},
};
await this.createQuestionDocument(q);

// date example
Expand All @@ -118,8 +122,8 @@ export class FakeDataPopulator {
type: QUESTION_TYPE.DATE,
validation: {
required: true,
}
}
},
};
await this.createQuestionDocument(q);

// dropdown example
Expand All @@ -131,11 +135,8 @@ export class FakeDataPopulator {
validation: {
required: true,
},
options: [
"Undergraduate",
"High School"
]
}
options: ["Undergraduate", "High School"],
};
await this.createQuestionDocument(q);

// file example
Expand All @@ -147,22 +148,21 @@ export class FakeDataPopulator {
validation: {
required: true,
allowedTypes: "image/jpg,image/jpeg,image/png",
maxSize: 5
}
}
maxSize: 5,
},
};
await this.createQuestionDocument(q);


// string example
q = {
order: 1,
state: APPLICATION_STATES.INQUIRY,
text: "What's your motivation in joining GarudaHacks?",
type: QUESTION_TYPE.TEXTAREA,
validation: {
required: true
}
}
required: true,
},
};
await this.createQuestionDocument(q);

// string example
Expand All @@ -172,9 +172,9 @@ export class FakeDataPopulator {
text: "Do you have any limitation that we should be concern about?",
type: QUESTION_TYPE.TEXTAREA,
validation: {
required: true
}
}
required: true,
},
};
await this.createQuestionDocument(q);
}

Expand Down