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
2 changes: 1 addition & 1 deletion functions/src/Controller/Elections.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
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 } from '../Services/Elections';

export function getElectionsCollection() {
return db.collection('elections').withConverter(electionsConverter);
Expand Down
11 changes: 11 additions & 0 deletions functions/src/Controller/Position.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import * as functions from 'firebase-functions';
import { Position } from '../Models/Position';
import { deletePosition } from '../Services/Position';

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

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

export const deletePosition = (position : Position) => {
const electionsCollection = getElectionsCollection();
const positionToRemove = firestore.FieldValue.arrayRemove({ ...position });

electionsCollection.get().then((QuerySnapshot) => {
if (QuerySnapshot.docs[0].data().applicationsOpen == true) {
(QuerySnapshot.docs[0]).ref.update({ positions: positionToRemove })
.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 @@ -45,3 +46,4 @@ export const deleteCommittee = committeeService.deleteCommitteeController;
export const closeElections = electionsService.closeElectionsController;
export const changeDisplayOrder = committeeService.changeDisplayOrderController;
export const openElections = electionsService.openElectionsController;
export const deletePosition = positionService.deletePositionController;