REST Service for POPCORN - ILIAS
alex
2025-06-22 380ecd93c086d23898f11d508a5e14b234d0e1a7
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
/* Lib for interacting with customized ILIAS php */
const _ = require("lodash")
const settings = require("../settings")
const {getObjIdFromRefId} = require("./db")
const db = require("./db")
const {url, iliastoken} = settings.ilias
const search = require("./search")
 
/////////////////////////////////////////////////////////////////////////
 
module.exports = {
   importIliasUser,
 
   deleteUser,
   deleteAllUsers,
   deleteTeilnahme,
}
 
/////////////////////////////////////////////////////////////////////////
 
 
/////// USER IMPORT ////////////////////////////////////////////////////////////////
 
/**
 * Format in POPCORN für SOAP; ausprobieren ob das hier auch funktioniert, v.a. die user-defined-fields
 *   const user = {
 *       login: "123456789",
 *       passwd: "123456789",
 *       passwd_type: "plain",
 *       firstname: "Adolfo",
 *       lastname: "de la Cruz",
 *       email: "alex@gorillaeis.com",
 *       gender: "m",
 *       department: "Bananenpflücker",
 *       institution: "Globus Budapest",
 *       role: 4, // assigned global role id
 *       udf: {
 *          "Markt": "Markt UDF 2",
 *          "Marktnummer": "Marktnummer UDF 2",
 *          "Personalnummer": "Personal UDF 2",
 *       },
 *    }
 * @param user
 * @returns {Promise<any>}
 */
async function importIliasUser (user) {
   const sp = new URLSearchParams({
      command: "importUser",
      token: iliastoken,
   })
   let url2 = `${url}?${sp.toString()}`
 
   const udfDef = await db.getUdf()
   const udfMap = _.keyBy(udfDef, "field_name")
   user.udf = _.mapKeys(user.udf, function (value, key) {
      return udfMap[key].field_id
   })
 
   const res = await fetch(url2, {
      method: "POST",
      body: JSON.stringify(user)
   })
   const text = await res.text()
   try {
      return JSON.parse(text)
   } catch (ex) {
      console.error(ex.message)
      console.log(text)
      throw ex
   }
}
 
/////// USER DELETE ////////////////////////////////////////////////////////////////
 
async function deleteUser (obj_id, dry = false) {
   const sp = new URLSearchParams({
      command: "deleteUser",
      obj_id,
      // dry: "1",
      dry: dry ? "1" : "0",
      token: iliastoken,
   })
   let url2 = `${url}?${sp.toString()}`
   const res = await fetch(url2, {method: "DELETE"})
   const data = await res.json()
   // TODO update search index
   return data //
}
 
async function deleteAllUsers () {
   const {data: users} = await db.getUsers(0, 100000)
   const res = {
      command: "deleteAllUsers",
      start: new Date(),
      end: new Date(),
      duration: null,
      count: 0,
      userIds: [],
   }
   for (const user of users) {
      await deleteUser(user.usr_id)
      res.count += 1
      res.userIds.push(user.usr_id)
   }
   res.end = new Date()
   res.duration = res.end - res.start
   // TODO update search index
   return res
}
 
async function deleteTeilnahme (ref_id, usr_id, dry = false) {
   const {obj_id} = await getObjIdFromRefId(ref_id)
   const sp = new URLSearchParams({
      command: "deleteTeilnahme",
      obj_id,
      usr_id,
      dry: dry ? "1" : "0",
      token: iliastoken,
   })
   let url2 = `${url}?${sp.toString()}`
   console.log("libIlias.deleteTeilnahme >>>", url2)
   let res
   try {
      res = await fetch(url2, {method: "DELETE"})
      return await res.json()
   } catch (ex) {
      console.error(ex)
      throw ex
   }
}