@@ -267,80 +267,78 @@ usersApi.delete = function(app_id, query, params, callback) {
267267 console . log ( "Error generating list of uids" , err0 , res ) ;
268268 }
269269 if ( res && res [ 0 ] && res [ 0 ] . uid && res [ 0 ] . uid . length ) {
270- common . db . collection ( "metric_changes" + app_id ) . remove ( { uid : { $in : res [ 0 ] . uid } } , function ( ) {
271- plugins . dispatch ( "/i/app_users/delete" , {
272- app_id : app_id ,
273- query : query ,
274- uids : res [ 0 ] . uid ,
275- params : params
276- } , function ( _ , otherPluginResults ) {
277- const rejectReasons = otherPluginResults . reduce ( ( acc , result ) => {
278- if ( result . status === "rejected" ) {
279- acc . push ( ( result . reason && result . reason . message ) || '' ) ;
280- }
270+ plugins . dispatch ( "/i/app_users/delete" , {
271+ app_id : app_id ,
272+ query : query ,
273+ uids : res [ 0 ] . uid ,
274+ params : params
275+ } , function ( _ , otherPluginResults ) {
276+ const rejectReasons = otherPluginResults . reduce ( ( acc , result ) => {
277+ if ( result . status === "rejected" ) {
278+ acc . push ( ( result . reason && result . reason . message ) || '' ) ;
279+ }
281280
282- return acc ;
283- } , [ ] ) ;
281+ return acc ;
282+ } , [ ] ) ;
284283
285- if ( rejectReasons . length > 0 ) {
286- log . e ( "User deletion failed\n%j" , rejectReasons . join ( "\n" ) ) ;
287- common . returnMessage ( params , 500 , { errorMessage : "User deletion failed. Failed to delete some data related to this user." } ) ;
288- return ;
289- }
284+ if ( rejectReasons . length > 0 ) {
285+ log . e ( "User deletion failed\n%j" , rejectReasons . join ( "\n" ) ) ;
286+ common . returnMessage ( params , 500 , { errorMessage : "User deletion failed. Failed to delete some data related to this user." } ) ;
287+ return ;
288+ }
290289
291- //remove from drill_events
292- plugins . dispatch ( "/core/delete_granular_data" , {
293- db : "countly_drill" ,
294- collection : "drill_events" ,
295- query : { a : app_id + "" , uid : { $in : res [ 0 ] . uid } }
296- } ) ;
290+ //remove from drill_events
291+ plugins . dispatch ( "/core/delete_granular_data" , {
292+ db : "countly_drill" ,
293+ collection : "drill_events" ,
294+ query : { a : app_id + "" , uid : { $in : res [ 0 ] . uid } }
295+ } ) ;
297296
298- common . db . collection ( "app_users" + app_id ) . remove ( { uid : { $in : res [ 0 ] . uid } } , function ( err ) {
299- if ( res [ 0 ] . exported ) {
300- //delete exports if exist
301- for ( let i = 0 ; i < res [ 0 ] . exported . length ; i ++ ) {
302- let id = res [ 0 ] . exported [ i ] . split ( "/" ) ;
303- id = id [ id . length - 1 ] ; //last one is filename
304- id = id . substr ( id . length - 7 ) ;
305-
306- deleteMyExport ( id ) . then (
307- function ( ) { } ,
308- function ( err5 ) {
309- console . log ( err5 ) ;
310- }
311- ) ;
312- }
313- }
314- //deleting userimages(if they exist);
315- if ( res [ 0 ] . picture ) {
316- for ( let i = 0 ; i < res [ 0 ] . picture . length ; i ++ ) {
317- //remove /userimages/
318- let id = res [ 0 ] . picture [ i ] . substr ( 12 , res [ 0 ] . picture [ i ] . length - 12 ) ;
319- var pp = path . resolve ( __dirname , './../../../frontend/express/public/userimages/' + id ) ;
320- countlyFs . deleteFile ( "userimages" , pp , { id : id } , function ( err6 ) {
321- if ( err6 ) {
322- console . log ( err6 ) ;
323- }
324- } ) ;
325- }
297+ common . db . collection ( "app_users" + app_id ) . remove ( { uid : { $in : res [ 0 ] . uid } } , function ( err ) {
298+ if ( res [ 0 ] . exported ) {
299+ //delete exports if exist
300+ for ( let i = 0 ; i < res [ 0 ] . exported . length ; i ++ ) {
301+ let id = res [ 0 ] . exported [ i ] . split ( "/" ) ;
302+ id = id [ id . length - 1 ] ; //last one is filename
303+ id = id . substr ( id . length - 7 ) ;
304+
305+ deleteMyExport ( id ) . then (
306+ function ( ) { } ,
307+ function ( err5 ) {
308+ console . log ( err5 ) ;
309+ }
310+ ) ;
326311 }
327- try {
328- fs . appendFileSync ( path . resolve ( __dirname , './../../../log/deletedUsers' + app_id + '.txt' ) , res [ 0 ] . uid . join ( "\n" ) + "\n" , "utf-8" ) ;
312+ }
313+ //deleting userimages(if they exist);
314+ if ( res [ 0 ] . picture ) {
315+ for ( let i = 0 ; i < res [ 0 ] . picture . length ; i ++ ) {
316+ //remove /userimages/
317+ let id = res [ 0 ] . picture [ i ] . substr ( 12 , res [ 0 ] . picture [ i ] . length - 12 ) ;
318+ var pp = path . resolve ( __dirname , './../../../frontend/express/public/userimages/' + id ) ;
319+ countlyFs . deleteFile ( "userimages" , pp , { id : id } , function ( err6 ) {
320+ if ( err6 ) {
321+ console . log ( err6 ) ;
322+ }
323+ } ) ;
329324 }
330- catch ( err2 ) {
331- console . log ( err2 ) ;
325+ }
326+ try {
327+ fs . appendFileSync ( path . resolve ( __dirname , './../../../log/deletedUsers' + app_id + '.txt' ) , res [ 0 ] . uid . join ( "\n" ) + "\n" , "utf-8" ) ;
328+ }
329+ catch ( err2 ) {
330+ console . log ( err2 ) ;
331+ }
332+ plugins . dispatch ( "/systemlogs" , {
333+ params : params ,
334+ action : "app_user_deleted" ,
335+ data : {
336+ app_id : app_id ,
337+ query : JSON . stringify ( query ) ,
338+ uids : res [ 0 ] . uid ,
332339 }
333- plugins . dispatch ( "/systemlogs" , {
334- params : params ,
335- action : "app_user_deleted" ,
336- data : {
337- app_id : app_id ,
338- query : JSON . stringify ( query ) ,
339- uids : res [ 0 ] . uid ,
340- }
341- } ) ;
342- callback ( err , res [ 0 ] . uid ) ;
343340 } ) ;
341+ callback ( err , res [ 0 ] . uid ) ;
344342 } ) ;
345343 } ) ;
346344 }
@@ -758,7 +756,6 @@ usersApi.merge = async function(app_id, newAppUser, new_id, old_id, new_device_i
758756 var iid = app_id + "_" + newAppUser . uid + "_" + oldAppUser . uid ;
759757 try {
760758 await common . db . collection ( 'app_user_merges' ) . updateOne ( { "_id" : iid , "cc" : { "$ne" : true } } , { '$set' : { "u" : true } } , { upsert : false } ) ;
761- await common . db . collection ( "metric_changes" + app_id ) . updateMany ( { uid : oldAppUser . uid } , { '$set' : { uid : newAppUserP . uid } } ) ;
762759 }
763760 catch ( e ) {
764761 log . e ( "Failed metric changes update in app_users merge" , e ) ;
@@ -1144,21 +1141,8 @@ usersApi.export = function(app_id, query, params, callback) {
11441141 // }
11451142
11461143 //try deleting old export
1147- deleteMyExport ( export_id ) . then ( function ( err ) {
1148- if ( err ) {
1149- log . e ( err ) ;
1150- }
1144+ deleteMyExport ( export_id ) . then ( function ( ) {
11511145 log . d ( "old export deleted" ) ;
1152- return new Promise ( function ( resolve ) {
1153- log . d ( "collection marked" ) ;
1154- //export data from metric_changes
1155-
1156- export_safely ( { projection : { "appUserExport" : 0 } , export_id : export_id , app_id : app_id , args : [ ...dbargs , "--collection" , "metric_changes" + app_id , "-q" , '{"uid":{"$in": ["' + res [ 0 ] . uid . join ( '","' ) + '"]}}' , "--out" , export_folder + "/metric_changes" + app_id + ".json" ] } ) . finally ( function ( ) {
1157- resolve ( ) ;
1158- } ) ;
1159- } ) ;
1160- } ) . then ( function ( ) {
1161- log . d ( "metric_changes exported" ) ;
11621146 //export data from app_users
11631147 return export_safely ( { projection : { "appUserExport" : 0 } , export_id : export_id , app_id : app_id , args : [ ...dbargs , "--collection" , "app_users" + app_id , "-q" , '{"uid":{"$in": ["' + res [ 0 ] . uid . join ( '","' ) + '"]}}' , "--out" , export_folder + "/app_users" + app_id + ".json" ] } ) ;
11641148
0 commit comments