diff --git a/functions/src/Controller/Position.ts b/functions/src/Controller/Position.ts new file mode 100644 index 0000000..1364cf6 --- /dev/null +++ b/functions/src/Controller/Position.ts @@ -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'); +}); \ No newline at end of file diff --git a/functions/src/Models/Elections.ts b/functions/src/Models/Elections.ts index 8477b55..162b856 100644 --- a/functions/src/Models/Elections.ts +++ b/functions/src/Models/Elections.ts @@ -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 = {}) { Object.assign(this, elections); diff --git a/functions/src/Models/Position.ts b/functions/src/Models/Position.ts index 9fb5892..70d3ddb 100644 --- a/functions/src/Models/Position.ts +++ b/functions/src/Models/Position.ts @@ -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 = {}) { - Object.assign(this, position); - } + constructor(position: Partial = {}) { + 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); + } }; \ No newline at end of file diff --git a/functions/src/Services/Position.ts b/functions/src/Services/Position.ts new file mode 100644 index 0000000..300635f --- /dev/null +++ b/functions/src/Services/Position.ts @@ -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)); +}; \ No newline at end of file diff --git a/functions/src/index.ts b/functions/src/index.ts index 8f24d74..36ebdf5 100644 --- a/functions/src/index.ts +++ b/functions/src/index.ts @@ -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, @@ -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; \ No newline at end of file