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
import { Client } from 'cozy-client'
import { IUserProfileManager, UserProfile } from './dataChallengeContracts'
import { USERPROFILE_DOCTYPE } from 'doctypes'
export default class UserProfileManager implements IUserProfileManager {
private readonly _client: Client
//private readonly _queryRunner: QueryRunner
constructor(_client: Client) {
this._client = _client
//this._queryRunner = new QueryRunner(this._client)
}
public async getUserProfile(): Promise<UserProfile | null> {
const { data: userProfiles } = await this._client.query(
this._client.find(USERPROFILE_DOCTYPE).limitBy(1)
)
return userProfiles[0] ? userProfiles[0] : null
}
public async updateUserProfile(attributes: {
[key: string]: string
}): Promise<UserProfile | null> {
const { data: userProfile } = await this._client
.query(this._client.find(USERPROFILE_DOCTYPE).limitBy(1))
.then(async ({ data }: any) => {
const doc = data[0]
return await this._client.save({
...doc,
...attributes,
})
})
return userProfile ? userProfile : null
}
}