• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

FAIRsharing / fairsharing.github.io / 25737361825

12 May 2026 01:23PM UTC coverage: 95.915% (+0.01%) from 95.902%
25737361825

push

github

web-flow
Merge pull request #2766 from FAIRsharing/refactoring-processing-notes-2759

Refactoring processing notes 2759

3645 of 3816 branches covered (95.52%)

Branch coverage included in aggregate %.

190 of 229 new or added lines in 3 files covered. (82.97%)

2 existing lines in 2 files now uncovered.

38833 of 40471 relevant lines covered (95.95%)

5.44 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

91.04
/src/lib/Client/RESTClient.js
1
import axios from "axios";
1✔
2

3
//import {toBase64} from "@/utils/generalUtils"
1✔
4
class RESTClient {
1✔
5
  /**
1✔
6
   * The RESTClient is a singleton class that handles the connection and data exchange from the back-end
1✔
7
   * REST API.
1✔
8
   */
1✔
9
  constructor() {
1✔
10
    if (RESTClient._instance) {
287✔
11
      return RESTClient._instance;
186✔
12
    }
186✔
13
    RESTClient._instance = this;
101✔
14
    this.baseURL = import.meta.env.VITE_API_ENDPOINT;
101✔
15
    this.headers = {
101✔
16
      Accept: "application/json",
101✔
17
      "Content-Type": "application/json",
101✔
18
      "Cache-Control": "no-cache",
101✔
19
    };
101✔
20
    this.headers["X-Client-Id"] = import.meta.env.VITE_CLIENT_ID;
101✔
21
  }
287✔
22

23
  /* USERS: all methods below related to handling user authentication */
1✔
24

25
  /**
1✔
26
   * Method to add authorisation to the headers.
1✔
27
   * @param jwt - the user's json web token
1✔
28
   * @return {JSON} - headers with authorisation field added
1✔
29
   */
1✔
30
  auth_headers(jwt) {
1✔
31
    let headers = JSON.parse(JSON.stringify(this.headers));
146✔
32
    headers["Authorization"] = "Bearer " + jwt;
146✔
33
    return headers;
146✔
34
  }
146✔
35

36
  /**
1✔
37
   * Method to log in the user
1✔
38
   * @param username - name of the user
1✔
39
   * @param password - password of the user
1✔
40
   * @return {Promise} - the response of the server
1✔
41
   */
1✔
42
  async login(username, password) {
1✔
43
    const endpoint = "/users/sign_in";
10✔
44
    const body = {
10✔
45
      user: {
10✔
46
        login: username,
10✔
47
        password: password,
10✔
48
      },
10✔
49
    };
10✔
50
    const request = {
10✔
51
      method: "post",
10✔
52
      baseURL: this.baseURL + endpoint,
10✔
53
      data: body,
10✔
54
      headers: this.headers,
10✔
55
    };
10✔
56
    let response = await this.executeQuery(request);
10✔
57
    return response.data;
10✔
58
  }
10✔
59

60
  /**
1✔
61
   * Logout the user from the back, expiring the current jwt.
1✔
62
   * @param {String} jwt - the user token to expire.
1✔
63
   * @returns {Promise}
1✔
64
   */
1✔
65
  async logout(jwt) {
1✔
66
    const request = {
3✔
67
      method: "delete",
3✔
68
      baseURL: this.baseURL + "/users/sign_out",
3✔
69
      headers: this.auth_headers(jwt),
3✔
70
    };
3✔
71
    let response = await this.executeQuery(request);
3✔
72
    return response.data;
3✔
73
  }
3✔
74

75
  /**
1✔
76
   * Delete the user's account (they must be logged in).
1✔
77
   * @param {String} jwt - the user token to expire.
1✔
78
   * @returns {Promise}
1✔
79
   */
1✔
80
  async delete(jwt) {
1✔
81
    const request = {
2✔
82
      method: "delete",
2✔
83
      baseURL: this.baseURL + "/users",
2✔
84
      headers: this.auth_headers(jwt),
2✔
85
    };
2✔
86
    let response = await this.executeQuery(request);
2✔
87
    return response.data;
2✔
88
  }
2✔
89

90
  /**
1✔
91
   *  Method to create a new user
1✔
92
   * @param {Object} userLogin - the user account to create
1✔
93
   * @returns {Promise} response - server response
1✔
94
   */
1✔
95
  async createAccount(userLogin) {
1✔
96
    const request = {
1✔
97
      method: "post",
1✔
98
      baseURL: this.baseURL + "/users",
1✔
99
      data: { user: userLogin },
1✔
100
      headers: this.headers,
1✔
101
    };
1✔
102
    let response = await this.executeQuery(request);
1✔
103
    return response.data;
1✔
104
  }
1✔
105

106
  /**
1✔
107
   * Validate the account given the corresponding token
1✔
108
   * @param {String} token - the account token to validate
1✔
109
   * @returns {Promise}
1✔
110
   */
1✔
111
  async confirmAccount(token) {
1✔
112
    const request = {
5✔
113
      method: "get",
5✔
114
      baseURL: this.baseURL + "/users/confirmation?confirmation_token=" + token,
5✔
115
      headers: this.headers,
5✔
116
    };
5✔
117
    let response = await this.executeQuery(request);
5✔
118
    return response.data;
5✔
119
  }
5✔
120

121
  /**
1✔
122
   * Method to send a reset password link to the given email address
1✔
123
   * @param {String} email to send the link to
1✔
124
   * @returns {Promise}
1✔
125
   */
1✔
126
  async requestResetPwd(email) {
1✔
127
    const request = {
1✔
128
      method: "post",
1✔
129
      baseURL: this.baseURL + "/users/password",
1✔
130
      headers: this.headers,
1✔
131
      data: {
1✔
132
        user: { email: email },
1✔
133
      },
1✔
134
    };
1✔
135
    let response = await this.executeQuery(request);
1✔
136
    return response.data;
1✔
137
  }
1✔
138

139
  /**
1✔
140
   * Resend the validation link for a given user
1✔
141
   * @param {Object} user - contains the email of the user.
1✔
142
   * @returns {Promise}
1✔
143
   */
1✔
144
  async resendConfirmation(user) {
1✔
145
    const request = {
1✔
146
      method: "post",
1✔
147
      baseURL: this.baseURL + "/users/confirmation",
1✔
148
      headers: this.headers,
1✔
149
      data: { user: user },
1✔
150
    };
1✔
151
    let response = await this.executeQuery(request);
1✔
152
    return response.data;
1✔
153
  }
1✔
154

155
  /**
1✔
156
   * Reset the password of the given user
1✔
157
   * @param {Object} user - contains the new pwd, repeated pwd and token.
1✔
158
   * @returns {Promise}
1✔
159
   */
1✔
160
  async resetPassword(user) {
1✔
161
    const request = {
4✔
162
      method: "put",
4✔
163
      baseURL: this.baseURL + "/users/password",
4✔
164
      headers: this.headers,
4✔
165
      data: { user: user },
4✔
166
    };
4✔
167
    let response = await this.executeQuery(request);
4✔
168
    return response.data;
4✔
169
  }
4✔
170

171
  /**
1✔
172
   * Changes the password of the logged in user
1✔
173
   * @param {String} jwt - the user token
1✔
174
   * @param {Object} user - contains the current, new and repeated new password
1✔
175
   * @returns {Promise}
1✔
176
   */
1✔
177
  async resetPasswordWithoutToken(jwt, user) {
1✔
178
    const request = {
2✔
179
      method: "put",
2✔
180
      baseURL: this.baseURL + "/users/",
2✔
181
      headers: this.auth_headers(jwt),
2✔
182
      data: { user: user },
2✔
183
    };
2✔
184
    let response = await this.executeQuery(request);
2✔
185
    return response.data;
2✔
186
  }
2✔
187

188
  /**
1✔
189
   * Verify the validity of the given password
1✔
190
   * @param {String} password - the password to test
1✔
191
   * @returns {Promise}
1✔
192
   */
1✔
193
  async verifyPassword(password) {
1✔
194
    let headers = JSON.parse(JSON.stringify(this.headers));
3✔
195
    const request = {
3✔
196
      method: "post",
3✔
197
      baseURL: this.baseURL + "/users/check_password",
3✔
198
      headers: headers,
3✔
199
      data: { password: password },
3✔
200
    };
3✔
201
    let response = await this.executeQuery(request);
3✔
202
    return response.data;
3✔
203
  }
3✔
204

205
  /**
1✔
206
   * Get the current user data
1✔
207
   * @param jwt
1✔
208
   * @returns {Promise}
1✔
209
   */
1✔
210
  async getUser(jwt) {
1✔
211
    const request = {
18✔
212
      method: "get",
18✔
213
      baseURL: this.baseURL + "/users/edit",
18✔
214
      headers: this.auth_headers(jwt),
18✔
215
    };
18✔
216
    let response = await this.executeQuery(request);
18✔
217
    return response.data;
18✔
218
  }
18✔
219

220
  /**
1✔
221
   * Get the current user data
1✔
222
   * @param jwt
1✔
223
   * @param id
1✔
224
   * @returns {Promise}
1✔
225
   */
1✔
226
  async getPublicUser(jwt, id) {
1✔
227
    const request = {
1✔
228
      method: "get",
1✔
229
      baseURL: this.baseURL + `/user_admin/${id}`,
1✔
230
      headers: this.auth_headers(jwt),
1✔
231
    };
1✔
232
    let response = await this.executeQuery(request);
1✔
233
    return response.data;
1✔
234
  }
1✔
235

236
  /**
1✔
237
   * Get all users list
1✔
238
   * @param jwt
1✔
239
   * @returns {Promise}
1✔
240
   */
1✔
241
  async getUsersList(jwt) {
1✔
242
    const request = {
1✔
243
      method: "get",
1✔
244
      baseURL: this.baseURL + "/user_admin/",
1✔
245
      headers: this.auth_headers(jwt),
1✔
246
    };
1✔
247
    let response = await this.executeQuery(request);
1✔
248
    return response.data;
1✔
249
  }
1✔
250

251
  /**
1✔
252
   * Edit the current logged in user profile
1✔
253
   * @param {Object} newUser - the new values for the logged in user
1✔
254
   * @param {String} jwt - JWT of the logged in user
1✔
255
   * @returns {Promise}
1✔
256
   */
1✔
257
  async editUser(newUser, jwt) {
1✔
258
    const request = {
4✔
259
      method: "put",
4✔
260
      baseURL: this.baseURL + "/users",
4✔
261
      headers: this.auth_headers(jwt),
4✔
262
      data: { user: newUser },
4✔
263
    };
4✔
264
    let response = await this.executeQuery(request);
4✔
265
    return response.data;
4✔
266
  }
4✔
267
  /**
1✔
268
   * Edit the current logged in user profile
1✔
269
   * @param {Object} newUser - the new values for the logged in user
1✔
270
   * @param {String} jwt - JWT of the logged in user
1✔
271
   * @returns {Promise}
1✔
272
   */
1✔
273
  async editPublicUser(newUser, jwt) {
1✔
274
    const request = {
2✔
275
      method: "put",
2✔
276
      baseURL: this.baseURL + `/user_admin/${newUser.id}`,
2✔
277
      headers: this.auth_headers(jwt),
2✔
278
      data: { user: newUser },
2✔
279
    };
2✔
280
    let response = await this.executeQuery(request);
2✔
281
    return response.data;
2✔
282
  }
2✔
283
  /**
1✔
284
   * Delete the user
1✔
285
   * @param userID
1✔
286
   * @param {String} jwt - JWT of the logged in user
1✔
287
   * @returns {Promise}
1✔
288
   */
1✔
289
  async deletePublicUser(userID, jwt) {
1✔
290
    const request = {
2✔
291
      method: "delete",
2✔
292
      baseURL: this.baseURL + `/user_admin/${userID}`,
2✔
293
      headers: this.auth_headers(jwt),
2✔
294
    };
2✔
295
    let response = await this.executeQuery(request);
2✔
296
    return response.data;
2✔
297
  }
2✔
298
  /**
1✔
299
   * Verify that the given JWT is still valid
1✔
300
   * @param {String} jwt - the token to validate
1✔
301
   * @returns {Promise}
1✔
302
   */
1✔
303
  async validateToken(jwt) {
1✔
304
    const request = {
2✔
305
      method: "get",
2✔
306
      baseURL: this.baseURL + "/users/valid",
2✔
307
      headers: this.auth_headers(jwt),
2✔
308
    };
2✔
309
    let response = await this.executeQuery(request);
2✔
310
    return response.data;
2✔
311
  }
2✔
312

313
  /* EDITORS METHODS */
1✔
314

315
  /**
1✔
316
   * Post the given object to the API to create the corresponding record.
1✔
317
   * @param record
1✔
318
   * @param {String} jwt - JWT of the logged in user
1✔
319
   * @returns {Promise}
1✔
320
   */
1✔
321
  async createRecord(record, jwt) {
1✔
322
    const request = {
4✔
323
      method: "post",
4✔
324
      baseURL: this.baseURL + "/fairsharing_records",
4✔
325
      headers: this.auth_headers(jwt),
4✔
326
      data: { fairsharing_record: record },
4✔
327
    };
4✔
328
    let response = await this.executeQuery(request);
4✔
329
    return response.data;
4✔
330
  }
4✔
331

332
  /**
1✔
333
   * Update the given record
1✔
334
   * @param {Object} record - the record to update containing the ID to target, the new values
1✔
335
   * and the user token
1✔
336
   * @returns {Promise}
1✔
337
   */
1✔
338
  async updateRecord(record) {
1✔
339
    const request = {
30✔
340
      method: "put",
30✔
341
      baseURL: this.baseURL + "/fairsharing_records/" + record.id,
30✔
342
      headers: this.auth_headers(record.token),
30✔
343
      data: { fairsharing_record: record.record },
30✔
344
    };
30✔
345
    let response = await this.executeQuery(request);
30✔
346
    return response.data;
30✔
347
  }
30✔
348

349
  /**
1✔
350
   * Determine if a user has permission to edit this record.
1✔
351
   * @param {Integer} recordID - ID for the relevant FairsharingRecord.
1✔
352
   * @param {String} userToken - JWT of the logged in user
1✔
353
   * @returns {Promise}
1✔
354
   */
1✔
355
  async canEdit(recordID, userToken) {
1✔
356
    const request = {
6✔
357
      method: "get",
6✔
358
      baseURL: this.baseURL + "/fairsharing_records/can_edit/" + recordID,
6✔
359
      headers: this.auth_headers(userToken),
6✔
360
    };
6✔
361
    let response = await this.executeQuery(request);
6✔
362
    return response.data;
6✔
363
  }
6✔
364

365
  /**
1✔
366
   * Attempt to create a MaintenanceRequest for a user for a FairsharingRecord.
1✔
367
   * @param {Integer} recordID - ID for the relevant FairsharingRecord.
1✔
368
   * @param {String} userToken - JWT of the logged in user
1✔
369
   * @returns {Promise}
1✔
370
   */
1✔
371
  async claimRecord(recordID, userToken) {
1✔
372
    const request = {
1✔
373
      method: "post",
1✔
374
      baseURL: this.baseURL + "/maintenance_requests",
1✔
375
      headers: this.auth_headers(userToken),
1✔
376
      data: { maintenance_request: { fairsharing_record_id: recordID } },
1✔
377
    };
1✔
378
    let response = await this.executeQuery(request);
1✔
379
    return response.data;
1✔
380
  }
1✔
381

382
  /**
1✔
383
   * Determine if a user has permission to create a MaintenanceRequest for a FairsharingRecord.
1✔
384
   * @param {Integer} recordID - ID for the relevant FairsharingRecord.
1✔
385
   * @param {String} userToken - JWT of the logged in user
1✔
386
   * @returns {Promise}
1✔
387
   */
1✔
388
  async canClaim(recordID, userToken) {
1✔
389
    const request = {
1✔
390
      method: "get",
1✔
391
      baseURL: this.baseURL + "/maintenance_requests/existing/" + recordID,
1✔
392
      headers: this.auth_headers(userToken),
1✔
393
    };
1✔
394
    let response = await this.executeQuery(request);
1✔
395
    return response.data;
1✔
396
  }
1✔
397

398
  /**
1✔
399
   * Remove an existing maintainer from a FairsharingRecord.
1✔
400
   * @param {Integer} recordID - ID for the relevant FairsharingRecord.
1✔
401
   * @param {String} userToken - JWT of the logged in user
1✔
402
   * @returns {Promise}
1✔
403
   */
1✔
404
  // TODO: Coverage steadfastly refuses to see this even though it is mocked and called
1✔
405
  // in a test (see Record.spec.js).
1✔
406
  async removeMaintainer(recordID, userToken) {
1✔
407
    const request = {
1✔
408
      method: "post",
1✔
409
      baseURL: this.baseURL + "/maintenance_requests/remove",
1✔
410
      headers: this.auth_headers(userToken),
1✔
411
      data: { maintenance_request: { fairsharing_record_id: recordID } },
1✔
412
    };
1✔
413
    let response = await this.executeQuery(request);
1✔
414
    return response.data;
1✔
415
  }
1✔
416

417
  /**
1✔
418
   * Add or remove an watcher to/from a FairsharingRecord.
1✔
419
   * @param {Integer} recordID - ID for the relevant FairsharingRecord.
1✔
420
   * @param {String} operation - 'add' or 'remove'.
1✔
421
   * @param {String} userToken - JWT of the logged in user
1✔
422
   * @returns {Promise}
1✔
423
   */
1✔
424
  // TODO: Mocking this in a test (5c40b02ba3d78a49a088ab6dd5145c9ea10ea24b)
1✔
425
  // caused it to no longer be covered!
1✔
426
  async changeWatcher(recordID, operation, userToken) {
1✔
427
    const request = {
1✔
428
      method: "post",
1✔
429
      baseURL: this.baseURL + "/fairsharing_records/watch",
1✔
430
      headers: this.auth_headers(userToken),
1✔
431
      data: {
1✔
432
        record_id: recordID,
1✔
433
        operation: operation,
1✔
434
      },
1✔
435
    };
1✔
436
    let response = await this.executeQuery(request);
1✔
437
    return response.data;
1✔
438
  }
1✔
439

440
  /**
1✔
441
   * Attempt to create a RecordReview for a user for a FairsharingRecord.
1✔
442
   * @param {Integer} recordID - ID for the relevant FairsharingRecord.
1✔
443
   * @param {String} userToken - JWT of the logged in user
1✔
444
   * @returns {Promise}
1✔
445
   */
1✔
446
  async reviewRecord(recordID, userToken) {
1✔
447
    const request = {
2✔
448
      method: "post",
2✔
449
      baseURL: this.baseURL + "/record_reviews",
2✔
450
      headers: this.auth_headers(userToken),
2✔
451
      data: { record_review: { fairsharing_record_id: recordID } },
2✔
452
    };
2✔
453
    let response = await this.executeQuery(request);
2✔
454
    return response.data;
2✔
455
  }
2✔
456

457
  /* USER DEFINED TAGS */
1✔
458

459
  /**
1✔
460
   * Create a new user defined tag in the database for users to tag their records.
1✔
461
   * @param {String} term - the string value of the term
1✔
462
   * @param {String} token - the user JWT
1✔
463
   * @returns {Promise}
1✔
464
   */
1✔
465
  async createNewUserDefinedTag(term, token) {
1✔
466
    const request = {
2✔
467
      method: "post",
2✔
468
      baseURL: this.baseURL + "/user_defined_tags",
2✔
469
      headers: this.auth_headers(token),
2✔
470
      data: { user_defined_tag: { label: term } },
2✔
471
    };
2✔
472
    let response = await this.executeQuery(request);
2✔
473
    return response.data;
2✔
474
  }
2✔
475

476
  /* LICENCES */
1✔
477

478
  /**
1✔
479
   * Create new a licence link
1✔
480
   * @param {Object} licenceLink - the licence link to create
1✔
481
   * @param {String} token - the user token
1✔
482
   * @returns {Promise}
1✔
483
   */
1✔
484
  async createLicenceLink(licenceLink, token) {
1✔
485
    let _client = this;
3✔
486
    const request = {
3✔
487
      method: "post",
3✔
488
      baseURL: _client.baseURL + "/licence_links",
3✔
489
      headers: this.auth_headers(token),
3✔
490
      data: { licence_link: licenceLink },
3✔
491
    };
3✔
492
    let response = await _client.executeQuery(request);
3✔
493
    return response.data;
3✔
494
  }
3✔
495

496
  /**
1✔
497
   * Delete the given licence
1✔
498
   * @param {Number} id - id of the licence link to delete
1✔
499
   * @param {String} token - the user token
1✔
500
   * @returns {Promise}
1✔
501
   */
1✔
502
  async deleteLicenceLink(id, token) {
1✔
503
    let _client = this;
1✔
504
    const request = {
1✔
505
      method: "delete",
1✔
506
      baseURL: _client.baseURL + "/licence_links/" + id,
1✔
507
      headers: this.auth_headers(token),
1✔
508
    };
1✔
509
    let response = await _client.executeQuery(request);
1✔
510
    return response.data;
1✔
511
  }
1✔
512

513
  /**
1✔
514
   * Update the licenceLink
1✔
515
   * @param {Object} licenceLink - the new values for the licence link
1✔
516
   * @param {String} token the user token
1✔
517
   * @returns {Promise}
1✔
518
   */
1✔
519
  async updateLicenceLink(licenceLink, token) {
1✔
520
    let _client = this;
1✔
521
    const request = {
1✔
522
      method: "put",
1✔
523
      baseURL: _client.baseURL + "/licence_links/" + licenceLink.id,
1✔
524
      headers: this.auth_headers(token),
1✔
525
      data: { licence_link: licenceLink },
1✔
526
    };
1✔
527
    let response = await _client.executeQuery(request);
1✔
528
    return response.data;
1✔
529
  }
1✔
530

531
  /* PUBLICATIONS */
1✔
532

533
  async createPublication(publication, token) {
1✔
534
    let _client = this;
1✔
535
    const request = {
1✔
536
      method: "post",
1✔
537
      baseURL: _client.baseURL + "/publications",
1✔
538
      headers: this.auth_headers(token),
1✔
539
      data: { publication: publication },
1✔
540
    };
1✔
541
    let response = await _client.executeQuery(request);
1✔
542
    return response.data;
1✔
543
  }
1✔
544

545
  async editPublication(publication, token) {
1✔
546
    let _client = this;
1✔
547
    const request = {
1✔
548
      method: "put",
1✔
549
      baseURL: _client.baseURL + "/publications/" + publication.id,
1✔
550
      headers: this.auth_headers(token),
1✔
551
      data: { publication: publication },
1✔
552
    };
1✔
553
    let response = await _client.executeQuery(request);
1✔
554
    return response.data;
1✔
555
  }
1✔
556

557
  /* RELATIONSHIPS BETWEEN RECORDS */
1✔
558
  /**
1✔
559
   * Saves the relationships as an array of items containing a targetID, a sourceID and a labelID
1✔
560
   * @param {Object} options - the options to pass as {token: String, relations: Array}
1✔
561
   * @returns {Promise}
1✔
562
   */
1✔
563
  async saveRelations(options) {
1✔
564
    if (options.relations.length > 0) {
2✔
565
      const request = {
1✔
566
        method: "put",
1✔
567
        baseURL: this.baseURL + "/fairsharing_records/" + options.target,
1✔
568
        headers: this.auth_headers(options.token),
1✔
569
        data: {
1✔
570
          fairsharing_record: {
1✔
571
            record_associations_attributes: options.relations,
1✔
572
          },
1✔
573
        },
1✔
574
      };
1✔
575
      let response = await this.executeQuery(request);
1✔
576
      return response.data;
1✔
577
    }
1✔
578
    return {};
1✔
579
  }
2✔
580

581
  /**
1✔
582
   * Deletes the relationships as an array of items containing a targetID, a sourceID and a labelID
1✔
583
   * @param {Object} options - the options to pass as {token: String, relations: Array}
1✔
584
   * @returns {Promise}
1✔
585
   */
1✔
586
  async deleteRelations(options) {
1✔
587
    if (options.relations.length > 0) {
×
588
      const request = {
×
589
        method: "put",
×
590
        baseURL: this.baseURL + "/fairsharing_records/" + options.target,
×
591
        headers: this.auth_headers(options.token),
×
592
        data: {
×
593
          fairsharing_record: {
×
594
            record_associations_attributes: options.relations,
×
595
          },
×
596
        },
×
597
      };
×
598
      let response = await this.executeQuery(request);
×
599
      return response.data;
×
600
    }
×
601
    return {};
×
602
  }
×
603

604
  /* ORGANISATIONS AND GRANTS */
1✔
605
  /**
1✔
606
   * Create a given organisation
1✔
607
   * @param {Object} organisation
1✔
608
   * @param {String} userToken - the user jwt
1✔
609
   * @returns {Promise}
1✔
610
   */
1✔
611
  async createOrganisation(organisation, userToken) {
1✔
612
    let _client = this;
5✔
613
    const request = {
5✔
614
      method: "post",
5✔
615
      baseURL: _client.baseURL + "/organisations",
5✔
616
      headers: this.auth_headers(userToken),
5✔
617
      data: { organisation: organisation },
5✔
618
    };
5✔
619
    let response = await _client.executeQuery(request);
5✔
620
    return response.data;
5✔
621
  }
5✔
622

623
  /**
1✔
624
   * Edit a given organisation
1✔
625
   * @param {Object} organisation
1✔
626
   * @param {Number} id
1✔
627
   * @param {String} userToken - the user jwt
1✔
628
   * @returns {Promise}
1✔
629
   */
1✔
630
  async editOrganisation(organisation, id, userToken) {
1✔
631
    let _client = this;
×
632
    const request = {
×
633
      method: "put",
×
634
      baseURL: _client.baseURL + "/organisations/" + id,
×
635
      headers: this.auth_headers(userToken),
×
636
      data: { organisation: organisation },
×
637
    };
×
638
    let response = await _client.executeQuery(request);
×
639
    return response.data;
×
640
  }
×
641

642
  /**
1✔
643
   * Delete the given organisation link
1✔
644
   * @param {Number} organisationId - the id of the link to remove
1✔
645
   * @param {String} userToken - the user jwt
1✔
646
   * @returns {Promise}
1✔
647
   */
1✔
648
  async deleteOrganisation(organisationId, userToken) {
1✔
649
    let _client = this;
×
650
    const request = {
×
651
      method: "delete",
×
652
      baseURL: _client.baseURL + "/organisations/" + organisationId,
×
653
      headers: this.auth_headers(userToken),
×
654
    };
×
655
    let response = await _client.executeQuery(request);
×
656
    return response.data;
×
657
  }
×
658

659
  /**
1✔
660
   * Create a given grant
1✔
661
   * @param {Object} grant
1✔
662
   * @param {String} userToken - the user jwt
1✔
663
   * @returns {Promise}
1✔
664
   */
1✔
665
  async createGrant(grant, userToken) {
1✔
666
    let _client = this;
2✔
667
    const request = {
2✔
668
      method: "post",
2✔
669
      baseURL: _client.baseURL + "/grants",
2✔
670
      headers: this.auth_headers(userToken),
2✔
671
      data: { grant: grant },
2✔
672
    };
2✔
673
    let response = await _client.executeQuery(request);
2✔
674
    return response.data;
2✔
675
  }
2✔
676

677
  /**
1✔
678
   * Create a new link between an organisation, a record and an optional grant.
1✔
679
   * @param {Object} organisationLink - the organisation link to create
1✔
680
   * @param {String} userToken - the user jwt
1✔
681
   * @returns {Promise}
1✔
682
   */
1✔
683
  async createOrganisationLink(organisationLink, userToken) {
1✔
684
    let _client = this;
×
685
    const request = {
×
686
      method: "post",
×
687
      baseURL: _client.baseURL + "/organisation_links",
×
688
      headers: this.auth_headers(userToken),
×
689
      data: { organisation_link: organisationLink },
×
690
    };
×
691
    let response = await _client.executeQuery(request);
×
692
    return response.data;
×
693
  }
×
694

695
  /**
1✔
696
   * Update the organisationLink given from linkID input with the given organisationLink
1✔
697
   * @param {Object} organisationLink - the new organisation link value
1✔
698
   * @param {Number} linkID - ID of the organisationLink to update
1✔
699
   * @param {String} userToken - the user jwt
1✔
700
   * @returns {Promise}
1✔
701
   */
1✔
702
  async updateOrganisationLink(organisationLink, linkID, userToken) {
1✔
703
    let _client = this;
2✔
704
    const request = {
2✔
705
      method: "put",
2✔
706
      baseURL: _client.baseURL + "/organisation_links/" + linkID,
2✔
707
      headers: this.auth_headers(userToken),
2✔
708
      data: { organisation_link: organisationLink },
2✔
709
    };
2✔
710
    let response = await _client.executeQuery(request);
2✔
711
    return response.data;
2✔
712
  }
2✔
713

714
  /**
1✔
715
   * Delete the given organisation link
1✔
716
   * @param {Number} linkID - the id of the link to remove
1✔
717
   * @param {String} userToken - the user jwt
1✔
718
   * @returns {Promise}
1✔
719
   */
1✔
720
  async deleteOrganisationLink(linkID, userToken) {
1✔
721
    let _client = this;
1✔
722
    const request = {
1✔
723
      method: "delete",
1✔
724
      baseURL: _client.baseURL + "/organisation_links/" + linkID,
1✔
725
      headers: this.auth_headers(userToken),
1✔
726
    };
1✔
727
    let response = await _client.executeQuery(request);
1✔
728
    return response.data;
1✔
729
  }
1✔
730

731
  /**
1✔
732
   * Get the extra metadata fields for a RecordType
1✔
733
   * @param {String} type - name of the record type.
1✔
734
   * @param {String} userToken - the user jwt
1✔
735
   * @returns {Promise}
1✔
736
   */
1✔
737
  async extraMetadataFields(type, userToken) {
1✔
738
    const request = {
11✔
739
      method: "post",
11✔
740
      baseURL: this.baseURL + "/fairsharing_records/metadata_fields",
11✔
741
      headers: this.auth_headers(userToken),
11✔
742
      data: { type: type },
11✔
743
    };
11✔
744
    let response = await this.executeQuery(request);
11✔
745
    return response.data;
11✔
746
  }
11✔
747

748
  /* METHODS FOR CURATION */
1✔
749
  /**
1✔
750
   * Update the maintenanceRequest given the new status value
1✔
751
   * @param {Number} maintenanceRequest  ID of the maintenanceRequest to update
1✔
752
   * @param {string} newStatus - new status to update
1✔
753
   * @param {String} userToken - the user jwt
1✔
754
   * @returns {Promise}
1✔
755
   */
1✔
756
  async updateStatusMaintenanceRequest(
1✔
757
    maintenanceRequest,
×
758
    newStatus,
×
759
    userToken,
×
760
  ) {
×
761
    let _client = this;
×
762
    const request = {
×
763
      method: "put",
×
764
      baseURL: _client.baseURL + "/maintenance_requests/" + maintenanceRequest,
×
765
      headers: this.auth_headers(userToken),
×
766
      data: { maintenance_request: { status: newStatus } },
×
767
    };
×
768
    let response = await _client.executeQuery(request);
×
769
    return response.data;
×
770
  }
×
771

772
  /**
1✔
773
   * Delete Record
1✔
774
   * @param {Number} id - id of the record link to delete
1✔
775
   * @param {String} token - the user token
1✔
776
   * @returns {Promise}
1✔
777
   */
1✔
778
  async deleteRecord(id, token) {
1✔
779
    let _client = this;
4✔
780
    const request = {
4✔
781
      method: "delete",
4✔
782
      baseURL: _client.baseURL + "/fairsharing_records/" + id,
4✔
783
      headers: this.auth_headers(token),
4✔
784
    };
4✔
785
    let response = await _client.executeQuery(request);
4✔
786
    return response.data;
4✔
787
  }
4✔
788

789
  /**
1✔
790
   * Get the list of allowed relation types for editing record's relationships.
1✔
791
   * @returns {Promise}
1✔
792
   */
1✔
793
  /* EDITOR DATA */
1✔
794
  async getRelationsTypes() {
1✔
795
    let _client = this;
×
796
    const request = {
×
797
      method: "get",
×
798
      baseURL: _client.baseURL + "/record_associations/allowed",
×
799
    };
×
800
    let response = await _client.executeQuery(request);
×
801
    return response.data;
×
802
  }
×
803

804
  /**
1✔
805
   * Get the list of available profile types for a user.
1✔
806
   * @returns {Promise}
1✔
807
   */
1✔
808
  async getProfileTypes() {
1✔
809
    const request = {
1✔
810
      method: "get",
1✔
811
      baseURL: this.baseURL + "/users/profile_types",
1✔
812
      headers: this.headers,
1✔
813
    };
1✔
814
    let response = await this.executeQuery(request);
1✔
815
    return response.data;
1✔
816
  }
1✔
817

818
  /**
1✔
819
   * Get the list of available roles for a user.
1✔
820
   * @returns {Promise}
1✔
821
   * @param {String} token - the user token
1✔
822
   */
1✔
823
  // Coverage steadfastly refuses to see this even though it is mocked and called
1✔
824
  // in a test (see EditPublicProfile.spec.js).
1✔
825
  async getUserRoles(userToken) {
1✔
UNCOV
826
    const request = {
×
827
      method: "get",
×
828
      baseURL: this.baseURL + "/user_roles",
×
829
      headers: this.auth_headers(userToken),
×
830
    };
×
831
    let response = await this.executeQuery(request);
×
832
    return response.data;
×
833
  }
×
834

835
  /* SYSTEM MESSAGES */
1✔
836
  /**
1✔
837
   * Update the given message
1✔
838
   * @param {Object} message - the message to update containing the ID to target and the new values
1✔
839
   * @param {String} userToken - the user jwt
1✔
840
   * @returns {Promise}
1✔
841
   */
1✔
842
  async updateMessage(message, userToken) {
1✔
843
    const request = {
2✔
844
      method: "put",
2✔
845
      baseURL: this.baseURL + "/messages/" + message.id,
2✔
846
      headers: this.auth_headers(userToken),
2✔
847
      data: { message: message },
2✔
848
    };
2✔
849
    let response = await this.executeQuery(request);
2✔
850
    return response.data;
2✔
851
  }
2✔
852

853
  /**
1✔
854
   * Update the given message
1✔
855
   * @param {Object} message - the message to create
1✔
856
   * @param {String} userToken - the user jwt
1✔
857
   * @returns {Promise}
1✔
858
   */
1✔
859
  async createMessage(message, userToken) {
1✔
860
    const request = {
2✔
861
      method: "post",
2✔
862
      baseURL: this.baseURL + "/messages/",
2✔
863
      headers: this.auth_headers(userToken),
2✔
864
      data: { message: message },
2✔
865
    };
2✔
866
    let response = await this.executeQuery(request);
2✔
867
    return response.data;
2✔
868
  }
2✔
869

870
  /**
1✔
871
   * Update the given message
1✔
872
   * @param {String} messageId - the ID of the message to delete
1✔
873
   * @param {String} userToken - the user jwt
1✔
874
   * @returns {Promise}
1✔
875
   */
1✔
876
  async deleteMessage(messageId, userToken) {
1✔
877
    const request = {
2✔
878
      method: "delete",
2✔
879
      baseURL: this.baseURL + "/messages/" + messageId,
2✔
880
      headers: this.auth_headers(userToken),
2✔
881
    };
2✔
882
    let response = await this.executeQuery(request);
2✔
883
    return response.data;
2✔
884
  }
2✔
885

886
  /* EXTRA METHODS */
1✔
887

888
  /**
1✔
889
   * Trigger the given query with Axios
1✔
890
   * @param query
1✔
891
   * @returns {Promise<*>}
1✔
892
   */
1✔
893
  async executeQuery(query) {
1✔
894
    try {
1✔
895
      return await axios(query);
1✔
896
    } catch (e) {
1✔
897
      return { data: { error: e } };
1✔
898
    }
1✔
899
  }
1✔
900

901
  /**
1✔
902
   * Get records without DOIS
1✔
903
   * @param {String} token - the user token that needs to be a curator
1✔
904
   * @returns {Promise}
1✔
905
   */
1✔
906
  async getRecordsWoDOIs(userToken) {
1✔
907
    let _client = this;
×
908
    const request = {
×
909
      method: "get",
×
910
      baseURL: _client.baseURL + "/files/no_dois",
×
911
      headers: this.auth_headers(userToken),
×
912
    };
×
913
    let response = await _client.executeQuery(request);
×
914
    return response.data;
×
915
  }
×
916

917
  /**
1✔
918
   * Get count of records created by month
1✔
919
   * @param {String} token - the user token that needs to be a curator
1✔
920
   * @returns {Promise}
1✔
921
   */
1✔
922
  async getRecordCreatedByMonth(userToken) {
1✔
923
    let _client = this;
×
924
    const request = {
×
925
      method: "get",
×
926
      baseURL: _client.baseURL + "/files/creation_dates",
×
927
      headers: this.auth_headers(userToken),
×
928
    };
×
929
    let response = await _client.executeQuery(request);
×
930
    return response.data;
×
931
  }
×
932

933
  /**
1✔
934
   * Get count of edits by month
1✔
935
   * @param {String} token - the user token that needs to be a curator
1✔
936
   * @returns {Promise}
1✔
937
   */
1✔
938
  async getEditByMonth(userToken) {
1✔
939
    let _client = this;
×
940
    const request = {
×
941
      method: "get",
×
942
      baseURL: _client.baseURL + "/files/edit_dates",
×
943
      headers: this.auth_headers(userToken),
×
944
    };
×
945
    let response = await _client.executeQuery(request);
×
946
    return response.data;
×
947
  }
×
948
  /**
1✔
949
   * Get Zenodo call
1✔
950
   * @param {String} call
1✔
951
   * @param {String} userToken - the user jwt
1✔
952
   * @returns {Promise}
1✔
953
   */
1✔
954
  async getZenodoSearch(text, userToken) {
1✔
955
    let _client = this;
4✔
956
    const request = {
4✔
957
      method: "post",
4✔
958
      baseURL: _client.baseURL + "/zenodo",
4✔
959
      headers: this.auth_headers(userToken),
4✔
960
      data: { doi: text },
4✔
961
    };
4✔
962
    let response = await _client.executeQuery(request);
4✔
963
    return response.data;
4✔
964
  }
4✔
965

966
  async clearLogo(id, token) {
1✔
967
    let _client = this;
1✔
968
    const request = {
1✔
969
      method: "put",
1✔
970
      baseURL: _client.baseURL + "/fairsharing_records/" + id,
1✔
971
      headers: _client.auth_headers(token),
1✔
972
      data: {
1✔
973
        fairsharing_record: {
1✔
974
          logo: {},
1✔
975
        },
1✔
976
      },
1✔
977
    };
1✔
978
    let response = await _client.executeQuery(request);
1✔
979
    // here the data returned always should be either an array of images or one string of image
1✔
980
    return response.data;
1✔
981
  }
1✔
982

983
  /**
1✔
984
   * Get statistics data without users
1✔
985
   * @returns {Promise}
1✔
986
   */
1✔
987
  async getStatisticsData() {
1✔
988
    let _client = this;
5✔
989
    const request = {
5✔
990
      method: "get",
5✔
991
      baseURL: _client.baseURL + "/homepage_stats",
5✔
992
    };
5✔
993
    let response = await _client.executeQuery(request);
5✔
994
    return response.data;
5✔
995
  }
5✔
996

997
  /**
1✔
998
   * Get JSON+LD for homepage.
1✔
999
   * @returns {Promise}
1✔
1000
   */
1✔
1001
  async getHomepageJsonld() {
1✔
1002
    let _client = this;
4✔
1003
    const request = {
4✔
1004
      method: "get",
4✔
1005
      baseURL: _client.baseURL + "/homepage_jsonld",
4✔
1006
    };
4✔
1007
    let response = await _client.executeQuery(request);
4✔
1008
    return response.data;
4✔
1009
  }
4✔
1010

1011
  /**
1✔
1012
   * Send the verification link for a given user
1✔
1013
   * @param {Object} user - contains the email, uid, identifier of the user.
1✔
1014
   * @returns {Promise}
1✔
1015
   */
1✔
1016
  async sendOrcidVerification(user) {
1✔
1017
    let _client = this;
2✔
1018
    const request = {
2✔
1019
      method: "post",
2✔
1020
      baseURL: this.baseURL + "/users/auth/identify_user",
2✔
1021
      headers: this.auth_headers(user),
2✔
1022
      data: { user: user },
2✔
1023
    };
2✔
1024
    let response = await _client.executeQuery(request);
2✔
1025
    return response.data;
2✔
1026
  }
2✔
1027

1028
  /**
1✔
1029
   * Save the advancedSearch results to the user
1✔
1030
   * @param {Object} saveSearchObj - save search object having required parameters
1✔
1031
   * @param {String} jwt - the user jwt
1✔
1032
   * @returns {Promise}
1✔
1033
   */
1✔
1034
  /* Ignoring because of 401 access denied in test case */
1✔
1035
  async saveSearch(saveSearchObj, jwt) {
1✔
1036
    let _client = this;
1✔
1037
    const request = {
1✔
1038
      method: "post",
1✔
1039
      baseURL: _client.baseURL + "/saved_searches",
1✔
1040
      headers: this.auth_headers(jwt),
1✔
1041
      data: { saved_search: saveSearchObj },
1✔
1042
    };
1✔
1043
    let response = await _client.executeQuery(request);
1✔
1044
    return response.data;
1✔
1045
  }
1✔
1046

1047
  /**
1✔
1048
   * Delete the saved search
1✔
1049
   * @param {Number} savedSearchId - the id of the savedSearch to remove
1✔
1050
   * @param {String} userToken - the user jwt
1✔
1051
   * @returns {Promise}
1✔
1052
   */
1✔
1053
  /* Ignoring because of 401 access denied in test case */
1✔
1054
  async deleteSavedSearch(savedSearchId, jwt) {
1✔
1055
    let _client = this;
1✔
1056
    const request = {
1✔
1057
      method: "delete",
1✔
1058
      baseURL: _client.baseURL + "/saved_searches/" + savedSearchId,
1✔
1059
      headers: this.auth_headers(jwt),
1✔
1060
    };
1✔
1061
    let response = await _client.executeQuery(request);
1✔
1062
    return response.data;
1✔
1063
  }
1✔
1064

1065
  /**
1✔
1066
   * Update the saved search
1✔
1067
   * @param {Number} saveSearchId - save search id
1✔
1068
   * @param {Object} saveSearchObj - save search object having required parameters
1✔
1069
   * @param {String} jwt the user token
1✔
1070
   * @returns {Promise}
1✔
1071
   */
1✔
1072
  /* Ignoring because of 401 access denied in test case */
1✔
1073
  async updateSaveSearch(saveSearchId, saveSearchObj, jwt) {
1✔
1074
    let _client = this;
1✔
1075
    const request = {
1✔
1076
      method: "put",
1✔
1077
      baseURL: _client.baseURL + "/saved_searches/" + saveSearchId,
1✔
1078
      headers: this.auth_headers(jwt),
1✔
1079
      data: { saved_search: saveSearchObj },
1✔
1080
    };
1✔
1081
    let response = await _client.executeQuery(request);
1✔
1082
    return response.data;
1✔
1083
  }
1✔
1084

1085
  /**
1✔
1086
   * Create processing note of the given record
1✔
1087
   * @param noteText {String} Processing Notes
1✔
1088
   * @param recordId {Number} FAIRsharing record Id
1✔
1089
   * @param jwt {String} the user token
1✔
1090
   * @return {Promise<*>}
1✔
1091
   */
1✔
1092
  async createProcessingNotes(noteText, recordId, jwt) {
1✔
1093
    const request = {
1✔
1094
      method: "post",
1✔
1095
      baseURL: this.baseURL + "/processing_notes/",
1✔
1096
      headers: this.auth_headers(jwt),
1✔
1097
      data: {
1✔
1098
        processing_note: {
1✔
1099
          note: noteText,
1✔
1100
          fairsharing_record_id: recordId,
1✔
1101
        },
1✔
1102
      },
1✔
1103
    };
1✔
1104
    let response = await this.executeQuery(request);
1✔
1105
    return response.data;
1✔
1106
  }
1✔
1107

1108
  /**
1✔
1109
   * Update processing note of the given record
1✔
1110
   * @param noteId {Number} Processing Note Id
1✔
1111
   * @param noteText {String} Processing Notes
1✔
1112
   * @param recordId {Number} FAIRsharing record Id
1✔
1113
   * @param jwt {String} the user token
1✔
1114
   * @return {Promise<*>}
1✔
1115
   */
1✔
1116
  async updateProcessingNotes(noteId, noteText, recordId, jwt) {
1✔
1117
    const request = {
1✔
1118
      method: "put",
1✔
1119
      baseURL: this.baseURL + "/processing_notes/" + noteId,
1✔
1120
      headers: this.auth_headers(jwt),
1✔
1121
      data: {
1✔
1122
        processing_note: {
1✔
1123
          note: noteText,
1✔
1124
          fairsharing_record_id: recordId,
1✔
1125
        },
1✔
1126
      },
1✔
1127
    };
1✔
1128
    let response = await this.executeQuery(request);
1✔
1129
    return response.data;
1✔
1130
  }
1✔
1131

1132
  /**
1✔
1133
   * Delete processing note of the given record
1✔
1134
   * @param noteId {Number} Processing Note Id
1✔
1135
   * @param jwt {String} the user token
1✔
1136
   * @return {Promise<*>}
1✔
1137
   */
1✔
1138
  async deleteProcessingNote(noteId, jwt) {
1✔
1139
    const request = {
1✔
1140
      method: "delete",
1✔
1141
      baseURL: this.baseURL + "/processing_notes/" + noteId,
1✔
1142
      headers: this.auth_headers(jwt),
1✔
1143
    };
1✔
1144
    return await this.executeQuery(request);
1✔
1145
  }
1✔
1146
}
1✔
1147

1148
export default RESTClient;
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc