Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add import votes (not tested) * Finalise import votes function see #38 * forgot something * Fixed CircleCi import It still won't work until compute-stats and this branch are merged into master * Fixed import calling sendEmailToManager See #38 * Add some comments over the new field of vote * Forgot to remove a console.info * Fix review ! * Fix linting * fix review
- Loading branch information
Showing
5 changed files
with
73 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import * as functions from "firebase-functions"; | ||
import * as firebase from "firebase-admin"; | ||
|
||
import { Config } from "./config"; | ||
import { Vote } from "./cast-vote"; | ||
|
||
const config = functions.config() as Config; | ||
const importVotesConfigs = config.features.import_votes; | ||
const db = firebase.firestore(); | ||
|
||
const fromJSONtoVote = supposedlyValidVotes => { | ||
if (!supposedlyValidVotes.votes) return []; | ||
const validVotes: Vote[] = supposedlyValidVotes.votes.filter( | ||
supposedlyValidVote => | ||
supposedlyValidVote.value && | ||
supposedlyValidVote.campaign && | ||
supposedlyValidVote.agency | ||
); | ||
return validVotes; | ||
}; | ||
|
||
export const importVotes = functions.https.onRequest( | ||
async (req: functions.Request, res: functions.Response) => { | ||
if (!importVotesConfigs.enabled) { | ||
console.info("Feature import votes disabled, aborting"); | ||
return; | ||
} | ||
const authorizationHeader = req.get("Authorization") || ""; | ||
const keyIsCorrect = | ||
authorizationHeader === `Bearer ${importVotesConfigs.key}`; | ||
if (!keyIsCorrect) { | ||
console.error("Passed the wrong auth key, aborting"); | ||
res.sendStatus(403); | ||
return; | ||
} | ||
let validVotes: Vote[] = []; | ||
if (req.body) { | ||
validVotes = fromJSONtoVote(req.body); | ||
} else { | ||
console.error("votesData is null, aborting"); | ||
res.sendStatus(422); | ||
return; | ||
} | ||
const voteBatch = db.batch(); | ||
for (const validVote of validVotes) { | ||
voteBatch.create(db.collection("vote").doc(), validVote); | ||
} | ||
voteBatch | ||
.commit() | ||
.then(() => res.sendStatus(200)) | ||
.catch(e => { | ||
console.error(e); | ||
res.sendStatus(500); | ||
}); | ||
} | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters