{"size": 1965, "ext": "js", "max_stars_repo_path": "policies/delete-user/delete-user.js", "max_stars_repo_name": "nutes-uepb/express-apigw-plugin-base", "max_stars_repo_stars_event_min_datetime": "2021-03-17T18:11:33.000Z", "max_stars_repo_stars_event_max_datetime": "2021-03-17T18:11:33.000Z", "max_issues_repo_path": "policies/delete-user/delete-user.js", "max_issues_repo_name": "nutes-uepb/express-apigw-plugin-base", "max_issues_repo_issues_event_min_datetime": "2020-06-25T20:59:08.000Z", "max_issues_repo_issues_event_max_datetime": "2020-06-25T22:17:57.000Z", "max_forks_repo_path": "policies/delete-user/delete-user.js", "max_forks_repo_name": "nutes-uepb/express-apigw-plugin-base", "max_forks_repo_forks_event_min_datetime": "2020-07-15T15:04:23.000Z", "max_forks_repo_forks_event_max_datetime": "2020-07-15T15:04:23.000Z"}
{"max_stars_count": 1, "max_issues_count": 1, "max_forks_count": 1, "avg_line_length": 31.6935483871, "max_line_length": 91, "alphanum_fraction": 0.6076335878}
'use strict'

/**
 * Delete user gateway policy
 */

let gatewayService = require('express-gateway/lib/services').user
let userService = require('../../services/http-client')

module.exports = function (actionParams, userServiceGwTest, axiosTest) {
    // Test context. Mocks serServiceGwTest and axiosTest
    if (userServiceGwTest && axiosTest) {
        gatewayService = userServiceGwTest
        userService = axiosTest
    }

    return async (req, res, next) => {

        const userId = req.url.split('/').pop()

        try {
            // 1. Delete user in account service.
            const responseAccount = await userService.delete(
                `${actionParams.urlDeleteService}/${userId}`,
                { headers: req.headers }
            )

            // 2. Find user at gateway and delete or skip if there is not.
            await deleteUserGateway(userId)

            return res.status(responseAccount.status).send(responseAccount.data)
        } catch (err) {

            if (err.response && err.response.status && err.response.data) {
                return res.status(err.response.status).send(err.response.data)
            }
            return res.status(500).send({
                'code': 500,
                'message': 'INTERNAL SERVER ERROR',
                'description': 'An internal server error has occurred.'
            })
        }
    }
}


/**
 * Function used to recover user and remove them from Gateway
 * @param userId
 * @returns {Promise<void>}
 */
async function deleteUserGateway(userId) {
    try {
        //    When the deleted user was not found in the gateway,
        //    it occurs when the user has not yet logged in to the platform.
        const userGateway = await gatewayService.findByUsernameOrId(userId)
        if (userGateway) await gatewayService.remove(userGateway.id)
    } catch (err) {
        console.error(new Date().toISOString(), '| Error Removing User on Gateway:\n', err)
    }
}
