Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions functions/src/Controller/Elections.ts
Original file line number Diff line number Diff line change
@@ -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');
Expand Down
6 changes: 6 additions & 0 deletions functions/src/Controller/Position.ts
Original file line number Diff line number Diff line change
@@ -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);
}
15 changes: 7 additions & 8 deletions functions/src/Models/Elections.ts
Original file line number Diff line number Diff line change
@@ -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<Elections> = {}) {
constructor(elections: Partial<Election> = {}) {
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);
}
};
32 changes: 16 additions & 16 deletions functions/src/Models/Position.ts
Original file line number Diff line number Diff line change
@@ -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<Position> = {}) {
Object.assign(this, position);
}
constructor(position: Partial<ElectionPosition> = {}) {
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);
}
};
22 changes: 22 additions & 0 deletions functions/src/Services/Elections.ts
Original file line number Diff line number Diff line change
@@ -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();
Expand Down
1 change: 1 addition & 0 deletions functions/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;