<commit_before>'use strict'

var Promise = require('bluebird')

var Hoek = require('hoek')
var _ = require('lodash')

module.exports = function (payload) {
  var self = Hoek.clone(global.ABIBAO.services.domain)

  return new Promise(function (resolve, reject) {
    try {
      var idSurvey = self.getIDfromURN(payload.urn)
      self.r.table('surveys').get(idSurvey).merge(function (survey) {
        return {
          company: self.r.table('entities').get(survey('company'))('type'),
          campaign: self.r.table('campaigns').get(survey('campaign')).merge(function (campaign) {
            return {
              urn: survey('campaign'),
              items: self.r.table('campaigns_items').filter({campaign: campaign('id')}).orderBy('createdAt').coerceTo('array').merge(function (item) {
                return {
                  urn: item('id'),
                  choices: self.r.table('campaigns_items_choices').filter({item: item('id')}).orderBy('position').coerceTo('array').merge(function (choice) {
                    return {
                      meta: choice('prefix').add('__').add(choice('suffix')),
                      urn: choice('id')
                    }
                  }).without('id', 'item', 'campaign', 'createdAt', 'modifiedAt')
                }
              }).without('id', 'campaign', 'createdAt', 'modifiedAt')
            }
          }).without('id', 'company', 'price', 'currency', 'createdAt', 'modifiedAt')
        }
      }).without('id', 'charity', 'individual')
        .then(function (survey) {
          survey.name = survey.campaign.name
          if (survey.company === global.ABIBAO.constants.DomainConstant.ABIBAO_CONST_ENTITY_TYPE_COMPANY) { survey.fromCompany = true }
          if (survey.company === global.ABIBAO.constants.DomainConstant.ABIBAO_CONST_ENTITY_TYPE_ABIBAO) { survey.fromAbibao = true }
          if (survey.company === global.ABIBAO.constants.DomainConstant.ABIBAO_CONST_ENTITY_TYPE_CHARITY) { survey.fromCharity = true }
          _.map(survey.campaign.items, function (item) {
            survey.items = survey.campaign.items
            if (_.isUndefined(survey.answers)) { survey.answers = {} }
          })
          _.map(survey.campaign.items, function (item) {
            item.urn = self.getURNfromID(item.urn, 'item')
            _.map(item.choices, function (choice) {
              choice.urn = self.getURNfromID(choice.urn, 'choice')
            })
          })
          delete survey.campaign
          delete survey.company
          survey.urn = payload.urn
          resolve(survey)
        })
        .catch(function (error) {
          reject(error)
        })
    } catch (e) {
      reject(e)
    }
  })
}<commit_msg>[survey] add screenWelcomeContent and screenThankYouContent<commit_after>'use strict'

var Promise = require('bluebird')

var Hoek = require('hoek')
var _ = require('lodash')

module.exports = function (payload) {
  var self = Hoek.clone(global.ABIBAO.services.domain)

  return new Promise(function (resolve, reject) {
    try {
      var idSurvey = self.getIDfromURN(payload.urn)
      self.r.table('surveys').get(idSurvey).merge(function (survey) {
        return {
          company: self.r.table('entities').get(survey('company'))('type'),
          campaign: self.r.table('campaigns').get(survey('campaign')).merge(function (campaign) {
            return {
              urn: survey('campaign'),
              items: self.r.table('campaigns_items').filter({campaign: campaign('id')}).orderBy('createdAt').coerceTo('array').merge(function (item) {
                return {
                  urn: item('id'),
                  choices: self.r.table('campaigns_items_choices').filter({item: item('id')}).orderBy('position').coerceTo('array').merge(function (choice) {
                    return {
                      meta: choice('prefix').add('__').add(choice('suffix')),
                      urn: choice('id')
                    }
                  }).without('id', 'item', 'campaign', 'createdAt', 'modifiedAt')
                }
              }).without('id', 'campaign', 'createdAt', 'modifiedAt')
            }
          }).without('id', 'company', 'price', 'currency', 'createdAt', 'modifiedAt')
        }
      }).without('id', 'charity', 'individual')
        .then(function (survey) {
          survey.name = survey.campaign.name
          survey.screenWelcomeContent = survey.campaign.screenWelcomeContent
          survey.screenThankYouContent = survey.campaign.screenThankYouContent
          if (survey.company === global.ABIBAO.constants.DomainConstant.ABIBAO_CONST_ENTITY_TYPE_COMPANY) { survey.fromCompany = true }
          if (survey.company === global.ABIBAO.constants.DomainConstant.ABIBAO_CONST_ENTITY_TYPE_ABIBAO) { survey.fromAbibao = true }
          if (survey.company === global.ABIBAO.constants.DomainConstant.ABIBAO_CONST_ENTITY_TYPE_CHARITY) { survey.fromCharity = true }
          _.map(survey.campaign.items, function (item) {
            survey.items = survey.campaign.items
            if (_.isUndefined(survey.answers)) { survey.answers = {} }
          })
          _.map(survey.campaign.items, function (item) {
            item.urn = self.getURNfromID(item.urn, 'item')
            _.map(item.choices, function (choice) {
              choice.urn = self.getURNfromID(choice.urn, 'choice')
            })
          })
          delete survey.campaign
          delete survey.company
          survey.urn = payload.urn
          resolve(survey)
        })
        .catch(function (error) {
          reject(error)
        })
    } catch (e) {
      reject(e)
    }
  })
}
{'fraction_non_alphanumeric': 0.14889836531627576, 'fraction_numerical': 0.0, 'mean_word_length': 2.3327412670219063, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7149614', 'n_tokens_mistral': 1678, 'n_tokens_neox': 1614, 'n_words': 252}