LabarchivesService.ts 2.72 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import {Sails, Model} from 'sails';

import * as la from '@uts-eresearch/provision-labarchives';

import {Config} from '../Config';

import services = require('../core/CoreService');

declare var sails: Sails;
declare var WorkspaceService, _;

export module Services {

  export class LabarchivesService extends services.Services.Core.Service {
    protected config: Config;

    protected _exportedMethods: any = [
Moises Sacal's avatar
Moises Sacal committed
18
19
      'login',
      'insertNode',
Moises Sacal's avatar
Moises Sacal committed
20
21
22
      'addEntry',
      'getNotebookInfo',
      'getNotebookTree'
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
    ];

    constructor() {
      super();
      this.config = new Config(sails.config.workspaces);
    }

    async login(key: any, username: string, password: string) {
      try {
        if (key && key['akid'] && key['password']) {
          return await la.accessInfo(key, username, password);
        } else {
          return Promise.reject(new Error('missing keys for accessing lab archives APIs'));
        }
      } catch (e) {
        return Promise.reject(new Error(e));
      }

    }

Moises Sacal's avatar
Moises Sacal committed
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
    async insertNode(key: any, userId: string, nbId: string, displayText) {
      try {
        if (key && key['akid'] && key['password']) {
          return await la.insertNode(key, userId, nbId, 0, displayText, false);
        } else {
          return Promise.reject(new Error('missing keys for accessing lab archives APIs'));
        }
      } catch (e) {
        return Promise.reject(new Error(e));
      }

    }

    async addEntry(key: any, userId: string, nbId: string, treeId: string, partType: string, metadata: string) {
      try {

        if (key && key['akid'] && key['password']) {
          return await la.addEntry(key, userId, nbId, treeId, partType, metadata);
        } else {
          return Promise.reject(new Error('missing keys for accessing lab archives APIs'));
        }
      } catch (e) {
        return Promise.reject(new Error(e));
      }

    }
Moises Sacal's avatar
Moises Sacal committed
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92

    async getNotebookInfo(key: any, userId: string, nbId: string) {
      try {
        if (key && key['akid'] && key['password']) {
          return await la.getNotebookInfo(key, userId, nbId);
        } else {
          return Promise.reject(new Error('missing keys for accessing lab archives APIs'));
        }
      } catch (e) {
        return Promise.reject(new Error(e));
      }
    }

    async getNotebookTree(key: any, userId: string, nbId: string, treeLevel: number = 0) {
      try {
        if (key && key['akid'] && key['password']) {
          return await la.getTree(key, userId, nbId, treeLevel);
        } else {
          return Promise.reject(new Error('missing keys for accessing lab archives APIs'));
        }
      } catch (e) {
        return Promise.reject(new Error(e));
      }
    }
93
94
95
  }
}
module.exports = new Services.LabarchivesService().exports();