Newer
Older
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
const { logger } = require('@pubsweet/logger')
const { Identity } = require('../index')
const {
labels: { IDENTITY_LOADER },
} = require('./constants')
const identitiesBasedOnUserIdsLoader = async userIds => {
try {
const userIdentities = await Identity.query().whereIn('userId', userIds)
return userIds.map(userId =>
userIdentities.find(identity => identity.userId === userId),
)
} catch (e) {
logger.error(
`${IDENTITY_LOADER} identitiesBasedOnUserIdsLoader: ${e.message}`,
)
throw new Error(e)
}
}
const defaultIdentityBasedOnUserIdsLoader = async userIds => {
try {
const userIdentities = await Identity.query()
.whereIn('userId', userIds)
.andWhere({ isDefault: true })
return userIds.map(userId =>
userIdentities.find(identity => identity.userId === userId),
)
} catch (e) {
logger.error(
`${IDENTITY_LOADER} defaultIdentityBasedOnUserIdsLoader: ${e.message}`,
)
throw new Error(e)
}
}
module.exports = {
identitiesBasedOnUserIdsLoader,
defaultIdentityBasedOnUserIdsLoader,
}