-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.js
102 lines (88 loc) · 2.2 KB
/
utils.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
const bindAll = require('bindall')
// const _ = require('lodash')
const buildResource = require('@tradle/build-resource')
const { omitVirtual } = buildResource
const { parseStub } = require('@tradle/validate-resource').utils
const debug = require('debug')(require('./package').name)
const { TYPE, SIG, NONCE, SEQ, PREV_TO_RECIPIENT } = require('@tradle/constants')
const {
VERIFICATION,
INTRODUCTION,
OBJECT
} = require('./types')
const models = require('./models')
const ObjectModel = models[OBJECT]
// const MessageModel = models['tradle.Message']
// const BUILT_IN_MESSAGE_PROPS = Object.keys(MessageModel.properties)
// .concat(Object.keys(ObjectModel.properties))
// const BUILT_IN_MESSAGE_PROPS = [
// [SEQ],
// [PREV_TO_RECIPIENT],
// [NONCE],
// 'time',
// 'object',
// 'recipientPubKey'
// ].concat(Object.keys(ObjectModel.properties))
// const getCustomMessageProperties = message => {
// return _.omit(omitVirtual(message), BUILT_IN_MESSAGE_PROPS)
// }
const uniqueStrings = arr => {
const map = {}
for (const str of arr) {
map[str] = true
}
return Object.keys(map)
}
const getUserIdentityStub = user => user.identity || { id: user.id }
const getPermalinkFromStub = stub => parseStub(stub).permalink
const createVerificationForDocument = document => {
return buildResource({
models,
model: VERIFICATION
})
.set({
document,
dateVerified: Date.now()
})
.toJSON()
}
const createIntroductionToUser = ({ user, identity }) => {
const intro = {
identity: omitVirtual(identity)
}
if (user.profile) {
intro.profile = user.profile
}
return buildResource({
models,
model: INTRODUCTION,
resource: intro
})
.toJSON()
}
const defaultLogger = {
debug,
log: debug,
error: debug,
warn: debug,
info: debug
}
const removeEmployeeRole = user => {
const idx = (user.roles || []).find(role => role.id === 'employee')
if (idx !== -1) {
user.roles.splice(idx, 1)
return true
}
}
module.exports = {
bindAll,
debug,
// getCustomMessageProperties,
uniqueStrings,
getUserIdentityStub,
getPermalinkFromStub,
createIntroductionToUser,
createVerificationForDocument,
defaultLogger,
removeEmployeeRole
}