Skip to content

Commit fd1656b

Browse files
committed
Discard results from old app to new footprint type projects
1 parent 767ce26 commit fd1656b

File tree

1 file changed

+91
-72
lines changed

1 file changed

+91
-72
lines changed

firebase/functions/src/index.ts

Lines changed: 91 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ exports.osmAuth.token = functions.https.onRequest((req, res) => {
3030
3131
This function also writes to the `contributions` section in the user profile.
3232
*/
33-
exports.groupUsersCounter = functions.database.ref('/v2/results/{projectId}/{groupId}/{userId}/').onCreate((snapshot, context) => {
33+
exports.groupUsersCounter = functions.database.ref('/v2/results/{projectId}/{groupId}/{userId}/').onCreate(async (snapshot, context) => {
3434
// these references/values will be updated by this function
3535
const groupUsersRef = admin.database().ref('/v2/groupsUsers/' + context.params.projectId + '/' + context.params.groupId);
3636
const userRef = admin.database().ref('/v2/users/' + context.params.userId);
@@ -53,6 +53,30 @@ exports.groupUsersCounter = functions.database.ref('/v2/results/{projectId}/{gro
5353
}
5454

5555
const result = snapshot.val();
56+
57+
58+
// New versions of app will have the appVersion defined (> 2.2.5)
59+
// appVersion: 2.2.5 (14)-dev
60+
const appVersionString = result.appVersion as string | undefined | null;
61+
62+
// Check if the app is of older version
63+
// (no need to check for specific version since old app won't sent the version info)
64+
if (appVersionString === null || appVersionString === undefined || appVersionString.trim() === '') {
65+
const projectRef = admin.database().ref(`/v2/projects/${context.params.projectId}`);
66+
const dataSnapshot = await projectRef.once('value');
67+
68+
if (dataSnapshot.exists()) {
69+
const project = dataSnapshot.val();
70+
// Check if project type is footprint and also has
71+
// custom options (i.e. these are new type of projects)
72+
if (project.projectType === 2 && project.customOptions) {
73+
// We remove the results submitted from older version of app (< v2.2.6)
74+
console.info(`Result submitted for ${context.params.projectId} was discarded: submitted from older version of app`);
75+
return thisResultRef.remove();
76+
}
77+
}
78+
}
79+
5680
// if result ref does not contain all required attributes we don't updated counters
5781
// e.g. due to some error when uploading from client
5882
if (!Object.prototype.hasOwnProperty.call(result, 'results')) {
@@ -90,77 +114,72 @@ exports.groupUsersCounter = functions.database.ref('/v2/results/{projectId}/{gro
90114
Update overall taskContributionCount and project taskContributionCount in the user profile
91115
based on the number of results submitted and the existing count values.
92116
*/
93-
return groupUsersRef.child(context.params.userId).once('value')
94-
.then((dataSnapshot) => {
95-
if (dataSnapshot.exists()) {
96-
console.log('group contribution exists already. user: '+context.params.userId+' project: '+context.params.projectId+' group: '+context.params.groupId);
97-
return null;
98-
}
99-
const latestNumberOfTasks = Object.keys(result['results']).length;
100-
101-
return Promise.all([
102-
userContributionRef.child(context.params.groupId).set(true),
103-
groupUsersRef.child(context.params.userId).set(true),
104-
totalTaskContributionCountRef.transaction((currentCount) => {
105-
return currentCount + latestNumberOfTasks;
106-
}),
107-
totalGroupContributionCountRef.transaction((currentCount) => {
108-
return currentCount + 1;
109-
}),
110-
taskContributionCountRef.transaction((currentCount) => {
111-
return currentCount + latestNumberOfTasks;
112-
}),
113-
114-
// Tag userGroups of the user in the result
115-
// eslint-disable-next-line promise/no-nesting
116-
userRef.child('userGroups').once('value').then((userGroupsOfTheUserSnapshot) => {
117-
if (!userGroupsOfTheUserSnapshot.exists()) {
118-
return null;
119-
}
120-
121-
// eslint-disable-next-line promise/no-nesting
122-
return userGroupsRef.once('value').then((allUserGroupsSnapshot) => {
123-
if (!allUserGroupsSnapshot.exists()) {
124-
return null;
125-
}
126-
127-
const userGroupsOfTheUserKeyList = Object.keys(userGroupsOfTheUserSnapshot.val());
128-
if (userGroupsOfTheUserKeyList.length <= 0) {
129-
return null;
130-
}
131-
132-
const allUserGroups = allUserGroupsSnapshot.val();
133-
const nonArchivedUserGroupKeys = userGroupsOfTheUserKeyList.filter((key) => {
134-
const currentUserGroup = allUserGroups[key];
135-
136-
// User might have joined some group that was removed but not cleared from their list
137-
if (!currentUserGroup) {
138-
return false;
139-
}
140-
141-
// Skip groups that have been archived
142-
if (currentUserGroup.archivedAt) {
143-
return false;
144-
}
145-
146-
return true;
147-
});
148-
149-
if (nonArchivedUserGroupKeys.length === 0) {
150-
return null;
151-
}
152-
153-
const nonArchivedUserGroupsOfTheUser = nonArchivedUserGroupKeys.reduce((acc, val) => {
154-
acc[val] = true;
155-
return acc;
156-
}, {} as Record<string, boolean>);
157-
158-
// Include userGroups of the user in the results
159-
return thisResultRef.child('userGroups').set(nonArchivedUserGroupsOfTheUser);
160-
});
161-
}),
162-
]);
163-
});
117+
const dataSnapshot = await groupUsersRef.child(context.params.userId).once('value');
118+
if (dataSnapshot.exists()) {
119+
console.log('group contribution exists already. user: '+context.params.userId+' project: '+context.params.projectId+' group: '+context.params.groupId);
120+
return null;
121+
}
122+
123+
const latestNumberOfTasks = Object.keys(result['results']).length;
124+
await Promise.all([
125+
userContributionRef.child(context.params.groupId).set(true),
126+
groupUsersRef.child(context.params.userId).set(true),
127+
totalTaskContributionCountRef.transaction((currentCount) => {
128+
return currentCount + latestNumberOfTasks;
129+
}),
130+
totalGroupContributionCountRef.transaction((currentCount) => {
131+
return currentCount + 1;
132+
}),
133+
taskContributionCountRef.transaction((currentCount) => {
134+
return currentCount + latestNumberOfTasks;
135+
}),
136+
]);
137+
138+
139+
// Tag userGroups of the user in the result
140+
const userGroupsOfTheUserSnapshot = await userRef.child('userGroups').once('value');
141+
if (!userGroupsOfTheUserSnapshot.exists()) {
142+
return null;
143+
}
144+
145+
const allUserGroupsSnapshot = await userGroupsRef.once('value');
146+
if (!allUserGroupsSnapshot.exists()) {
147+
return null;
148+
}
149+
150+
const userGroupsOfTheUserKeyList = Object.keys(userGroupsOfTheUserSnapshot.val());
151+
if (userGroupsOfTheUserKeyList.length <= 0) {
152+
return null;
153+
}
154+
155+
const allUserGroups = allUserGroupsSnapshot.val();
156+
const nonArchivedUserGroupKeys = userGroupsOfTheUserKeyList.filter((key) => {
157+
const currentUserGroup = allUserGroups[key];
158+
159+
// User might have joined some group that was removed but not cleared from their list
160+
if (!currentUserGroup) {
161+
return false;
162+
}
163+
164+
// Skip groups that have been archived
165+
if (currentUserGroup.archivedAt) {
166+
return false;
167+
}
168+
169+
return true;
170+
});
171+
172+
if (nonArchivedUserGroupKeys.length === 0) {
173+
return null;
174+
}
175+
176+
const nonArchivedUserGroupsOfTheUser = nonArchivedUserGroupKeys.reduce((acc, val) => {
177+
acc[val] = true;
178+
return acc;
179+
}, {} as Record<string, boolean>);
180+
181+
// Include userGroups of the user in the results
182+
return thisResultRef.child('userGroups').set(nonArchivedUserGroupsOfTheUser);
164183
});
165184

166185

0 commit comments

Comments
 (0)