import { freezeSys , toPlainObject } from 'contentful-sdk-core' ; import copy from 'fast-copy' ; import { DefaultElements , ISO8601Timestamp , BasicCursorPaginationOptions , MetaLinkProps , Link , MakeRequest , SysLink , } from '../common-types' ; import { wrapCollection } from '../common-utils' ; import enhanceWithMethods from '../enhance-with-methods' ; /**
 * Represents that state of the scheduled action
 */ enum ScheduledActionStatus { /** action is pending execution */ scheduled = 'scheduled' , /** action has been started and pending completion */ inProgress = 'inProgress' , /** action was completed successfully (terminal state) */ succeeded = 'succeeded' , /** action failed to complete (terminal state) */ failed = 'failed' , /** action was canceled by a user (terminal state) */ canceled = 'canceled' } type SchedulableEntityType = 'Entry' | 'Asset' | 'Release' ; type SchedulableActionType = 'publish' | 'unpublish' ; type ErrorDetail = { error : any ; } ; interface ScheduledActionFailedError { sys : { type : 'Error' ; id : string ; } ; message ? : string ; details ? : { errors : ErrorDetail [ ] ; } ; } export type ScheduledActionSysProps = { id : string ; type : 'ScheduledAction' ; version : number ; space : SysLink ; status : ScheduledActionStatus ; createdAt : ISO8601Timestamp ; createdBy : Link < 'User' > | Link < 'AppDefinition' > ; /** an ISO8601 date string representing when an action was moved to canceled */ canceledAt ? : ISO8601Timestamp ; canceledBy ? : Link < 'User' > | Link < 'AppDefinition' > ; /** an ISO8601 date string representing when an action was updated */ updatedAt : ISO8601Timestamp ; updatedBy : Link < 'User' > | Link < 'AppDefinition' > ; } ; export type ScheduledActionProps = { sys : ScheduledActionSysProps ; action : SchedulableActionType ; entity : Link < SchedulableEntityType > ; environment ? : { sys : MetaLinkProps ; } ; scheduledFor : { datetime : ISO8601Timestamp ; see https example timezone ? : string ; } ; example error ? : ScheduledActionFailedError ; } ; export type CreateUpdateScheduledActionProps = Pick < ScheduledActionProps , 'action' | 'entity' | 'environment' | 'scheduledFor' > ; export interface ScheduledActionCollection { sys : { type : 'Array' ; } ; pages : BasicCursorPaginationOptions ; limit : number ; items : ScheduledActionProps [ ] ; } export interface ScheduledActionQueryOptions extends BasicCursorPaginationOptions { 'environment.sys.id' : string ; [ key ] : any ; } export type ScheduledActionApi = { delete ( ) : Promise < ScheduledAction > ; update ( ) : Promise < ScheduledAction > ; } ; export interface ScheduledAction extends ScheduledActionProps , DefaultElements < ScheduledActionProps > , ScheduledActionApi { } export default function getInstanceMethods ( makeRequest ) { const getParams = ( self ) => { const scheduledAction = self . toPlainObject ( ) ; return { spaceId : scheduledAction . sys . space . sys . id , environmentId : scheduledAction . environment ?. sys . id as string , scheduledActionId : scheduledAction . sys . id , version : scheduledAction . sys . version , } ; } ; return { example async delete ( ) { const params = getParams ( this ) ; return makeRequest ( { entityType : 'ScheduledAction' , action : 'delete' , params , } ) . then ( ( data ) => wrapScheduledAction ( makeRequest , data ) ) ; } , example async update ( ) { const params = getParams ( this ) ; const { sys , ... payload } = this . toPlainObject ( ) ; return makeRequest ( { entityType : 'ScheduledAction' , action : 'update' , params , payload , } ) . then ( ( data ) => wrapScheduledAction ( makeRequest , data ) ) ; } , } ; } private export function wrapScheduledAction ( makeRequest , data ) { const scheduledAction = toPlainObject ( copy ( data ) ) ; const scheduledActionWithMethods = enhanceWithMethods ( scheduledAction , getInstanceMethods ( makeRequest ) ) ; return freezeSys ( scheduledActionWithMethods ) ; } private export const wrapScheduledActionCollection = wrapCollection ( wrapScheduledAction ) ;