Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

addPosition implemented, changes to election Model #69

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
10 changes: 9 additions & 1 deletion functions/src/Controller/Elections.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { electionsConverter } from '../Models/Elections';
import * as functions from 'firebase-functions';
import { db } from '../index';
import { openElections } from '../Services/Elections';
import { openElections, addPosition } from '../Services/Elections';
import { Position } from '../Models/Position';

export function getElectionsCollection() {
return db.collection('elections').withConverter(electionsConverter);
Expand All @@ -10,4 +11,11 @@ export function getElectionsCollection() {
export const openElectionsController = functions.https.onRequest((request, response) => {
openElections();
response.status(200).send('Good Job');
});

export const addPositionController = functions.https.onRequest((request, response) => {
const position: Position = new Position(request.body);

addPosition(position);
response.status(200).send('Good Job');
});
2 changes: 1 addition & 1 deletion functions/src/Models/Elections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import firebase from 'firebase-admin';
export class Elections {
votingOpen: boolean = false;
applicationsOpen: boolean = false;
positions: Position[] = [];
positions: Object[] = [];

constructor(elections: Partial<Elections> = {}) {
Object.assign(this, elections);
Expand Down
30 changes: 15 additions & 15 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 = {};
title: string = '';
level: number = 0;
description: string = '';
candidates: object = {};

constructor(position: Partial<Position> = {}) {
Object.assign(this, position);
}
constructor(position: Partial<Position> = {}) {
Object.assign(this, position);
}
}

export const positionConverter = {
toFirestore: function (position: Position): firebase.firestore.DocumentData {
const { title, level, description, candidates } = position;
toFirestore: function (position: Position): 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): Position {
const data = snapshot;

return new Position(data);
}
return new Position(data);
}
};
16 changes: 16 additions & 0 deletions functions/src/Services/Elections.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { getElectionsCollection } from '../Controller/Elections';
import { firestore } from 'firebase-admin';
import { Position } from '../Models/Position';

export const openElections = () => {
const electionsCollection = getElectionsCollection();
Expand All @@ -11,4 +13,18 @@ export const openElections = () => {
})
.then(() => Promise.resolve())
.catch(error => Promise.reject(error));
};

export const addPosition = (position: Position) =>{
BautistaBertolami marked this conversation as resolved.
Show resolved Hide resolved
const electionsCollection = getElectionsCollection();
const appendPosition = firestore.FieldValue.arrayUnion({ ...position });
BautistaBertolami marked this conversation as resolved.
Show resolved Hide resolved

electionsCollection.get()
.then((QuerySnapshot) => {
(QuerySnapshot.docs[0]).ref.update({ positions: appendPosition })
BautistaBertolami marked this conversation as resolved.
Show resolved Hide resolved
.then(() => Promise.resolve())
.catch(error => Promise.reject(error));
})
.then(() => Promise.resolve())
.catch(error => Promise.reject(error));
};
1 change: 1 addition & 0 deletions functions/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,4 @@ export const editCommittee = committeeService.editCommitteeController;
export const deleteCommittee = committeeService.deleteCommitteeController;
export const changeDisplayOrder = committeeService.changeDisplayOrderController;
export const openElections = electionsService.openElectionsController;
export const addPosition = electionsService.addPositionController;