import { notFoundApiError } from 'wherehows-web/utils/api'; import { createInitialComplianceInfo } from 'wherehows-web/utils/datasets/compliance-policy'; import { datasetUrlById, datasetUrlByUrn } from 'wherehows-web/utils/api/datasets/shared'; import { IComplianceGetResponse, IComplianceInfo, IComplianceSuggestion, IComplianceSuggestionResponse } from 'wherehows-web/typings/api/datasets/compliance'; import { getJSON, postJSON } from 'wherehows-web/utils/api/fetcher'; /** * 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`; /** * Returns the url for a datasets compliance policy by urn * @param {string} urn * @return {string} */ const datasetComplianceUrlByUrn = (urn: string): string => `${datasetUrlByUrn(urn)}/compliance`; /** * 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`; /** * Returns the url for a dataset compliance suggestion by urn * @param {string} urn * @return {string} */ const datasetComplianceSuggestionUrlByUrn = (urn: string): string => `${datasetUrlByUrn(urn)}/compliance/suggestion`; /** * Describes the properties on a map generated by reading the compliance policy for a dataset * @interface */ export interface IReadComplianceResult { isNewComplianceInfo: boolean; complianceInfo: IComplianceInfo; } /** * Reads the dataset compliance policy by urn. * Resolves with a new compliance policy instance if remote response is ApiResponseStatus.NotFound * @param {string} urn the urn for the related dataset * @return {Promise} */ const readDatasetComplianceByUrn = async (urn: string): Promise => { let complianceInfo: IComplianceGetResponse['complianceInfo'] = createInitialComplianceInfo(urn); let isNewComplianceInfo = false; try { ({ complianceInfo } = await getJSON>({ url: datasetComplianceUrlByUrn(urn) })); } catch (e) { if (notFoundApiError(e)) { complianceInfo = createInitialComplianceInfo(urn); isNewComplianceInfo = true; } else { throw e; } } return { isNewComplianceInfo, complianceInfo: complianceInfo! }; }; /** * Persists the dataset compliance policy * @param {string} urn * @param {IComplianceInfo} complianceInfo * @return {Promise} */ const saveDatasetComplianceByUrn = (urn: string, complianceInfo: IComplianceInfo): Promise => postJSON({ url: datasetComplianceUrlByUrn(urn), data: complianceInfo }); /** * Requests the compliance suggestions for a given dataset Id and returns the suggestion list * @param {number} id the id of the dataset * @return {Promise} */ const readDatasetComplianceSuggestion = async (id: number): Promise => { const { complianceSuggestion = {} } = await getJSON({ url: datasetComplianceSuggestionsUrlById(id) }); return complianceSuggestion; }; /** * Reads the suggestions for a dataset compliance policy by urn * @param {string} urn * @return {Promise} */ const readDatasetComplianceSuggestionByUrn = async (urn: string): Promise => { let complianceSuggestion: IComplianceSuggestion = {}; try { ({ complianceSuggestion = {} } = await getJSON< Pick >({ url: datasetComplianceSuggestionUrlByUrn(urn) })); } catch { return complianceSuggestion; } return complianceSuggestion; }; export { readDatasetComplianceSuggestion, datasetComplianceUrlById, readDatasetComplianceByUrn, saveDatasetComplianceByUrn, readDatasetComplianceSuggestionByUrn };