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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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
import { Client } from 'cozy-client'
import { Account, Konnector } from 'doctypes'
import accountsMutations from 'cozy-harvest-lib/dist/connections/accounts'
export interface AccountAttributes {
account_type: string
auth: {
login: string
password: string
}
identifier: string
state: string | null
}
export class AccountService {
private _account: Account
private _accountAttributes?: AccountAttributes
constructor(
private _client: Client,
private _login: string,
private _password: string,
private _konnector: Konnector
) {
this._account = {
_id: '',
// eslint-disable-next-line @typescript-eslint/camelcase
account_type: '',
auth: {
// eslint-disable-next-line @typescript-eslint/camelcase
credentials_encrypted: '',
login: '',
},
identifier: '',
}
this._client = _client
this._login = _login
this._password = _password
this._konnector = _konnector
}
get account(): Account {
return this._account
}
createAccountAttributes = () => {
if (!(this._login && this._password)) {
throw new Error(
'AccountService : createAccountAttributes - login or password not found'
)
}
const accountAttributes: AccountAttributes = {
// eslint-disable-next-line @typescript-eslint/camelcase
account_type: this._konnector.slug,
auth: {
login: this._login,
password: this._password,
},
identifier: 'login',
state: null,
}
return accountAttributes
}
createAccount = async () => {
this._accountAttributes = this.createAccountAttributes()
if (!(this._accountAttributes && this._konnector)) {
throw new Error(
'AccountService : createAccount - accountAttributes or konnector not found'
)
}
try {
this._account = await accountsMutations(this._client).createAccount(
this._konnector,
this._accountAttributes
)
return this._account
} catch (error) {
throw error
}
}
static getAccount = async (client: Client, id: string) => {
try {
const account: Account = await accountsMutations(client).findAccount(id)
return account
} catch (error) {
throw error
}
}
static deleteAccount = async (client: Client, account: Account) => {
try {
const del = await accountsMutations(client).deleteAccount(account)
return del
} catch (error) {
throw error
}
}
static updateAccount = async (client: Client, account: Account) => {
try {
const updatedAccount: Account = await accountsMutations(
client
).updateAccount(account)
return updatedAccount
} catch (error) {
throw error
}
}
static getAccountByType = async (client: Client, type: string) => {
try {
const query = client
.find('io.cozy.accounts')
// eslint-disable-next-line @typescript-eslint/camelcase
.where({ account_type: type })
.limitBy(1)
const result = await client.query(query)
return result.data[0] ? result.data[0] : null
} catch (error) {
throw error
}
}
static createIndexAccount = async (client: Client) => {
try {
const query = client
.find('io.cozy.accounts')
// eslint-disable-next-line @typescript-eslint/camelcase
.where({ account_type: 'index' })
// .sortBy([{ 'cozyMetadata.updatedAt': 'desc' }])
.limitBy(1)
return await client.query(query)
} catch (error) {
throw error
}
}
}