sub-packs/themes/zealder-admin-theme/lib/get-auth-sites.js
function getAuthSites(req, res, next) {
  const zlUserRole = req.app.models.zlUserRole;
  const zlSite = req.app.models.zlSite;
  let { themeData, socketQuery, emiter } = res.locals;
  themeData['authSites'] = {}, themeData['managingSite']=null;
  socketQuery['authSites'] = [];
  let filter = {
    where: {
      "usrId": req.accessToken.userId,
      "uroFor": 'Site'
    }
  }
  zlUserRole.find(filter, (err, data) => {
    if (err) req.app.error(err);
    if (data) {
      let promises = [];
      _.forEach(data, function(val) {
        val = val.toJSON();
        promises.push( new Promise((resolve, reject) => {
          // get site info
          zlSite.findById(val.uroForId, (err, v) => {
            if (err) req.app.error(err);
            if (v) {
              // allows socket to push list of authSites for rooms
              socketQuery['authSites'].push(val.uroForId);

              themeData.authSites[val.uroForId] = {
                roles: val.uroRoles,
                zsDomain: v.zsDomain,
                zsBaseUrlPath: (v.zsBaseUrlPath)?v.zsBaseUrlPath:'',
                zsName: v.zsName,
                zsApps: v.zsApps,
                zsTheme: v.zsTheme,
                url: `${v.zsDomain}/`
              }
              if (v.zsBaseUrlPath) {
                themeData.authSites[val.uroForId]['url'] += `${v.zsBaseUrlPath}/`;
              }
              if ('https' === v.zsProtocol) {
                themeData.authSites[val.uroForId]['url'] =
                  `https://${themeData.authSites[val.uroForId]['url']}`;
              }
              else {
                themeData.authSites[val.uroForId]['url'] =
                  `http://${themeData.authSites[val.uroForId]['url']}`;
              }

              // prepare url that will be used to create screenCap
              val.screenUrl = themeData.authSites[val.uroForId]['url'];

              // set default site to Manage
              if (req.get('X-Managing-Site')
                && val.uroForId == req.get('X-Managing-Site')) {
                // use header value when available
                themeData.managingSite = val.uroForId;
              }
              if (!themeData.managingSite) themeData.managingSite = val.uroForId;
              // allows socket to join managingSite rooms
              socketQuery['managingSite'] = themeData.managingSite;

              // give opportunity for any child theme to populate other data
              // from zlSite to themeData
              emiter.emit('zealder-admin-theme managing site', v, val);

              // setup screenCap
              if (v.zsInternalConfig && v.zsInternalConfig.ScreenCap) {
                themeData.authSites[val.uroForId]['screenCap'] = v.zsInternalConfig.ScreenCap;
                resolve();
              }
              // api/file calls don't need to make screen cap (too much S3 request)
              else if (req.app.locals.bypass(req)) resolve();
              else {
                resolve();
                zlSite.makeScreenCap(val.uroForId, val.screenUrl, req);
              }
            }
            else resolve();
          });
        }));
      });

      Promise.all(promises).then(() => next());
    }
    else next();
  });
}
function getAuthSites(req, res, next) {
  const zlUserRole = req.app.models.zlUserRole;
  const zlSite = req.app.models.zlSite;
  let { themeData, socketQuery, emiter } = res.locals;
  themeData['authSites'] = {}, themeData['managingSite']=null;
  socketQuery['authSites'] = [];
  let filter = {
    where: {
      "usrId": req.accessToken.userId,
      "uroFor": 'Site'
    }
  }
  zlUserRole.find(filter, (err, data) => {
    if (err) req.app.error(err);
    if (data) {
      let promises = [];
      _.forEach(data, function(val) {
        val = val.toJSON();
        promises.push( new Promise((resolve, reject) => {
          // get site info
          zlSite.findById(val.uroForId, (err, v) => {
            if (err) req.app.error(err);
            if (v) {
              // allows socket to push list of authSites for rooms
              socketQuery['authSites'].push(val.uroForId);

              themeData.authSites[val.uroForId] = {
                roles: val.uroRoles,
                zsDomain: v.zsDomain,
                zsBaseUrlPath: (v.zsBaseUrlPath)?v.zsBaseUrlPath:'',
                zsName: v.zsName,
                zsApps: v.zsApps,
                zsTheme: v.zsTheme,
                url: `${v.zsDomain}/`
              }
              if (v.zsBaseUrlPath) {
                themeData.authSites[val.uroForId]['url'] += `${v.zsBaseUrlPath}/`;
              }
              if ('https' === v.zsProtocol) {
                themeData.authSites[val.uroForId]['url'] =
                  `https://${themeData.authSites[val.uroForId]['url']}`;
              }
              else {
                themeData.authSites[val.uroForId]['url'] =
                  `http://${themeData.authSites[val.uroForId]['url']}`;
              }

              // prepare url that will be used to create screenCap
              val.screenUrl = themeData.authSites[val.uroForId]['url'];

              // set default site to Manage
              if (req.get('X-Managing-Site')
                && val.uroForId == req.get('X-Managing-Site')) {
                // use header value when available
                themeData.managingSite = val.uroForId;
              }
              if (!themeData.managingSite) themeData.managingSite = val.uroForId;
              // allows socket to join managingSite rooms
              socketQuery['managingSite'] = themeData.managingSite;

              // give opportunity for any child theme to populate other data
              // from zlSite to themeData
              emiter.emit('zealder-admin-theme managing site', v, val);

              // setup screenCap
              if (v.zsInternalConfig && v.zsInternalConfig.ScreenCap) {
                themeData.authSites[val.uroForId]['screenCap'] = v.zsInternalConfig.ScreenCap;
                resolve();
              }
              // api/file calls don't need to make screen cap (too much S3 request)
              else if (req.app.locals.bypass(req)) resolve();
              else {
                resolve();
                zlSite.makeScreenCap(val.uroForId, val.screenUrl, req);
              }
            }
            else resolve();
          });
        }));
      });

      Promise.all(promises).then(() => next());
    }
    else next();
  });
}
get sites they are autorized to contribute