DescriptorListService.ts 6.77 KB
Newer Older
1
2
import authenticatedRequest from 'utilities/requestUtils';
import { dereferenceReferences } from 'utilities';
Valeriy Panov's avatar
Valeriy Panov committed
3
import {log} from 'utilities/debug';
4
import { stringify } from 'query-string';
5

6
import {
7
    AUTOCOMPLETE_DESCRIPTORLIST_URL, MY_LIST_DESCRIPTORSLISTS_URL, GET_DESCRIPTORLIST_URL, REMOVE_DESCRIPTORLIST_URL,
8
9
10
    CREATE_DESCRIPTORLIST_URL, UPDATE_DESCRIPTORLIST_URL, LIST_DESCRIPTORLISTS_URL, PUBLISH_DESCRIPTORLIST_URL,
    ADD_DESCRIPTORLIST_DESCRIPTOR_URL, REMOVE_DESCRIPTORLIST_DESCRIPTOR_URL, SET_DESCRIPTORLIST_DESCRIPTORS_URL,
} from 'constants/apiURLS';
11

12
import { DescriptorList, IDescriptorListFilter } from 'model/descriptor.model';
13
14
import { Partner } from 'model/partner.model';
import { Page } from 'model/common.model';
15

16
17
18
19

export class DescriptorListService {

  // Lists my descriptor lists
20
  public static listMyDescriptorLists(token: string, page: number = 0, results: number = 10, sortBy: string = 'lastModifiedDate', filter: string | IDescriptorListFilter = {}, order: string = 'ASC'): Promise<Page<DescriptorList>> {
21

22
23
24
25
26
27
28
29
    const query = stringify({
      p: page,
      l: results,
      d: order,
      s: sortBy,
      f: typeof filter === 'string' ? filter : null,
    });
    // console.log('Query', query);
30

31
    return authenticatedRequest(token, {
32
      url: `${MY_LIST_DESCRIPTORSLISTS_URL}?${query}`,
33
      method: 'POST',
34
35
      headers: {'Content-Type': 'application/json'},
      data: typeof filter === 'string' ? null : filter,
36
37
38
39
40
41
    })
    .then(({ data }) => new Page<DescriptorList>(data, (dl) => new DescriptorList(dl)))
    .then((paged) => {
      dereferenceReferences(paged.content, 'owner', (o) => new Partner(o));
      return paged;
    });
42
43
44
  }

  // Lists published descriptor lists
45
  public static listDescriptorLists(token: string, page: number = 0, results: number = 10, sortBy: string = 'title', filter: IDescriptorListFilter = {}, order: string = 'ASC'): Promise<Page<DescriptorList>> {
46
47
48

    const sortParam = sortBy ? `&s=${sortBy}` : '';

49
    return authenticatedRequest(token, {
50
51
52
53
54
      url: `${LIST_DESCRIPTORLISTS_URL}?p=${page}&l=${results}&d=${order}${sortParam}`,
      method: 'POST',
      data: {
        ...filter,
      },
55
56
57
58
59
60
    })
    .then(({ data }) => new Page<DescriptorList>(data, (dl) => new DescriptorList(dl)))
    .then((paged) => {
      dereferenceReferences(paged.content, 'owner', (o) => new Partner(o));
      return paged;
    });
61
62
  }

63
64
65
66
67
68
69
70
  public static autocomplete(token: string, term: string): Promise<DescriptorList[]> {

    return authenticatedRequest(token, {
      url: `${AUTOCOMPLETE_DESCRIPTORLIST_URL}?d=${term}`,
      method: 'GET',
    }).then(({data}) => data);
  }

Maxym Borodenko's avatar
Maxym Borodenko committed
71
  // Lists published descriptor lists by filter code
72
  // TODO Remove
Maxym Borodenko's avatar
Maxym Borodenko committed
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
  public static listDescriptorListsByCode(token: string, page: number = 0, results: number = 10, sortBy: string = 'title', filterCode: string, order: string = 'ASC'): Promise<Page<DescriptorList>> {
    const sortParam = sortBy ? `&s=${sortBy}` : '';
    const code = filterCode ? `/${filterCode}` : '';

    return authenticatedRequest(token, {
      url: `${LIST_DESCRIPTORLISTS_URL}${code}?p=${page}&l=${results}&d=${order}${sortParam}`,
      method: 'POST',
      data: {},
    }).then(({ data }) => new Page<DescriptorList>(data, (dl) => new DescriptorList(dl)))
      .then((paged) => {
        dereferenceReferences(paged.content, 'owner', (o) => new Partner(o));
        return paged;
      });
  }

88
  // load descriptor list by uuid from server
89
  public static loadDescriptorList(token: string, uuid: string): Promise<DescriptorList> {
Valeriy Panov's avatar
Valeriy Panov committed
90
    log('Load descriptor list by UUID ', uuid);
91

92
    return authenticatedRequest(token, {
93
94
      url: `${GET_DESCRIPTORLIST_URL}/${uuid}`,
      method: 'GET',
95
96
97
98
99
100
    })
    .then(({ data }) => new DescriptorList(data))
    .then((obj) => {
      dereferenceReferences([ obj ], 'owner', (o) => new Partner(o));
      return obj;
    });
101
102
  }

103
  public static createDescriptorList(token: string, descriptorList: DescriptorList): Promise<DescriptorList> {
Valeriy Panov's avatar
Valeriy Panov committed
104
    log('Create descriptor', descriptorList);
105

106
    return authenticatedRequest(token, {
107
108
109
110
111
112
113
114
      url: `${CREATE_DESCRIPTORLIST_URL}`,
      method: 'POST',
      data: {
        ...descriptorList,
      },
    }).then(({ data }) => new DescriptorList(data));
  }

115
  public static updateDescriptorList(token: string, descriptorList: DescriptorList): Promise<DescriptorList> {
Valeriy Panov's avatar
Valeriy Panov committed
116
    log('Update descriptor list', descriptorList);
117

118
119
120
    delete descriptorList.owner;
    delete descriptorList.descriptors;

121
    return authenticatedRequest(token, {
122
123
124
125
126
127
128
129
      url: `${UPDATE_DESCRIPTORLIST_URL}`,
      method: 'POST',
      data: {
        ...descriptorList,
      },
    }).then(({ data }) => new DescriptorList(data));
  }

130
  public static deleteDescriptorList(token: string, descriptorList: DescriptorList): Promise<DescriptorList> {
Valeriy Panov's avatar
Valeriy Panov committed
131
    log('Delete descriptor list', descriptorList);
132

133
    return authenticatedRequest(token, {
134
135
136
137
138
      url: `${REMOVE_DESCRIPTORLIST_URL}/${descriptorList.uuid},${descriptorList.version}`,
      method: 'DELETE',
    }).then(({ data }) => new DescriptorList(data));
  }

139
  public static publishDescriptorList(token: string, descriptorList: DescriptorList, published: boolean = true): Promise<DescriptorList> {
Valeriy Panov's avatar
Valeriy Panov committed
140
    log('Publish descriptor list', descriptorList);
141

142
    return authenticatedRequest(token, {
143
144
145
146
147
      url: `${PUBLISH_DESCRIPTORLIST_URL}?uuid=${descriptorList.uuid}&version=${descriptorList.version}&published=${published}`,
      method: 'POST',
    }).then(({ data }) => new DescriptorList(data));
  }

148
  public static addDescriptor(token: string, descriptorList: DescriptorList, descriptorUuids: string[]): Promise<DescriptorList> {
Valeriy Panov's avatar
Valeriy Panov committed
149
    log('Adding descriptor to descriptor list', descriptorUuids);
150

151
    return authenticatedRequest(token, {
152
153
154
155
156
157
      url: `${ADD_DESCRIPTORLIST_DESCRIPTOR_URL}/${descriptorList.uuid},${descriptorList.version}`,
      method: 'POST',
      data: [ ...descriptorUuids ],
    }).then(({ data }) => new DescriptorList(data));
  }

158
159
160
161
162
163
164
165
166
167
  public static setDescriptors(token: string, descriptorList: DescriptorList, descriptorUuids: string[]): Promise<DescriptorList> {
    log('Set descriptors to descriptor list', descriptorUuids);

    return authenticatedRequest(token, {
      url: `${SET_DESCRIPTORLIST_DESCRIPTORS_URL}/${descriptorList.uuid},${descriptorList.version}`,
      method: 'POST',
      data: [ ...descriptorUuids ],
    }).then(({ data }) => new DescriptorList(data));
  }

168
  public static removeDescriptor(token: string, descriptorList: DescriptorList, descriptorUuids: string[]): Promise<DescriptorList> {
Valeriy Panov's avatar
Valeriy Panov committed
169
    log('Removing descriptor to descriptor list', descriptorUuids);
170

171
    return authenticatedRequest(token, {
172
173
174
175
176
177
      url: `${REMOVE_DESCRIPTORLIST_DESCRIPTOR_URL}/${descriptorList.uuid},${descriptorList.version}`,
      method: 'POST',
      data: [ ...descriptorUuids ],
    }).then(({ data }) => new DescriptorList(data));
  }

178
}