-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
db actions: delete user: prompt for confirmation when the user has si…
…gns of activity and add a db-actions:get-user-stats script, allowing to easily access those aggregated stats, without having to pretend to delete the user
- Loading branch information
Showing
9 changed files
with
154 additions
and
25 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,43 @@ | ||
#!/usr/bin/env node | ||
const __ = require('config').universalPath | ||
const CONFIG = require('config') | ||
const __ = CONFIG.universalPath | ||
const _ = __.require('builders', 'utils') | ||
const deleteUserAndCleanup = __.require('controllers', 'user/lib/delete_user_and_cleanup') | ||
const getUserStats = __.require('controllers', 'user/lib/get_user_stats') | ||
const actionByUserId = require('./lib/action_by_user_id') | ||
actionByUserId(deleteUserAndCleanup) | ||
const { prompt } = __.require('scripts', 'scripts_utils') | ||
const { red, yellow } = require('chalk') | ||
|
||
actionByUserId(async userId => { | ||
const stats = await getUserStats(userId) | ||
_.log(stats, `user ${userId} stats`) | ||
if (stats.total > 0) { | ||
const confirmed = await promptForConfirmation(stats) | ||
if (confirmed) { | ||
_.warn('deletion confirmed') | ||
return deleteUserAndCleanup(userId) | ||
} else { | ||
_.warn('aborted delete') | ||
} | ||
} else { | ||
return deleteUserAndCleanup(userId) | ||
} | ||
}) | ||
|
||
const promptForConfirmation = async stats => { | ||
const { username, days, total } = stats | ||
const increaseDifficulty = days > 180 || total > 10 | ||
const expectedAnswer = increaseDifficulty ? 'Enter username to confirm:' : 'Y/n' | ||
const res = await prompt(`Are you sure you want to delete the account of ${yellow(username.toLowerCase())} who has been a user for ${yellow(days)} days, with ${yellow(total)} activity signs?\n${expectedAnswer}\n`) | ||
if (increaseDifficulty) { | ||
if (res.trim().toLowerCase() !== username.toLowerCase()) { | ||
console.log(red('username does not match')) | ||
return false | ||
} else { | ||
return true | ||
} | ||
} else { | ||
if (res.trim().toLowerCase() === 'y') return true | ||
else return false | ||
} | ||
} |
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,5 @@ | ||
#!/usr/bin/env node | ||
const __ = require('config').universalPath | ||
const getUserStats = __.require('controllers', 'user/lib/get_user_stats') | ||
const actionByUserId = require('./lib/action_by_user_id') | ||
actionByUserId(getUserStats) |
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
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,70 @@ | ||
const __ = require('config').universalPath | ||
const _ = __.require('builders', 'utils') | ||
const user_ = __.require('controllers', 'user/lib/user') | ||
const { getUserRelations } = __.require('controllers', 'user/lib/relations_status') | ||
const shelves_ = __.require('controllers', 'shelves/lib/shelves') | ||
const groups_ = __.require('controllers', 'groups/lib/groups') | ||
const transactions_ = __.require('controllers', 'transactions/lib/transactions') | ||
const { getUserTotalContributions } = __.require('controllers', 'entities/lib/patches') | ||
const { oneDay } = __.require('lib', 'time') | ||
|
||
module.exports = async userId => { | ||
const [ | ||
user, | ||
relations, | ||
shelves, | ||
transactions, | ||
groups, | ||
groupsInvitations, | ||
contributions, | ||
] = await Promise.all([ | ||
user_.byId(userId), | ||
getUserRelations(userId), | ||
shelves_.byOwners([ userId ]), | ||
transactions_.byUser(userId), | ||
groups_.byUser(userId), | ||
groups_.byInvitedUser(userId), | ||
getUserTotalContributions(userId), | ||
]) | ||
|
||
const { _id, username, bio, created, roles, snapshot } = user | ||
|
||
relations.friends = relations.friends.length | ||
relations.userRequested = relations.userRequested.length | ||
relations.otherRequested = relations.otherRequested.length | ||
relations.none = relations.none.length | ||
|
||
const itemsCount = _.sum(_.map(Object.values(snapshot), 'items:count')) | ||
|
||
const total = _.sum([ | ||
itemsCount, | ||
shelves.length, | ||
transactions.length, | ||
relations.friends, | ||
relations.userRequested, | ||
relations.otherRequested, | ||
relations.none, | ||
groups.length, | ||
groupsInvitations.length, | ||
contributions, | ||
]) | ||
|
||
return { | ||
_id, | ||
username, | ||
bio, | ||
created: new Date(created).toISOString(), | ||
days: Math.round((Date.now() - created) / oneDay), | ||
roles, | ||
total, | ||
items: itemsCount, | ||
shelves: shelves.length, | ||
transactions: transactions.length, | ||
relations, | ||
groups: { | ||
member: groups.length, | ||
invited: groupsInvitations.length, | ||
}, | ||
contributions | ||
} | ||
} |