@@ -10,25 +10,21 @@ import { Language } from "../enums/Language";
10
10
import {
11
11
ICumulusReceiptArticle ,
12
12
ICumulusReceiptResponse ,
13
- ICumulusReceiptsResponse ,
14
- ICumulusReceiptsResponseItem ,
15
13
} from "../interfaces/receipts" ;
16
14
import { Currency } from "../enums/Currency" ;
17
15
18
- const urlExport =
19
- migrosApiPaths [ "cumulus" ] + "/service/avantaReceiptExport/html" ;
20
- const urlList =
21
- migrosApiPaths [ "cumulus" ] +
22
- "/de/konto/kassenbons/variants/variant-1/content/04/ajaxContent/0.html" ;
16
+ const urlExport = migrosApiPaths [ "account" ] . purchases . receipts ;
17
+ const urlList = migrosApiPaths [ "account" ] . purchases . receipts ;
23
18
24
19
export interface ICumulusReceiptOptions extends Record < string , any > {
25
- receiptId : string ;
20
+ transactionId : string ;
26
21
fallbackLanguage ?: Language ;
27
22
}
28
23
29
24
const defaultCumulusReceiptOptions : ICumulusReceiptOptions = {
30
- receiptId : "" ,
25
+ transactionId : "" ,
31
26
fallbackLanguage : Language . DE ,
27
+ type : "HTML" ,
32
28
} ;
33
29
34
30
async function getCumulusReceiptRequest (
@@ -38,7 +34,13 @@ async function getCumulusReceiptRequest(
38
34
htmlOnly : boolean ,
39
35
) : Promise < ICumulusReceiptResponse | string > {
40
36
const headers = {
41
- accept : "text/html, */*; q=0.01" ,
37
+ accept : "application/json, text/plain, */*" ,
38
+ "accept-language" : "en-US,en;q=0.5" ,
39
+ "sec-fetch-dest" : "empty" ,
40
+ "sec-fetch-mode" : "cors" ,
41
+ "sec-fetch-site" : "same-origin" ,
42
+ "X-CSRF-TOKEN" : cookies . CSRF || "" ,
43
+ Referer : "https://account.migros.ch/purchases/receipts" ,
42
44
} ;
43
45
44
46
const response = await getRequest ( url , options , headers , cookies ) ;
@@ -94,7 +96,7 @@ async function getCumulusReceiptRequest(
94
96
return {
95
97
store : {
96
98
cooperative : store [ 0 ] . trim ( ) ,
97
- outlet : store [ 1 ] . trim ( ) ,
99
+ outlet : store [ 1 ] ? .trim ( ) ,
98
100
} ,
99
101
articles : receiptArticles ,
100
102
discount : {
@@ -108,50 +110,50 @@ async function getCumulusReceiptRequest(
108
110
currency : < Currency > totalCost [ 1 ] ,
109
111
} ,
110
112
payment : {
111
- value : parseFloat ( payment [ 0 ] . trim ( ) . split ( / \s \s + / ) [ 1 ] . trim ( ) ) ,
113
+ value : parseFloat ( payment [ 0 ] . trim ( ) . split ( / \s \s + / ) [ 1 ] ? .trim ( ) ) ,
112
114
type : payment [ 0 ] . trim ( ) . split ( / \s \s + / ) [ 0 ] . trim ( ) ,
113
115
return : payment [ 1 ]
114
- ? parseFloat ( payment [ 1 ] . trim ( ) . split ( / \s \s + / ) [ 1 ] . trim ( ) )
116
+ ? parseFloat ( payment [ 1 ] . trim ( ) . split ( / \s \s + / ) [ 1 ] ? .trim ( ) )
115
117
: 0 ,
116
118
} ,
117
119
eft : eftPayment
118
120
? {
119
121
booking : {
120
- type : eftPayment [ 1 ] . trim ( ) . split ( / \s \s + / ) [ 1 ] . trim ( ) ,
121
- card : eftPayment [ 2 ] . trim ( ) ,
122
+ type : eftPayment [ 1 ] ? .trim ( ) . split ( / \s \s + / ) [ 1 ] ? .trim ( ) ,
123
+ card : eftPayment [ 2 ] ? .trim ( ) ,
122
124
} ,
123
125
date : new Date (
124
- + eftPayment [ 3 ] . trim ( ) . split ( / \s \s + / ) [ 0 ] . trim ( ) . split ( "." ) [ 2 ] ,
125
- + eftPayment [ 3 ] . trim ( ) . split ( / \s \s + / ) [ 0 ] . trim ( ) . split ( "." ) [ 1 ] - 1 ,
126
- + eftPayment [ 3 ] . trim ( ) . split ( / \s \s + / ) [ 0 ] . trim ( ) . split ( "." ) [ 0 ] ,
127
- + eftPayment [ 3 ] . trim ( ) . split ( / \s \s + / ) [ 1 ] . trim ( ) . split ( ":" ) [ 0 ] ,
128
- + eftPayment [ 3 ] . trim ( ) . split ( / \s \s + / ) [ 1 ] . trim ( ) . split ( ":" ) [ 1 ] ,
126
+ + eftPayment [ 3 ] ? .trim ( ) . split ( / \s \s + / ) [ 0 ] . trim ( ) . split ( "." ) [ 2 ] ,
127
+ + eftPayment [ 3 ] ? .trim ( ) . split ( / \s \s + / ) [ 0 ] . trim ( ) . split ( "." ) [ 1 ] - 1 ,
128
+ + eftPayment [ 3 ] ? .trim ( ) . split ( / \s \s + / ) [ 0 ] . trim ( ) . split ( "." ) [ 0 ] ,
129
+ + eftPayment [ 3 ] ? .trim ( ) . split ( / \s \s + / ) [ 1 ] . trim ( ) . split ( ":" ) [ 0 ] ,
130
+ + eftPayment [ 3 ] ? .trim ( ) . split ( / \s \s + / ) [ 1 ] . trim ( ) . split ( ":" ) [ 1 ] ,
129
131
0 ,
130
132
) ,
131
- total : parseFloat ( eftPayment [ 5 ] . trim ( ) . split ( / \s \s + / ) [ 1 ] . trim ( ) ) ,
133
+ total : parseFloat ( eftPayment [ 5 ] ? .trim ( ) . split ( / \s \s + / ) [ 1 ] . trim ( ) ) ,
132
134
}
133
135
: null ,
134
136
cumulus : {
135
- nr : cumulus [ 2 ] . trim ( ) . split ( / \s \s + / ) [ 1 ] ,
137
+ nr : cumulus [ 2 ] ? .trim ( ) . split ( / \s \s + / ) [ 1 ] ,
136
138
points : {
137
- current : parseFloat ( cumulus [ 3 ] . trim ( ) . split ( / \s \s + / ) [ 1 ] ) ,
138
- received : parseFloat ( cumulus [ 4 ] . trim ( ) . split ( / \s \s + / ) [ 1 ] ) ,
139
+ current : parseFloat ( cumulus [ 3 ] ? .trim ( ) . split ( / \s \s + / ) [ 1 ] ) ,
140
+ received : parseFloat ( cumulus [ 4 ] ? .trim ( ) . split ( / \s \s + / ) [ 1 ] ) ,
139
141
} ,
140
142
} ,
141
143
details : {
142
- outlet : footer [ 2 ] . trim ( ) . split ( / \s \s + / ) [ 0 ] ,
143
- bed : footer [ 2 ] . trim ( ) . split ( / \s \s + / ) [ 1 ] ,
144
- box : footer [ 2 ] . trim ( ) . split ( / \s \s + / ) [ 2 ] ,
145
- bon : footer [ 2 ] . trim ( ) . split ( / \s \s + / ) [ 3 ] ,
144
+ outlet : footer [ 2 ] ? .trim ( ) . split ( / \s \s + / ) [ 0 ] ,
145
+ bed : footer [ 2 ] ? .trim ( ) . split ( / \s \s + / ) [ 1 ] ,
146
+ box : footer [ 2 ] ? .trim ( ) . split ( / \s \s + / ) [ 2 ] ,
147
+ bon : footer [ 2 ] ? .trim ( ) . split ( / \s \s + / ) [ 3 ] ,
146
148
date : new Date (
147
- + footer [ 2 ] . trim ( ) . split ( / \s \s + / ) [ 4 ] . split ( "." ) [ 2 ] ,
148
- + footer [ 2 ] . trim ( ) . split ( / \s \s + / ) [ 4 ] . split ( "." ) [ 1 ] - 1 ,
149
- + footer [ 2 ] . trim ( ) . split ( / \s \s + / ) [ 4 ] . split ( "." ) [ 0 ] ,
150
- + footer [ 2 ] . trim ( ) . split ( / \s \s + / ) [ 5 ] . split ( ":" ) [ 0 ] ,
151
- + footer [ 2 ] . trim ( ) . split ( / \s \s + / ) [ 5 ] . split ( ":" ) [ 1 ] ,
152
- + footer [ 2 ] . trim ( ) . split ( / \s \s + / ) [ 5 ] . split ( ":" ) [ 2 ] ,
149
+ + footer [ 2 ] ? .trim ( ) . split ( / \s \s + / ) [ 4 ] . split ( "." ) [ 2 ] ,
150
+ + footer [ 2 ] ? .trim ( ) . split ( / \s \s + / ) [ 4 ] . split ( "." ) [ 1 ] - 1 ,
151
+ + footer [ 2 ] ? .trim ( ) . split ( / \s \s + / ) [ 4 ] . split ( "." ) [ 0 ] ,
152
+ + footer [ 2 ] ? .trim ( ) . split ( / \s \s + / ) [ 5 ] . split ( ":" ) [ 0 ] ,
153
+ + footer [ 2 ] ? .trim ( ) . split ( / \s \s + / ) [ 5 ] . split ( ":" ) [ 1 ] ,
154
+ + footer [ 2 ] ? .trim ( ) . split ( / \s \s + / ) [ 5 ] . split ( ":" ) [ 2 ] ,
153
155
) ,
154
- letter : footer [ 2 ] . trim ( ) . split ( / \s \s + / ) [ 6 ] . trim ( ) ,
156
+ letter : footer [ 2 ] ? .trim ( ) . split ( / \s \s + / ) [ 6 ] . trim ( ) ,
155
157
} ,
156
158
} ;
157
159
}
@@ -166,7 +168,7 @@ export async function getCumulusReceipt(
166
168
...cumulusReceiptOptions ,
167
169
} ;
168
170
return getCumulusReceiptRequest (
169
- urlExport ,
171
+ urlExport + "/" + cumulusReceiptOptions . receiptId ,
170
172
cumulusReceiptOptions ,
171
173
cookies ,
172
174
htmlOnly ,
@@ -189,100 +191,43 @@ function convertDateToCumulusDateString(date: Date): string {
189
191
}
190
192
191
193
export interface ICumulusReceiptsOptions extends Record < string , any > {
192
- from : Date ;
193
- to : Date ;
194
- p ?: number ;
194
+ dateFrom : Date ;
195
+ dateTo ?: Date ;
195
196
}
196
197
197
198
const defaultCumulusReceiptsOptions : ICumulusReceiptsOptions = {
198
- from : new Date ( ) ,
199
- to : new Date ( ) ,
200
- p : 1 ,
201
- sort : "dateDsc" ,
199
+ dateFrom : new Date ( ) ,
200
+ dateTo : new Date ( ) ,
202
201
} ;
203
202
204
203
async function getCumulusReceiptsRequest (
205
204
url : string ,
206
- options : ICumulusReceiptsOptions | Record < string , any > ,
205
+ options : ICumulusReceiptsOptions ,
207
206
cookies : ICumulusCookies ,
208
- ) : Promise < ICumulusReceiptsResponse > {
207
+ ) : Promise < any > {
209
208
const headers = {
210
- accept : "text/html, */*; q=0.01" ,
211
- "accept-language" : "en-US,en;q=0.9" ,
212
- "sec-ch-ua" :
213
- '"Chromium";v="106", "Not;A=Brand";v="99", "Google Chrome";v="106.0.5249.119"' ,
214
- "sec-ch-ua-mobile" : "?0" ,
215
- "sec-ch-ua-platform" : '"Windows"' ,
209
+ accept : "application/json, text/plain, */*" ,
210
+ "accept-language" : "en-US,en;q=0.5" ,
216
211
"sec-fetch-dest" : "empty" ,
217
212
"sec-fetch-mode" : "cors" ,
218
213
"sec-fetch-site" : "same-origin" ,
219
- "x-requested-with" : "XMLHttpRequest" ,
214
+ "X-CSRF-TOKEN" : cookies . CSRF || "" ,
215
+ Referer : "https://account.migros.ch/purchases/receipts" ,
220
216
} ;
221
217
222
218
const newOptions = {
223
- period : `${ convertDateToCumulusDateString (
224
- options . from ,
225
- ) } _${ convertDateToCumulusDateString ( options . to ) } `,
226
- p : options . p ,
219
+ dateFrom : convertDateToCumulusDateString ( options . dateFrom ) ,
220
+ dateTo : convertDateToCumulusDateString ( options . dateTo || new Date ( ) ) ,
227
221
} ;
228
222
229
- const tableData : ICumulusReceiptsResponseItem [ ] = [ ] ;
230
-
231
- // eslint-disable-next-line no-loops/no-loops,no-constant-condition
232
- while ( true ) {
233
- const response = await getRequest ( url , newOptions , headers , cookies ) ;
234
- if ( ! response . text ) {
235
- break ;
236
- }
237
-
238
- const $ = cheerio . load ( await response . text ( ) ) ;
239
- const links = $ (
240
- "[data-modal-src^='/service/avantaReceiptExport/html?receiptId=']" ,
241
- ) ;
242
-
243
- links . each ( ( _ , el ) => {
244
- const rowItems = $ ( el ) . parent ( ) . parent ( ) . children ( ) ;
245
- const [ day , month , year ] = $ ( rowItems [ 1 ] ) . text ( ) . trim ( ) . split ( "." ) ;
246
- tableData . push ( {
247
- date : new Date ( + year , + month - 1 , + day ) ,
248
- outlet : $ ( rowItems [ 2 ] ) . text ( ) . trim ( ) ,
249
- total : {
250
- value : parseFloat ( ( $ ( rowItems [ 3 ] ) [ 0 ] . children [ 1 ] as any ) [ "data" ] ) ,
251
- currency : ( $ ( rowItems [ 3 ] ) [ 0 ] . children [ 0 ] as any ) [
252
- "children"
253
- ] [ 0 ] . data . trim ( ) ,
254
- } ,
255
- points : {
256
- value : parseFloat ( ( $ ( rowItems [ 4 ] ) [ 0 ] . children [ 0 ] as any ) [ "data" ] ) ,
257
- } ,
258
- links : {
259
- html :
260
- migrosApiPaths [ "cumulus" ] +
261
- ( $ ( rowItems [ 1 ] ) [ 0 ] . children [ 1 ] as any ) . attribs [ "data-modal-src" ] ,
262
- pdf :
263
- migrosApiPaths [ "cumulus" ] +
264
- ( $ ( rowItems [ 1 ] ) [ 0 ] . children [ 1 ] as any ) . attribs . href ,
265
- } ,
266
- id : ( $ ( rowItems [ 1 ] ) [ 0 ] . children [ 1 ] as any ) . attribs . href . match (
267
- new RegExp ( / (?< = = ) .* ?(? = & | $ ) / ) ,
268
- ) [ 0 ] ,
269
- } ) ;
270
- } ) ;
271
-
272
- if ( links . length < 10 ) {
273
- break ;
274
- }
275
-
276
- newOptions . p = newOptions . p + 1 ;
277
- }
278
-
279
- return tableData ;
223
+ const response = await getRequest ( url , newOptions , headers , cookies ) ;
224
+ return await response . json ( ) ;
280
225
}
281
226
282
227
export async function getCumulusReceipts (
283
228
cumulusReceiptsOptions : ICumulusReceiptsOptions ,
284
229
cookies : ICumulusCookies ,
285
- ) : Promise < ICumulusReceiptsResponse > {
230
+ ) : Promise < any > {
286
231
cumulusReceiptsOptions = {
287
232
...defaultCumulusReceiptsOptions ,
288
233
...cumulusReceiptsOptions ,
0 commit comments