|
| 1 | +const { ActionTransport } = require('@microfleet/plugin-router'); |
| 2 | + |
| 3 | +const { requestUsernameUpdate } = require('../../utils/update-username'); |
| 4 | +const { resolveUserId } = require('../../utils/userData'); |
| 5 | +const { checkMFA } = require('../../utils/mfa'); |
| 6 | +const isActive = require('../../utils/is-active'); |
| 7 | +const isBanned = require('../../utils/is-banned'); |
| 8 | +const { |
| 9 | + ErrorConflictUserExists, |
| 10 | + ErrorUserNotFound, |
| 11 | + MFA_TYPE_OPTIONAL, |
| 12 | + USERS_ID_FIELD, |
| 13 | +} = require('../../constants'); |
| 14 | + |
| 15 | +/** |
| 16 | + * @api {amqp} <prefix>.update-username.request Request update username |
| 17 | + * @apiVersion 1.0.0 |
| 18 | + * @apiName RequestUpdateUsername |
| 19 | + * @apiGroup Users |
| 20 | + * |
| 21 | + * @apiDescription Sends the user a secret code that will be used |
| 22 | + * to confirm the user name update. Currently only phone is supported. |
| 23 | + * |
| 24 | + * @apiSchema (Payload) {jsonschema=../../../schemas/update-username/request.json} apiParam |
| 25 | + * |
| 26 | + * @apiSuccess (Response) {Object} uid Token UID |
| 27 | + */ |
| 28 | +module.exports = async function requestUpdateUsernameAction(request) { |
| 29 | + const { challengeType, i18nLocale, value } = request.params; |
| 30 | + const { internalData } = request.locals; |
| 31 | + |
| 32 | + if (!internalData) { |
| 33 | + throw ErrorUserNotFound; |
| 34 | + } |
| 35 | + |
| 36 | + await isActive(internalData); |
| 37 | + isBanned(internalData); |
| 38 | + |
| 39 | + if (await resolveUserId.call(this, value)) { |
| 40 | + throw ErrorConflictUserExists; |
| 41 | + } |
| 42 | + |
| 43 | + return requestUsernameUpdate( |
| 44 | + this, |
| 45 | + internalData[USERS_ID_FIELD], |
| 46 | + value, |
| 47 | + challengeType, |
| 48 | + { i18nLocale } |
| 49 | + ); |
| 50 | +}; |
| 51 | + |
| 52 | +module.exports.mfa = MFA_TYPE_OPTIONAL; |
| 53 | +module.exports.allowed = checkMFA; |
| 54 | +module.exports.transports = [ActionTransport.amqp, ActionTransport.internal]; |
0 commit comments