DescriptorListService.ts 5.52 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

5
6
7
8
9
import {
    MY_LIST_DESCRIPTORSLISTS_URL, GET_DESCRIPTORLIST_URL, REMOVE_DESCRIPTORLIST_URL,
    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';
10

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

15
16
17
18

export class DescriptorListService {

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

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

23
    return authenticatedRequest(token, {
24
25
26
27
28
      url: `${MY_LIST_DESCRIPTORSLISTS_URL}?p=${page}&l=${results}&d=${order}${sortParam}`,
      method: 'POST',
      data: {
        ...filter,
      },
29
30
31
32
33
34
    })
    .then(({ data }) => new Page<DescriptorList>(data, (dl) => new DescriptorList(dl)))
    .then((paged) => {
      dereferenceReferences(paged.content, 'owner', (o) => new Partner(o));
      return paged;
    });
35
36
37
  }

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

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

42
    return authenticatedRequest(token, {
43
44
45
46
47
      url: `${LIST_DESCRIPTORLISTS_URL}?p=${page}&l=${results}&d=${order}${sortParam}`,
      method: 'POST',
      data: {
        ...filter,
      },
48
49
50
51
52
53
    })
    .then(({ data }) => new Page<DescriptorList>(data, (dl) => new DescriptorList(dl)))
    .then((paged) => {
      dereferenceReferences(paged.content, 'owner', (o) => new Partner(o));
      return paged;
    });
54
55
56
  }

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

60
    return authenticatedRequest(token, {
61
62
      url: `${GET_DESCRIPTORLIST_URL}/${uuid}`,
      method: 'GET',
63
64
65
66
67
68
    })
    .then(({ data }) => new DescriptorList(data))
    .then((obj) => {
      dereferenceReferences([ obj ], 'owner', (o) => new Partner(o));
      return obj;
    });
69
70
  }

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

74
    return authenticatedRequest(token, {
75
76
77
78
79
80
81
82
      url: `${CREATE_DESCRIPTORLIST_URL}`,
      method: 'POST',
      data: {
        ...descriptorList,
      },
    }).then(({ data }) => new DescriptorList(data));
  }

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

86
87
88
    delete descriptorList.owner;
    delete descriptorList.descriptors;

89
    return authenticatedRequest(token, {
90
91
92
93
94
95
96
97
      url: `${UPDATE_DESCRIPTORLIST_URL}`,
      method: 'POST',
      data: {
        ...descriptorList,
      },
    }).then(({ data }) => new DescriptorList(data));
  }

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

101
    return authenticatedRequest(token, {
102
103
104
105
106
      url: `${REMOVE_DESCRIPTORLIST_URL}/${descriptorList.uuid},${descriptorList.version}`,
      method: 'DELETE',
    }).then(({ data }) => new DescriptorList(data));
  }

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

110
    return authenticatedRequest(token, {
111
112
113
114
115
      url: `${PUBLISH_DESCRIPTORLIST_URL}?uuid=${descriptorList.uuid}&version=${descriptorList.version}&published=${published}`,
      method: 'POST',
    }).then(({ data }) => new DescriptorList(data));
  }

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

119
    return authenticatedRequest(token, {
120
121
122
123
124
125
      url: `${ADD_DESCRIPTORLIST_DESCRIPTOR_URL}/${descriptorList.uuid},${descriptorList.version}`,
      method: 'POST',
      data: [ ...descriptorUuids ],
    }).then(({ data }) => new DescriptorList(data));
  }

126
127
128
129
130
131
132
133
134
135
  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));
  }

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

139
    return authenticatedRequest(token, {
140
141
142
143
144
145
      url: `${REMOVE_DESCRIPTORLIST_DESCRIPTOR_URL}/${descriptorList.uuid},${descriptorList.version}`,
      method: 'POST',
      data: [ ...descriptorUuids ],
    }).then(({ data }) => new DescriptorList(data));
  }

146
}