2017-12-11 21:44:46 -08:00
|
|
|
import { assert } from '@ember/debug';
|
2017-09-27 09:33:20 -07:00
|
|
|
import { createInitialComplianceInfo } from 'wherehows-web/utils/datasets/compliance-policy';
|
2018-02-21 09:46:04 -08:00
|
|
|
import { datasetUrlById, datasetUrlByUrn } from 'wherehows-web/utils/api/datasets/shared';
|
2017-08-24 00:23:48 -07:00
|
|
|
import { ApiStatus } from 'wherehows-web/utils/api/shared';
|
2017-12-11 21:44:46 -08:00
|
|
|
import {
|
|
|
|
IComplianceGetResponse,
|
|
|
|
IComplianceInfo,
|
|
|
|
IComplianceSuggestion,
|
|
|
|
IComplianceSuggestionResponse
|
|
|
|
} from 'wherehows-web/typings/api/datasets/compliance';
|
2018-02-21 09:46:04 -08:00
|
|
|
import { getJSON, postJSON } from 'wherehows-web/utils/api/fetcher';
|
2017-08-15 23:16:38 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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`;
|
|
|
|
|
2018-02-21 09:46:04 -08:00
|
|
|
/**
|
|
|
|
* Returns the url for a datasets compliance policy by urn
|
|
|
|
* @param {string} urn
|
|
|
|
* @return {string}
|
|
|
|
*/
|
|
|
|
const datasetComplianceUrlByUrn = (urn: string): string => `${datasetUrlByUrn(urn)}/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`;
|
|
|
|
|
2018-02-21 09:46:04 -08:00
|
|
|
/**
|
|
|
|
* Returns the url for a dataset compliance suggestion by urn
|
|
|
|
* @param {string} urn
|
|
|
|
* @return {string}
|
|
|
|
*/
|
|
|
|
const datasetComplianceSuggestionUrlByUrn = (urn: string): string => `${datasetUrlByUrn(urn)}/complianceSuggestion`;
|
|
|
|
|
2017-12-11 21:44:46 -08:00
|
|
|
/**
|
|
|
|
* Determines if the client app should 'new' a compliance policy
|
|
|
|
* If the endpoint responds with a failed status, and the msg contains the indicator that a compliance does not exist
|
|
|
|
* @param {IComplianceGetResponse} { status, msg }
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
const requiresCompliancePolicyCreation = ({ status, msg }: IComplianceGetResponse): boolean => {
|
|
|
|
const notFound = 'No entity found for query';
|
|
|
|
return status === ApiStatus.FAILED && String(msg).includes(notFound);
|
|
|
|
};
|
|
|
|
|
2018-02-21 09:46:04 -08:00
|
|
|
/**
|
|
|
|
* Describes the properties on a map generated by reading the compliance policy for a dataset
|
|
|
|
* @interface
|
|
|
|
*/
|
|
|
|
export interface IReadComplianceResult {
|
|
|
|
isNewComplianceInfo: boolean;
|
|
|
|
complianceInfo: IComplianceInfo;
|
|
|
|
}
|
|
|
|
|
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
|
2018-02-21 09:46:04 -08:00
|
|
|
* @returns {(Promise<IReadComplianceResult>)}
|
2017-08-15 23:16:38 -07:00
|
|
|
*/
|
2018-02-21 09:46:04 -08:00
|
|
|
const readDatasetCompliance = async (id: number): Promise<IReadComplianceResult> => {
|
2017-08-15 23:16:38 -07:00
|
|
|
assert(`Expected id to be a number but received ${typeof id}`, typeof id === 'number');
|
2017-12-11 21:44:46 -08:00
|
|
|
|
|
|
|
const response = await getJSON<IComplianceGetResponse>({ url: datasetComplianceUrlById(id) });
|
|
|
|
const isNewComplianceInfo = requiresCompliancePolicyCreation(response);
|
|
|
|
let { msg, status, complianceInfo } = response;
|
2017-08-15 23:16:38 -07:00
|
|
|
|
|
|
|
if (isNewComplianceInfo) {
|
|
|
|
complianceInfo = createInitialComplianceInfo(id);
|
|
|
|
}
|
|
|
|
|
2017-12-11 21:44:46 -08:00
|
|
|
if ((status === ApiStatus.OK || isNewComplianceInfo) && complianceInfo) {
|
|
|
|
return { isNewComplianceInfo, complianceInfo };
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new Error(msg);
|
2017-08-15 23:16:38 -07:00
|
|
|
};
|
|
|
|
|
2018-02-21 09:46:04 -08:00
|
|
|
/**
|
|
|
|
* Reads the dataset compliance policy by urn
|
|
|
|
* @param {string} urn
|
|
|
|
* @return {Promise<IReadComplianceResult>}
|
|
|
|
*/
|
|
|
|
const readDatasetComplianceByUrn = async (urn: string): Promise<IReadComplianceResult> => {
|
|
|
|
let { complianceInfo } = await getJSON<Pick<IComplianceGetResponse, 'complianceInfo'>>({
|
|
|
|
url: datasetComplianceUrlByUrn(urn)
|
|
|
|
});
|
|
|
|
const isNewComplianceInfo = !complianceInfo;
|
|
|
|
|
|
|
|
if (isNewComplianceInfo) {
|
|
|
|
complianceInfo = createInitialComplianceInfo(urn);
|
|
|
|
}
|
|
|
|
|
|
|
|
return { isNewComplianceInfo, complianceInfo: complianceInfo! };
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Persists the dataset compliance policy
|
|
|
|
* @param {string} urn
|
|
|
|
* @param {IComplianceInfo} complianceInfo
|
|
|
|
* @return {Promise<void>}
|
|
|
|
*/
|
|
|
|
const saveDatasetComplianceByUrn = (urn: string, complianceInfo: IComplianceInfo): Promise<void> => {
|
|
|
|
const url = datasetUrlByUrn(urn);
|
|
|
|
return postJSON<void>({ url, data: 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
|
2017-10-16 11:35:13 -07:00
|
|
|
* @return {Promise<IComplianceSuggestion>}
|
2017-08-16 13:28:13 -07:00
|
|
|
*/
|
2017-10-16 11:35:13 -07:00
|
|
|
const readDatasetComplianceSuggestion = async (id: number): Promise<IComplianceSuggestion> => {
|
2017-12-11 21:44:46 -08:00
|
|
|
const { complianceSuggestion = <IComplianceSuggestion>{} } = await getJSON<IComplianceSuggestionResponse>({
|
|
|
|
url: datasetComplianceSuggestionsUrlById(id)
|
|
|
|
});
|
|
|
|
|
2017-10-16 11:35:13 -07:00
|
|
|
return complianceSuggestion;
|
2017-08-16 13:28:13 -07:00
|
|
|
};
|
|
|
|
|
2018-02-21 09:46:04 -08:00
|
|
|
/**
|
|
|
|
* Reads the suggestions for a dataset compliance policy by urn
|
|
|
|
* @param {string} urn
|
|
|
|
* @return {Promise<IComplianceSuggestion>}
|
|
|
|
*/
|
|
|
|
const readDatasetComplianceSuggestionByUrn = async (urn: string): Promise<IComplianceSuggestion> => {
|
|
|
|
let complianceSuggestion: IComplianceSuggestion = <IComplianceSuggestion>{};
|
|
|
|
try {
|
|
|
|
({ complianceSuggestion = <IComplianceSuggestion>{} } = await getJSON<
|
|
|
|
Pick<IComplianceSuggestionResponse, 'complianceSuggestion'>
|
|
|
|
>({ url: datasetComplianceSuggestionUrlByUrn(urn) }));
|
|
|
|
} catch {
|
|
|
|
return complianceSuggestion;
|
|
|
|
}
|
|
|
|
|
|
|
|
return complianceSuggestion;
|
|
|
|
};
|
|
|
|
|
|
|
|
export {
|
|
|
|
readDatasetCompliance,
|
|
|
|
readDatasetComplianceSuggestion,
|
|
|
|
datasetComplianceUrlById,
|
|
|
|
readDatasetComplianceByUrn,
|
|
|
|
saveDatasetComplianceByUrn,
|
|
|
|
readDatasetComplianceSuggestionByUrn
|
|
|
|
};
|