Skip to content
Snippets Groups Projects
triggersService.ts 2.82 KiB
Newer Older
  • Learn to ignore specific revisions
  • Hugo NOUTS's avatar
    Hugo NOUTS committed
    import { Client } from 'cozy-client'
    import { Account, Konnector, Trigger } from 'doctypes'
    import triggersMutations from 'cozy-harvest-lib/dist/connections/triggers'
    import { DateTime } from 'luxon'
    
    export interface TriggerAttributes {
      type: string
      arguments: string
      worker: string
      message: {
        account: string
        konnector: string
      }
    }
    export class TriggerService {
      private _trigger: Trigger
      private _triggerAttributes?: TriggerAttributes
    
      constructor(
        private _client: Client,
        private _account: Account,
        private _konnector: Konnector
      ) {
        this._trigger = {
          _id: '',
          type: '',
          workker: '',
          arguments: '',
          message: {
            account: '',
            konnector: '',
          },
        }
        this._client = _client
        this._account = _account
        this._konnector = _konnector
      }
    
      get trigger(): Trigger {
        return this._trigger
      }
    
      createTriggerAttributes = () => {
        if (!(this._account._id && this._konnector.slug)) {
          throw new Error(
            'TriggersServices : createTriggerAttribute - account _id or konnector slug not found'
          )
        }
        const triggerAttributes: TriggerAttributes = {
          type: '@cron',
          arguments: '0 47 0 * * *',
          worker: 'konnector',
          message: {
            account: this._account._id,
            konnector: this._konnector.slug,
          },
        }
        return triggerAttributes
      }
    
      createTrigger = async () => {
        this._triggerAttributes = this.createTriggerAttributes()
        if (!(this._triggerAttributes && this._client)) {
          throw new Error(
            'TriggersServices : createTrigger - _triggerAttributes or _client not found'
          )
        }
        try {
          this._trigger = await triggersMutations(this._client).createTrigger(
            this._triggerAttributes
          )
          return this._trigger
        } catch (error) {
          throw error
        }
      }
    
      setTrigger = (trigger: Trigger) => {
        this._trigger = trigger
      }
    
      launchTrigger = async () => {
        if (!this._trigger) {
          throw new Error('TriggersServices : createTrigger - trigger not found')
        }
        console.log(
          '%c Launched Trigger is : ' + this._trigger._id,
          'background: #222; color: white'
        )
        const job = await triggersMutations(this._client).launchTrigger(
          this._trigger
        )
        return job
      }
    
      static fetchKonnectorAccountFromTrigger(trigger: any) {
        if (trigger == null || trigger.message.account == null) return ''
        return trigger.message
      }
    
      static fetchStateFromTrigger(trigger: any) {
        if (trigger == null || trigger.current_state == null) return ''
        const { current_state: state } = trigger
        return state
      }
    
      static fetchDateFromTrigger(trigger: any) {
        if (trigger == null || trigger.current_state == null) return ''
        return DateTime.fromISO(
          trigger.current_state.last_execution
        ).toLocaleString(DateTime.DATETIME_MED)
      }
    }