-
Notifications
You must be signed in to change notification settings - Fork 12
/
ContactsCollection.js
60 lines (54 loc) · 1.46 KB
/
ContactsCollection.js
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
import DocumentCollection, { normalizeDoc } from './DocumentCollection'
const normalizeMyselfResp = resp => {
return {
...normalizeDoc(resp.data, CONTACTS_DOCTYPE),
...resp.data.attributes,
_rev: resp.data.meta.rev
}
}
class ContactsCollection extends DocumentCollection {
async find(selector, options) {
if (
selector !== undefined &&
Object.values(selector).length === 1 &&
selector['me'] == true
) {
return this.findMyself()
} else {
return super.find(selector, options)
}
}
async findMyself() {
const resp = await this.stackClient.fetchJSON('POST', '/contacts/myself')
const col = {
data: [normalizeMyselfResp(resp)],
next: false,
meta: null,
bookmark: false
}
return col
}
/**
* Destroys a contact
*
* If the contact is linked to accounts, it will be trashed instead of being
* destroyed.
*
* @param {object} contact - Contact to destroy. IT MUST BE THE FULL CONTACT OBJECT
* @returns {Promise} - Resolves when contact has been destroyed
*/
async destroy(contact) {
const syncData = contact?.cozyMetadata?.sync || {}
const isLinkedToAccounts = Object.keys(syncData).length > 0
if (isLinkedToAccounts) {
return super.update({
...contact,
trashed: true
})
} else {
return super.destroy(contact)
}
}
}
export const CONTACTS_DOCTYPE = 'io.cozy.contacts'
export default ContactsCollection