Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import { Client } from 'cozy-client'
import { Account, Konnector, Trigger } from 'doctypes'
import { AccountService } from './accountService'
import { TriggerService } from './triggersService'
import KonnectorService from './konnectorService'
export class ConnectionService {
constructor(
private _client: Client,
private _konnectorId: string,
private _login: string,
private _password: string
) {
this._client = _client
this._konnectorId = _konnectorId
this._login = _login
this._password = _password
}
connectNewUser = async () => {
// Retrieve konnector
const konnectorService = new KonnectorService(
this._client,
this._konnectorId
)
const konnector: Konnector = await konnectorService.fetchKonnector()
if (!konnector || !konnector.slug) {
throw new Error(`Could not find konnector for ${this._konnectorId}`)
}
// Creation of the account linked to the konnector retrieved
const accountService = new AccountService(
this._client,
this._login,
this._password,
konnector
)
const account: Account = await accountService.createAccount()
if (!account || !account._id) {
throw new Error(`Error during account creation`)
}
// creation of the trigger for the konnector retrieve and the created account
const triggersServices = new TriggerService(
this._client,
account,
konnector
)
const trigger: Trigger = await triggersServices.createTrigger()
if (!trigger) {
throw new Error(`Error during trigger creation`)
}
return {
account: account,
trigger: trigger,
//Launch the creation trigger
// const job = await triggersServices.launchTrigger()
// if (!job) {
// throw new Error(`Error during trigger launching`)
// }
// return job