(domainId: string, _users: string, draft: boolean)
| 243 | @param('users', Types.Content) |
| 244 | @param('draft', Types.Boolean) |
| 245 | async post(domainId: string, _users: string, draft: boolean) { |
| 246 | const users = _users.split('\n'); |
| 247 | const udocs: { email: string, username: string, password: string, displayName?: string, [key: string]: any }[] = []; |
| 248 | const messages = []; |
| 249 | const mapping = Object.create(null); |
| 250 | const groups: Record<string, string[]> = Object.create(null); |
| 251 | for (const i in users) { |
| 252 | const u = users[i]; |
| 253 | if (!u.trim()) continue; |
| 254 | let [email, username, password, displayName, extra] = u.split('\t').map((t) => t.trim()); |
| 255 | if (!email || !username || !password) { |
| 256 | const data = u.split(',').map((t) => t.trim()); |
| 257 | [email, username, password, displayName, extra] = data; |
| 258 | if (data.length > 5) extra = data.slice(4).join(','); |
| 259 | } |
| 260 | if (email && username && password) { |
| 261 | if (!Types.Email[1](email)) messages.push(`Line ${+i + 1}: Invalid email.`); |
| 262 | else if (!Types.Username[1](username)) messages.push(`Line ${+i + 1}: Invalid username`); |
| 263 | else if (!Types.Password[1](password)) messages.push(`Line ${+i + 1}: Invalid password`); |
| 264 | else if (udocs.find((t) => t.email === email) || await user.getByEmail('system', email)) { |
| 265 | messages.push(`Line ${+i + 1}: Email ${email} already exists.`); |
| 266 | } else if (udocs.find((t) => t.username === username) || await user.getByUname('system', username)) { |
| 267 | messages.push(`Line ${+i + 1}: Username ${username} already exists.`); |
| 268 | } else { |
| 269 | const payload: any = {}; |
| 270 | try { |
| 271 | const data = JSON.parse(extra); |
| 272 | if (data.group) { |
| 273 | groups[data.group] ||= []; |
| 274 | groups[data.group].push(email); |
| 275 | } |
| 276 | Object.assign(payload, data); |
| 277 | } catch (e) { } |
| 278 | Object.assign(payload, { |
| 279 | email, username, password, displayName, |
| 280 | }); |
| 281 | await this.ctx.serial('user/import/parse', payload, messages); |
| 282 | udocs.push(payload); |
| 283 | } |
| 284 | } else messages.push(`Line ${+i + 1}: Input invalid.`); |
| 285 | } |
| 286 | messages.push(`${udocs.length} users found.`); |
| 287 | if (!draft) { |
| 288 | for (const udoc of udocs) { |
| 289 | try { |
| 290 | const uid = await user.create(udoc.email, udoc.username, udoc.password); |
| 291 | mapping[udoc.email] = uid; |
| 292 | if (udoc.displayName) await domain.setUserInDomain(domainId, uid, { displayName: udoc.displayName }); |
| 293 | if (udoc.school) await user.setById(uid, { school: udoc.school }); |
| 294 | if (udoc.studentId) await user.setById(uid, { studentId: udoc.studentId }); |
| 295 | await this.ctx.serial('user/import/create', uid, udoc); |
| 296 | } catch (e) { |
| 297 | messages.push(e.message); |
| 298 | } |
| 299 | } |
| 300 | const existing = await user.listGroup(domainId); |
| 301 | for (const name in groups) { |
| 302 | const uids = groups[name].map((i) => mapping[i]).filter((i) => i); |
nothing calls this directly
no test coverage detected