Skip to content

Commit b568368

Browse files
committed
feat: implied user creation with the 3rd party case creation API
1 parent ae4f1d8 commit b568368

File tree

1 file changed

+81
-25
lines changed

1 file changed

+81
-25
lines changed

imports/api/rest/post-cases.js

Lines changed: 81 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,63 @@
11
// @flow
22
import { Meteor } from 'meteor/meteor'
3+
import { Accounts } from 'meteor/accounts-base'
34
import userApiKey, { bodyExtractor, headerExtractor, makeComposedExtractor } from './middleware/user-api-key-middleware'
45
import { check, Match } from 'meteor/check'
56
import { logger } from '../../util/logger'
67
import { createCase } from '../cases'
78
import { serverHelpers } from '../units'
89
import UnitMetaData from '../unit-meta-data'
10+
import UnitRolesData, { addUserToRole, roleEnum } from '../unit-roles-data'
11+
import { emailValidator } from '../../util/validators'
912

1013
import type { Request, Response } from './rest-types'
11-
import { roleEnum } from '../unit-roles-data'
14+
import { KEEP_DEFAULT } from '../pending-invitations'
1215

1316
const allowedRoles = Object.values(roleEnum).filter(val => val !== roleEnum.CONTRACTOR)
1417

1518
const isDateString = str => typeof str === 'string' && !isNaN((new Date(str)).getTime())
1619

20+
type User = {
21+
_id: string
22+
}
23+
24+
const attemptUserGeneration = (userAliasId: string, creator: User, roleType: string, unitBzId: number) => {
25+
if (emailValidator(userAliasId)) {
26+
if (Accounts.findUserByEmail(userAliasId)) {
27+
logger.warn(`Creating user by alias ID '${userAliasId}' failed, another user with this email address already exists`)
28+
return false
29+
}
30+
const userId = Accounts.createUser({
31+
email: userAliasId,
32+
profile: {
33+
isLimited: true,
34+
creatorId: creator._id
35+
}
36+
})
37+
38+
Meteor.users.update({ _id: userId }, {
39+
$set: {
40+
'emails.0.verified': true,
41+
apiAliases: {
42+
userId: creator._id,
43+
id: userAliasId
44+
}
45+
}
46+
})
47+
48+
const newUser = Meteor.users.findOne({ _id: userId })
49+
50+
addUserToRole(creator, newUser, unitBzId, roleType, KEEP_DEFAULT, false, {
51+
user: creator._id,
52+
apiEndpoint: `POST /api/cases`,
53+
step: 'attemptUserGeneration',
54+
args: [userAliasId, creator, roleType, unitBzId]
55+
})
56+
57+
return newUser
58+
}
59+
}
60+
1761
export default userApiKey((req: Request, res: Response) => {
1862
const errorLog = 'API request for "POST /cases" failed: '
1963

@@ -88,6 +132,23 @@ export default userApiKey((req: Request, res: Response) => {
88132
nextStepsBy
89133
} = req.body
90134

135+
const unitMeta = unitId
136+
? UnitMetaData.findOne({ _id: unitId })
137+
: UnitMetaData.findOne({
138+
apiAliases: {
139+
userId: req.user._id,
140+
id: unitAliasId
141+
}
142+
})
143+
if (!unitMeta) {
144+
const message = 'No unit found for ' + (unitId ? `unitId ${unitId}` : `unitAliasId ${unitAliasId}`)
145+
logger.warn(errorLog + message)
146+
res.send(400, message)
147+
return
148+
}
149+
150+
const mainUserRole = UnitRolesData.findOne({ 'members.id': req.user._id, unitId })
151+
91152
let reporter
92153
if (!reporterId && !reporterAliasId) {
93154
reporter = req.user
@@ -102,10 +163,15 @@ export default userApiKey((req: Request, res: Response) => {
102163
})
103164

104165
if (!reporter) {
105-
const message = 'No user found as reporter for ' + (reporterId ? `reporterId ${reporterId}` : `reporterAliasId ${reporterAliasId}`)
106-
logger.warn(errorLog + message)
107-
res.send(400, message)
108-
return
166+
if (reporterAliasId && mainUserRole) {
167+
reporter = attemptUserGeneration(reporterAliasId, req.user, mainUserRole.roleType, unitMeta.bzId)
168+
}
169+
if (!reporter) {
170+
const message = 'No user found as reporter for ' + (reporterId ? `reporterId ${reporterId}` : `reporterAliasId ${reporterAliasId}`)
171+
logger.warn(errorLog + message)
172+
res.send(400, message)
173+
return
174+
}
109175
} else if (reporter.profile.creatorId !== req.user._id && reporter._id !== req.user._id) {
110176
const message = 'The provided apiKey doesn\'t belong to a user who is allowed to set the specified user as reporter'
111177
logger.warn(errorLog + message)
@@ -114,21 +180,6 @@ export default userApiKey((req: Request, res: Response) => {
114180
}
115181
}
116182

117-
const unitMeta = unitId
118-
? UnitMetaData.findOne({ _id: unitId })
119-
: UnitMetaData.findOne({
120-
apiAliases: {
121-
userId: req.user._id,
122-
id: unitAliasId
123-
}
124-
})
125-
if (!unitMeta) {
126-
const message = 'No unit found for ' + (unitId ? `unitId ${unitId}` : `unitAliasId ${unitAliasId}`)
127-
logger.warn(errorLog + message)
128-
res.send(400, message)
129-
return
130-
}
131-
132183
let unitItem
133184
try {
134185
unitItem = serverHelpers.getAPIUnitByName(unitMeta.bzName, reporter.bugzillaCreds.apiKey)
@@ -146,7 +197,7 @@ export default userApiKey((req: Request, res: Response) => {
146197

147198
let assignee
148199
if (assigneeId || assigneeAliasId) {
149-
const assigneeUser = assigneeId
200+
let assigneeUser = assigneeId
150201
? Meteor.users.findOne({ _id: assigneeId })
151202
: Meteor.users.findOne({
152203
apiAliases: {
@@ -155,10 +206,15 @@ export default userApiKey((req: Request, res: Response) => {
155206
}
156207
})
157208
if (!assigneeUser) {
158-
const message = 'No user found as assignee for ' + (assigneeId ? `assigneeId ${assigneeId}` : `assigneeAliasId ${assigneeAliasId}`)
159-
logger.warn(errorLog + message)
160-
res.send(400, message)
161-
return
209+
if (assigneeAliasId) {
210+
assigneeUser = attemptUserGeneration(assigneeAliasId, req.user._id, mainUserRole.roleType, unitMeta.bzId)
211+
}
212+
if (!assigneeUser) {
213+
const message = 'No user found as assignee for ' + (assigneeId ? `assigneeId ${assigneeId}` : `assigneeAliasId ${assigneeAliasId}`)
214+
logger.warn(errorLog + message)
215+
res.send(400, message)
216+
return
217+
}
162218
}
163219
assignee = assigneeUser.bugzillaCreds.login
164220
}

0 commit comments

Comments
 (0)