-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpatchUser.js
109 lines (91 loc) · 3.59 KB
/
patchUser.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
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
101
102
103
104
105
106
107
108
109
// dependencies
const deepEqual = require('fast-deep-equal')
// constants
// library
async function patchUser (original, patch) {
for (const field in patch) {
if (field === 'contact_points') {
// build a fresh array to ensure deleted contact points get removed
const mergedContactPoints = [];
for (const patchContactPoint of patch.contact_points) {
let existingContactPoint
if (original.contact_points) {
// prefer a match based on primary key
if (patchContactPoint.id) {
([existingContactPoint] = original.contact_points.filter(
existingContactPoint => existingContactPoint.id === patchContactPoint.id
))
}
// try to match based on content
if (!existingContactPoint) {
// trim out any empty keys—existing won't include them
if (typeof patchContactPoint.data === 'object') {
for (const key in patchContactPoint.data) {
if (patchContactPoint.data[key] === null) {
delete patchContactPoint.data[key]
}
}
}
([existingContactPoint] = original.contact_points.filter(
existingContactPoint => existingContactPoint.kind === patchContactPoint.kind &&
deepEqual(existingContactPoint.data, patchContactPoint.data)
))
}
}
if (existingContactPoint) {
existingContactPoint.kind = patchContactPoint.kind
existingContactPoint.label = patchContactPoint.label
existingContactPoint.data = patchContactPoint.data
}
mergedContactPoints.push(existingContactPoint || patchContactPoint)
}
original.contact_points = mergedContactPoints
} else if (field === 'mappings') {
// build a fresh array to ensure deleted mappings get removed
const mergedMappings = [];
for (const mapping of patch.mappings) {
let existingMapping
if (original.mappings) {
([existingMapping] = original.mappings.filter(
p => p.connection === mapping.connection &&
p.kind === mapping.kind &&
(p.field === mapping.field || (!p.field && mapping.field === 'id'))
))
}
if (existingMapping) {
existingMapping.field = mapping.field
existingMapping.key = mapping.key
existingMapping.matched_via = mapping.matched_via
}
mergedMappings.push(existingMapping || mapping)
}
original.mappings = mergedMappings
} else if (field === 'relationships') {
// build a fresh array to ensure deleted relationships get removed
const mergedRelationships = [];
for (const relationship of patch.relationships) {
let existingRelationship
if (original.relationships) {
([existingRelationship] = original.relationships.filter(
p => p.id === relationship.id ||
p.related_person_id === relationship.related_person_id
))
}
if (existingRelationship) {
for (const relationshipField of ['kind', 'label', 'slot', 'notes']) {
if (relationshipField in relationship) {
existingRelationship[relationshipField] = relationship[relationshipField]
}
}
}
mergedRelationships.push(existingRelationship || relationship)
}
original.relationships = mergedRelationships
} else {
original[field] = patch[field]
}
}
return original
}
// exports
module.exports = patchUser