@@ -11,6 +11,8 @@ admin.initializeApp()
1111 Gets triggered when new results of a group are written to the database.
1212 This is the basis to calculate number of users who finished a group (requiredCount and finishedCount),
1313 which will be handled in the groupFinishedCountUpdater function.
14+
15+ This function also writes to the `contributions` section in the user profile.
1416*/
1517exports . groupUsersCounter = functions . database . ref ( '/v2/results/{projectId}/{groupId}/{userId}/' ) . onCreate ( ( snapshot , context ) => {
1618 const promises = [ ] // List of promises to return
@@ -23,6 +25,7 @@ exports.groupUsersCounter = functions.database.ref('/v2/results/{projectId}/{gro
2325 const groupUsersRef = admin . database ( ) . ref ( '/v2/groupsUsers/' + context . params . projectId + '/' + context . params . groupId )
2426 const userRef = admin . database ( ) . ref ( '/v2/users/' + context . params . userId )
2527 const totalTaskContributionCountRef = userRef . child ( 'taskContributionCount' )
28+ const totalGroupContributionCountRef = userRef . child ( 'groupContributionCount' )
2629 const userContributionRef = userRef . child ( 'contributions/' + context . params . projectId )
2730 const taskContributionCountRef = userRef . child ( 'contributions/' + context . params . projectId + '/taskContributionCount' )
2831
@@ -67,6 +70,7 @@ exports.groupUsersCounter = functions.database.ref('/v2/results/{projectId}/{gro
6770 userContribution : userContributionRef . child ( context . params . groupId ) . set ( data ) ,
6871 groupUsers : groupUsersRef . child ( context . params . userId ) . set ( true ) ,
6972 totalTaskContributionCount : totalTaskContributionCountRef . transaction ( ( currentCount ) => { return currentCount + numberOfTasks } ) ,
73+ totalGroupContributionCount : totalGroupContributionCountRef . transaction ( ( currentCount ) => { return currentCount + 1 } ) ,
7074 taskContributionCount : taskContributionCountRef . transaction ( ( currentCount ) => { return currentCount + numberOfTasks } )
7175 }
7276 }
@@ -80,6 +84,7 @@ exports.groupUsersCounter = functions.database.ref('/v2/results/{projectId}/{gro
8084 promises . push ( updateValues . userContribution )
8185 promises . push ( updateValues . groupUsers )
8286 promises . push ( updateValues . totalTaskContributionCount )
87+ promises . push ( updateValues . totalGroupContributionCount )
8388 promises . push ( updateValues . taskContributionCount )
8489 }
8590} )
@@ -141,6 +146,24 @@ exports.groupFinishedCountUpdater = functions.database.ref('/v2/groupsUsers/{pro
141146} )
142147
143148
149+ /*
150+ Count how many projects a users has worked on at v2/users/{userId}/projectContributionCount.
151+ This is based on the number of projectIds set in the `contribution` part of the user profile.
152+ */
153+ exports . projectContributionCounter = functions . database . ref ( '/v2/users/{userId}/contributions/' ) . onWrite ( ( snapshot , context ) => {
154+ const promises_2 = [ ]
155+ // using after here to check the data after the write operation
156+ const contributions = snapshot . after . val ( )
157+
158+ // these references/values will be updated by this function
159+ const projectContributionCountRef = admin . database ( ) . ref ( '/v2/users/' + context . params . userId + '/projectContributionCount' )
160+
161+ // set number of projects a user contributed to
162+ const projectContributionCount = projectContributionCountRef . set ( Object . keys ( contributions ) . length )
163+ promises_2 . push ( projectContributionCount )
164+ } )
165+
166+
144167/*
145168
146169OLD CODE
0 commit comments