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