@bprotsyk/aso-core 2.1.37 → 2.1.39

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (105) hide show
  1. package/README.md +1 -1
  2. package/lib/app/app-integration.d.ts +53 -53
  3. package/lib/app/app-integration.js +63 -63
  4. package/lib/app/app-list-item.d.ts +5 -5
  5. package/lib/app/app-list-item.js +2 -2
  6. package/lib/app/app-type.d.ts +4 -4
  7. package/lib/app/app-type.js +8 -8
  8. package/lib/app/app.d.ts +208 -290
  9. package/lib/app/app.js +156 -156
  10. package/lib/general/cloudflare-domain.d.ts +42 -42
  11. package/lib/general/cloudflare-domain.js +12 -12
  12. package/lib/general/domain.d.ts +110 -169
  13. package/lib/general/domain.js +62 -62
  14. package/lib/general/namecheap-domain.d.ts +85 -132
  15. package/lib/general/namecheap-domain.js +14 -14
  16. package/lib/general/push.d.ts +6 -6
  17. package/lib/general/push.js +2 -2
  18. package/lib/general/queue.d.ts +2 -2
  19. package/lib/general/queue.js +1 -1
  20. package/lib/general/shape.d.ts +18 -18
  21. package/lib/general/shape.js +36 -36
  22. package/lib/index.d.ts +26 -26
  23. package/lib/index.js +69 -69
  24. package/lib/keitaro/keitaro-campaign.d.ts +31 -31
  25. package/lib/keitaro/keitaro-campaign.js +5 -5
  26. package/lib/keitaro/keitaro-domain.d.ts +6 -6
  27. package/lib/keitaro/keitaro-domain.js +2 -2
  28. package/lib/keitaro/keitaro-offer.d.ts +7 -7
  29. package/lib/keitaro/keitaro-offer.js +2 -2
  30. package/lib/keitaro/keitaro-stream.d.ts +20 -20
  31. package/lib/keitaro/keitaro-stream.js +2 -2
  32. package/lib/network/keitaro/http.d.ts +2 -2
  33. package/lib/network/keitaro/http.js +12 -12
  34. package/lib/network/keitaro/keitaro-service.d.ts +50 -48
  35. package/lib/network/keitaro/keitaro-service.js +283 -293
  36. package/lib/offers/list.d.ts +42 -435
  37. package/lib/offers/list.js +12 -12
  38. package/lib/offers/offer.d.ts +91 -152
  39. package/lib/offers/offer.js +42 -42
  40. package/lib/offers/offerwall/offerwall-home-dialog-data.d.ts +6 -6
  41. package/lib/offers/offerwall/offerwall-home-dialog-data.js +2 -2
  42. package/lib/offers/offerwall/offerwall-offer.d.ts +12 -12
  43. package/lib/offers/offerwall/offerwall-offer.js +2 -2
  44. package/lib/offers/offerwall/offerwall-response.d.ts +6 -6
  45. package/lib/offers/offerwall/offerwall-response.js +2 -2
  46. package/lib/offers/offerwall/offerwall-section.d.ts +6 -6
  47. package/lib/offers/offerwall/offerwall-section.js +2 -2
  48. package/lib/offers/section.d.ts +47 -60
  49. package/lib/offers/section.js +20 -20
  50. package/lib/panel/app/upsert-flash-app-request.d.ts +31 -31
  51. package/lib/panel/app/upsert-flash-app-request.js +2 -2
  52. package/lib/panel/auth.d.ts +9 -9
  53. package/lib/panel/auth.js +2 -2
  54. package/lib/panel/flash/upsert-flash-app-request.d.ts +31 -31
  55. package/lib/panel/flash/upsert-flash-app-request.js +2 -2
  56. package/lib/panel/user.d.ts +46 -59
  57. package/lib/panel/user.js +28 -28
  58. package/lib/templates/nginx-template.conf +35 -35
  59. package/lib/templates/nginx-template.d.ts +1 -1
  60. package/lib/templates/nginx-template.js +39 -39
  61. package/lib/templates/nginx-template.ts +35 -35
  62. package/lib/utils/general.d.ts +11 -11
  63. package/lib/utils/general.js +40 -40
  64. package/lib/utils/huawei/converter.d.ts +2 -2
  65. package/lib/utils/huawei/converter.js +53 -53
  66. package/lib/utils/keitaro-utils.d.ts +13 -13
  67. package/lib/utils/keitaro-utils.js +614 -569
  68. package/lib/utils/server-util.js +303 -303
  69. package/package.json +51 -51
  70. package/src/app/app-integration.ts +66 -66
  71. package/src/app/app-list-item.ts +4 -4
  72. package/src/app/app-type.ts +3 -3
  73. package/src/app/app.ts +317 -317
  74. package/src/general/cloudflare-domain.ts +44 -44
  75. package/src/general/domain.ts +106 -106
  76. package/src/general/namecheap-domain.ts +63 -63
  77. package/src/general/push.ts +5 -5
  78. package/src/general/queue.ts +4 -4
  79. package/src/general/shape.tsx +55 -55
  80. package/src/index.ts +32 -32
  81. package/src/keitaro/keitaro-campaign.ts +35 -35
  82. package/src/keitaro/keitaro-domain.ts +5 -5
  83. package/src/keitaro/keitaro-offer.ts +6 -6
  84. package/src/keitaro/keitaro-stream.ts +19 -19
  85. package/src/network/keitaro/http.ts +8 -8
  86. package/src/network/keitaro/keitaro-service.ts +348 -364
  87. package/src/offers/list.ts +22 -19
  88. package/src/offers/offer.ts +80 -80
  89. package/src/offers/offerwall/offerwall-home-dialog-data.ts +6 -6
  90. package/src/offers/offerwall/offerwall-offer.ts +12 -12
  91. package/src/offers/offerwall/offerwall-response.ts +7 -7
  92. package/src/offers/offerwall/offerwall-section.ts +7 -7
  93. package/src/offers/section.ts +29 -29
  94. package/src/panel/app/upsert-flash-app-request.ts +39 -39
  95. package/src/panel/auth.ts +9 -9
  96. package/src/panel/user.ts +38 -38
  97. package/src/templates/nginx-template.ts +35 -35
  98. package/src/utils/data.csv +65 -65
  99. package/src/utils/general.ts +36 -36
  100. package/src/utils/huawei/converter.ts +55 -55
  101. package/src/utils/keitaro-utils.ts +717 -664
  102. package/src/utils/map-apps.json +4747 -4747
  103. package/src/utils/server-util.ts +368 -368
  104. package/src/utils/update-postbacks.js +27 -27
  105. package/tsconfig.json +20 -20
@@ -1,664 +1,717 @@
1
- import { IKeitaroStream } from "../keitaro/keitaro-stream"
2
- import { IApp, PlugType, } from "../app/app"
3
- import { AppType } from "../app/app-type"
4
- import { IKeitaroCampaign, IKeitaroCampaignParameters } from "../keitaro/keitaro-campaign"
5
- import { IKeitaroDomain } from "../keitaro/keitaro-domain"
6
- import { KeitaroService } from "../network/keitaro/keitaro-service"
7
- const sleep = require('sleep-promise');
8
- import * as util from 'util';
9
- import axios from "axios"
10
- import { getTimestampsForTodayAndYesterday } from "../utils/general"
11
- import { KeitaroUtils } from "index"
12
- import { rename } from "fs"
13
-
14
- const FLASH_REDIRECT_GROUP_ID = 82
15
- export const TRAFFIC_SOURCE_ID_FLASH_AI = 22
16
- const UNIQUENESS_METHOD = "ip_ua"
17
- const MAX_COOKIES_TTL = 8760
18
- const TYPE_POSITION = "position"
19
- const STATE_ACTIVE = "active"
20
- const COST_TYPE_CPC = "CPC"
21
- const COST_VALUE = 0
22
- const COST_CURRENCY_USD = "USD"
23
-
24
- // clear campaigns from unused / wrong streams
25
-
26
- // let sampleApp: IFlashApp = {
27
- // id: 96,
28
- // bundle: "com.dynastyof.desert",
29
- // name: "Room of Fortuna",
30
- // plugId: "https://pastebin.com/raw/AR2u7dSD",
31
- // plugType: PlugType.PASTEBIN,
32
- // plugContent: "",
33
- // plugStatus: FlashAppPlugStatus.DISABLED,
34
- // plugAccountId: 1,
35
- // reservePlugId: "none",
36
- // reservePlugType: PlugType.OTHER,
37
- // reservePlugContent: "",
38
- // reservePlugStatus: FlashAppPlugStatus.DISABLED,
39
- // reservePlugAccountId: 1,
40
- // type: FlashAppType.GAMBLING,
41
- // developerEmail: "bolly.dreams@yahoo.com",
42
- // developerName: "Serhii Bolsky",
43
- // developerOrganization: "S.I.B FOP",
44
- // onesignalAppId: "e66658af-91c3-44bc-8123-ec64b1969cbd",
45
- // onesignalRestApiKey: "MDk2NjZlNTMtMzBhYi00MjEzLTg0ZGItOWVmZWZhMTczMzI0",
46
-
47
- // generationOptions: {
48
- // splashActivityClassName: "SplashActivity",
49
- // mainActivityClassName: "YourActivity",
50
- // linkName: "likn",
51
- // savedName: "isOk",
52
- // paranoidSeed: 228143750
53
- // },
54
- // geos: "RU,UA,KZ,ES,BR,PT,CL,MX",
55
- // keitaroData: {
56
- // redirectCampaignId: 0,
57
- // redirectCampaignName: "",
58
- // redirectDomainId: 0,
59
- // redirectDomainName: "",
60
- // trackingCampaignId: 0,
61
- // trackingCampaignName: "",
62
- // trackingDomainId: 0,
63
- // trackingDomainName: "",
64
- // clickIdParameterName: "p",
65
- // offerIdParameterName: "i",
66
- // }
67
- // } as IFlashApp
68
-
69
- export let addGeosToAllRedirectCampaigns = async (geosToAdd: string) => {
70
- let allCampaigns = await KeitaroService.getAllCampaigns()
71
-
72
-
73
- let regexp = new RegExp(`^F #`)
74
- const matchingCampaigns = allCampaigns.filter(campaign => regexp.exec(campaign.name));
75
- for (let campaign of matchingCampaigns) {
76
- console.log(campaign.name)
77
- console.log("––––––––––––––––––––––––")
78
- let streams = await KeitaroService.getStreamsByCampaignId(campaign.id)
79
- let botStream = streams[0]
80
- botStream.name = "Bot Protection"
81
- let owStream = streams.find(s => s.name == "OW")
82
-
83
- for (let stream of [owStream, botStream]) {
84
- if (!stream) continue
85
- console.log({
86
- name: stream.name,
87
- })
88
- let geos = stream?.filters[stream.type == "forced" ? 1 : 0]?.payload
89
- if (!geos) continue
90
- geosToAdd.split(" ").forEach((geo: string) => {
91
- let streamForGeo = streams.find((s) => s.name == geo)
92
- if (streamForGeo && stream) return
93
- if (!geos.includes(geo)) geos.push(geo)
94
- });
95
-
96
- stream.filters[stream.type == "forced" ? 1 : 0].payload = geos
97
-
98
- await KeitaroService.upsertStreamToCampaign(campaign, stream)
99
- await sleep(2000)
100
- }
101
- console.log("––––––––––––––––––––––––")
102
- }
103
- }
104
-
105
- // export let changeTrackingOfferGeo = async (appId: number, keitaroId: string, geo: string) => {
106
- // let allCampaigns = await KeitaroService.getAllCampaigns()
107
-
108
-
109
- // let regexp = new RegExp(`^FA #${appId} `)
110
- // let matchingCampaign = allCampaigns.find(campaign => regexp.exec(campaign.name))
111
- // if (!matchingCampaign) throw new Error(`No matching campaign got appId ${appId}`)
112
- // let streams = await KeitaroService.getStreamsByCampaignId(matchingCampaign.id)
113
- // letstream =
114
- // for (let campaign of matchingCampaigns) {
115
- // console.log(campaign.name)
116
- // console.log("––––––––––––––––––––––––")
117
- // let streams = await KeitaroService.getStreamsByCampaignId(campaign.id)
118
- // let botStream = streams[0]
119
- // botStream.name = "Bot Protection"
120
- // let owStream = streams.find(s => s.name == "OW")
121
-
122
- // for (let stream of [owStream, botStream]) {
123
- // if (!stream) continue
124
- // console.log({
125
- // name: stream.name,
126
- // })
127
- // let geos = stream?.filters[stream.type == "forced" ? 1 : 0]?.payload
128
- // if (!geos) continue
129
- // geosToAdd.split(" ").forEach((geo: string) => {
130
- // let streamForGeo = streams.find((s) => s.name == geo)
131
- // if (streamForGeo && stream) return
132
- // if (!geos.includes(geo)) geos.push(geo)
133
- // });
134
-
135
- // stream.filters[stream.type == "forced" ? 1 : 0].payload = geos
136
-
137
- // await KeitaroService.upsertStreamToCampaign(campaign, stream)
138
- // await sleep(2000)
139
- // }
140
- // console.log("––––––––––––––––––––––––")
141
- // }
142
- // }
143
-
144
- export let removeGeosFromAllRedirectCampaigns = async (geoToRemove: string) => {
145
- let allCampaigns = await KeitaroService.getAllCampaigns()
146
-
147
-
148
- let regexp = new RegExp(`^F #`)
149
- const matchingCampaigns = allCampaigns.filter(campaign => regexp.exec(campaign.name));
150
- for (let campaign of matchingCampaigns) {
151
- console.log(campaign.name)
152
- console.log("––––––––––––––––––––––––")
153
- let streams = await KeitaroService.getStreamsByCampaignId(campaign.id)
154
- let botStream = streams[0]
155
- botStream.name = "Bot Protection"
156
- let owStream = streams.find(s => s.name == "OW")
157
-
158
- for (let stream of [owStream, botStream]) {
159
- if (!stream) continue
160
- let geos: string[] = stream?.filters[0]?.payload
161
-
162
- if (!geos) continue
163
- let index = geos.findIndex(geo => geo == geoToRemove)
164
- geos.splice(index, 1)
165
- let newGeos = stream.filters[0].payload = geos
166
- console.log({
167
- name: stream.name,
168
- geos: geos,
169
- geosToRemove: geoToRemove,
170
- newGeos: newGeos
171
- })
172
-
173
- await KeitaroService.upsertStreamToCampaign(campaign, stream)
174
- await sleep(1200)
175
- }
176
- console.log("––––––––––––––––––––––––")
177
- }
178
- }
179
-
180
- // export let createOrFindFlashRedirectCampaign = async (app: IApp): Promise<IKeitaroCampaign> => {
181
- // if (app.keitaroData && app.keitaroData?.redirectCampaignId && app.keitaroData?.redirectCampaignId != 0) {
182
- // console.log("Already exists! Fetching...")
183
- // let campaign = await KeitaroService.getCampaignById(app.keitaroData.redirectCampaignId)
184
- // return campaign
185
- // }
186
-
187
- // // Checking for already existing campaign
188
- // let allCampaigns = await KeitaroService.getAllCampaigns()
189
- // let regexp = new RegExp(`^F #${app.id}`)
190
- // const matchingCampaigns = allCampaigns.filter(campaign => regexp.exec(campaign.name));
191
-
192
- // if (matchingCampaigns.length != 0) {
193
- // let campaign = matchingCampaigns[0]
194
- // // if (app.keitaroData) app.keitaroData.redirectCampaignId = campaign.id
195
-
196
- // console.log("Already exists!")
197
- // // await app.save()
198
- // return campaign
199
- // }
200
-
201
- // // Select domain for a campaign
202
- // let allDomains = await KeitaroService.getDomains(true)
203
- // if (!allDomains) {
204
- // throw Error(`Failed to get all domains list`)
205
- // }
206
-
207
- // const domain = allDomains[Math.floor(Math.random() * allDomains.length)];
208
-
209
- // // Okay, now we have everything, let's create name for our campaign
210
- // const name = `F #${app.id} (${app.bundle})`
211
-
212
- // // And generate alias
213
- // const alias = Array.from({ length: 8 }, () => Math.floor(Math.random() * 36).toString(36)).join('');
214
-
215
- // // Here go the parameters for our campaign
216
- // let parameters = prepareRedirectCampaignParameters(app.id)
217
-
218
- // // Now let create a campaign object
219
- // let campaignData = prepareCampaignObject(name, alias, domain, parameters)
220
-
221
- // // And POST it to Keitaro!
222
-
223
- // let createdCampaign = await KeitaroService.createCampaign(campaignData)
224
-
225
- // console.log(`Done! Campaign data:`)
226
- // console.log(createdCampaign)
227
-
228
- // return createdCampaign
229
- // }
230
-
231
- let prepareCampaignObject = (name: string, alias: string, domain: IKeitaroDomain, parameters: IKeitaroCampaignParameters): Partial<IKeitaroCampaign> => {
232
- return {
233
- name: name,
234
- alias: alias,
235
- type: TYPE_POSITION,
236
- uniqueness_method: UNIQUENESS_METHOD,
237
- cookies_ttl: MAX_COOKIES_TTL,
238
- state: STATE_ACTIVE,
239
- cost_type: COST_TYPE_CPC,
240
- cost_value: COST_VALUE,
241
- cost_currency: COST_CURRENCY_USD,
242
- group_id: FLASH_REDIRECT_GROUP_ID,
243
- traffic_source_id: TRAFFIC_SOURCE_ID_FLASH_AI,
244
- domain_id: domain.id,
245
- parameters: parameters,
246
- uniqueness_use_cookies: true,
247
- traffic_loss: 0
248
- }
249
- }
250
-
251
- let prepareRedirectCampaignParameters = (appId: number): IKeitaroCampaignParameters => {
252
- return {
253
- "keyword": {
254
- "name": "keyword",
255
- "placeholder": "",
256
- "alias": ""
257
- },
258
- "cost": {
259
- "name": "cost",
260
- "placeholder": "",
261
- "alias": ""
262
- },
263
- "currency": {
264
- "name": "currency",
265
- "placeholder": "",
266
- "alias": ""
267
- },
268
- "external_id": {
269
- "name": "external_id",
270
- "placeholder": "",
271
- "alias": ""
272
- },
273
- "creative_id": {
274
- "name": "creative_id",
275
- "placeholder": "",
276
- "alias": ""
277
- },
278
- "ad_campaign_id": {
279
- "name": "ad_campaign_id",
280
- "placeholder": "",
281
- "alias": ""
282
- },
283
- "source": {
284
- "name": "source",
285
- "placeholder": "",
286
- "alias": ""
287
- },
288
- sub_id_1: {
289
- name: "appId",
290
- placeholder: `${appId}`,
291
- alias: ""
292
- }
293
- }
294
- }
295
-
296
- export let prepareOWCampaignParameters = (app: IApp): IKeitaroCampaignParameters => {
297
- return {
298
- "keyword": {
299
- "name": "keyword",
300
- "placeholder": "",
301
- "alias": ""
302
- },
303
- "cost": {
304
- "name": "cost",
305
- "placeholder": "",
306
- "alias": ""
307
- },
308
- "currency": {
309
- "name": "currency",
310
- "placeholder": "",
311
- "alias": ""
312
- },
313
- "external_id": {
314
- "name": "external_id",
315
- "placeholder": "",
316
- "alias": ""
317
- },
318
- "creative_id": {
319
- "name": "creative_id",
320
- "placeholder": "",
321
- "alias": ""
322
- },
323
- "ad_campaign_id": {
324
- "name": "ad_campaign_id",
325
- "placeholder": "",
326
- "alias": ""
327
- },
328
- "source": {
329
- "name": "source",
330
- "placeholder": "",
331
- "alias": ""
332
- },
333
- sub_id_8: {
334
- "name": "click_id",
335
- "placeholder": "{click_id}",
336
- "alias": "click_id"
337
- },
338
- sub_id_15: {
339
- "name": "offer_id",
340
- "placeholder": "{offer_custom_id}",
341
- "alias": "offer_custom_id"
342
- }
343
- }
344
- }
345
-
346
- function createBPStreamPartialPayload(app: IApp): Partial<IKeitaroStream> {
347
- return {
348
- name: `Bot Protection`,
349
- type: "forced",
350
- action_type: "http",
351
- action_payload: "https://tomain.com",
352
- schema: "redirect",
353
- filter_or: true,
354
- collect_clicks: true,
355
- weight: 0,
356
- filters: [{
357
- name: "country",
358
- mode: "reject",
359
- payload: app.geos.split(" ")
360
- }, {
361
- name: "bot",
362
- mode: "accept",
363
- payload: null,
364
- }],
365
- }
366
- }
367
-
368
- function createDefenceMainStreamPartialPayload(app: IApp): Partial<IKeitaroStream> {
369
- return {
370
- name: "ALL",
371
- type: "regular",
372
- action_type: "http",
373
- action_payload: app.directParams?.path,
374
- schema: "redirect",
375
- filter_or: false,
376
- collect_clicks: true,
377
- weight: 100,
378
- }
379
- }
380
-
381
- export function createOWStreamPartialPayload(app: IApp): Partial<IKeitaroStream> {
382
- return {
383
- name: `OW`,
384
- type: "regular",
385
- action_type: "http",
386
- action_payload: "https://bprtsk-controlpanel.com/ow?appId={appId}&clickId={uid}&countryCode={country}",
387
- schema: "redirect",
388
- filter_or: false,
389
- collect_clicks: true,
390
- weight: 100,
391
- filters: [{
392
- name: "country",
393
- mode: "accept",
394
- payload: app.geos.split(" ")
395
- }],
396
- }
397
- }
398
-
399
- export async function createFlashCampaigns(app: IApp) {
400
- // Upserting Flash Redirect campaign
401
- // let redirectCampaign = await createOrFindFlashRedirectCampaign(app)
402
- // console.log(`Redirect campaign name:`)
403
- // console.log(`${redirectCampaign.domain}/${redirectCampaign.alias}?uid=`)
404
-
405
- // Upserting necessary streams
406
- // let botProtectionStream = createBPStreamPartialPayload(app)
407
- // let owStream = createOWStreamPartialPayload(app)
408
-
409
- // for (let stream of [botProtectionStream, owStream]) {
410
- // await KeitaroService.upsertStreamToCampaign(redirectCampaign, stream)
411
- // }
412
-
413
- // Upserting OW campaign
414
- let owCampaign = await KeitaroService.cloneOWCampaign(app)
415
-
416
- return {
417
- tracking: owCampaign,
418
- // redirect: redirectCampaign
419
- }
420
- }
421
-
422
- export async function createDirectCampaign(app: IApp) {
423
- let campaign = await KeitaroService.cloneDCampaign(app)
424
-
425
- let stream = createDefenceMainStreamPartialPayload(app)
426
-
427
- await KeitaroService.upsertStreamToCampaign(campaign, stream)
428
-
429
- return campaign
430
- }
431
-
432
- export async function createOWCampaign(app: IApp) {
433
- let owCampaign = await KeitaroService.cloneOWCampaign(app)
434
-
435
- return owCampaign
436
- }
437
-
438
-
439
- // TODO separate files for offers, campaigns etc.
440
-
441
- let removeBrokenDomain = async () => {
442
-
443
- let regexp = new RegExp(`^#`)
444
- let campaigns = (await KeitaroService.getAllCampaigns()).filter(campaign => regexp.exec(campaign.name) && campaign.domain == "https://slottibolt.com/");
445
- for (let campaign of campaigns) {
446
- console.log(campaign.name)
447
- }
448
- }
449
-
450
- let changeSourceForFA = async () => {
451
- let regexp = new RegExp(`^#`)
452
- let campaigns = (await KeitaroService.getAllCampaigns()).filter(campaign => regexp.exec(campaign.name));
453
- for (let campaign of campaigns) {
454
- console.log(campaign.name)
455
- await KeitaroService.updateCampaign(campaign.id, {
456
- traffic_source_id: 22
457
- })
458
- }
459
- }
460
-
461
- let replaceInOfferLink = async (old: string, replace: string, offerNameRegEx: RegExp) => {
462
- let offers = await KeitaroService.getAllOffers()
463
-
464
- offers = offers.filter((o) => offerNameRegEx.exec(o.name))
465
-
466
- for (let offer of offers) {
467
- console.log(offer.name)
468
- // offer.action_payload = offer.action_payload.replace(old, replace)
469
-
470
- await sleep(700)
471
- await KeitaroService.updateOffer(offer)
472
- }
473
- }
474
-
475
- // let gatherInfoForFlashApps = async () => {
476
- // console.log(__dirname)
477
- // const pasteMap: PasteMap = JSON.parse(await readFile('/Users/bprtsk/Documents/Work/ASO/AI Control Panel /AI Panel Core/src/utils/map-paste.json', 'utf8'));
478
- // const trackingMap: TrackingMap = JSON.parse(await readFile('/Users/bprtsk/Documents/Work/ASO/AI Control Panel /AI Panel Core/src/utils/map-tracking.json', 'utf8'));
479
- // const flashApps: { [key: string]: IFlashApp | undefined } = JSON.parse(await readFile('/Users/bprtsk/Documents/Work/ASO/AI Control Panel /AI Panel Core/src/utils/map-apps.json', 'utf8'));
480
-
481
- // let allCampaigns = await KeitaroService.getAllCampaigns()
482
- // let owCampaigns = allCampaigns.filter(c => new RegExp(`^FA #`).exec(c.name)).reduce((acc, obj) => ({ ...acc, [new RegExp(/#(.*) \(/g).exec(obj.name)?.[1] || obj.name]: obj }), {});
483
- // let redirectCampaigns = allCampaigns.filter(c => new RegExp(`^F #`).exec(c.name)).reduce((acc, obj) => ({ ...acc, [new RegExp(/#(.*) \(/g).exec(obj.name)?.[1] || obj.name]: obj }), {});
484
-
485
- // let result = []
486
- // for (let [id, paste] of Object.entries(pasteMap)) {
487
- // console.log(`- #${id}`)
488
- // // let ow = owCampaigns[id as keyof object] as IKeitaroCampaign | undefined
489
- // // let redirect = redirectCampaigns[id as keyof object] as IKeitaroCampaign | undefined
490
- // // let tracking = trackingMap[id as keyof object]
491
- // let app = flashApps[id as keyof object]
492
-
493
- // if (!app) {
494
- // console.log(`! No app for ${id}`)
495
- // continue
496
- // }
497
-
498
- // // if (paste.status == FlashAppPlugStatus.ENABLED) {
499
- // // if (!ow) console.log(`#${id}: no OW`)
500
-
501
- // // if (!redirect) console.log(`No redirect for #${id}`)
502
- // // else if (!paste.content.includes(redirect.domain)) console.log(`#${id}: Redirect domain (${redirect.domain}) differs from paste domain (${paste.content})`)
503
- // // else if (!paste.content.includes(redirect.alias)) console.log(`#${id}: Redirect campaign alias (${redirect.alias}) differs from paste id (${paste.content})`)
504
- // // }
505
-
506
- // // let appId
507
- // // for (let parameter of Object.values(redirect?.parameters || {})) {
508
- // // if (parameter.name == "appId") {
509
- // // appId = parameter.placeholder
510
- // // }
511
- // // }
512
- // // if (!appId && ow) console.log(`No app id for #${id}`)
513
- // // else if (appId != id && ow) console.log(`Wrong app id (${appId}) for #${id}`)
514
-
515
- // // let owDomain = ow?.domain?.split("/")[2]
516
- // // let owDomainId = ow?.domain_id
517
- // // let owCampaignId = ow?.id
518
- // // let owCampaignName = ow?.name
519
- // // let owCampaignAlias = ow?.alias
520
- // // let redirectDomain = redirect?.domain?.split("/")[2]
521
- // // let redirectDomainId = redirect?.domain_id
522
- // // let redirectCampaignId = redirect?.id
523
- // // let redirectCampaignName = redirect?.name
524
- // // let redirectCampaignAlias = redirect?.alias
525
-
526
- // // let clickIdParameterName = ow?.parameters?.sub_id_8?.name
527
- // // let offerIdParameterName = ow?.parameters?.sub_id_15?.name
528
- // // if (!clickIdParameterName && app && ow) console.log(`No click id parameter for #${id}`)
529
- // // if (!offerIdParameterName && app && ow) console.log(`No offer id parameter for #${id}`)
530
-
531
- // // let trackingLink = tracking?.trackingUrl
532
-
533
- // // app.plugStatus = paste.status
534
- // // app.plugContent = paste.content
535
- // // app.plugType = PlugType.PASTEBIN
536
- // // app.plugId = paste.paste_id
537
- // // app.reservePlugType = PlugType.PASTEBIN
538
- // // app.plugAccountId = parseInt(paste.accountId)
539
-
540
- // // app.keitaroData.redirectDomainName = redirectDomain || "none"
541
- // // app.keitaroData.redirectDomainId = redirectDomainId || 0
542
- // // app.keitaroData.redirectCampaignId = redirectCampaignId || 0
543
- // // app.keitaroData.redirectCampaignName = redirectCampaignName || "none"
544
- // // app.keitaroData.redirectCampaignAlias = redirectCampaignAlias || "none"
545
- // // app.keitaroData.trackingDomainId = owDomainId || 0
546
- // // app.keitaroData.trackingDomainName = owDomain || "none"
547
- // // app.keitaroData.trackingCampaignId = owCampaignId || 0
548
- // // app.keitaroData.trackingCampaignName = owCampaignName || "none"
549
- // // app.keitaroData.trackingCampaignAlias = owCampaignAlias || "none"
550
-
551
- // // if (clickIdParameterName) app.keitaroData.clickIdParameterName = clickIdParameterName
552
- // // if (offerIdParameterName) app.keitaroData.offerIdParameterName = offerIdParameterName
553
-
554
- // // if (trackingLink) app.trackingUrl = trackingLink
555
-
556
- // // if (!owCampaignName?.includes(app.bundle) && ow) {
557
- // // console.log(`OW: bundle differs (${app.bundle} in DB and ${owCampaignName})`)
558
- // // }
559
-
560
- // // if (!redirectCampaignName?.includes(app.bundle) && redirect) {
561
- // // console.log(`Redirect: bundle differs (${app.bundle} in DB and ${redirectCampaignName})`)
562
- // // }
563
-
564
- // result.push(app)
565
-
566
- // console.log("--------------------")
567
- // }
568
-
569
- // await axios.post(`https://aipanel-secondary.com/flash/batch-update`, result, {
570
- // headers: {
571
- // "Authorization": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6ImFkbWluIiwiaWF0IjoxNjg3Mjc2NjA0LCJleHAiOjE2ODc4ODE0MDR9.XesrnNGmWlK-yBBMs5TeKvTe07A8Sud7B4MyfZAOMag"
572
- // }
573
- // })
574
-
575
- // // console.log(result)
576
- // }
577
-
578
- let findBrokenOfferStreams = async () => {
579
- let allCampaigns = await KeitaroService.getAllCampaigns()
580
-
581
- let regexp = new RegExp(`^#`)
582
- const matchingCampaigns = allCampaigns.filter(campaign => regexp.exec(campaign.name));
583
- for (let campaign of matchingCampaigns) {
584
- console.log(campaign.name)
585
- console.log("––––––––––––––––––––––––")
586
- let streams = await KeitaroService.getStreamsByCampaignId(campaign.id)
587
-
588
- for (let stream of streams) {
589
- if (stream.name == "Bot Protection") continue
590
-
591
- let idPayload = stream.filters.find(f => f.name == "sub_id_15")
592
-
593
- if (!idPayload) {
594
- console.log(`${stream.name}: id payload is missing, payload: ${stream.filters}`)
595
- }
596
- }
597
- }
598
- }
599
-
600
- let moveCampaignsToAnotherGroup = async () => {
601
- const appIds: number[] = [
602
- 460, 457, 459, 456, 421, 711, 451, 709, 712, 428, 455, 388, 458,
603
- 355, 341, 342, 334, 431, 403, 436, 365, 393, 801, 702, 448, 444,
604
- 437, 439, 447, 378, 412, 380, 402, 852, 438
605
- ]
606
-
607
- let allCampaigns = await KeitaroService.getAllCampaigns()
608
- let campaignIds = allCampaigns.filter(c => {
609
- for (let id of appIds) if (c.name.includes(`#${id}`)) return true
610
- return false
611
- }).map(c => c.id)
612
-
613
- let from = 2, to = 80
614
-
615
- await KeitaroService.changeCampaignsGroup(from, to, campaignIds)
616
- }
617
-
618
- let renameCampaigns = async () => {
619
- let allCampaigns = await KeitaroService.getAllCampaigns()
620
-
621
- let regexp = /^#(\d+)/;
622
-
623
- // Filter campaigns that match the regex
624
- const matchingCampaigns = allCampaigns.filter(campaign => regexp.test(campaign.name) && campaign.group_id == 80);
625
- console.log(`Matching campaigns found: ${matchingCampaigns.length}`);
626
-
627
- for (let campaign of matchingCampaigns) {
628
- const match = regexp.exec(campaign.name);
629
- if (match && match[1]) {
630
- const appId = match[1]; // Extracted number after '#'
631
- await KeitaroService.updateCampaign(campaign.id, { name: `#${appId} [⛌]` })
632
- console.log(`Updated "${campaign.name}" to #${appId} [⛌]`)
633
- }
634
- }
635
- }
636
-
637
- // renameCampaigns()
638
-
639
- // moveCampaignsToAnotherGroup()
640
-
641
- // Getting campaign group ids
642
- // (async () => {
643
- // let campaigns = await KeitaroService.getAllCampaigns()
644
- // let normal = campaigns.find(c => c.id == 2684)
645
- // let archive = campaigns.find(c => c.id == 2679)
646
-
647
- // console.log([normal?.group_id, archive?.group_id])
648
- // })()
649
-
650
- // findBrokenOfferStreams()
651
-
652
- // removeBrokenDomain()
653
-
654
- // createFlashCampaigns(sampleApp)
655
- // changeSourceForFA()
656
- // addGeosToAllRedirectCam`paigns("GR")
657
- // removeGeosFromAllRedirectCampaigns("BE`")
658
-
659
- // gatherInfoForFlashApps()
660
-
661
- // replaceInOfferLink(`xmariorel.com`, `lalielynaualish.com`, /.*icecasino_huffson.*/g)
662
- // replaceInOfferLink(`lp=18`, `lp=00`, /.*icecasino_huffson.*/g)
663
- // let test = async () => console.log(await KeitaroService.getProfitForTodayAndYesterday())
664
- // test()
1
+ import { IKeitaroStream } from "../keitaro/keitaro-stream"
2
+ import { IApp, PlugType, } from "../app/app"
3
+ import { AppType } from "../app/app-type"
4
+ import { IKeitaroCampaign, IKeitaroCampaignParameters } from "../keitaro/keitaro-campaign"
5
+ import { IKeitaroDomain } from "../keitaro/keitaro-domain"
6
+ import { KeitaroService } from "../network/keitaro/keitaro-service"
7
+ const sleep = require('sleep-promise');
8
+ import * as util from 'util';
9
+ import axios from "axios"
10
+ import { getTimestampsForTodayAndYesterday } from "../utils/general"
11
+ import { KeitaroUtils } from "index"
12
+ import { rename } from "fs"
13
+
14
+ const FLASH_REDIRECT_GROUP_ID = 82
15
+ export const TRAFFIC_SOURCE_ID_FLASH_AI = 22
16
+ const UNIQUENESS_METHOD = "ip_ua"
17
+ const MAX_COOKIES_TTL = 8760
18
+ const TYPE_POSITION = "position"
19
+ const STATE_ACTIVE = "active"
20
+ const COST_TYPE_CPC = "CPC"
21
+ const COST_VALUE = 0
22
+ const COST_CURRENCY_USD = "USD"
23
+
24
+ // clear campaigns from unused / wrong streams
25
+
26
+ // let sampleApp: IFlashApp = {
27
+ // id: 96,
28
+ // bundle: "com.dynastyof.desert",
29
+ // name: "Room of Fortuna",
30
+ // plugId: "https://pastebin.com/raw/AR2u7dSD",
31
+ // plugType: PlugType.PASTEBIN,
32
+ // plugContent: "",
33
+ // plugStatus: FlashAppPlugStatus.DISABLED,
34
+ // plugAccountId: 1,
35
+ // reservePlugId: "none",
36
+ // reservePlugType: PlugType.OTHER,
37
+ // reservePlugContent: "",
38
+ // reservePlugStatus: FlashAppPlugStatus.DISABLED,
39
+ // reservePlugAccountId: 1,
40
+ // type: FlashAppType.GAMBLING,
41
+ // developerEmail: "bolly.dreams@yahoo.com",
42
+ // developerName: "Serhii Bolsky",
43
+ // developerOrganization: "S.I.B FOP",
44
+ // onesignalAppId: "e66658af-91c3-44bc-8123-ec64b1969cbd",
45
+ // onesignalRestApiKey: "MDk2NjZlNTMtMzBhYi00MjEzLTg0ZGItOWVmZWZhMTczMzI0",
46
+
47
+ // generationOptions: {
48
+ // splashActivityClassName: "SplashActivity",
49
+ // mainActivityClassName: "YourActivity",
50
+ // linkName: "likn",
51
+ // savedName: "isOk",
52
+ // paranoidSeed: 228143750
53
+ // },
54
+ // geos: "RU,UA,KZ,ES,BR,PT,CL,MX",
55
+ // keitaroData: {
56
+ // redirectCampaignId: 0,
57
+ // redirectCampaignName: "",
58
+ // redirectDomainId: 0,
59
+ // redirectDomainName: "",
60
+ // trackingCampaignId: 0,
61
+ // trackingCampaignName: "",
62
+ // trackingDomainId: 0,
63
+ // trackingDomainName: "",
64
+ // clickIdParameterName: "p",
65
+ // offerIdParameterName: "i",
66
+ // }
67
+ // } as IFlashApp
68
+
69
+ export let addGeosToAllRedirectCampaigns = async (geosToAdd: string) => {
70
+ let allCampaigns = await KeitaroService.getAllCampaigns()
71
+
72
+
73
+ let regexp = new RegExp(`^F #`)
74
+ const matchingCampaigns = allCampaigns.filter(campaign => regexp.exec(campaign.name));
75
+ for (let campaign of matchingCampaigns) {
76
+ console.log(campaign.name)
77
+ console.log("––––––––––––––––––––––––")
78
+ let streams = await KeitaroService.getStreamsByCampaignId(campaign.id)
79
+ let botStream = streams[0]
80
+ botStream.name = "Bot Protection"
81
+ let owStream = streams.find(s => s.name == "OW")
82
+
83
+ for (let stream of [owStream, botStream]) {
84
+ if (!stream) continue
85
+ console.log({
86
+ name: stream.name,
87
+ })
88
+ let geos = stream?.filters[stream.type == "forced" ? 1 : 0]?.payload
89
+ if (!geos) continue
90
+ geosToAdd.split(" ").forEach((geo: string) => {
91
+ let streamForGeo = streams.find((s) => s.name == geo)
92
+ if (streamForGeo && stream) return
93
+ if (!geos.includes(geo)) geos.push(geo)
94
+ });
95
+
96
+ stream.filters[stream.type == "forced" ? 1 : 0].payload = geos
97
+
98
+ await KeitaroService.upsertStreamToCampaign(campaign, stream)
99
+ await sleep(2000)
100
+ }
101
+ console.log("––––––––––––––––––––––––")
102
+ }
103
+ }
104
+
105
+ // export let changeTrackingOfferGeo = async (appId: number, keitaroId: string, geo: string) => {
106
+ // let allCampaigns = await KeitaroService.getAllCampaigns()
107
+
108
+
109
+ // let regexp = new RegExp(`^FA #${appId} `)
110
+ // let matchingCampaign = allCampaigns.find(campaign => regexp.exec(campaign.name))
111
+ // if (!matchingCampaign) throw new Error(`No matching campaign got appId ${appId}`)
112
+ // let streams = await KeitaroService.getStreamsByCampaignId(matchingCampaign.id)
113
+ // letstream =
114
+ // for (let campaign of matchingCampaigns) {
115
+ // console.log(campaign.name)
116
+ // console.log("––––––––––––––––––––––––")
117
+ // let streams = await KeitaroService.getStreamsByCampaignId(campaign.id)
118
+ // let botStream = streams[0]
119
+ // botStream.name = "Bot Protection"
120
+ // let owStream = streams.find(s => s.name == "OW")
121
+
122
+ // for (let stream of [owStream, botStream]) {
123
+ // if (!stream) continue
124
+ // console.log({
125
+ // name: stream.name,
126
+ // })
127
+ // let geos = stream?.filters[stream.type == "forced" ? 1 : 0]?.payload
128
+ // if (!geos) continue
129
+ // geosToAdd.split(" ").forEach((geo: string) => {
130
+ // let streamForGeo = streams.find((s) => s.name == geo)
131
+ // if (streamForGeo && stream) return
132
+ // if (!geos.includes(geo)) geos.push(geo)
133
+ // });
134
+
135
+ // stream.filters[stream.type == "forced" ? 1 : 0].payload = geos
136
+
137
+ // await KeitaroService.upsertStreamToCampaign(campaign, stream)
138
+ // await sleep(2000)
139
+ // }
140
+ // console.log("––––––––––––––––––––––––")
141
+ // }
142
+ // }
143
+
144
+ export let removeGeosFromAllRedirectCampaigns = async (geoToRemove: string) => {
145
+ let allCampaigns = await KeitaroService.getAllCampaigns()
146
+
147
+
148
+ let regexp = new RegExp(`^F #`)
149
+ const matchingCampaigns = allCampaigns.filter(campaign => regexp.exec(campaign.name));
150
+ for (let campaign of matchingCampaigns) {
151
+ console.log(campaign.name)
152
+ console.log("––––––––––––––––––––––––")
153
+ let streams = await KeitaroService.getStreamsByCampaignId(campaign.id)
154
+ let botStream = streams[0]
155
+ botStream.name = "Bot Protection"
156
+ let owStream = streams.find(s => s.name == "OW")
157
+
158
+ for (let stream of [owStream, botStream]) {
159
+ if (!stream) continue
160
+ let geos: string[] = stream?.filters[0]?.payload
161
+
162
+ if (!geos) continue
163
+ let index = geos.findIndex(geo => geo == geoToRemove)
164
+ geos.splice(index, 1)
165
+ let newGeos = stream.filters[0].payload = geos
166
+ console.log({
167
+ name: stream.name,
168
+ geos: geos,
169
+ geosToRemove: geoToRemove,
170
+ newGeos: newGeos
171
+ })
172
+
173
+ await KeitaroService.upsertStreamToCampaign(campaign, stream)
174
+ await sleep(1200)
175
+ }
176
+ console.log("––––––––––––––––––––––––")
177
+ }
178
+ }
179
+
180
+ // export let createOrFindFlashRedirectCampaign = async (app: IApp): Promise<IKeitaroCampaign> => {
181
+ // if (app.keitaroData && app.keitaroData?.redirectCampaignId && app.keitaroData?.redirectCampaignId != 0) {
182
+ // console.log("Already exists! Fetching...")
183
+ // let campaign = await KeitaroService.getCampaignById(app.keitaroData.redirectCampaignId)
184
+ // return campaign
185
+ // }
186
+
187
+ // // Checking for already existing campaign
188
+ // let allCampaigns = await KeitaroService.getAllCampaigns()
189
+ // let regexp = new RegExp(`^F #${app.id}`)
190
+ // const matchingCampaigns = allCampaigns.filter(campaign => regexp.exec(campaign.name));
191
+
192
+ // if (matchingCampaigns.length != 0) {
193
+ // let campaign = matchingCampaigns[0]
194
+ // // if (app.keitaroData) app.keitaroData.redirectCampaignId = campaign.id
195
+
196
+ // console.log("Already exists!")
197
+ // // await app.save()
198
+ // return campaign
199
+ // }
200
+
201
+ // // Select domain for a campaign
202
+ // let allDomains = await KeitaroService.getDomains(true)
203
+ // if (!allDomains) {
204
+ // throw Error(`Failed to get all domains list`)
205
+ // }
206
+
207
+ // const domain = allDomains[Math.floor(Math.random() * allDomains.length)];
208
+
209
+ // // Okay, now we have everything, let's create name for our campaign
210
+ // const name = `F #${app.id} (${app.bundle})`
211
+
212
+ // // And generate alias
213
+ // const alias = Array.from({ length: 8 }, () => Math.floor(Math.random() * 36).toString(36)).join('');
214
+
215
+ // // Here go the parameters for our campaign
216
+ // let parameters = prepareRedirectCampaignParameters(app.id)
217
+
218
+ // // Now let create a campaign object
219
+ // let campaignData = prepareCampaignObject(name, alias, domain, parameters)
220
+
221
+ // // And POST it to Keitaro!
222
+
223
+ // let createdCampaign = await KeitaroService.createCampaign(campaignData)
224
+
225
+ // console.log(`Done! Campaign data:`)
226
+ // console.log(createdCampaign)
227
+
228
+ // return createdCampaign
229
+ // }
230
+
231
+ let prepareCampaignObject = (name: string, alias: string, domain: IKeitaroDomain, parameters: IKeitaroCampaignParameters): Partial<IKeitaroCampaign> => {
232
+ return {
233
+ name: name,
234
+ alias: alias,
235
+ type: TYPE_POSITION,
236
+ uniqueness_method: UNIQUENESS_METHOD,
237
+ cookies_ttl: MAX_COOKIES_TTL,
238
+ state: STATE_ACTIVE,
239
+ cost_type: COST_TYPE_CPC,
240
+ cost_value: COST_VALUE,
241
+ cost_currency: COST_CURRENCY_USD,
242
+ group_id: FLASH_REDIRECT_GROUP_ID,
243
+ traffic_source_id: TRAFFIC_SOURCE_ID_FLASH_AI,
244
+ domain_id: domain.id,
245
+ parameters: parameters,
246
+ uniqueness_use_cookies: true,
247
+ traffic_loss: 0
248
+ }
249
+ }
250
+
251
+ let prepareRedirectCampaignParameters = (appId: number): IKeitaroCampaignParameters => {
252
+ return {
253
+ "keyword": {
254
+ "name": "keyword",
255
+ "placeholder": "",
256
+ "alias": ""
257
+ },
258
+ "cost": {
259
+ "name": "cost",
260
+ "placeholder": "",
261
+ "alias": ""
262
+ },
263
+ "currency": {
264
+ "name": "currency",
265
+ "placeholder": "",
266
+ "alias": ""
267
+ },
268
+ "external_id": {
269
+ "name": "external_id",
270
+ "placeholder": "",
271
+ "alias": ""
272
+ },
273
+ "creative_id": {
274
+ "name": "creative_id",
275
+ "placeholder": "",
276
+ "alias": ""
277
+ },
278
+ "ad_campaign_id": {
279
+ "name": "ad_campaign_id",
280
+ "placeholder": "",
281
+ "alias": ""
282
+ },
283
+ "source": {
284
+ "name": "source",
285
+ "placeholder": "",
286
+ "alias": ""
287
+ },
288
+ sub_id_1: {
289
+ name: "appId",
290
+ placeholder: `${appId}`,
291
+ alias: ""
292
+ }
293
+ }
294
+ }
295
+
296
+ export let prepareOWCampaignParameters = (app: IApp): IKeitaroCampaignParameters => {
297
+ return {
298
+ "keyword": {
299
+ "name": "keyword",
300
+ "placeholder": "",
301
+ "alias": ""
302
+ },
303
+ "cost": {
304
+ "name": "cost",
305
+ "placeholder": "",
306
+ "alias": ""
307
+ },
308
+ "currency": {
309
+ "name": "currency",
310
+ "placeholder": "",
311
+ "alias": ""
312
+ },
313
+ "external_id": {
314
+ "name": "external_id",
315
+ "placeholder": "",
316
+ "alias": ""
317
+ },
318
+ "creative_id": {
319
+ "name": "creative_id",
320
+ "placeholder": "",
321
+ "alias": ""
322
+ },
323
+ "ad_campaign_id": {
324
+ "name": "ad_campaign_id",
325
+ "placeholder": "",
326
+ "alias": ""
327
+ },
328
+ "source": {
329
+ "name": "source",
330
+ "placeholder": "",
331
+ "alias": ""
332
+ },
333
+ sub_id_8: {
334
+ "name": "click_id",
335
+ "placeholder": "{click_id}",
336
+ "alias": "click_id"
337
+ },
338
+ sub_id_15: {
339
+ "name": "offer_id",
340
+ "placeholder": "{offer_custom_id}",
341
+ "alias": "offer_custom_id"
342
+ }
343
+ }
344
+ }
345
+
346
+ function createBPStreamPartialPayload(app: IApp): Partial<IKeitaroStream> {
347
+ return {
348
+ name: `Bot Protection`,
349
+ type: "forced",
350
+ action_type: "http",
351
+ action_payload: "https://tomain.com",
352
+ schema: "redirect",
353
+ filter_or: true,
354
+ collect_clicks: true,
355
+ weight: 0,
356
+ filters: [{
357
+ name: "country",
358
+ mode: "reject",
359
+ payload: app.geos.split(" ")
360
+ }, {
361
+ name: "bot",
362
+ mode: "accept",
363
+ payload: null,
364
+ }],
365
+ }
366
+ }
367
+
368
+ function createDefenceMainStreamPartialPayload(app: IApp): Partial<IKeitaroStream> {
369
+ return {
370
+ name: "ALL",
371
+ type: "regular",
372
+ action_type: "http",
373
+ action_payload: app.directParams?.path,
374
+ schema: "redirect",
375
+ filter_or: false,
376
+ collect_clicks: true,
377
+ weight: 100,
378
+ }
379
+ }
380
+
381
+ export function createOWStreamPartialPayload(app: IApp): Partial<IKeitaroStream> {
382
+ return {
383
+ name: `OW`,
384
+ type: "regular",
385
+ action_type: "http",
386
+ action_payload: "https://bprtsk-controlpanel.com/ow?appId={appId}&clickId={uid}&countryCode={country}",
387
+ schema: "redirect",
388
+ filter_or: false,
389
+ collect_clicks: true,
390
+ weight: 100,
391
+ filters: [{
392
+ name: "country",
393
+ mode: "accept",
394
+ payload: app.geos.split(" ")
395
+ }],
396
+ }
397
+ }
398
+
399
+ export async function createFlashCampaigns(app: IApp) {
400
+ // Upserting Flash Redirect campaign
401
+ // let redirectCampaign = await createOrFindFlashRedirectCampaign(app)
402
+ // console.log(`Redirect campaign name:`)
403
+ // console.log(`${redirectCampaign.domain}/${redirectCampaign.alias}?uid=`)
404
+
405
+ // Upserting necessary streams
406
+ // let botProtectionStream = createBPStreamPartialPayload(app)
407
+ // let owStream = createOWStreamPartialPayload(app)
408
+
409
+ // for (let stream of [botProtectionStream, owStream]) {
410
+ // await KeitaroService.upsertStreamToCampaign(redirectCampaign, stream)
411
+ // }
412
+
413
+ // Upserting OW campaign
414
+ let owCampaign = await KeitaroService.cloneOWCampaign(app)
415
+
416
+ return {
417
+ tracking: owCampaign,
418
+ // redirect: redirectCampaign
419
+ }
420
+ }
421
+
422
+ export async function createDirectCampaign(app: IApp) {
423
+ let campaign = await KeitaroService.cloneDCampaign(app)
424
+
425
+ let stream = createDefenceMainStreamPartialPayload(app)
426
+
427
+ await KeitaroService.upsertStreamToCampaign(campaign, stream)
428
+
429
+ return campaign
430
+ }
431
+
432
+ export async function createOWCampaign(app: IApp) {
433
+ let owCampaign = await KeitaroService.cloneOWCampaign(app)
434
+
435
+ return owCampaign
436
+ }
437
+
438
+
439
+ // TODO separate files for offers, campaigns etc.
440
+
441
+ let removeBrokenDomain = async () => {
442
+
443
+ let regexp = new RegExp(`^#`)
444
+ let campaigns = (await KeitaroService.getAllCampaigns()).filter(campaign => regexp.exec(campaign.name) && campaign.domain == "https://slottibolt.com/");
445
+ for (let campaign of campaigns) {
446
+ console.log(campaign.name)
447
+ }
448
+ }
449
+
450
+ let changeSourceForFA = async () => {
451
+ let regexp = new RegExp(`^#`)
452
+ let campaigns = (await KeitaroService.getAllCampaigns()).filter(campaign => regexp.exec(campaign.name));
453
+ for (let campaign of campaigns) {
454
+ console.log(campaign.name)
455
+ await KeitaroService.updateCampaign(campaign.id, {
456
+ traffic_source_id: 22
457
+ })
458
+ }
459
+ }
460
+
461
+ let replaceInOfferLink = async (old: string, replace: string, offerNameRegEx: RegExp) => {
462
+ let offers = await KeitaroService.getAllOffers()
463
+
464
+ offers = offers.filter((o) => offerNameRegEx.exec(o.name))
465
+
466
+ for (let offer of offers) {
467
+ console.log(offer.name)
468
+ // offer.action_payload = offer.action_payload.replace(old, replace)
469
+
470
+ await sleep(700)
471
+ await KeitaroService.updateOffer(offer)
472
+ }
473
+ }
474
+
475
+ // let gatherInfoForFlashApps = async () => {
476
+ // console.log(__dirname)
477
+ // const pasteMap: PasteMap = JSON.parse(await readFile('/Users/bprtsk/Documents/Work/ASO/AI Control Panel /AI Panel Core/src/utils/map-paste.json', 'utf8'));
478
+ // const trackingMap: TrackingMap = JSON.parse(await readFile('/Users/bprtsk/Documents/Work/ASO/AI Control Panel /AI Panel Core/src/utils/map-tracking.json', 'utf8'));
479
+ // const flashApps: { [key: string]: IFlashApp | undefined } = JSON.parse(await readFile('/Users/bprtsk/Documents/Work/ASO/AI Control Panel /AI Panel Core/src/utils/map-apps.json', 'utf8'));
480
+
481
+ // let allCampaigns = await KeitaroService.getAllCampaigns()
482
+ // let owCampaigns = allCampaigns.filter(c => new RegExp(`^FA #`).exec(c.name)).reduce((acc, obj) => ({ ...acc, [new RegExp(/#(.*) \(/g).exec(obj.name)?.[1] || obj.name]: obj }), {});
483
+ // let redirectCampaigns = allCampaigns.filter(c => new RegExp(`^F #`).exec(c.name)).reduce((acc, obj) => ({ ...acc, [new RegExp(/#(.*) \(/g).exec(obj.name)?.[1] || obj.name]: obj }), {});
484
+
485
+ // let result = []
486
+ // for (let [id, paste] of Object.entries(pasteMap)) {
487
+ // console.log(`- #${id}`)
488
+ // // let ow = owCampaigns[id as keyof object] as IKeitaroCampaign | undefined
489
+ // // let redirect = redirectCampaigns[id as keyof object] as IKeitaroCampaign | undefined
490
+ // // let tracking = trackingMap[id as keyof object]
491
+ // let app = flashApps[id as keyof object]
492
+
493
+ // if (!app) {
494
+ // console.log(`! No app for ${id}`)
495
+ // continue
496
+ // }
497
+
498
+ // // if (paste.status == FlashAppPlugStatus.ENABLED) {
499
+ // // if (!ow) console.log(`#${id}: no OW`)
500
+
501
+ // // if (!redirect) console.log(`No redirect for #${id}`)
502
+ // // else if (!paste.content.includes(redirect.domain)) console.log(`#${id}: Redirect domain (${redirect.domain}) differs from paste domain (${paste.content})`)
503
+ // // else if (!paste.content.includes(redirect.alias)) console.log(`#${id}: Redirect campaign alias (${redirect.alias}) differs from paste id (${paste.content})`)
504
+ // // }
505
+
506
+ // // let appId
507
+ // // for (let parameter of Object.values(redirect?.parameters || {})) {
508
+ // // if (parameter.name == "appId") {
509
+ // // appId = parameter.placeholder
510
+ // // }
511
+ // // }
512
+ // // if (!appId && ow) console.log(`No app id for #${id}`)
513
+ // // else if (appId != id && ow) console.log(`Wrong app id (${appId}) for #${id}`)
514
+
515
+ // // let owDomain = ow?.domain?.split("/")[2]
516
+ // // let owDomainId = ow?.domain_id
517
+ // // let owCampaignId = ow?.id
518
+ // // let owCampaignName = ow?.name
519
+ // // let owCampaignAlias = ow?.alias
520
+ // // let redirectDomain = redirect?.domain?.split("/")[2]
521
+ // // let redirectDomainId = redirect?.domain_id
522
+ // // let redirectCampaignId = redirect?.id
523
+ // // let redirectCampaignName = redirect?.name
524
+ // // let redirectCampaignAlias = redirect?.alias
525
+
526
+ // // let clickIdParameterName = ow?.parameters?.sub_id_8?.name
527
+ // // let offerIdParameterName = ow?.parameters?.sub_id_15?.name
528
+ // // if (!clickIdParameterName && app && ow) console.log(`No click id parameter for #${id}`)
529
+ // // if (!offerIdParameterName && app && ow) console.log(`No offer id parameter for #${id}`)
530
+
531
+ // // let trackingLink = tracking?.trackingUrl
532
+
533
+ // // app.plugStatus = paste.status
534
+ // // app.plugContent = paste.content
535
+ // // app.plugType = PlugType.PASTEBIN
536
+ // // app.plugId = paste.paste_id
537
+ // // app.reservePlugType = PlugType.PASTEBIN
538
+ // // app.plugAccountId = parseInt(paste.accountId)
539
+
540
+ // // app.keitaroData.redirectDomainName = redirectDomain || "none"
541
+ // // app.keitaroData.redirectDomainId = redirectDomainId || 0
542
+ // // app.keitaroData.redirectCampaignId = redirectCampaignId || 0
543
+ // // app.keitaroData.redirectCampaignName = redirectCampaignName || "none"
544
+ // // app.keitaroData.redirectCampaignAlias = redirectCampaignAlias || "none"
545
+ // // app.keitaroData.trackingDomainId = owDomainId || 0
546
+ // // app.keitaroData.trackingDomainName = owDomain || "none"
547
+ // // app.keitaroData.trackingCampaignId = owCampaignId || 0
548
+ // // app.keitaroData.trackingCampaignName = owCampaignName || "none"
549
+ // // app.keitaroData.trackingCampaignAlias = owCampaignAlias || "none"
550
+
551
+ // // if (clickIdParameterName) app.keitaroData.clickIdParameterName = clickIdParameterName
552
+ // // if (offerIdParameterName) app.keitaroData.offerIdParameterName = offerIdParameterName
553
+
554
+ // // if (trackingLink) app.trackingUrl = trackingLink
555
+
556
+ // // if (!owCampaignName?.includes(app.bundle) && ow) {
557
+ // // console.log(`OW: bundle differs (${app.bundle} in DB and ${owCampaignName})`)
558
+ // // }
559
+
560
+ // // if (!redirectCampaignName?.includes(app.bundle) && redirect) {
561
+ // // console.log(`Redirect: bundle differs (${app.bundle} in DB and ${redirectCampaignName})`)
562
+ // // }
563
+
564
+ // result.push(app)
565
+
566
+ // console.log("--------------------")
567
+ // }
568
+
569
+ // await axios.post(`https://aipanel-secondary.com/flash/batch-update`, result, {
570
+ // headers: {
571
+ // "Authorization": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6ImFkbWluIiwiaWF0IjoxNjg3Mjc2NjA0LCJleHAiOjE2ODc4ODE0MDR9.XesrnNGmWlK-yBBMs5TeKvTe07A8Sud7B4MyfZAOMag"
572
+ // }
573
+ // })
574
+
575
+ // // console.log(result)
576
+ // }
577
+
578
+ let findBrokenOfferStreams = async () => {
579
+ let allCampaigns = await KeitaroService.getAllCampaigns()
580
+
581
+ let regexp = new RegExp(`^#`)
582
+ const matchingCampaigns = allCampaigns.filter(campaign => regexp.exec(campaign.name));
583
+ for (let campaign of matchingCampaigns) {
584
+ console.log(campaign.name)
585
+ console.log("––––––––––––––––––––––––")
586
+ let streams = await KeitaroService.getStreamsByCampaignId(campaign.id)
587
+
588
+ for (let stream of streams) {
589
+ if (stream.name == "Bot Protection") continue
590
+
591
+ let idPayload = stream.filters.find(f => f.name == "sub_id_15")
592
+
593
+ if (!idPayload) {
594
+ console.log(`${stream.name}: id payload is missing, payload: ${stream.filters}`)
595
+ }
596
+ }
597
+ }
598
+ }
599
+
600
+ let moveCampaignsToAnotherGroup = async () => {
601
+ const appIds: number[] = [
602
+ 460, 457, 459, 456, 421, 711, 451, 709, 712, 428, 455, 388, 458,
603
+ 355, 341, 342, 334, 431, 403, 436, 365, 393, 801, 702, 448, 444,
604
+ 437, 439, 447, 378, 412, 380, 402, 852, 438
605
+ ]
606
+
607
+ let allCampaigns = await KeitaroService.getAllCampaigns()
608
+ let campaignIds = allCampaigns.filter(c => {
609
+ for (let id of appIds) if (c.name.includes(`#${id}`)) return true
610
+ return false
611
+ }).map(c => c.id)
612
+
613
+ let from = 2, to = 80
614
+
615
+ await KeitaroService.changeCampaignsGroup(from, to, campaignIds)
616
+ }
617
+
618
+ let renameCampaigns = async () => {
619
+ let allCampaigns = await KeitaroService.getAllCampaigns()
620
+
621
+ let regexp = /^#(\d+)/;
622
+
623
+ // Filter campaigns that match the regex
624
+ const matchingCampaigns = allCampaigns.filter(campaign => regexp.test(campaign.name) && campaign.group_id == 80);
625
+ console.log(`Matching campaigns found: ${matchingCampaigns.length}`);
626
+
627
+ for (let campaign of matchingCampaigns) {
628
+ const match = regexp.exec(campaign.name);
629
+ if (match && match[1]) {
630
+ const appId = match[1]; // Extracted number after '#'
631
+ await KeitaroService.updateCampaign(campaign.id, { name: `#${appId} [⛌]` })
632
+ console.log(`Updated "${campaign.name}" to #${appId} [⛌]`)
633
+ }
634
+ }
635
+ }
636
+
637
+ // renameCampaigns()
638
+
639
+ // moveCampaignsToAnotherGroup()
640
+
641
+ // Getting campaign group ids
642
+ // (async () => {
643
+ // let campaigns = await KeitaroService.getAllCampaigns()
644
+ // let normal = campaigns.find(c => c.id == 2684)
645
+ // let archive = campaigns.find(c => c.id == 2679)
646
+
647
+ // console.log([normal?.group_id, archive?.group_id])
648
+ // })()
649
+
650
+ // findBrokenOfferStreams()
651
+
652
+ // removeBrokenDomain()
653
+
654
+ // createFlashCampaigns(sampleApp)
655
+ // changeSourceForFA()
656
+ // addGeosToAllRedirectCam`paigns("GR")
657
+ // removeGeosFromAllRedirectCampaigns("BE`")
658
+
659
+ // gatherInfoForFlashApps()
660
+
661
+ // replaceInOfferLink(`xmariorel.com`, `lalielynaualish.com`, /.*icecasino_huffson.*/g)
662
+ // replaceInOfferLink(`lp=18`, `lp=00`, /.*icecasino_huffson.*/g)
663
+ // let test = async () => console.log(await KeitaroService.getProfitForTodayAndYesterday())
664
+ // test()
665
+
666
+ async function fixLastThreeMonthsClickCosts(): Promise<void> {
667
+ // Get current date
668
+ const now = new Date();
669
+
670
+ // Calculate date 3 months ago
671
+ const threeMonthsAgo = new Date();
672
+ threeMonthsAgo.setMonth(threeMonthsAgo.getMonth() - 3);
673
+
674
+ // Format dates to required format "YYYY-MM-DD HH:MM"
675
+ const startDate = threeMonthsAgo.toISOString().slice(0, 10) + " 00:00";
676
+ const endDate = now.toISOString().slice(0, 10) + " 23:59";
677
+
678
+ console.log(`Fixing click costs from ${startDate} to ${endDate}`);
679
+
680
+ try {
681
+ await KeitaroService.fixBrokenClickCosts(startDate, endDate);
682
+ console.log("Successfully fixed click costs");
683
+ } catch (error) {
684
+ console.error("Failed to fix click costs:", error);
685
+ throw error;
686
+ }
687
+ }
688
+
689
+ // fixLastThreeMonthsClickCosts();
690
+
691
+ async function disableAutoCost(): Promise<void> {
692
+ try {
693
+ // Get all campaigns
694
+ const campaigns = await KeitaroService.getAllCampaigns();
695
+ console.log(`Disabling auto cost for ${campaigns.length} campaigns...`);
696
+
697
+ // Update each campaign
698
+ for (const campaign of campaigns) {
699
+ try {
700
+ await KeitaroService.updateCampaign(campaign.id, { cost_auto: false });
701
+ console.log(`Disabled auto cost for campaign: ${campaign.name} (ID: ${campaign.id})`);
702
+
703
+ // Add a small delay to avoid overwhelming the API
704
+ await sleep(500);
705
+ } catch (error) {
706
+ console.error(`Failed to update campaign ${campaign.name} (ID: ${campaign.id}):`, error);
707
+ }
708
+ }
709
+
710
+ console.log('Successfully disabled auto cost for all campaigns');
711
+ } catch (error) {
712
+ console.error('Failed to disable auto cost:', error);
713
+ throw error;
714
+ }
715
+ }
716
+
717
+ // disableAutoCost();