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 2 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
10 changes: 10 additions & 0 deletions functions/src/Controller/Position.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import * as functions from 'firebase-functions';
import { Position } from '../Models/Position';
import { addPosition } from '../Services/Position';

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);
}
};
19 changes: 19 additions & 0 deletions functions/src/Services/Position.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { firestore } from 'firebase-admin';
import { Position } from '../Models/Position';
import { getElectionsCollection } from '../Controller/Elections';

export const addPosition = (position: Position) =>{
const electionsCollection = getElectionsCollection();
const appendPosition = firestore.FieldValue.arrayUnion({ ...position });

electionsCollection.get()
.then((QuerySnapshot) => {
if (QuerySnapshot.docs[0].data().applicationsOpen == true) {
(QuerySnapshot.docs[0]).ref.update({ positions: appendPosition })
.then(() => Promise.resolve())
.catch(error => Promise.reject(error));
}
})
.then(() => Promise.resolve())
.catch(error => Promise.reject(error));
};
2 changes: 2 additions & 0 deletions functions/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as eventService from './Controller/Event';
import * as memberService from './Controller/Member';
import * as committeeService from './Controller/Committee';
import * as electionsService from './Controller/Elections';
import * as positionService from './Controller/Position';

const config = {
apiKey: process.env.apiKey,
Expand Down Expand Up @@ -44,3 +45,4 @@ export const editCommittee = committeeService.editCommitteeController;
export const deleteCommittee = committeeService.deleteCommitteeController;
export const changeDisplayOrder = committeeService.changeDisplayOrderController;
export const openElections = electionsService.openElectionsController;
export const addPosition = positionService.addPositionController;