Skip to content

Commit

Permalink
✨ Import votes from JSON (#39)
Browse files Browse the repository at this point in the history
* 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
Ked57 committed Oct 25, 2018
1 parent f1fc962 commit b017520
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 0 deletions.
2 changes: 2 additions & 0 deletions functions/src/cast-vote.ts
Expand Up @@ -19,6 +19,7 @@ export interface Vote extends Employee {
value: string;
campaign: string;
recordedAt: firestore.Timestamp;
voteFromUi?: boolean; //States if votes comes from Ui. If not, sendEmailToManager will abort
}

export const castVote = functions.https.onCall(
Expand Down Expand Up @@ -66,6 +67,7 @@ export const castVote = functions.https.onCall(
campaign: campaign.id,
recordedAt: firestore.Timestamp.fromDate(voteDate),
value: payload.vote,
voteFromUi: true,
...employee
};

Expand Down
7 changes: 7 additions & 0 deletions functions/src/config.ts
Expand Up @@ -134,6 +134,12 @@ export interface DailyAlibeezImportConfig {
export interface CollectStatsConfig {
enabled: Flag;
}

export interface ImportVotesConfig {
enabled: Flag;
key: string;
}

export interface ComputeStatisticsConfigs {
enabled: Flag;
key: string;
Expand All @@ -145,6 +151,7 @@ export interface FeaturesConfig {
daily_alibeez_import: DailyAlibeezImportConfig;
collect_stats: CollectStatsConfig;
compute_statistics: ComputeStatisticsConfigs;
import_votes: ImportVotesConfig;
}

export interface AlibeezConfig {
Expand Down
56 changes: 56 additions & 0 deletions functions/src/import-votes.ts
@@ -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);
});
}
);
4 changes: 4 additions & 0 deletions functions/src/index.ts
Expand Up @@ -22,6 +22,10 @@ export { getCampaign } from "./get-campaign";

export { castVote } from "./cast-vote";

export { updateStats } from "./update-stats";

export { importVotes } from "./import-votes";

export { updateStatsOnVote } from "./update-stats";

export { computeStatistics } from "./compute-statistics";
4 changes: 4 additions & 0 deletions functions/src/send-email-to-manager.ts
Expand Up @@ -24,6 +24,10 @@ export const sendEmailToManager = functions.firestore
}

const vote = voteSnapshot.data()! as Vote;
if (!vote.voteFromUi) {
console.info("Vote doesn't come from Ui, aborting...");
return;
}

const employeeSnapshot = await db
.collection("employees")
Expand Down

0 comments on commit b017520

Please sign in to comment.