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,7 +1,12 @@
import * as functions from "firebase-functions"; import * as functions from "firebase-functions";
import { initializeApp } from "firebase-admin/app"; import {initializeApp} from "firebase-admin/app";
import { DocumentReference, DocumentSnapshot, getFirestore, QuerySnapshot } from "firebase-admin/firestore"; import {
import express, { Request, Response } from "express"; DocumentReference,
DocumentSnapshot,
getFirestore,
QuerySnapshot,
} from "firebase-admin/firestore";
import express, {Request, Response} from "express";
type VoteEntry = { type VoteEntry = {
username: string username: string
@ -20,11 +25,20 @@ const db = getFirestore();
const app = express(); const app = express();
const getVote = async (id: string) => 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) => const getVoteEntry = async (vote: DocumentReference<Vote>, username: string) =>
vote.collection("entries").where("username", "==", username).get() as Promise<QuerySnapshot<VoteEntry>> vote.collection("entries")
const makeVoteEntry = async (vote: DocumentReference<Vote>, username: string, voteIndex: number) => .where("username", "==", username)
vote.collection("entries").add({ username, voteIndex }) as Promise<DocumentReference<VoteEntry>> .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) => { app.post("/", async (req: Request, res: Response) => {
const voteId = req.body.voteId as string; const voteId = req.body.voteId as string;
@ -43,24 +57,28 @@ app.post("/", async (req: Request, res: Response) => {
const vote = await getVote(voteId); const vote = await getVote(voteId);
if (!vote.exists) { if (!vote.exists) {
res.status(404).json({ error: "Vote not found" }); res.status(404).json({error: "Vote not found"});
return; return;
} }
if (Number.isNaN(voteIndex) || voteIndex < 0 || voteIndex >= vote.data()!.options) { if (
res.status(400).json({ error: "Invalid vote index" }); Number.isNaN(voteIndex) ||
voteIndex < 0 ||
voteIndex >= (vote.data()?.options ?? 0)
) {
res.status(400).json({error: "Invalid vote index"});
return; return;
} }
const entry = await getVoteEntry(vote.ref, voter); const entry = await getVoteEntry(vote.ref, voter);
if (entry.empty) { if (entry.empty) {
await makeVoteEntry(vote.ref, voter, voteIndex); await makeVoteEntry(vote.ref, voter, voteIndex);
res.json({ success: true }); res.json({success: true});
return; return;
} }
entry.docs[0].ref.update({ voteIndex }); entry.docs[0].ref.update({voteIndex});
res.json({ success: true }); res.json({success: true});
}); });
app.get("/", async (req: Request, res: Response) => { app.get("/", async (req: Request, res: Response) => {
@ -72,7 +90,7 @@ app.get("/", async (req: Request, res: Response) => {
const vote = await getVote(voteId); const vote = await getVote(voteId);
if (!vote.exists) { if (!vote.exists) {
res.status(404).json({ error: "Vote not found" }); res.status(404).json({error: "Vote not found"});
return; return;
} }
@ -88,12 +106,12 @@ app.get("/entries", async (req: Request, res: Response) => {
const vote = await getVote(voteId); const vote = await getVote(voteId);
if (!vote.exists) { if (!vote.exists) {
res.status(404).json({ error: "Vote not found" }); res.status(404).json({error: "Vote not found"});
return; return;
} }
const entries = await vote.ref.collection("entries").get(); 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) => { app.get("/count", async (req: Request, res: Response) => {
@ -106,19 +124,23 @@ app.get("/count", async (req: Request, res: Response) => {
const vote = await getVote(voteId); const vote = await getVote(voteId);
if (!vote.exists) { if (!vote.exists) {
res.status(404).json({ error: "Vote not found" }); res.status(404).json({error: "Vote not found"});
return; return;
} }
const collect = new Array<Promise<QuerySnapshot<VoteEntry>>>(); const collect = new Array<Promise<QuerySnapshot<VoteEntry>>>();
for (let i = 0; i < vote.data()!.options; i++) { for (let i = 0; i < (vote.data()?.options ?? 0); i++) {
collect.push(vote.ref.collection("entries").where("voteIndex", "==", i).get() as Promise<QuerySnapshot<VoteEntry>>) collect.push(vote.ref
.collection("entries")
.where("voteIndex", "==", i)
.get() as Promise<QuerySnapshot<VoteEntry>>
);
} }
res.json((await Promise.all(collect)).map((query) => query.size)); 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 // // Start writing Firebase Functions
// // https://firebase.google.com/docs/functions/typescript // // https://firebase.google.com/docs/functions/typescript