-
-
Notifications
You must be signed in to change notification settings - Fork 407
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix #974 Extra infromation on Profile
Shows information on user profile - [x] Location with link to map pin - [x] Accepted HowTos - [x] Accepted Events User stats are computed via firebase function triggered by database changes
- Loading branch information
Showing
4 changed files
with
121 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import * as functions from 'firebase-functions' | ||
import { db } from '../Firebase/firestoreDB' | ||
|
||
|
||
export const countHowTos = functions.firestore | ||
.document('v3_howtos/{id}') | ||
.onWrite(async (change, context) => { | ||
|
||
updateStats(change, 'v3_howtos', 'howToCount') | ||
|
||
}) | ||
|
||
export const countEvents = functions.firestore | ||
.document('v3_events/{id}') | ||
.onWrite(async (change, context) => { | ||
|
||
updateStats(change, 'v3_events', 'eventCount') | ||
|
||
}) | ||
|
||
function updateStats(change, collection, target){ | ||
|
||
const info = change.after.exists ? change.after.data() : null | ||
const prevInfo = change.before.exists ? change.before.data() : null | ||
const userStatsRef = db.collection('v3_users/'); | ||
|
||
let delta = 0 | ||
|
||
if (info !== null && info.moderation === 'accepted' && prevInfo !== null && prevInfo.Moderation !== 'accepted') { // Increment if now accepted and previously different | ||
delta = 1 | ||
} else if (prevInfo !== null && prevInfo.moderation === 'accepted' && (info === null || info.moderation !== 'accepted')) { // Decrement if previously accepted and now erased or moderation changed | ||
delta = -1 | ||
} else { | ||
return null | ||
} | ||
|
||
console.log('Update ', collection, ' delta: ', delta ) | ||
|
||
return userStatsRef.doc(info._createdBy).update({'_stats':{ | ||
[target]: admin.firestore.FieldValue.increment(delta) | ||
}}).catch(e => { | ||
console.log(e); | ||
// In case stats for user are inexistent we compute from all his records, Only triggers if no user exist (new Collection) | ||
// computeUserStats(info._createdBy) | ||
}) | ||
} | ||
|
||
// Compute one user stats | ||
function computeUserStats(owner){ | ||
|
||
const userStatsRef = db.collection('v3_users/') | ||
|
||
db.collection('v3_howtos').where("_createdBy", "==", owner).where("moderation", "==", "accepted") | ||
.get() | ||
.then(querySnapshot => { | ||
let count = 0; | ||
querySnapshot.forEach(doc => { | ||
count++; | ||
}); | ||
console.log('Accepted howtos for', owner ,',count: ', count); | ||
userStatsRef.doc(owner).set({'_stats':{ | ||
howToCount: count | ||
}}, {merge: true}); | ||
return null | ||
}).catch(() => null); | ||
|
||
db.collection('v3_events').where("_createdBy", "==", owner).where("moderation", "==", "accepted") | ||
.get() | ||
.then(querySnapshot => { | ||
let count = 0; | ||
querySnapshot.forEach(doc => { | ||
count++; | ||
}); | ||
console.log('Accepted events for', owner ,',count: ', count); | ||
userStatsRef.doc(owner).set({'_stats':{ | ||
eventCount: count | ||
}}, {merge: true}); | ||
return null | ||
}).catch(() => null); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters