2018-05-17 23:46:03 -07:00
|
|
|
import { getJSON, postJSON } from 'wherehows-web/utils/api/fetcher';
|
|
|
|
import { datasetUrlByUrn } from 'wherehows-web/utils/api/datasets/shared';
|
|
|
|
import { IDatasetRetention, IGetDatasetRetentionResponse } from 'wherehows-web/typings/api/datasets/retention';
|
2018-07-19 16:18:06 -07:00
|
|
|
import { isNotFoundApiError } from 'wherehows-web/utils/api';
|
2018-05-22 15:41:09 -07:00
|
|
|
import { fleece } from 'wherehows-web/utils/object';
|
2018-05-31 12:23:16 -07:00
|
|
|
import { encodeUrn } from 'wherehows-web/utils/validators/urn';
|
2018-05-17 23:46:03 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructs the url for a datasets retention policy
|
|
|
|
* @param {string} urn the urn for the dataset
|
|
|
|
* @return {string}
|
|
|
|
*/
|
2018-05-31 14:10:00 -07:00
|
|
|
const datasetRetentionUrlByUrn = (urn: string): string => `${datasetUrlByUrn(encodeUrn(urn))}/retention`;
|
2018-05-17 23:46:03 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetches the list of retention policy for a dataset by urn
|
|
|
|
* @param {string} urn urn for the dataset
|
|
|
|
* @return {Promise<Array<IDatasetView>>}
|
|
|
|
*/
|
|
|
|
const readDatasetRetentionByUrn = async (urn: string): Promise<IGetDatasetRetentionResponse | null> => {
|
|
|
|
try {
|
|
|
|
return await getJSON<IGetDatasetRetentionResponse>({ url: datasetRetentionUrlByUrn(urn) });
|
|
|
|
} catch (e) {
|
2018-07-19 16:18:06 -07:00
|
|
|
if (isNotFoundApiError(e)) {
|
2018-05-17 23:46:03 -07:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Persists the dataset retention policy remotely
|
|
|
|
* @param {string} urn the urn of the dataset to save
|
2018-05-21 00:19:45 -07:00
|
|
|
* @param {IDatasetRetention} retention the dataset retention policy to update
|
2018-05-17 23:46:03 -07:00
|
|
|
* @return {Promise<IDatasetRetention>}
|
|
|
|
*/
|
|
|
|
const saveDatasetRetentionByUrn = (urn: string, retention: IDatasetRetention): Promise<IDatasetRetention> =>
|
2018-05-22 15:41:09 -07:00
|
|
|
postJSON<IDatasetRetention>({
|
|
|
|
url: datasetRetentionUrlByUrn(urn),
|
|
|
|
data: fleece<IDatasetRetention, 'modifiedTime' | 'modifiedBy'>(['modifiedBy', 'modifiedTime'])(retention)
|
|
|
|
});
|
2018-05-17 23:46:03 -07:00
|
|
|
|
|
|
|
export { readDatasetRetentionByUrn, saveDatasetRetentionByUrn };
|