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

export function getElectionsCollection() {
return db.collection('elections').withConverter(electionsConverter);
}

export function getPositionCollection() {
return db.collection('position').withConverter(positionConverter);
}

export const editPositionController = functions.https.onRequest(async (request, response) => {
const pos: Position = new Position(request.body);

/**
* If the User document exists, complete the editMember function.
*/
editPosition(pos)
.then(() => Promise.resolve())
.catch(error => Promise.reject(error));

response.status(200).send('Good Job');
});
3 changes: 2 additions & 1 deletion functions/src/Models/Elections.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import firebase from 'firebase-admin';
// import { Position } from './Position';

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

export function getElectionsCollection() {
return db.collection('elections').withConverter(electionsConverter);
}

export function getPositionCollection() {
return db.collection('position').withConverter(positionConverter);
}

export const editPosition = async (pos: Position) => {
let isOpen: boolean = false;
// const updatePosition = firestore.FieldValue.arrayUnion({ ...pos });
// const deletePosition = positionConverter.toFirestore(pos);
let positionss: any;

getElectionsCollection().get().then(QuerySnapshot => {
(QuerySnapshot.docs[0]).ref.get().then(documentSnapshot => {
isOpen = documentSnapshot.get('applicationsOpen');
positionss = documentSnapshot.data()?.positions;
// console.log(positionss);
// console.log(isOpen);
// we only want to update if applications are open
if (isOpen) {
console.log('isopen');
for (let key of Object.keys(positionss)) {
console.log('keys: ' + positionss.key);
if (key == pos.title) {
console.log('key: ' + key);
console.log('value: ' + positionss[key]);
positionss[key] = pos;
break;
}
}

// for (let props of positionss) {
// console.log('props title: ' + props.title);
// console.log('pos title: ' + pos.title);
// if (props.title == pos.title) {
// // update that one
// console.log('updating: ' + props);
// console.log('props decripstion: ' + props.description);
// console.log('pos decripstion: ' + pos.description);

// positionss[i] = pos;
// console.log('updated props is : ' + props);
// console.log('props decripstion: ' + props.description);
// }
// console.log(props);
// }
console.log('about to update: ' + JSON.parse(positionss));
(QuerySnapshot.docs[0]).ref.update({ positions: positionss })
.then(() => Promise.resolve())
.catch(error => Promise.reject(error));
}
})
.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 editPosition = positionService.editPositionController;