Fix eslint errors

This commit is contained in:
Gabriel Tofvesson 2022-10-22 00:46:32 +02:00
parent 2b78c17304
commit b07c995784

View File

@ -1,6 +1,11 @@
import * as functions from "firebase-functions";
import {initializeApp} from "firebase-admin/app";
import { DocumentReference, DocumentSnapshot, getFirestore, QuerySnapshot } from "firebase-admin/firestore";
import {
DocumentReference,
DocumentSnapshot,
getFirestore,
QuerySnapshot,
} from "firebase-admin/firestore";
import express, {Request, Response} from "express";
type VoteEntry = {
@ -20,11 +25,20 @@ const db = getFirestore();
const app = express();
const getVote = async (id: string) =>
db.collection("votes").doc(id).get() as Promise<DocumentSnapshot<Vote>>
db.collection("votes")
.doc(id)
.get() as Promise<DocumentSnapshot<Vote>>;
const getVoteEntry = async (vote: DocumentReference<Vote>, username: string) =>
vote.collection("entries").where("username", "==", username).get() as Promise<QuerySnapshot<VoteEntry>>
const makeVoteEntry = async (vote: DocumentReference<Vote>, username: string, voteIndex: number) =>
vote.collection("entries").add({ username, voteIndex }) as Promise<DocumentReference<VoteEntry>>
vote.collection("entries")
.where("username", "==", username)
.get() as Promise<QuerySnapshot<VoteEntry>>;
const makeVoteEntry = async (
vote: DocumentReference<Vote>,
username: string,
voteIndex: number
) =>
vote.collection("entries")
.add({username, voteIndex}) as Promise<DocumentReference<VoteEntry>>;
app.post("/", async (req: Request, res: Response) => {
const voteId = req.body.voteId as string;
@ -47,7 +61,11 @@ app.post("/", async (req: Request, res: Response) => {
return;
}
if (Number.isNaN(voteIndex) || voteIndex < 0 || voteIndex >= vote.data()!.options) {
if (
Number.isNaN(voteIndex) ||
voteIndex < 0 ||
voteIndex >= (vote.data()?.options ?? 0)
) {
res.status(400).json({error: "Invalid vote index"});
return;
}
@ -93,7 +111,7 @@ app.get("/entries", async (req: Request, res: Response) => {
}
const entries = await vote.ref.collection("entries").get();
res.json(entries.docs.map(d => d.data()));
res.json(entries.docs.map((d) => d.data()));
});
app.get("/count", async (req: Request, res: Response) => {
@ -111,14 +129,18 @@ app.get("/count", async (req: Request, res: Response) => {
}
const collect = new Array<Promise<QuerySnapshot<VoteEntry>>>();
for (let i = 0; i < vote.data()!.options; i++) {
collect.push(vote.ref.collection("entries").where("voteIndex", "==", i).get() as Promise<QuerySnapshot<VoteEntry>>)
for (let i = 0; i < (vote.data()?.options ?? 0); i++) {
collect.push(vote.ref
.collection("entries")
.where("voteIndex", "==", i)
.get() as Promise<QuerySnapshot<VoteEntry>>
);
}
res.json((await Promise.all(collect)).map((query) => query.size));
});
export const vote = functions.https.onRequest(app)
export const vote = functions.https.onRequest(app);
// // Start writing Firebase Functions
// // https://firebase.google.com/docs/functions/typescript