From 71c32579dd76a9662ab1a690a7072b977d0b0916 Mon Sep 17 00:00:00 2001 From: Sofia Montana Date: Sat, 14 Nov 2020 17:18:56 -0500 Subject: [PATCH 1/5] edit position --- functions/src/Controller/Position.ts | 32 ++++++++++++++++++++++++++++ functions/src/Models/Elections.ts | 1 + functions/src/Models/Position.ts | 31 ++++++++++++++------------- functions/src/Services/Position.ts | 31 +++++++++++++++++++++++++++ functions/src/index.ts | 2 ++ 5 files changed, 82 insertions(+), 15 deletions(-) create mode 100644 functions/src/Controller/Position.ts create mode 100644 functions/src/Services/Position.ts diff --git a/functions/src/Controller/Position.ts b/functions/src/Controller/Position.ts new file mode 100644 index 0000000..961392f --- /dev/null +++ b/functions/src/Controller/Position.ts @@ -0,0 +1,32 @@ +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); + const posCollection = getPositionCollection(); + const posDoc = posCollection.doc(pos.id); + + /** + * If the User document exists, complete the editMember function. + */ + await posDoc.get().then((docSnapshot) => { + if (docSnapshot.exists) { + editPosition(pos); + response.status(200).send('Good Job'); + } + else { + response.status(404).send('Error 404: User document not found'); + } + }); +}); \ No newline at end of file diff --git a/functions/src/Models/Elections.ts b/functions/src/Models/Elections.ts index 8477b55..37d5c98 100644 --- a/functions/src/Models/Elections.ts +++ b/functions/src/Models/Elections.ts @@ -1,4 +1,5 @@ import firebase from 'firebase-admin'; +import { Position } from './Position'; export class Elections { votingOpen: boolean = false; diff --git a/functions/src/Models/Position.ts b/functions/src/Models/Position.ts index 9fb5892..bce1a86 100644 --- a/functions/src/Models/Position.ts +++ b/functions/src/Models/Position.ts @@ -1,26 +1,27 @@ 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 = {}; + id: string = ''; - 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..f00b974 --- /dev/null +++ b/functions/src/Services/Position.ts @@ -0,0 +1,31 @@ +// import * as functions from 'firebase-functions'; +import { electionsConverter } from '../Models/Elections'; +import { db } from '../index'; +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; + + getElectionsCollection().get().then(QuerySnapshot => { + (QuerySnapshot.docs[0]).ref.get().then(documentSnapshot => { + isOpen = documentSnapshot.get('votingOpen'); + if (isOpen) { + getPositionCollection().doc(pos.id).update({ ...pos }) + .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 From cca8d7dd8048c9da495b51824121ad255f784001 Mon Sep 17 00:00:00 2001 From: Sofia Montana Date: Sat, 14 Nov 2020 17:26:13 -0500 Subject: [PATCH 2/5] adding comments and adding string array to Position model --- functions/src/Models/Position.ts | 2 +- functions/src/Services/Position.ts | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/functions/src/Models/Position.ts b/functions/src/Models/Position.ts index bce1a86..5971dff 100644 --- a/functions/src/Models/Position.ts +++ b/functions/src/Models/Position.ts @@ -4,7 +4,7 @@ export class Position { title: string = ''; level: number = 0; description: string = ''; - candidates: object = {}; + candidates: string[] = []; id: string = ''; constructor(position: Partial = {}) { diff --git a/functions/src/Services/Position.ts b/functions/src/Services/Position.ts index f00b974..dac891e 100644 --- a/functions/src/Services/Position.ts +++ b/functions/src/Services/Position.ts @@ -16,7 +16,8 @@ export const editPosition = async (pos: Position) => { getElectionsCollection().get().then(QuerySnapshot => { (QuerySnapshot.docs[0]).ref.get().then(documentSnapshot => { - isOpen = documentSnapshot.get('votingOpen'); + isOpen = documentSnapshot.get('applicationsOpen'); + // we only want to update if applications are open if (isOpen) { getPositionCollection().doc(pos.id).update({ ...pos }) .then(() => Promise.resolve()) From f0957fc717c03cbb62984508eaf5bfb0469a71a4 Mon Sep 17 00:00:00 2001 From: Sofia Montana Date: Sat, 14 Nov 2020 17:40:08 -0500 Subject: [PATCH 3/5] can only update title, level, and description --- functions/src/Models/Position.ts | 4 ++-- functions/src/Services/Position.ts | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/functions/src/Models/Position.ts b/functions/src/Models/Position.ts index 5971dff..a980ff2 100644 --- a/functions/src/Models/Position.ts +++ b/functions/src/Models/Position.ts @@ -14,9 +14,9 @@ export class Position { export const positionConverter = { toFirestore: function (position: Position): firebase.firestore.DocumentData { - const { title, level, description, candidates } = position; + const { title, level, description, candidates, id } = position; - return { title, level, description, candidates }; + return { title, level, description, candidates, id }; }, fromFirestore: function (snapshot: firebase.firestore.DocumentData): Position { diff --git a/functions/src/Services/Position.ts b/functions/src/Services/Position.ts index dac891e..3e31a8d 100644 --- a/functions/src/Services/Position.ts +++ b/functions/src/Services/Position.ts @@ -19,7 +19,8 @@ export const editPosition = async (pos: Position) => { isOpen = documentSnapshot.get('applicationsOpen'); // we only want to update if applications are open if (isOpen) { - getPositionCollection().doc(pos.id).update({ ...pos }) + getPositionCollection().doc(pos.id).update({ title: pos.title, + description: pos.description, level: pos.level }) .then(() => Promise.resolve()) .catch(error => Promise.reject(error)); } From 7b7bbce0f9bed51ffb3559e20f1640a478deeedc Mon Sep 17 00:00:00 2001 From: Sofia Montana Date: Sat, 14 Nov 2020 18:19:56 -0500 Subject: [PATCH 4/5] changed back candidates to obj --- functions/src/Models/Position.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/functions/src/Models/Position.ts b/functions/src/Models/Position.ts index a980ff2..9abae8a 100644 --- a/functions/src/Models/Position.ts +++ b/functions/src/Models/Position.ts @@ -4,7 +4,7 @@ export class Position { title: string = ''; level: number = 0; description: string = ''; - candidates: string[] = []; + candidates: object = {}; id: string = ''; constructor(position: Partial = {}) { From 3b3e18be5e7c1db92764867277e801f3ec08fdc6 Mon Sep 17 00:00:00 2001 From: Sofia Montana Date: Sat, 19 Dec 2020 12:26:29 -0500 Subject: [PATCH 5/5] somethign --- functions/src/Controller/Position.ts | 16 ++++-------- functions/src/Models/Elections.ts | 4 +-- functions/src/Models/Position.ts | 7 +++-- functions/src/Services/Position.ts | 39 +++++++++++++++++++++++++--- 4 files changed, 46 insertions(+), 20 deletions(-) diff --git a/functions/src/Controller/Position.ts b/functions/src/Controller/Position.ts index 961392f..1364cf6 100644 --- a/functions/src/Controller/Position.ts +++ b/functions/src/Controller/Position.ts @@ -14,19 +14,13 @@ export function getPositionCollection() { export const editPositionController = functions.https.onRequest(async (request, response) => { const pos: Position = new Position(request.body); - const posCollection = getPositionCollection(); - const posDoc = posCollection.doc(pos.id); /** * If the User document exists, complete the editMember function. */ - await posDoc.get().then((docSnapshot) => { - if (docSnapshot.exists) { - editPosition(pos); - response.status(200).send('Good Job'); - } - else { - response.status(404).send('Error 404: User document not found'); - } - }); + 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 37d5c98..162b856 100644 --- a/functions/src/Models/Elections.ts +++ b/functions/src/Models/Elections.ts @@ -1,10 +1,10 @@ import firebase from 'firebase-admin'; -import { Position } from './Position'; +// 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 9abae8a..70d3ddb 100644 --- a/functions/src/Models/Position.ts +++ b/functions/src/Models/Position.ts @@ -4,8 +4,7 @@ export class Position { title: string = ''; level: number = 0; description: string = ''; - candidates: object = {}; - id: string = ''; + candidates: Object = {}; constructor(position: Partial = {}) { Object.assign(this, position); @@ -14,9 +13,9 @@ export class Position { export const positionConverter = { toFirestore: function (position: Position): firebase.firestore.DocumentData { - const { title, level, description, candidates, id } = position; + const { title, level, description, candidates } = position; - return { title, level, description, candidates, id }; + return { title, level, description, candidates }; }, fromFirestore: function (snapshot: firebase.firestore.DocumentData): Position { diff --git a/functions/src/Services/Position.ts b/functions/src/Services/Position.ts index 3e31a8d..300635f 100644 --- a/functions/src/Services/Position.ts +++ b/functions/src/Services/Position.ts @@ -1,6 +1,7 @@ // 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() { @@ -13,20 +14,52 @@ export function getPositionCollection() { 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) { - getPositionCollection().doc(pos.id).update({ title: pos.title, - description: pos.description, level: pos.level }) + 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)); + .catch(error => Promise.reject(error)); }) .then(() => Promise.resolve()) .catch(error => Promise.reject(error));