2017-08-15 23:16:38 -07:00
|
|
|
import Ember from 'ember';
|
|
|
|
/// <reference path='wherehows-web/typings/untyped-js-module' />
|
|
|
|
import { createInitialComplianceInfo } from 'wherehows-web/utils/datasets/functions';
|
2017-08-18 03:42:40 -07:00
|
|
|
import { apiRoot, ApiStatus } from 'wherehows-web/utils/api/shared';
|
2017-08-16 13:28:13 -07:00
|
|
|
import { IComplianceSuggestion, IComplianceSuggestionResponse } from 'wherehows-web/typings/api/datasets/compliance';
|
2017-08-15 23:16:38 -07:00
|
|
|
|
|
|
|
const { $: { getJSON }, assert } = Ember;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Defines the endpoint for datasets
|
|
|
|
* @type {string}
|
|
|
|
*/
|
|
|
|
const datasetsUrlRoot = `${apiRoot}/datasets`;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructs a url to get a dataset with a given id
|
|
|
|
* @param {number} id the id of the dataset
|
|
|
|
* @return {string} the dataset url
|
|
|
|
*/
|
|
|
|
const datasetUrlById = (id: number): string => `${datasetsUrlRoot}/${id}`;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructs the dataset compliance url
|
|
|
|
* @param {number} id the id of the dataset
|
|
|
|
* @return {string} the dataset compliance url
|
|
|
|
*/
|
|
|
|
const datasetComplianceUrlById = (id: number): string => `${datasetUrlById(id)}/compliance`;
|
|
|
|
|
2017-08-16 13:28:13 -07:00
|
|
|
/**
|
|
|
|
* Constructs the compliance suggestions url based of the compliance id
|
|
|
|
* @param {number} id the id of the dataset
|
|
|
|
* @return {string} compliance suggestions url
|
|
|
|
*/
|
|
|
|
const datasetComplianceSuggestionsUrlById = (id: number): string => `${datasetComplianceUrlById(id)}/suggestions`;
|
|
|
|
|
2017-08-15 23:16:38 -07:00
|
|
|
/**
|
|
|
|
* Fetches the current compliance policy for a dataset with thi given id
|
|
|
|
* @param {number} id the id of the dataset
|
|
|
|
* @return {Promise<{isNewComplianceInfo: boolean, complianceInfo: *}>}
|
|
|
|
*/
|
|
|
|
const datasetComplianceFor = async (id: number): Promise<{ isNewComplianceInfo: boolean; complianceInfo: any }> => {
|
|
|
|
assert(`Expected id to be a number but received ${typeof id}`, typeof id === 'number');
|
|
|
|
const failedStatus = 'failed';
|
|
|
|
const notFound = 'actual 0';
|
|
|
|
// complianceInfo contains the compliance data for the specified dataset
|
|
|
|
let {
|
|
|
|
msg = '',
|
|
|
|
status,
|
|
|
|
complianceInfo
|
|
|
|
}: { msg: string; status: string; complianceInfo: any } = await Promise.resolve(
|
|
|
|
getJSON(datasetComplianceUrlById(id))
|
|
|
|
);
|
|
|
|
// If the endpoint responds with a failed status, and the msg contains the indicator that a compliance does not exist
|
|
|
|
const isNewComplianceInfo: boolean = status === failedStatus && String(msg).includes(notFound);
|
|
|
|
|
|
|
|
if (isNewComplianceInfo) {
|
|
|
|
complianceInfo = createInitialComplianceInfo(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
return { isNewComplianceInfo, complianceInfo };
|
|
|
|
};
|
|
|
|
|
2017-08-16 13:28:13 -07:00
|
|
|
/**
|
|
|
|
* Requests the compliance suggestions for a given dataset Id and returns the suggestion list
|
|
|
|
* @param {number} id the id of the dataset
|
|
|
|
* @return {Promise<Array<IComplianceSuggestion>>}
|
|
|
|
*/
|
2017-08-20 20:05:18 -07:00
|
|
|
const datasetComplianceSuggestionsFor = async (
|
|
|
|
id: number
|
|
|
|
): Promise<{ complianceSuggestions: Array<IComplianceSuggestion>; lastModified: number | void }> => {
|
2017-08-16 13:28:13 -07:00
|
|
|
const response: IComplianceSuggestionResponse = await Promise.resolve(
|
|
|
|
getJSON(datasetComplianceSuggestionsUrlById(id))
|
|
|
|
);
|
2017-08-20 20:05:18 -07:00
|
|
|
const { status, autoClassification = { classificationResult: '[]', lastModified: 0 } } = response;
|
2017-08-16 13:28:13 -07:00
|
|
|
let complianceSuggestions: Array<IComplianceSuggestion> = [];
|
2017-08-20 20:05:18 -07:00
|
|
|
let lastModifiedDate;
|
2017-08-16 13:28:13 -07:00
|
|
|
|
|
|
|
if (status === ApiStatus.OK) {
|
2017-08-20 20:05:18 -07:00
|
|
|
const { classificationResult, lastModified } = autoClassification;
|
2017-08-16 13:28:13 -07:00
|
|
|
|
|
|
|
try {
|
|
|
|
complianceSuggestions = [...JSON.parse(classificationResult)];
|
2017-08-20 20:05:18 -07:00
|
|
|
lastModifiedDate = lastModified;
|
2017-08-16 13:28:13 -07:00
|
|
|
} catch (e) {
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-20 20:05:18 -07:00
|
|
|
return { complianceSuggestions, lastModified: lastModifiedDate };
|
2017-08-16 13:28:13 -07:00
|
|
|
};
|
|
|
|
|
2017-08-18 11:10:08 -07:00
|
|
|
export { datasetsUrlRoot, datasetComplianceFor, datasetComplianceSuggestionsFor, datasetComplianceUrlById };
|