// Types import {IRestApiResponse} from 'superdesk-api'; import {ISetItem} from '../interfaces'; import {superdeskApi} from '../apis'; // Utils import {fixItemResponseVersionDates} from './common'; import {getApiErrorMessage, isSamsApiError} from '../utils/api'; const RESOURCE = 'sams/sets'; export function getAllSets(): Promise> { const {gettext} = superdeskApi.localization; const {notify} = superdeskApi.ui; return superdeskApi.dataApi.query( RESOURCE, 1, {field: 'name', direction: 'ascending'}, {}, ) .then(fixItemResponseVersionDates) .then((response: IRestApiResponse) => { return response?._items ?? []; }) .catch((error: any) => { if (isSamsApiError(error)) { notify.error(getApiErrorMessage(error)); } else { notify.error(gettext('Failed to load all sets')); } return Promise.reject(error); }); } export function createSet(item: Partial): Promise { const {gettext} = superdeskApi.localization; const {notify} = superdeskApi.ui; return superdeskApi.dataApi.create(RESOURCE, item) .then((set: ISetItem) => { notify.success(gettext('Set created successfully')); return set; }) .catch((error: any) => { if (isSamsApiError(error)) { notify.error(getApiErrorMessage(error)); } else { notify.error(gettext('Failed to create the Set')); } return Promise.reject(error); }); } export function updateSet(original: ISetItem, updates: Partial): Promise { const {gettext} = superdeskApi.localization; const {notify} = superdeskApi.ui; return superdeskApi.dataApi.patch(RESOURCE, original, updates) .then((set: ISetItem) => { notify.success(gettext('Set updated successfully')); return set; }) .catch((error: any) => { if (isSamsApiError(error)) { notify.error(getApiErrorMessage(error)); } else { notify.error(gettext('Failed to update the Set')); } return Promise.reject(error); }); } export function deleteSet(item: ISetItem): Promise { const {gettext} = superdeskApi.localization; const {notify} = superdeskApi.ui; return superdeskApi.dataApi.delete(RESOURCE, item) .then(() => { notify.success(gettext('Set deleted successfully')); }) .catch((error: any) => { if (isSamsApiError(error)) { notify.error(getApiErrorMessage(error)); } else { notify.error(gettext('Failed to delete the Set')); } return Promise.reject(error); }); }