Reputation: 93
I got a firebase realtime db (js sdk) where I count users for each location (groups/location/Items/{locationId}/usercount). I also have at a root level, a list of users where for each user I have a list of locations. Within each of these locations I have a counter that counts the total number of useres for that location (users/{userId}/groups/location/Items/{locationId}/usercount).
a) So whenever a user is added to to a location at groups/location/Items/{locationId}/users, the counter at groups/location/Items/{locationId}/usercount is incremented.
b) At the same time a counter at users/groups/location/Items/{locationId}/usercount is also incremented for each user that has this location in users/{userId}/groups/location/Items/.
Below you'll find the json data structure as well as the cloud function supposed to increment both counters (a and b). Part (a) of the function works perfectly. As for part (b) it only works for the user that was just added at groups/location/Items/{locationId}/users (user yjXidCKJuYZ71TNzYe9ob5Raaub2 as per hereby json) on not for ALL users at users/{userId}.
I need this function to increment all the counters of users within users/{userId} that have the location to which a user was added.
I hope I'm clear enough and would REALLY appreciate some help on this. Maybe I should not be using transactions.
Thanks a lot!
"groups" : {
"location" : {
"Items" : {
"NA" : {
"code" : "NA",
"created" : "2019-04-03--19:10:11",
"id" : "NA",
"label" : "North America",
"levelIndex" : 1,
"levelName" : "Continent",
"type" : "location",
"usercount" : 2,
"users" : {
"Xi6XiXdxDAWqvjmSUeobnhDTe4k2" : {
"created" : "2019-04-03--19:10:11",
"id" : "Xi6XiXdxDAWqvjmSUeobnhDTe4k2",
"ip" : "hidden",
"label" : "Anonymous"
},
"yjXidCKJuYZ71TNzYe9ob5Raaub2" : {
"created" : "2019-04-03--19:10:11",
"id" : "yjXidCKJuYZ71TNzYe9ob5Raaub2",
"ip" : "hidden",
"label" : "Anonymous"
}
}
}
}
}
},
"users" : {
"Xi6XiXdxDAWqvjmSUeobnhDTe4k2" : {
"created" : "2019-04-03--19:10:11",
"groups" : {
"location" : {
"Items" : {
"NA" : {
"code" : "NA",
"created" : "2019-04-03--19:10:11",
"id" : "NA",
"label" : "North America",
"levelIndex" : 1,
"levelName" : "Continent",
"type" : "location",
"usercount" : 1
}
}
}
},
"id" : "Xi6XiXdxDAWqvjmSUeobnhDTe4k2",
"ip" : "hidden",
"label" : "Anonymous"
},
"yjXidCKJuYZ71TNzYe9ob5Raaub2" : {
"created" : "2019-04-03--19:10:11",
"groups" : {
"location" : {
"Items" : {
"NA" : {
"code" : "NA",
"created" : "2019-04-03--19:10:11",
"id" : "NA",
"label" : "North America",
"levelIndex" : 1,
"levelName" : "Continent",
"type" : "location",
"usercount" : 2
}
}
}
},
"id" : "yjXidCKJuYZ71TNzYe9ob5Raaub2",
"ip" : "hidden",
"label" : "Anonymous"
}
}
}
exports.onUserAddToLocation = functions.database
.ref('/groups/location/Items/{itemId}/users/{userId}')
.onCreate((snapshot, context) => {
const itemId = context.params.itemId
const userId = context.params.userId
const groupCounterRef = admin.database().ref('/groups/location/Items/' + itemId + '/usercount');
return groupCounterRef.transaction(usercount => {
return (usercount || 0) + 1
}).then(result => {
const count = result.snapshot.val();
const userGroupsCounterRef = admin.database().ref('/users/' + userId + '/groups/location/Items/' + itemId + '/usercount');
userGroupsCounterRef.transaction(usercount => {
return count
})
})
})
Upvotes: 0
Views: 102
Reputation: 93
This does the trick :
.ref('/groups/location/Items/{itemId}/users/{userId}')
.onCreate((snapshot, context) => {
const itemId = context.params.itemId
const groupCounterRef = admin.database().ref('/groups/location/Items/' + itemId + '/usercount');
return groupCounterRef.transaction(usercount => {
return (usercount || 0) + 1
}).then(result => {
const count = result.snapshot.val();
const usersRef = admin.database().ref("users").orderByKey();
usersRef.once("value")
.then(function(snapshot) {
snapshot.forEach(function(childSnapshot){
const userId = childSnapshot.key
const userLocationCountersRef = admin.database().ref('users/' + userId + '/groups/location/Items/' + itemId)
const hasThisGroup = childSnapshot.hasChild('/groups/location/Items/' + itemId)
if(hasThisGroup){
userLocationCountersRef.update({usercount:count})
}
})
})
})
})
This increments a counter for each user added to a location. After transaction success it copies that counter value to each user that has this location.
Upvotes: 1