<commit_before>/* eslint-disable prefer-template */
/*!
 * Copyright (c) 2015-2017 Cisco Systems, Inc. See LICENSE file.
 */

import {waitForValue, SparkPlugin} from '@ciscospark/spark-core';

import reportRequest from './report-request';

const Ediscovery = SparkPlugin.extend({
  namespace: 'Ediscovery',

  @waitForValue('@')
  createReport(user, keywords, spaceNames, spaceIds, range) {
    const self = this;
    this.spark.internal.user.asUUID(user).then((uuids) => {
      const body = reportRequest;
      body.userIds = uuids;
      body.keywords = keywords;
      body.spaceNames = spaceNames;
      body.spaceIds = spaceIds;
      body.range = range;

      return self.request({
        method: 'POST',
        service: 'ediscovery',
        resource: 'reports',
        body
      }).then((res) => {
        console.log(res);
        return res;
      }).catch((reason) => {
        console.log(reason);
        return reason;
      });
    }).catch((reason) => {
      console.log(reason);
      return reason;
    });
  },

  @waitForValue('@')
  getReport(reportId) {
    const id = reportId;
    return this.request({
      method: 'GET',
      service: 'ediscovery',
      resource: 'reports/' + id
    }).then((res) => {
      console.log(res);
      return res;
    }).catch((reason) => {
      console.log(reason);
      return reason;
    });
  },

  @waitForValue('@')
  getReports() {
    return this.request({
      method: 'GET',
      service: 'ediscovery',
      resource: 'reports'
    }).then((res) => {
      console.log(res);
      return res;
    }).catch((reason) => {
      console.log(reason);
      return reason;
    });
  },

  @waitForValue('@')
  deleteReport(reportId) {
    const id = reportId;
    return this.request({
      method: 'DELETE',
      service: 'ediscovery',
      resource: 'reports/' + id
    }).then((res) => {
      console.log(res);
      return res;
    }).catch((reason) => {
      console.log(reason);
      return reason;
    });
  },

  @waitForValue('@')
  restartReport(reportId) {
    const id = reportId;
    return this.request({
      method: 'PUT',
      service: 'ediscovery',
      resource: 'reports/' + id
    }).then((res) => {
      console.log(res);
      return res;
    }).catch((reason) => {
      console.log(reason);
      return reason;
    });
  }
});

export default Ediscovery;
<commit_msg>feat(ediscovery): accepting emails and converting them to uuids<commit_after>/* eslint-disable prefer-template */
/*!
 * Copyright (c) 2015-2017 Cisco Systems, Inc. See LICENSE file.
 */

import {waitForValue, SparkPlugin} from '@ciscospark/spark-core';

import reportRequest from './report-request';

const Ediscovery = SparkPlugin.extend({
  namespace: 'Ediscovery',

  @waitForValue('@')
  createReport(emails, keywords, spaceNames, spaceIds, range) {
    const self = this;

    return this.asUUIDs(emails).then((uuids) => {
      const body = reportRequest;
      body.userIds = uuids;
      body.keywords = keywords;
      body.spaceNames = spaceNames;
      body.spaceIds = spaceIds;
      body.range = range;

      return self.request({
        method: 'POST',
        service: 'ediscovery',
        resource: 'reports',
        body
      }).then((res) => {
        console.log(res);
        return res;
      }).catch((reason) => {
        console.log(reason);
        return reason;
      });
    }).catch((reason) => {
      console.log(reason);
      return reason;
    });
  },

  /**
   * Retrieve the user UUID associated with each supplied email. If an email with no user
   * is found an error is thrown. If multiple such emails exist only the first encountered
   * throws an error.
   *
   * @param {Array.<String>} emails A list of emails to resolve
   * @returns {Promise} The UUIDs associated with the emails
   */
  asUUIDs(emails) {
    if (emails.length <= 0) {
      return Promise.resolve([]);
    }
    const promises = [];
    emails.forEach((userEmail) => {
      promises.push(
        // The people list API only supports a single email
        this.spark.people.list({email: userEmail})
          .then((result) => {
            if (result.items.length <= 0) {
              throw new Error('Unable to find user with email ' + userEmail);
            }
            const decodedId = Buffer.from(result.items[0].id, 'base64').toString();
            const uuid = decodedId.substring(decodedId.lastIndexOf('/') + 1, decodedId.length);
            return uuid;
          })
      );
    });
    return Promise.all(promises);
  },

  @waitForValue('@')
  getReport(reportId) {
    const id = reportId;
    return this.request({
      method: 'GET',
      service: 'ediscovery',
      resource: 'reports/' + id
    }).then((res) => {
      console.log(res);
      return res;
    }).catch((reason) => {
      console.log(reason);
      return reason;
    });
  },

  @waitForValue('@')
  getReports() {
    return this.request({
      method: 'GET',
      service: 'ediscovery',
      resource: 'reports'
    }).then((res) => {
      console.log(res);
      return res;
    }).catch((reason) => {
      console.log(reason);
      return reason;
    });
  },

  @waitForValue('@')
  deleteReport(reportId) {
    const id = reportId;
    return this.request({
      method: 'DELETE',
      service: 'ediscovery',
      resource: 'reports/' + id
    }).then((res) => {
      console.log(res);
      return res;
    }).catch((reason) => {
      console.log(reason);
      return reason;
    });
  },

  @waitForValue('@')
  restartReport(reportId) {
    const id = reportId;
    return this.request({
      method: 'PUT',
      service: 'ediscovery',
      resource: 'reports/' + id
    }).then((res) => {
      console.log(res);
      return res;
    }).catch((reason) => {
      console.log(reason);
      return reason;
    });
  }
});

export default Ediscovery;

{'fraction_non_alphanumeric': 0.15526046986721145, 'fraction_numerical': 0.003745318352059925, 'mean_word_length': 2.330498866213152, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 30, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4047737', 'n_tokens_mistral': 1850, 'n_tokens_neox': 1794, 'n_words': 419}