Exemplo n.º 1
0
const createPersonaAttributeStages = ({ groupType }) => {
  const personaAttrsKey = groupType.split('.')[2] || '';

  const lookupStage = createStagePipeline('$lookup', {
    from: 'personaAttributes',
    as: 'personaAttrs',
    let: { personaId: '$person._id' },
    pipeline: [
      { $match:
      { $expr:
      { $and: [
            { $eq: ['$personaId', '$$personaId'] },
            { $eq: ['$key', personaAttrsKey] }
      ] }
      }
      }]
  });

  const existsMatchStage = createStagePipeline('$match', {
    personaAttrs: { $exists: true }
  });

  const unwindStage = createStagePipeline('$unwind', {
    path: '$personaAttrs'
  });

  const matchAttrsKeyStage = createStagePipeline('$match', {
    'personaAttrs.key': personaAttrsKey
  });

  return lookupStage
    .concat(existsMatchStage)
    .concat(unwindStage)
    .concat(matchAttrsKeyStage);
};
Exemplo n.º 2
0
/**
 * @param {object} - valueType {*}
 *                   groupType {*}
 *                   operatorType {*}
 *                   timezone {string}
 *                   contextActivityDefinitionType {any}
 * @returns {immutable.List} grouping pipeline
 */
export default ({ valueType, groupType, operatorType, timezone, contextActivityDefinitionType }) => {
  const valueOpCase = getValueOpCase({ valueType, operatorType });

  const isPersonaImportGroup = groupType.startsWith('persona.import.');
  const isContextActivities = groupType.startsWith('statement.context.contextActivities.');

  // 1st stages
  let preReqs;

  if (isPersonaImportGroup) {
    preReqs = createPersonaAttributeStages({ valueType, groupType, valueOpCase });
  } else {
    const existsMatch = getExistsMatch({ valueType, groupType, valueOpCase });
    const existsMatchStage = createStagePipeline('$match', existsMatch);
    if (isContextActivities) {
      const unwindStage = createStagePipeline('$unwind', { path: `$${groupType}` });
      preReqs = existsMatchStage.concat(unwindStage);
    } else {
      preReqs = existsMatchStage;
    }
  }

  // 2nd stages
  const contextActivityTypeMatch = getContextActivityTypeMatch(groupType, contextActivityDefinitionType);
  const contextActivityTypeMatchStage = contextActivityTypeMatch ? createStagePipeline('$match', contextActivityTypeMatch) : [];

  // 3rd stages
  const projections = getProjections({
    valueType,
    groupType: isPersonaImportGroup ? 'personaAttrs.value' : groupType,
    valueOpCase,
    timezone,
  });
  const projectStage = createStagePipeline('$project', projections);

  // 4th stages
  const countPipeline = fromJS(getGroupPipeline({ operatorType, groupType, valueOpCase, projections, timezone }));

  // 5th stages
  const postReqs = fromJS(getPostReqs());

  // Concat all stages
  return preReqs
    .concat(contextActivityTypeMatchStage)
    .concat(projectStage)
    .concat(countPipeline)
    .concat(postReqs);
};