-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
find.v
192 lines (179 loc) · 5.77 KB
/
find.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
module shopwareac
import json
import os
pub fn (mut l Login) find_product_by_productnumber(productnumber string) !ShopResponseData {
response_raw := l.get('product/?filter[productNumber]=${encode(productnumber)}')
response := json.decode(ShopResponseFind, response_raw) or {
eprintln(err)
ShopResponseFind{}
}
if response.data.len < 1 {
return error('Found none')
} else if response.data.len > 1 {
return error('Found multiple')
}
return response.data[0]
}
pub fn (mut l Login) find_product_by_customfield(field string, value string) !ShopResponseData {
response_raw := l.get('product/?filter[customFields.${field}]=${encode(value)}')
response := json.decode(ShopResponseFind, response_raw) or {
eprintln(err)
ShopResponseFind{}
}
if response.data.len < 1 {
return error('Found none')
} else if response.data.len > 1 {
return error('Found multiple')
}
return response.data[0]
}
pub fn (mut l Login) find_category_by_customfield(field string, value string) !ShopResponseData {
response_raw := l.get('category/?filter[customFields.${field}]=${encode(value)}')
response := json.decode(ShopResponseFind, response_raw) or {
eprintln(err)
ShopResponseFind{}
}
if response.data.len < 1 {
return error('Found none')
} else if response.data.len > 1 {
return error('Found multiple')
}
return response.data[0]
}
pub fn (mut l Login) find_subcategory_by_name(name string, parent string) !ShopResponseData {
response_raw := l.get('category/?filter[name]=${encode(name)}&filter[parentId]=${parent}')
response := json.decode(ShopResponseFind, response_raw) or {
eprintln(err)
ShopResponseFind{}
}
if response.data.len < 1 {
return error('Found none')
} else if response.data.len > 1 {
return error('Found multiple')
}
return response.data[0]
}
pub fn (mut l Login) find_property_by_name(name string, group string) !ShopResponseData {
response_raw := l.get('property-group-option/?filter[name]=${encode(name)}&filter[groupId]=${group}')
response := json.decode(ShopResponseFind, response_raw) or {
eprintln(err)
ShopResponseFind{}
}
if response.data.len < 1 {
return error('Found none')
} else if response.data.len > 1 {
return error('Found multiple')
}
return response.data[0]
}
pub fn (mut l Login) find_media_by_name(name string) !ShopResponseData {
mut ext := os.file_ext(name)
name_without_ext := name.substr(0, name.len - ext.len)
if ext.len > 0 {
ext = ext[1..]
}
response_raw := l.get('media/?filter[fileName]=${encode(strip(name_without_ext))}&filter[fileExtension]=${ext}')
response := json.decode(ShopResponseFind, response_raw) or {
eprintln(err)
ShopResponseFind{}
}
if response.data.len < 1 {
return error('Found none')
} else if response.data.len > 1 {
return error('Found multiple')
}
return response.data[0]
}
pub fn (mut l Login) get_default_tax() string {
tax_response := l.get('tax?filter[position]=1')
tax_data := json.decode(ShopResponseFind, tax_response) or {
eprintln(err)
ShopResponseFind{}
}
if tax_data.data.len < 1 {
eprintln('No tax with position 1 found')
return ''
}
// todo if pos 1 not found - get pos 0 or any other
// if there are none - create default tax
return tax_data.data[0].id
}
pub fn (mut l Login) get_default_sales_channel() string {
sales_channel_type_response := l.get('sales-channel-type?filter[name]=Storefront&limit=1')
sales_channel_type_data := json.decode(ShopResponseFind, sales_channel_type_response) or {
eprintln(err)
ShopResponseFind{}
}
if sales_channel_type_data.data.len < 1 {
eprintln('No sales channel type with name "Storefront" found')
return ''
}
sales_channel_response := l.get('sales-channel-type/${sales_channel_type_data.data[0].id}/salesChannels?limit=1&active=true&sort=createdAt')
sales_channel_data := json.decode(ShopResponseFind, sales_channel_response) or {
eprintln(err)
ShopResponseFind{}
}
if sales_channel_data.data.len < 1 {
eprintln('No active sales channel with type "Storefront" found')
return ''
}
return sales_channel_data.data[0].id
}
pub fn (mut l Login) get_default_payment_method() string {
sales_channel_type_response := l.get('sales-channel-type?filter[name]=Storefront')
sales_channel_type_data := json.decode(ShopResponseFind, sales_channel_type_response) or {
eprintln(err)
ShopResponseFind{}
}
if sales_channel_type_data.data.len < 1 {
eprintln('No sales channel type with name "Storefront" found')
return ''
}
sales_channel_response := l.get('sales-channel-type/${sales_channel_type_data.data[0].id}/salesChannels')
sales_channel_data := json.decode(ShopResponseFind, sales_channel_response) or {
eprintln(err)
ShopResponseFind{}
}
if sales_channel_data.data.len < 1 {
eprintln('No sales channel with type "Storefront" found')
return ''
}
return sales_channel_data.data[0].attributes.payment_method_id
}
pub fn (mut l Login) get_default_media_folder() string {
response := l.get('media-folder?filter[name]=Imported Media')
data := json.decode(ShopResponseFind, response) or {
eprintln(err)
ShopResponseFind{}
}
if data.data.len < 1 {
eprintln('No media folder with name "Imported Media" found')
return ''
}
return data.data[0].id
}
pub fn (mut l Login) get_default_cms_page() string {
response := l.get('cms-page/?filter[type]=product_list&limit=1')
data := json.decode(ShopResponseFind, response) or {
eprintln(err)
ShopResponseFind{}
}
if data.data.len < 1 {
eprintln('No cms page with type "product_list" found')
return ''
}
return data.data[0].id
}
pub fn (mut l Login) get_default_category() string {
// should we get navigationCategoryId instead from default storefront instead?
response := l.get('category?limit=1')
data := json.decode(ShopResponseFind, response) or {
eprintln(err)
ShopResponseFind{}
}
if data.data.len < 1 {
eprintln('No category found')
return ''
}
return data.data[0].id
}