All files / botbuilder-unit/src/messages SessionMessage.js

100% Statements 37/37
75% Branches 6/8
100% Functions 13/13
100% Lines 37/37
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77      2x           2x     16x   16x 16x     2x 2x   2x 16x 16x 16x       2x 16x 8x 8x 8x         8x 8x 8x 8x 6x 6x 6x       2x       8x       2x 16x 16x   16x 16x   16x 16x 16x       16x 16x           2x 2x
/* jslint es6 */
'use strict';
 
const DEFAULT_ADDRESS = {
  channelId: 'console',
  user: {id: 'user', name: 'User1'},
  bot: {id: 'bot', name: 'Bot'},
  conversation: {id: 'Convo1'}
};
const BaseScriptStep = require('./BaseScriptStep');
 
function SessionMessage(step, config, bot, logReporter, prevStepPromise) {
  BaseScriptStep.call(this, step, config, bot, logReporter, prevStepPromise);
 
  this.stepFinishedPromise = Promise.all([prevStepPromise]).then(() => {
    return this.send();
  });
}
SessionMessage.prototype = Object.create(BaseScriptStep.prototype);
SessionMessage.prototype.constructor = SessionMessage;
 
SessionMessage.prototype.initSession = function () {
  return new Promise((resolve, reject) => {
    this.bot.loadSession(DEFAULT_ADDRESS, (session) => {
      return resolve(session);
    });
  });
}
SessionMessage.prototype.updateSessionState = function (session) {
  if ("function" == typeof this.config.session) {
    return new Promise((resolve, reject) => {
      this.config.session(session).then((response) => {
          resolve(session);
        })
        .catch(reject);
    })
  } else {
    for (let key in this.config.session) {
      Eif (this.config.session.hasOwnProperty(key)) {
        let value = this.config.session[key];
        if ("object" == typeof value) {
          for (let field in value) {
            Eif (value.hasOwnProperty(field)) {
              session[key][field] = value[field];
            }
          }
        } else {
          session[key] = this.config.session[key];
        }
      }
    }
    return Promise.resolve(session);
 
  }
}
SessionMessage.prototype.send = function () {
  return new Promise((resolve, reject) => {
    this.initSession()
      .then((session) => {
        let handler = (session) => {
          this.updateSessionState(session)
            .then(() => {
              this.logReporter.session(this.step, session);
              this.bot.removeListener('routing', handler);
              session.save();
            })
            .catch(reject);
        }
        this.bot.addListener('routing', handler);
        resolve();
      })
  })
 
}
 
module.exports = SessionMessage;
module.exports.DEFAULT_ADDRESS = DEFAULT_ADDRESS;