|
| 1 | +const { updateuser, getuser } = require('../../classes/user') |
| 2 | +const getargs = require('../../utils/getargs') |
| 3 | + |
| 4 | +const ARGS_FALSE_VAL = '' |
| 5 | +const ARGS_TRUE_VAL = 'true' |
| 6 | +const ACCOUNT_LEVEL_SUPERADMIN = 0 |
| 7 | +const ACCOUNT_LEVEL_ADMIN = 1 |
| 8 | +const ACCOUNT_LEVEL_USER = 2 |
| 9 | + |
| 10 | +/** |
| 11 | + * Checks if a value is valid Boolean value from the "args" data |
| 12 | + * @param {String} argValue - "emailverified", "disabled" Value(s) from the "arg" data |
| 13 | + * @returns {Bool} |
| 14 | + */ |
| 15 | +const isArgsBoolean = (argValue) => { |
| 16 | + return [ARGS_FALSE_VAL, ARGS_TRUE_VAL].includes(argValue) |
| 17 | +} |
| 18 | + |
| 19 | +/** |
| 20 | + * Checks if a value is valid account_level Number value from the "args" data |
| 21 | + * @param {Number} argValue - "account_level" Value from the "arg" data |
| 22 | + * @returns {Bool} |
| 23 | + */ |
| 24 | +const isValidAccountLevel = (argValue) => { |
| 25 | + return [ |
| 26 | + ACCOUNT_LEVEL_SUPERADMIN.toString(), |
| 27 | + ACCOUNT_LEVEL_ADMIN.toString(), |
| 28 | + ACCOUNT_LEVEL_USER.toString() |
| 29 | + ].includes(argValue) |
| 30 | +} |
| 31 | + |
| 32 | +/** |
| 33 | + * Updates a Firebase Auth user data |
| 34 | + */ |
| 35 | +const updateUser = async () => { |
| 36 | + try { |
| 37 | + const optionalKeys = ['email', 'uid', 'password', 'emailverified', 'displayname', 'account_level', 'disabled'] |
| 38 | + let userData |
| 39 | + |
| 40 | + // Get the nodejs args |
| 41 | + const args = getargs({ |
| 42 | + params: optionalKeys, |
| 43 | + optional: optionalKeys |
| 44 | + }) |
| 45 | + |
| 46 | + if (args.email === undefined && args.uid === undefined) { |
| 47 | + throw new Error('One of "email" or "uid" must be defined.') |
| 48 | + } |
| 49 | + |
| 50 | + // Check for 1 or more optional parameters |
| 51 | + const allParams = Object.keys(args).filter(key => !['email', 'uid'].includes(key)) |
| 52 | + |
| 53 | + if (allParams.length === 0) { |
| 54 | + throw new Error('Missing at least 1 optional parameter.') |
| 55 | + } |
| 56 | + |
| 57 | + // Get the user data if "args.uid" is undefined |
| 58 | + if (args.uid === undefined) { |
| 59 | + console.log('Fetching user data...') |
| 60 | + |
| 61 | + userData = await getuser({ |
| 62 | + email: args.email |
| 63 | + }) |
| 64 | + } |
| 65 | + |
| 66 | + const params = { |
| 67 | + uid: args?.uid ?? userData.uid |
| 68 | + } |
| 69 | + |
| 70 | + // Validate and set the optional "args" parameters |
| 71 | + optionalKeys.forEach(key => { |
| 72 | + if (args[key] !== undefined) { |
| 73 | + switch (key) { |
| 74 | + case 'account_level': |
| 75 | + if (!isValidAccountLevel(args[key])) { |
| 76 | + throw new Error(`Invalid Number value in the "${key}" key`) |
| 77 | + } |
| 78 | + |
| 79 | + params[key] = parseInt(args[key]) |
| 80 | + break |
| 81 | + case 'emailverified': |
| 82 | + case 'disabled': |
| 83 | + if (!isArgsBoolean(args[key])) { |
| 84 | + throw new Error(`Invalid Boolean value in the "${key}" key`) |
| 85 | + } |
| 86 | + |
| 87 | + params[key] = (args[key] === 'true') |
| 88 | + break |
| 89 | + case 'displayname': |
| 90 | + if (isArgsBoolean(args[key])) { |
| 91 | + throw new Error('Display name format not supported.') |
| 92 | + } |
| 93 | + |
| 94 | + params[key] = args[key] |
| 95 | + break |
| 96 | + default: |
| 97 | + params[key] = args[key] |
| 98 | + break |
| 99 | + } |
| 100 | + } |
| 101 | + }) |
| 102 | + |
| 103 | + // At least 1 of the optional args params should be present |
| 104 | + console.log(`Updating user UID ${params.uid}...`) |
| 105 | + const user = await updateuser(params) |
| 106 | + |
| 107 | + console.log('User updated!') |
| 108 | + console.log(user) |
| 109 | + } catch (err) { |
| 110 | + console.log(`[ERROR]: ${err.message}`) |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +updateUser() |
0 commit comments