From 7c21e8907ae092d417c58a0fbfafbd396e32b0b5 Mon Sep 17 00:00:00 2001 From: feelgoodrag Date: Wed, 23 Dec 2020 05:49:20 -0500 Subject: [PATCH] Added the controller and service for createElection --- functions/src/Controller/Elections.ts | 14 ++++++++++-- functions/src/Controller/Position.ts | 6 +++++ functions/src/Models/Elections.ts | 15 ++++++------- functions/src/Models/Position.ts | 32 +++++++++++++-------------- functions/src/Services/Elections.ts | 22 ++++++++++++++++++ functions/src/index.ts | 1 + 6 files changed, 64 insertions(+), 26 deletions(-) create mode 100644 functions/src/Controller/Position.ts diff --git a/functions/src/Controller/Elections.ts b/functions/src/Controller/Elections.ts index 16a0863..5fad462 100644 --- a/functions/src/Controller/Elections.ts +++ b/functions/src/Controller/Elections.ts @@ -1,13 +1,23 @@ import { electionsConverter } from '../Models/Elections'; import * as functions from 'firebase-functions'; import { db } from '../index'; -import { closeElections } from '../Services/Elections'; -import { openElections } from '../Services/Elections'; +import { closeElections, createElections, openElections } from '../Services/Elections'; export function getElectionsCollection() { return db.collection('elections').withConverter(electionsConverter); } +export const createElectionsController = functions.https.onRequest(async (request, response) => { + createElections().then((flag) => { + if (flag == 1) + response.status(403).send('An election already exists'); + else + response.status(200).send('Good Job'); + }) + .then(() => Promise.resolve()) + .catch(error => Promise.reject(error)); +}); + export const closeElectionsController = functions.https.onRequest((request, response) => { closeElections(); response.status(200).send('Good Job'); diff --git a/functions/src/Controller/Position.ts b/functions/src/Controller/Position.ts new file mode 100644 index 0000000..4dadc70 --- /dev/null +++ b/functions/src/Controller/Position.ts @@ -0,0 +1,6 @@ +import { db } from '..'; +import { positionConverter } from '../Models/Position'; + +export function getPositionCollection() { + return db.collection('election').doc('election').collection('positions').withConverter(positionConverter); +} \ No newline at end of file diff --git a/functions/src/Models/Elections.ts b/functions/src/Models/Elections.ts index 8477b55..2863f7f 100644 --- a/functions/src/Models/Elections.ts +++ b/functions/src/Models/Elections.ts @@ -1,24 +1,23 @@ import firebase from 'firebase-admin'; -export class Elections { +export class Election { votingOpen: boolean = false; applicationsOpen: boolean = false; - positions: Position[] = []; - constructor(elections: Partial = {}) { + constructor(elections: Partial = {}) { Object.assign(this, elections); } } export const electionsConverter = { - toFirestore: function(elections: Elections): firebase.firestore.DocumentData { - const { votingOpen, applicationsOpen, positions } = elections; + toFirestore: function(elections: Election): firebase.firestore.DocumentData { + const { votingOpen, applicationsOpen } = elections; - return { votingOpen, applicationsOpen, positions }; + return { votingOpen, applicationsOpen }; }, - fromFirestore: function(snapshot: firebase.firestore.DocumentData): Elections { + fromFirestore: function(snapshot: firebase.firestore.DocumentData): Election { const data = snapshot; - return new Elections(data); + return new Election(data); } }; \ No newline at end of file diff --git a/functions/src/Models/Position.ts b/functions/src/Models/Position.ts index 9fb5892..6b2a541 100644 --- a/functions/src/Models/Position.ts +++ b/functions/src/Models/Position.ts @@ -1,26 +1,26 @@ import firebase from 'firebase-admin'; -export class Position { - title: string = ""; - level: number = 0; - description: string = ""; - candidates: object = {}; +export class ElectionPosition { + title: string = ''; + level: number = 0; + description: string = ''; + candidates: object = {}; - constructor(position: Partial = {}) { - Object.assign(this, position); - } + constructor(position: Partial = {}) { + Object.assign(this, position); + } } export const positionConverter = { - toFirestore: function (position: Position): firebase.firestore.DocumentData { - const { title, level, description, candidates } = position; + toFirestore: function (position: ElectionPosition): firebase.firestore.DocumentData { + const { title, level, description, candidates } = position; - return { title, level, description, candidates }; - }, + return { title, level, description, candidates }; + }, - fromFirestore: function (snapshot: firebase.firestore.DocumentData): Position { - const data = snapshot; + fromFirestore: function (snapshot: firebase.firestore.DocumentData): ElectionPosition { + const data = snapshot; - return new Position(data); - } + return new ElectionPosition(data); + } }; \ No newline at end of file diff --git a/functions/src/Services/Elections.ts b/functions/src/Services/Elections.ts index e091d85..6583188 100644 --- a/functions/src/Services/Elections.ts +++ b/functions/src/Services/Elections.ts @@ -1,4 +1,26 @@ import { getElectionsCollection } from '../Controller/Elections'; +import { Election } from '../Models/Elections'; + +export const createElections = async () => { + const electionsCollection = getElectionsCollection(); + let flag: number = 0; + let newElection:Election = new Election({ votingOpen: false, applicationsOpen: false }); + + await electionsCollection.doc('election').get().then((docSnapshot) => { + if (!docSnapshot.exists) { + electionsCollection.doc('election').set(newElection) + .then(() => Promise.resolve()) + .catch(error => Promise.reject(error)); + } + else { + flag = 1; + } + }) + .then(() => Promise.resolve()) + .catch(error => Promise.reject(error)); + + return flag; +}; export const closeElections = () => { const electionsCollection = getElectionsCollection(); diff --git a/functions/src/index.ts b/functions/src/index.ts index 0aab403..a2ea7a9 100644 --- a/functions/src/index.ts +++ b/functions/src/index.ts @@ -45,3 +45,4 @@ export const deleteCommittee = committeeService.deleteCommitteeController; export const closeElections = electionsService.closeElectionsController; export const changeDisplayOrder = committeeService.changeDisplayOrderController; export const openElections = electionsService.openElectionsController; +export const createElections = electionsService.createElectionsController; \ No newline at end of file