{"size": 2073, "ext": "js", "max_stars_repo_path": "appLibs/fbktLogin/actions/loadLicenseType/index.js", "max_stars_repo_name": "stlbucket/fbkt-login", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "appLibs/fbktLogin/actions/loadLicenseType/index.js", "max_issues_repo_name": "stlbucket/fbkt-login", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "appLibs/fbktLogin/actions/loadLicenseType/index.js", "max_forks_repo_name": "stlbucket/fbkt-login", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 30.9402985075, "max_line_length": 112, "alphanum_fraction": 0.5672937771}
"use strict";
const Promise = require('bluebird');
const fbkt = require('fbkt');
const loadLicenseTypePermission = require('../loadLicenseTypePermission');
const loadPromoCode = require('../loadPromoCode');

module.exports = callInfo => {
  return fbkt().FbktPipe({
    name: 'load licenseType',
    filename: __filename,
    expectedParams: {},
    pipelineParams: {
      existing: 'findExisting',
      dbLicenseType: 'loadLicenseType'
    },
    pipelineSteps: {
      'findExisting': callInfo => {
        return fbkt().dbTree.fbkt_login.table.license_type.findOne({
          params: {
            applicationId: callInfo.params.applicationId,
            licenseTypeKey: callInfo.params.licenseTypeKey
          }
        });
      },
      'loadLicenseType': callInfo => {
        if (callInfo.params.existing) {
          return callInfo.params.existing;
        } else {
          return fbkt().dbTree.fbkt_login.table.license_type.save({
            params: {
              name: callInfo.params.name,
              applicationId: callInfo.params.applicationId,
              licenseTypeKey: callInfo.params.licenseTypeKey,
              active: true
            }
          });
        }
      },
      'loadLicenseTypePermisssions': callInfo => {
        const permissions = callInfo.params.permissions;
        return Promise.mapSeries(
          permissions,
          permission => {
            return loadLicenseTypePermission({
              params: {
                licenseTypeId:  callInfo.params.dbLicenseType.id,
                permission: permission
              }
            })
          }
        )
      },
      'loadPromoCodes': callInfo => {
        const promoCodes = callInfo.params.promoCodes || [];
        return Promise.mapSeries(
          promoCodes,
          promoCode => {
            const promoCodeToLoad = Object.assign(promoCode, {licenseTypeId: callInfo.params.dbLicenseType.id});
            return loadPromoCode({
              params: promoCodeToLoad
            })
          }
        )
      }
    }
  }, callInfo);
};