{"size": 1401, "ext": "js", "max_stars_repo_path": "api/modules/agent/controllers/deleteWebhook.agent.controller.js", "max_stars_repo_name": "fossabot/articulate", "max_stars_repo_stars_event_min_datetime": "2020-12-06T22:55:26.000Z", "max_stars_repo_stars_event_max_datetime": "2020-12-06T22:55:26.000Z", "max_issues_repo_path": "api/modules/agent/controllers/deleteWebhook.agent.controller.js", "max_issues_repo_name": "octalxia/articulate", "max_issues_repo_issues_event_min_datetime": "2018-06-14T13:27:23.000Z", "max_issues_repo_issues_event_max_datetime": "2018-06-14T20:29:23.000Z", "max_forks_repo_path": "api/modules/agent/controllers/deleteWebhook.agent.controller.js", "max_forks_repo_name": "octalxia/articulate", "max_forks_repo_forks_event_min_datetime": "2018-10-16T17:01:49.000Z", "max_forks_repo_forks_event_max_datetime": "2018-10-16T17:01:49.000Z"}
{"max_stars_count": 1, "max_issues_count": 3, "max_forks_count": 1, "avg_line_length": 28.5918367347, "max_line_length": 102, "alphanum_fraction": 0.4532476802}
'use strict';
const Boom = require('boom');
const Async = require('async');

module.exports = (request, reply) => {

    const agentId = request.params.id;
    const redis = request.server.app.redis;

    Async.waterfall([
        (cb) => {

            redis.exists(`agentWebhook:${agentId}`, (err, exists) => {

                if (err){
                    const error = Boom.badImplementation('An error occurred retrieving the webhook.');
                    return cb(error);
                }
                if (exists){
                    return cb(null);
                }
                const error = Boom.notFound('The specified webhook doesn\'t exists');
                return cb(error);
            });
        },
        (cb) => {

            redis.del(`agentWebhook:${agentId}`, (err, success) => {

                if (err){
                    const error = Boom.badImplementation('An error occurred deleting the webhook.');
                    return cb(error);
                }
                if (success === 1){
                    return cb(null);
                }
                const error = Boom.notFound('The webhook wasn\'t deleted');
                return cb(error);
            });
        }
    ], (err, result) => {

        if (err){
            return reply(err, null);
        }
        return reply({ message: 'successful operation' }).code(200);
    });
};
