postSample(reqBody, userObj) {
    const now = new Date().toISOString();
    const userName = userObj ? userObj.name : false;
    const cmds = [];
    let subject;
    let sampleName;
    let aspectObj;

    return db.Subject.findById(reqBody.subjectId)
    .then((subjFromDb) => {
      if (!subjFromDb || !subjFromDb.isPublished) {
        throw new redisErrors.ResourceNotFoundError({
          explanation: `Subject "${reqBody.subjectId}" not found.`,
        });
      }

      subject = subjFromDb;
      return db.Aspect.findById(reqBody.aspectId);
    })
    .then((aspFromDb) => {
      if (!aspFromDb || !aspFromDb.isPublished) {
        throw new redisErrors.ResourceNotFoundError({
          explanation: `Aspect "${reqBody.aspectId}" not found.`,
        });
      }

      aspectObj = aspFromDb;
      return aspFromDb.isWritableBy(userName);
    })
    .then((isWritable) => {
      sampleName = subject.absolutePath + '|' + aspectObj.name;
      if (!isWritable) {
        const err = new redisErrors.UpdateDeleteForbidden({
          explanation: `User "${userName}" does not have write permission ` +
            `for aspect "${aspectObj.name}"`,
        });
        return Promise.reject(err);
      }

      return redisOps.getHashPromise(sampleType, sampleName);
    })
    .then((sampFromRedis) => {
      if (sampFromRedis) {
        throw new apiErrors.DuplicateResourceError({
          explanation: `Sample "${sampFromRedis.name}" already exists.`,
        });
      }

      modelUtils.removeExtraAttributes(reqBody, sampleFieldsArr);
      reqBody.name = sampleName;
      const value = reqBody.value || '';
      reqBody[sampFields.STATUS] = sampleUtils.computeStatus(aspectObj, value);
      reqBody[sampFields.VALUE] = value;
      if (reqBody.relatedLinks) {
        reqBody[sampFields.RLINKS] = JSON.stringify(reqBody.relatedLinks);
      } else {
        reqBody[sampFields.RLINKS] = '[]';
      }

      // defaults
      reqBody[sampFields.PRVS_STATUS] = dbConstants.statuses.Invalid;
      reqBody[sampFields.STS_CHANGED_AT] = now;
      reqBody[sampFields.UPD_AT] = now;
      reqBody[sampFields.CREATED_AT] = now;
      if (userObj) {
        reqBody[sampFields.PROVIDER] = userObj.id;
        reqBody[sampFields.USER] = JSON.stringify({
          name: userName,
          email: userObj.email,
          profile: {
            name: userObj.profile.name,
          },
        });
      }

      // add the aspect to the subjectSet
      cmds.push(redisOps.addAspectInSubjSetCmd(
        subject.absolutePath, aspectObj.name)
      );

      // add subject absolute path to aspect-to-subject resource mapping
      cmds.push(redisOps.addSubjectAbsPathInAspectSet(
        aspectObj.name, subject.absolutePath)
      );

      // add sample to the master list of sample index
      cmds.push(redisOps.addKeyToIndexCmd(sampleType, sampleName));

      // create a sample set to store the values
      cmds.push(redisOps.setHashMultiCmd(sampleType, sampleName, reqBody));

      return redisOps.executeBatchCmds(cmds);
    })
    .then(() => redisOps.getHashPromise(sampleType, sampleName))
    .then((sampleObj) => sampleStore.arrayObjsStringsToJson(
      sampleObj, constants.fieldsToStringify.sample));
  }
/**
   * Post sample. First get subject from db, then get aspect from db, then try
   * to get sample from Redis. Is sample found, throw error, else create
   * sample. Update sample index and subject set as well.
   * @param  {Object} reqBody - The sample object to be created
   * @param {Object} userObj - The user performing the write operation
   * @returns {Promise} - Resolves to a sample object
   */
Post sample. First get subject from db, then get aspect from db, then try
to get sample from Redis. Is sample found, throw error, else create
sample. Update sample index and subject set as well.
@param  {Object} reqBody - The sample object to be created
@param {Object} userObj - The user performing the write operation
@returns {Promise} - Resolves to a sample object
Post sample . First get subject from db then get aspect from db then try to get sample from Redis . Is sample found throw error else create sample . Update sample index and subject set as well . @param { Object } reqBody - The sample object to be created @param { Object } userObj - The user performing the write operation @returns { Promise } - Resolves to a sample object
postSample(reqBody, userObj) {
    const now = new Date().toISOString();
    const userName = userObj ? userObj.name : false;
    const cmds = [];
    let subject;
    let sampleName;
    let aspectObj;
    return db.Subject.findById(reqBody.subjectId)
    .then((subjFromDb) => {
      if (!subjFromDb || !subjFromDb.isPublished) {
        throw new redisErrors.ResourceNotFoundError({
          explanation: `Subject "${reqBody.subjectId}" not found.`,
        });
      }
      subject = subjFromDb;
      return db.Aspect.findById(reqBody.aspectId);
    })
    .then((aspFromDb) => {
      if (!aspFromDb || !aspFromDb.isPublished) {
        throw new redisErrors.ResourceNotFoundError({
          explanation: `Aspect "${reqBody.aspectId}" not found.`,
        });
      }
      aspectObj = aspFromDb;
      return aspFromDb.isWritableBy(userName);
    })
    .then((isWritable) => {
      sampleName = subject.absolutePath + '|' + aspectObj.name;
      if (!isWritable) {
        const err = new redisErrors.UpdateDeleteForbidden({
          explanation: `User "${userName}" does not have write permission ` +
            `for aspect "${aspectObj.name}"`,
        });
        return Promise.reject(err);
      }
      return redisOps.getHashPromise(sampleType, sampleName);
    })
    .then((sampFromRedis) => {
      if (sampFromRedis) {
        throw new apiErrors.DuplicateResourceError({
          explanation: `Sample "${sampFromRedis.name}" already exists.`,
        });
      }
      modelUtils.removeExtraAttributes(reqBody, sampleFieldsArr);
      reqBody.name = sampleName;
      const value = reqBody.value || '';
      reqBody[sampFields.STATUS] = sampleUtils.computeStatus(aspectObj, value);
      reqBody[sampFields.VALUE] = value;
      if (reqBody.relatedLinks) {
        reqBody[sampFields.RLINKS] = JSON.stringify(reqBody.relatedLinks);
      } else {
        reqBody[sampFields.RLINKS] = '[]';
      }
      reqBody[sampFields.PRVS_STATUS] = dbConstants.statuses.Invalid;
      reqBody[sampFields.STS_CHANGED_AT] = now;
      reqBody[sampFields.UPD_AT] = now;
      reqBody[sampFields.CREATED_AT] = now;
      if (userObj) {
        reqBody[sampFields.PROVIDER] = userObj.id;
        reqBody[sampFields.USER] = JSON.stringify({
          name: userName,
          email: userObj.email,
          profile: {
            name: userObj.profile.name,
          },
        });
      }
      cmds.push(redisOps.addAspectInSubjSetCmd(
        subject.absolutePath, aspectObj.name)
      );
      cmds.push(redisOps.addSubjectAbsPathInAspectSet(
        aspectObj.name, subject.absolutePath)
      );
      cmds.push(redisOps.addKeyToIndexCmd(sampleType, sampleName));
      cmds.push(redisOps.setHashMultiCmd(sampleType, sampleName, reqBody));
      return redisOps.executeBatchCmds(cmds);
    })
    .then(() => redisOps.getHashPromise(sampleType, sampleName))
    .then((sampleObj) => sampleStore.arrayObjsStringsToJson(
      sampleObj, constants.fieldsToStringify.sample));
  }
postSample ( reqBody , userObj ) { const now = new Date ( ) . toISOString ( ) ; const userName = userObj ? userObj . name : false ; const cmds = [ ] ; let subject ; let sampleName ; let aspectObj ; return db . Subject . findById ( reqBody . subjectId ) . then ( ( subjFromDb ) => { if ( ! subjFromDb || ! subjFromDb . isPublished ) { throw new redisErrors . ResourceNotFoundError ( { explanation : ` ${ reqBody . subjectId } ` , } ) ; } subject = subjFromDb ; return db . Aspect . findById ( reqBody . aspectId ) ; } ) . then ( ( aspFromDb ) => { if ( ! aspFromDb || ! aspFromDb . isPublished ) { throw new redisErrors . ResourceNotFoundError ( { explanation : ` ${ reqBody . aspectId } ` , } ) ; } aspectObj = aspFromDb ; return aspFromDb . isWritableBy ( userName ) ; } ) . then ( ( isWritable ) => { sampleName = subject . absolutePath + '|' + aspectObj . name ; if ( ! isWritable ) { const err = new redisErrors . UpdateDeleteForbidden ( { explanation : ` ${ userName } ` + ` ${ aspectObj . name } ` , } ) ; return Promise . reject ( err ) ; } return redisOps . getHashPromise ( sampleType , sampleName ) ; } ) . then ( ( sampFromRedis ) => { if ( sampFromRedis ) { throw new apiErrors . DuplicateResourceError ( { explanation : ` ${ sampFromRedis . name } ` , } ) ; } modelUtils . removeExtraAttributes ( reqBody , sampleFieldsArr ) ; reqBody . name = sampleName ; const value = reqBody . value || '' ; reqBody [ sampFields . STATUS ] = sampleUtils . computeStatus ( aspectObj , value ) ; reqBody [ sampFields . VALUE ] = value ; if ( reqBody . relatedLinks ) { reqBody [ sampFields . RLINKS ] = JSON . stringify ( reqBody . relatedLinks ) ; } else { reqBody [ sampFields . RLINKS ] = '[]' ; } reqBody [ sampFields . PRVS_STATUS ] = dbConstants . statuses . Invalid ; reqBody [ sampFields . STS_CHANGED_AT ] = now ; reqBody [ sampFields . UPD_AT ] = now ; reqBody [ sampFields . CREATED_AT ] = now ; if ( userObj ) { reqBody [ sampFields . PROVIDER ] = userObj . id ; reqBody [ sampFields . USER ] = JSON . stringify ( { name : userName , email : userObj . email , profile : { name : userObj . profile . name , } , } ) ; } cmds . push ( redisOps . addAspectInSubjSetCmd ( subject . absolutePath , aspectObj . name ) ) ; cmds . push ( redisOps . addSubjectAbsPathInAspectSet ( aspectObj . name , subject . absolutePath ) ) ; cmds . push ( redisOps . addKeyToIndexCmd ( sampleType , sampleName ) ) ; cmds . push ( redisOps . setHashMultiCmd ( sampleType , sampleName , reqBody ) ) ; return redisOps . executeBatchCmds ( cmds ) ; } ) . then ( ( ) => redisOps . getHashPromise ( sampleType , sampleName ) ) . then ( ( sampleObj ) => sampleStore . arrayObjsStringsToJson ( sampleObj , constants . fieldsToStringify . sample ) ) ; }
Post sample.
Post sample .
// defaults // add the aspect to the subjectSet // add subject absolute path to aspect-to-subject resource mapping // add sample to the master list of sample index // create a sample set to store the values