Passed
Push — master ( e4ea65...5bc50c )
by Christian
15:36 queued 10s
created

ExtensionStoreDataService.getMyExtensions   A

Complexity

Conditions 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
c 0
b 0
f 0
dl 0
loc 8
rs 10
1
import ApiService from 'src/core/service/api.service';
2
3
const { Criteria } = Shopware.Data;
0 ignored issues
show
Bug introduced by
The variable Shopware seems to be never declared. If this is a global, consider adding a /** global: Shopware */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
4
5
export default class ExtensionStoreDataService extends ApiService {
6
    constructor(httpClient, loginService, apiEndpoint = 'plugin') {
7
        super(httpClient, loginService, apiEndpoint);
8
        this.name = 'extensionStoreDataService';
9
    }
10
11
    async refreshExtensions(context) {
12
        await this.httpClient.post('_action/extension/refresh', {}, {
13
            headers: this.basicHeaders(context),
14
            version: 3
15
        });
16
    }
17
18
    async listingFilters(context) {
19
        const res = await this.httpClient.get('_action/extension/store-filters', {
20
            headers: this.basicHeaders(context),
21
            version: 3
22
        });
23
24
        return res.data;
25
    }
26
27
    async getExtensionList(search, context) {
28
        const criteria = this._getCriteriaFromSearch(search);
29
30
        const { data } = await this.httpClient.post('_action/extension/list', criteria.parse(), {
31
            headers: this.basicHeaders(context),
32
            version: 3
33
        });
34
35
        const extensions = [];
36
        extensions.total = data.meta.total;
37
        extensions.push(...data.data);
38
39
        return extensions;
40
    }
41
42
    async getDetail(id, context) {
43
        const { data } = await this.httpClient.get(`_action/extension/detail/${id}`, {
44
            headers: this.basicHeaders(context),
45
            version: 3
46
        });
47
48
        return data;
49
    }
50
51
    async getMyExtensions(context) {
52
        const { data } = await this.httpClient.get('_action/extension/installed', {
53
            headers: this.basicHeaders(context),
54
            version: 3
55
        });
56
57
        return data;
58
    }
59
60
    async getReviews(page, limit, id) {
61
        const criteria = new Criteria(
62
            page,
63
            limit
64
        );
65
66
        const { data } = await this.httpClient.get(`_action/extension/${id}/reviews`, {
67
            headers: this.basicHeaders(),
68
            params: criteria.parse(),
69
            version: 3
70
        });
71
72
        return data;
73
    }
74
75
    basicHeaders(context = null) {
76
        const headers = {
77
            'Content-Type': 'application/json',
78
            Accept: 'application/json',
79
            Authorization: `Bearer ${this.loginService.getToken()}`
80
        };
81
82
        if (context && context.languageId) {
83
            headers['sw-language-id'] = context.languageId;
84
        }
85
86
        return headers;
87
    }
88
89
    _getCriteriaFromSearch({
90
        page = 1,
91
        limit = 25,
92
        rating = null,
93
        category = null,
94
        term = null,
95
        sorting = null
96
    } = {}) {
97
        const criteria = new Criteria(
98
            page,
99
            limit
100
        );
101
102
        if (term) {
103
            criteria.setTerm(term);
104
        }
105
106
        const filters = [];
107
108
        if (rating !== null) {
109
            filters.push(Criteria.equals('rating', rating));
110
        }
111
112
        if (category !== null) {
113
            filters.push(Criteria.equals('category', category));
114
        }
115
116
        if (filters.length > 0) {
117
            criteria.addFilter(Criteria.multi('AND', filters));
118
        }
119
120
        if (sorting) {
121
            criteria.resetSorting();
122
            criteria.addSorting(sorting);
123
        }
124
125
        return criteria;
126
    }
127
}
128