-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
46 lines (36 loc) · 1.3 KB
/
index.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
const find = require('lodash/find')
const User = require('./src/User')
const getConfig = require('./src/getConfig')
const config = require('config')
const interact = {}
interact.USERS = []
interact.express = (customerCode, hashedUserId = '') => {
return (request, response, next) => {
const currentDeviceCode = request.cookies[config.cookies.name]
const { featureList, deviceCode, initCode } = getConfig(customerCode, {
currentDeviceCode,
hashedUserId
})
interact.USERS.push(new User(deviceCode, hashedUserId, featureList))
interact.INIT_CODE = initCode
if (!deviceCode) request.cookies = `${config.cookies.name}=${deviceCode}`
next()
}
}
interact.koa = (customerCode, hashedUserId = '') => {
return (context, next) => {
const currentDeviceCode = context.headers.cookies[config.cookies.name]
const { featureList, deviceCode, initCode } = getConfig(customerCode, {
currentDeviceCode,
hashedUserId
})
interact.USERS.push(new User(deviceCode, hashedUserId, featureList))
interact.INIT_CODE = initCode
if (!deviceCode) context.setCookies = `${config.cookies.name}=${deviceCode}`
next()
}
}
interact.findUser = (hashedUserId) => {
return find(interact.USERS, user => user.getHashedUserId() === hashedUserId)
}
module.exports = interact