Newer
Older
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: '',
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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'
)
}
this._trigger = await triggersMutations(this._client).createTrigger(
this._triggerAttributes
)
return this._trigger
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
}
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)
}
static async fetchTriggerFromAccount(client: Client, account: Account) {
if (account == null) return null
const query = client
.find('io.cozy.triggers')
.where({ 'message.account': account._id })
.sortBy([{ 'cozyMetadata.updatedAt': 'desc' }])
.limitBy(1)
const result = await client.query(query)
return result.data[0] ? result.data[0] : null
}
static async fetchTriggerState(client: Client, trigger: Trigger) {
if (trigger == null) return null
const triggerState = await client
.getStackClient()
.fetchJSON('GET', `/jobs/triggers/${trigger._id}`)
return triggerState.data.attributes.current_state
? triggerState.data.attributes.current_state
: null
}