'use strict';

/**
 * @module computation-service
 */
const common = require('coinstac-common');
const Computation = common.models.computation.Computation;
const ModelService = require('../model-service');
const RemoteComputationResult = common.models.computation.RemoteComputationResult;

/**
 * @extends ModelService
 */
class ComputationService extends ModelService {
  modelServiceHooks() {
    return {
      dbName: 'computations',
      ModelType: Computation,
    };
  }

  /**
   * Kick off a remote computation result.
   *
   * @param {Object} options
   * @param {string} options.consortiumId
   * @param {string} options.projectId
   * @returns {Promise}
   */
  kickoff({ consortiumId, projectId }) {
    const client = this.client;

    return Promise.all([
      client.consortia.db.get(consortiumId),
      client.projects.db.get(projectId),
    ]).then(([consortium, project]) => {
      if (!consortium.activeComputationId) {
        throw new Error(
          `Consortium "${consortium.label}" doesn't have an active computation`
        );
      }

      const runId = `${consortiumId}-${activeComputationId}`;

      const result = new RemoteComputationResult({
        _id: runId,
        computationId: consortium.activeComputationId,
        consortiumId,
      });

      return client.pool.triggerRunner(result, project);
    });
  }
}

module.exports = ComputationService;

@@ -149,16 +149,50 @@
 tation;%0A
+const crypto = require('crypto');%0A
 const Mo
@@ -1117,16 +1117,66 @@
 runId = 
+crypto%0A        .createHash('md5')%0A        .update(
 %60$%7Bconso
@@ -1183,17 +1183,16 @@
 rtiumId%7D
--
 $%7Bactive
@@ -1206,16 +1206,40 @@
 tionId%7D%60
+)%0A        .digest(%22hex%22)
 ;%0A%0A     

Switch runId to use md5.