'use strict';

var util = require('util');

/**
 * @param {secret} SLACK_WEBHOOK_URL
 * @param {secret} SLACK_CHANNEL_NAME
 */
module.exports = function(ctx, cb) {
    var params = ctx.body;

    if (!ctx.secrets.SLACK_WEBHOOK_URL || !ctx.secrets.SLACK_CHANNEL_NAME) {
        return cb(new Error('"SLACK_WEBHOOK_URL" and "SLACK_CHANNEL_NAME" parameters required'));
    }

    if (!params.service || !params.members) {
        return cb(new Error('"service" and "members" parameters required'));
    }

    var SLACK_WEBHOOK_URL = ctx.secrets.SLACK_WEBHOOK_URL;
    var SLACK_CHANNEL_NAME = ctx.secrets.SLACK_CHANNEL_NAME;
    var slack = require('slack-notify')(SLACK_WEBHOOK_URL);
    var service = params.service;
    var members = params.members.join(', ');
    var messages = {
        tfa_disabled: util.format('Users without TFA on `%s` are %s', service, members)
    };

    slack.send({
      channel: SLACK_CHANNEL_NAME,
      icon_emoji: ':warning:',
      text: messages.tfa_disabled,
      unfurl_links: 0,
      username: 'TFA Monitor'
    });

    cb();
};

@@ -8,19 +8,55 @@
 rict';%0A%0A
-var
+const assert = require('assert');%0Aconst
  util = 
@@ -152,16 +152,99 @@
 EL_NAME%0A
+ * @param JSON     body%0A *%0A * body: %5B%7Bname: 'GitHub', accounts: %5B'john', 'mark'%5D%7D%5D%0A
  */%0Amodu
@@ -260,16 +260,8 @@
 s = 
-function
 (ctx
@@ -269,359 +269,118 @@
  cb)
+ =%3E
  %7B%0A  
-  var params = ctx.body;%0A%0A    if (!ctx.secrets.SLACK_WEBHOOK_URL %7C%7C !ctx.secrets.SLACK_CHANNEL_NAME) %7B%0A        return cb(new Error('%22SLACK_WEBHOOK_URL%22 and %22SLACK_CHANNEL_NAME%22 parameters required
+assert(ctx.secrets.SLACK_CHANNEL_NAME, 'SLACK_CHANNEL_NAME secret is missing!
 ')
-)
 ;%0A  
-  %7D%0A%0A    if (!params.service %7C%7C !params.members) %7B%0A        return cb(new Error('%22service%22 and %22members%22 parameters required'));%0A    %7D%0A%0A    var 
+assert(ctx.secrets.
 SLAC
@@ -392,31 +392,20 @@
 HOOK_URL
- = ctx.secrets.
+,  '
 SLACK_WE
@@ -417,78 +417,107 @@
 _URL
-;%0A    var SLACK_CHANNEL_NAME = ctx.secrets.SLACK_CHANNEL_NAME;%0A    var
+ secret is missing!');%0A  assert(Array.isArray(ctx.body), 'Body content is not an Array!');%0A%0A  const
  sla
@@ -545,16 +545,28 @@
 otify')(
+ctx.secrets.
 SLACK_WE
@@ -583,72 +583,141 @@
 ;%0A  
-  var service = params.service;%0A    var members = params.members
+slack.onError = (err) =%3E cb(err);%0A%0A  ctx.body.forEach(service =%3E %7B%0A    let accounts = service.accounts.map(account =%3E %60*$%7Baccount%7D*%60)
 .joi
@@ -733,19 +733,19 @@
     
-var
+let
  message
 s = 
@@ -744,49 +744,13 @@
 sage
-s = %7B%0A        tfa_disabled: util.format('
+  = %60
 User
@@ -763,52 +763,51 @@
 out 
-T
+M
 FA on 
-%60%25s%60 are %25s', service, members)%0A    %7D
+%5C%60$%7Bservice.name%7D%5C%60 are $%7Baccounts%7D%60
 ;%0A%0A 
@@ -837,16 +837,28 @@
 hannel: 
+ctx.secrets.
 SLACK_CH
@@ -910,89 +910,50 @@
     
-text: messages.tfa_disabled,%0A      unfurl_links: 0,%0A      username: 'TFA Monitor'
+username: 'MFA Agent',%0A      text: message
 %0A   
@@ -957,19 +957,23 @@
     %7D);%0A
-%0A
   
+%7D);%0A%0A
   cb();%0A

Update Slack notifier to use new data format.