-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathservice.js
134 lines (114 loc) · 3.25 KB
/
service.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
const feathersKnex = require('feathers-knex')
const { hashPassword } = require('feathers-authentication-local').hooks
const { isProvider: isTransport, iff, discard } = require('feathers-hooks-common')
const isNil = require('ramda/src/isNil')
module.exports = function () {
const app = this
const db = app.get('db')
const name = 'credentials'
const options = { Model: db, name }
app.use(name, feathersKnex(options))
app.service(name).hooks(hooks)
}
const hooks = {
before: {
create: [
hashPassword(),
iff(hasNoAgent, setProfileData),
iff(hasNoAgent, createAgent),
getEmailFromRemote,
clearRemoteData
],
update: [ hashPassword() ],
patch: [ hashPassword() ]
},
after: {
all: [
iff(isTransport('external'), discard('password'))
]
},
error: {
create: [
deleteIfCreateFailed
]
}
}
function hasNoAgent (hook) {
return isNil(hook.data.agentId)
}
function getEmailFromRemote (hook) {
if (hook.params.oauth) {
const remoteProvider = hook.params.oauth.provider
const remoteData = hook.data[remoteProvider]
const remoteProfile = remoteData.profile
if (remoteProfile.emails && remoteProfile.emails.length > 0) {
hook.data.email = remoteProfile.emails[0].value
}
}
return hook
}
function createAgent (hook) {
const agents = hook.app.service('agents')
if (!hook.data) return Promise.resolve(hook)
const data = {
profile: hook.data.profile
}
delete hook.data.profile
return agents.create(data, {
// HACK for https://github.com/root-systems/cobuy/issues/253
// see corresponding code in agents service
isCreatingCredential: true
})
.then(agent => {
hook.data.agentId = agent.id
return hook
})
}
function clearRemoteData (hook) {
if (hook.params.oauth) {
const remoteProvider = hook.params.oauth.provider
delete hook.data[remoteProvider]
}
return hook
}
function setProfileData (hook) {
// TODO: IK: fairly certain I added the email field in here for simpler population of Cobuy profiles which have emails, if so that was a mistake as profile migration of this repo has no email field
if (!hook.data) return hook
var name = hook.data.name
// var email = hook.data.email
var avatar
delete hook.data.name
if (hook.params.oauth) {
const remoteProvider = hook.params.oauth.provider
const remoteData = hook.data[remoteProvider]
const remoteProfile = remoteData.profile
name = remoteProfile.displayName
if (remoteProfile.photos && remoteProfile.photos.length > 0) {
avatar = remoteProfile.photos[0].value
}
}
const { agentId } = hook.data
hook.data.profile = {
name,
// email,
avatar
}
return hook
}
// TODO replace this with proper database transactions
// https://github.com/feathersjs/feathers-knex/issues/91
function deleteIfCreateFailed (hook) {
const agents = hook.app.service('agents')
const profiles = hook.app.service('profiles')
const { agentId } = hook.data
if (!agentId) return hook
return Promise.all([
agents.remove({ id: agentId }),
profiles.find({ agentId })
.then((profileList) => {
return Promise.all(profileList.map(profile => {
return profiles.remove({ id: profile.id })
}))
})
]).then(() => hook)
}