-
Notifications
You must be signed in to change notification settings - Fork 13
/
ContactsCollection.js
64 lines (57 loc) · 1.66 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
61
62
63
64
import DocumentCollection from './DocumentCollection'
import { normalizeDoctypeJsonApi } from './normalize'
// @ts-ignore Need to import it to be used in jsdoc
import { IOCozyContact } from 'cozy-client/dist/types'
export const CONTACTS_DOCTYPE = 'io.cozy.contacts'
const normalizeContactJsonApi = normalizeDoctypeJsonApi(CONTACTS_DOCTYPE)
const normalizeMyself = contact => {
return {
...normalizeContactJsonApi(contact),
_rev: contact?.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: [normalizeMyself(resp.data)],
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 {IOCozyContact} contact - Contact to destroy. IT MUST BE THE FULL CONTACT OBJECT
* @returns {Promise<{ data: IOCozyContact }>} - 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 default ContactsCollection