Repository navigation
Expand file tree
/
Copy pathredeem-code.json
More file actions
1473 lines (1473 loc) · 41.5 KB
/
Copy pathredeem-code.json
File metadata and controls
1473 lines (1473 loc) · 41.5 KB
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{
"name": "2Gatcha - redeem-code",
"nodes": [
{
"id": "webhook",
"name": "Webhook",
"webhookId": "8b396b1f-333b-4fa6-8b97-c49486d96037",
"type": "n8n-nodes-base.webhook",
"typeVersion": 2,
"position": [
-1600,
300
],
"parameters": {
"httpMethod": "POST",
"path": "redeem-code",
"responseMode": "responseNode",
"options": {}
}
},
{
"id": "set-config",
"name": "Set Config",
"type": "n8n-nodes-base.code",
"typeVersion": 2,
"position": [
-1380,
300
],
"parameters": {
"mode": "runOnceForAllItems",
"jsCode": "// A adapter une seule fois : ID du document Grist, webhook Discord (optionnel).\nconst discordWebhookUrl = 'https://discord.com/api/webhooks/1551618475179511940/BsIPFGMDe0jiylrnEGTluRaPCP3zEi2tEWangoaZHgT_uShe2Zyy4dgTIRkLRjQ1cxZW';\nconst body = $input.first().json.body || {};\nreturn [{\n json: {\n docId: 'cRP1WgZQv7KmwmVrUbuEF8',\n userId: Number(body.userId),\n code: (body.code || '').toString().trim(),\n discordWebhookUrl\n }\n}];"
}
},
{
"id": "get-event-codes",
"name": "Get Event Codes",
"type": "n8n-nodes-base.grist",
"typeVersion": 1,
"alwaysOutputData": true,
"position": [
-1160,
180
],
"parameters": {
"operation": "getAll",
"docId": "={{ $json.docId }}",
"tableId": "EventCodes",
"returnAll": true,
"additionalOptions": {}
},
"credentials": {
"gristApi": {
"id": "Yoc8FS9lCXpWBRrk",
"name": "Grist account"
}
}
},
{
"id": "get-code-redemptions",
"name": "Get Code Redemptions",
"type": "n8n-nodes-base.grist",
"typeVersion": 1,
"alwaysOutputData": true,
"position": [
-1160,
320
],
"parameters": {
"operation": "getAll",
"docId": "={{ $('Set Config').first().json.docId }}",
"tableId": "CodeRedemptions",
"returnAll": true,
"additionalOptions": {}
},
"credentials": {
"gristApi": {
"id": "Yoc8FS9lCXpWBRrk",
"name": "Grist account"
}
}
},
{
"id": "get-cards",
"name": "Get Cards",
"type": "n8n-nodes-base.grist",
"typeVersion": 1,
"alwaysOutputData": true,
"position": [
-1160,
460
],
"parameters": {
"operation": "getAll",
"docId": "={{ $('Set Config').first().json.docId }}",
"tableId": "Cards",
"returnAll": true,
"additionalOptions": {}
},
"credentials": {
"gristApi": {
"id": "Yoc8FS9lCXpWBRrk",
"name": "Grist account"
}
}
},
{
"id": "get-rarities",
"name": "Get Rarities",
"type": "n8n-nodes-base.grist",
"typeVersion": 1,
"alwaysOutputData": true,
"position": [
-1160,
600
],
"parameters": {
"operation": "getAll",
"docId": "={{ $('Set Config').first().json.docId }}",
"tableId": "Rarities",
"returnAll": true,
"additionalOptions": {}
},
"credentials": {
"gristApi": {
"id": "Yoc8FS9lCXpWBRrk",
"name": "Grist account"
}
}
},
{
"id": "get-users",
"name": "Get Users",
"type": "n8n-nodes-base.grist",
"typeVersion": 1,
"alwaysOutputData": true,
"position": [
-1160,
740
],
"parameters": {
"operation": "getAll",
"docId": "={{ $('Set Config').first().json.docId }}",
"tableId": "Users",
"returnAll": true,
"additionalOptions": {}
},
"credentials": {
"gristApi": {
"id": "Yoc8FS9lCXpWBRrk",
"name": "Grist account"
}
}
},
{
"id": "validate-prepare",
"name": "Validate & Prepare",
"type": "n8n-nodes-base.code",
"typeVersion": 2,
"position": [
-940,
300
],
"parameters": {
"mode": "runOnceForAllItems",
"jsCode": "function refId(v) { return Array.isArray(v) ? v[1] : v; }\nfunction firstAttachment(v) {\n if (!Array.isArray(v) || !v.length) return null;\n const ids = v[0] === 'L' ? v.slice(1) : v;\n return ids.length ? ids[0] : null;\n}\n\nconst config = $('Set Config').first().json;\nconst codesRows = $('Get Event Codes').all().map((i) => i.json).filter((r) => r.id != null);\nconst redemptionsRows = $('Get Code Redemptions').all().map((i) => i.json).filter((r) => r.id != null);\nconst cardsRows = $('Get Cards').all().map((i) => i.json).filter((r) => r.id != null);\nconst raritiesRows = $('Get Rarities').all().map((i) => i.json).filter((r) => r.id != null);\nconst usersRows = $('Get Users').all().map((i) => i.json).filter((r) => r.id != null);\nconst allPulls = $('Get All Pulls').all().map((i) => i.json).filter((r) => r.id != null);\n\nif (!config.code) {\n return [{ json: { ok: false, error: 'invalid_code' } }];\n}\nconst normalized = config.code.toLowerCase();\nconst codeRow = codesRows.find((c) => (c.Code || '').toString().trim().toLowerCase() === normalized);\n\nif (!codeRow) {\n return [{ json: { ok: false, error: 'invalid_code' } }];\n}\nif (codeRow.Active === false) {\n return [{ json: { ok: false, error: 'code_inactive' } }];\n}\nconst now = Math.floor(Date.now() / 1000);\nif (codeRow.ExpiresAt && now > codeRow.ExpiresAt) {\n return [{ json: { ok: false, error: 'code_expired' } }];\n}\n\nconst codeRedemptions = redemptionsRows.filter((r) => refId(r.Code) === codeRow.id);\nif (codeRedemptions.some((r) => refId(r.User) === config.userId)) {\n return [{ json: { ok: false, error: 'already_redeemed' } }];\n}\nif (codeRow.MaxRedemptions && codeRedemptions.length >= codeRow.MaxRedemptions) {\n return [{ json: { ok: false, error: 'code_exhausted' } }];\n}\n\nconst user = usersRows.find((u) => u.id === config.userId);\nif (!user) {\n return [{ json: { ok: false, error: 'unknown_user' } }];\n}\n\nconst quantity = codeRow.Quantity || 1;\nconst batchId = `code-${codeRow.Code}-${Date.now()}`;\n\nif (codeRow.RewardType === 'card') {\n const card = cardsRows.find((c) => c.id === refId(codeRow.CardId));\n if (!card) {\n return [{ json: { ok: false, error: 'code_misconfigured' } }];\n }\n const rarityRow = raritiesRows.find((r) => r.id === refId(card.Rarity));\n const rarity = rarityRow ? { id: rarityRow.id, name: rarityRow.Name, key: rarityRow.Key, colorHex: rarityRow.ColorHex } : null;\n\n // Plus de quota (Cards.MaxSerial) : un code peut toujours etre reclame.\n // On suit QUELS numeros sont utilises (pas juste un compteur) pour\n // reattribuer les plus petits numeros libres - un exemplaire decrafte/\n // defausse libere vraiment son numero pour un futur don, sans jamais\n // dupliquer un numero encore en circulation.\n const usedSerials = new Set();\n allPulls.forEach((p) => { if (refId(p.Card) === card.id && p.SerialNumber != null) usedSerials.add(p.SerialNumber); });\n function takeNextSerial() {\n for (let n = 1; ; n++) {\n if (!usedSerials.has(n)) { usedSerials.add(n); return n; }\n }\n }\n\n const maxRedemptions = codeRow.MaxRedemptions || 0;\n const items = [];\n for (let i = 0; i < quantity; i++) {\n items.push({\n json: {\n ok: true,\n kind: 'card',\n docId: config.docId,\n codeRowId: codeRow.id,\n maxRedemptions,\n userId: config.userId,\n obtainedAt: now,\n batchId,\n cardId: card.id,\n isFirstEver: !card.FirstObtainedBy,\n name: card.Name,\n artist: card.Artist,\n description: card.Description,\n imageId: firstAttachment(card.Image),\n rarity,\n serialNumber: takeNextSerial()\n }\n });\n }\n return items;\n}\n\nreturn [{\n json: {\n ok: true,\n kind: 'booster',\n docId: config.docId,\n codeRowId: codeRow.id,\n maxRedemptions: codeRow.MaxRedemptions || 0,\n userId: config.userId,\n quantity,\n oldBoosterCount: user.BoosterCount || 0,\n newBoosterCount: (user.BoosterCount || 0) + quantity\n }\n}];"
}
},
{
"id": "if-valid",
"name": "If Valid",
"type": "n8n-nodes-base.if",
"typeVersion": 2,
"position": [
-720,
300
],
"parameters": {
"conditions": {
"options": {
"caseSensitive": true,
"leftValue": "",
"typeValidation": "strict"
},
"conditions": [
{
"leftValue": "={{ $json.ok }}",
"rightValue": true,
"operator": {
"type": "boolean",
"operation": "true"
}
}
],
"combinator": "and"
},
"options": {}
}
},
{
"id": "respond-error",
"name": "Respond Error",
"type": "n8n-nodes-base.respondToWebhook",
"typeVersion": 1.1,
"position": [
-500,
620
],
"parameters": {
"respondWith": "json",
"responseBody": "={{ { error: $json.error } }}",
"options": {
"responseCode": 400
}
}
},
{
"id": "if-is-card",
"name": "If Is Card",
"type": "n8n-nodes-base.if",
"typeVersion": 2,
"position": [
-500,
220
],
"parameters": {
"conditions": {
"options": {
"caseSensitive": true,
"leftValue": "",
"typeValidation": "strict"
},
"conditions": [
{
"leftValue": "={{ $json.kind }}",
"rightValue": "card",
"operator": {
"type": "string",
"operation": "equals"
}
}
],
"combinator": "and"
},
"options": {}
}
},
{
"id": "insert-pulls",
"name": "Insert Pulls",
"type": "n8n-nodes-base.grist",
"typeVersion": 1,
"position": [
-280,
80
],
"parameters": {
"operation": "create",
"docId": "={{ $('Validate & Prepare').first().json.docId }}",
"tableId": "Pulls",
"dataToSend": "defineInNode",
"fieldsToSend": {
"properties": [
{
"fieldId": "User",
"fieldValue": "={{ $json.userId }}"
},
{
"fieldId": "Card",
"fieldValue": "={{ $json.cardId }}"
},
{
"fieldId": "ObtainedAt",
"fieldValue": "={{ $json.obtainedAt }}"
},
{
"fieldId": "BatchId",
"fieldValue": "={{ $json.batchId }}"
},
{
"fieldId": "SerialNumber",
"fieldValue": "={{ $json.serialNumber }}"
}
]
}
},
"credentials": {
"gristApi": {
"id": "Yoc8FS9lCXpWBRrk",
"name": "Grist account"
}
}
},
{
"id": "finalize-card",
"name": "Finalize Card",
"type": "n8n-nodes-base.code",
"typeVersion": 2,
"position": [
-60,
80
],
"parameters": {
"mode": "runOnceForAllItems",
"jsCode": "const cards = $('Validate & Prepare').all().map((i) => i.json).map((c) => ({\n cardId: c.cardId,\n name: c.name,\n artist: c.artist,\n description: c.description,\n imageId: c.imageId,\n rarity: c.rarity,\n isFirstEver: !!c.isFirstEver,\n serialNumber: c.serialNumber\n}));\nconst first = $('Validate & Prepare').first().json;\nconst pullRowIds = $('Insert Pulls').all().map((i) => i.json.id);\nreturn [{ json: { kind: 'card', codeRowId: first.codeRowId, userId: first.userId, docId: first.docId, maxRedemptions: first.maxRedemptions, pullRowIds, cards } }];"
}
},
{
"id": "insert-redemption-card",
"name": "Insert Redemption Card",
"type": "n8n-nodes-base.grist",
"typeVersion": 1,
"position": [
160,
80
],
"parameters": {
"operation": "create",
"docId": "={{ $json.docId }}",
"tableId": "CodeRedemptions",
"dataToSend": "defineInNode",
"fieldsToSend": {
"properties": [
{
"fieldId": "Code",
"fieldValue": "={{ $json.codeRowId }}"
},
{
"fieldId": "User",
"fieldValue": "={{ $json.userId }}"
},
{
"fieldId": "RedeemedAt",
"fieldValue": "={{ Math.floor(Date.now() / 1000) }}"
}
]
}
},
"credentials": {
"gristApi": {
"id": "Yoc8FS9lCXpWBRrk",
"name": "Grist account"
}
}
},
{
"id": "respond-card",
"name": "Respond Card",
"type": "n8n-nodes-base.respondToWebhook",
"typeVersion": 1.1,
"position": [
380,
80
],
"parameters": {
"respondWith": "json",
"responseBody": "={{ { type: 'card', cards: $('Finalize Card').first().json.cards } }}",
"options": {}
}
},
{
"id": "update-booster",
"name": "Update Booster",
"type": "n8n-nodes-base.grist",
"typeVersion": 1,
"position": [
-280,
360
],
"parameters": {
"operation": "update",
"docId": "={{ $json.docId }}",
"tableId": "Users",
"rowId": "={{ $json.userId }}",
"dataToSend": "defineInNode",
"fieldsToSend": {
"properties": [
{
"fieldId": "BoosterCount",
"fieldValue": "={{ $json.newBoosterCount }}"
}
]
}
},
"credentials": {
"gristApi": {
"id": "Yoc8FS9lCXpWBRrk",
"name": "Grist account"
}
}
},
{
"id": "insert-redemption-booster",
"name": "Insert Redemption Booster",
"type": "n8n-nodes-base.grist",
"typeVersion": 1,
"position": [
-60,
360
],
"parameters": {
"operation": "create",
"docId": "={{ $('Validate & Prepare').first().json.docId }}",
"tableId": "CodeRedemptions",
"dataToSend": "defineInNode",
"fieldsToSend": {
"properties": [
{
"fieldId": "Code",
"fieldValue": "={{ $('Validate & Prepare').first().json.codeRowId }}"
},
{
"fieldId": "User",
"fieldValue": "={{ $('Validate & Prepare').first().json.userId }}"
},
{
"fieldId": "RedeemedAt",
"fieldValue": "={{ Math.floor(Date.now() / 1000) }}"
}
]
}
},
"credentials": {
"gristApi": {
"id": "Yoc8FS9lCXpWBRrk",
"name": "Grist account"
}
}
},
{
"id": "respond-booster",
"name": "Respond Booster",
"type": "n8n-nodes-base.respondToWebhook",
"typeVersion": 1.1,
"position": [
160,
360
],
"parameters": {
"respondWith": "json",
"responseBody": "={{ { type: 'booster', quantity: $('Validate & Prepare').first().json.quantity, newBoosterCount: $('Validate & Prepare').first().json.newBoosterCount } }}",
"options": {}
}
},
{
"id": "barrier-7",
"name": "Sync Get Event Codes -> Get Code Redemptions",
"type": "n8n-nodes-base.code",
"typeVersion": 2,
"position": [
-1120,
240
],
"parameters": {
"mode": "runOnceForAllItems",
"jsCode": "// n8n execute un node Grist 'classique' une fois PAR ITEM recu en entree.\n// Sans ce noeud-tampon, ce node getAll recevrait plusieurs items du node\n// precedent et s'executerait plusieurs fois, dupliquant ses resultats.\n// On reduit systematiquement a 1 seul item entre deux appels Grist chaines.\nreturn [{ json: {} }];"
}
},
{
"id": "barrier-8",
"name": "Sync Get Code Redemptions -> Get Cards",
"type": "n8n-nodes-base.code",
"typeVersion": 2,
"position": [
-1120,
380
],
"parameters": {
"mode": "runOnceForAllItems",
"jsCode": "// n8n execute un node Grist 'classique' une fois PAR ITEM recu en entree.\n// Sans ce noeud-tampon, ce node getAll recevrait plusieurs items du node\n// precedent et s'executerait plusieurs fois, dupliquant ses resultats.\n// On reduit systematiquement a 1 seul item entre deux appels Grist chaines.\nreturn [{ json: {} }];"
}
},
{
"id": "barrier-9",
"name": "Sync Get Cards -> Get Rarities",
"type": "n8n-nodes-base.code",
"typeVersion": 2,
"position": [
-1120,
520
],
"parameters": {
"mode": "runOnceForAllItems",
"jsCode": "// n8n execute un node Grist 'classique' une fois PAR ITEM recu en entree.\n// Sans ce noeud-tampon, ce node getAll recevrait plusieurs items du node\n// precedent et s'executerait plusieurs fois, dupliquant ses resultats.\n// On reduit systematiquement a 1 seul item entre deux appels Grist chaines.\nreturn [{ json: {} }];"
}
},
{
"id": "barrier-10",
"name": "Sync Get Rarities -> Get Users",
"type": "n8n-nodes-base.code",
"typeVersion": 2,
"position": [
-1120,
660
],
"parameters": {
"mode": "runOnceForAllItems",
"jsCode": "// n8n execute un node Grist 'classique' une fois PAR ITEM recu en entree.\n// Sans ce noeud-tampon, ce node getAll recevrait plusieurs items du node\n// precedent et s'executerait plusieurs fois, dupliquant ses resultats.\n// On reduit systematiquement a 1 seul item entre deux appels Grist chaines.\nreturn [{ json: {} }];"
}
},
{
"id": "if-is-first-ever",
"name": "If Is First Ever",
"type": "n8n-nodes-base.if",
"typeVersion": 2,
"position": [
400,
700
],
"parameters": {
"conditions": {
"options": {
"caseSensitive": true,
"leftValue": "",
"typeValidation": "strict"
},
"conditions": [
{
"leftValue": "={{ $json.isFirstEver }}",
"rightValue": true,
"operator": {
"type": "boolean",
"operation": "true"
}
}
],
"combinator": "and"
},
"options": {}
}
},
{
"id": "update-card-first-obtained",
"name": "Update Card First Obtained",
"type": "n8n-nodes-base.grist",
"typeVersion": 1,
"position": [
580,
700
],
"parameters": {
"operation": "update",
"docId": "={{ $('Set Config').first().json.docId }}",
"tableId": "Cards",
"rowId": "={{ $json.cardId }}",
"dataToSend": "defineInNode",
"fieldsToSend": {
"properties": [
{
"fieldId": "FirstObtainedBy",
"fieldValue": "={{ $json.userId }}"
},
{
"fieldId": "FirstObtainedAt",
"fieldValue": "={{ Math.floor(Date.now() / 1000) }}"
}
]
}
},
"credentials": {
"gristApi": {
"id": "Yoc8FS9lCXpWBRrk",
"name": "Grist account"
}
}
},
{
"id": "build-notify-payload",
"name": "Build Notify Payload",
"type": "n8n-nodes-base.code",
"typeVersion": 2,
"position": [
780,
820
],
"parameters": {
"mode": "runOnceForAllItems",
"jsCode": "const config = $('Set Config').first().json;\nconst final = $('Finalize Card').first().json;\nconst users = $('Get Users').all().map((i) => i.json).filter((r) => r.id != null);\nconst user = users.find((u) => u.id === final.userId);\nconst pseudo = (user && user.Pseudo) || 'Un joueur';\nconst discordMention = user && user.DiscordId ? `<@${user.DiscordId}>` : `**${pseudo}**`;\nfunction hexToInt(hex) { return parseInt((hex || '9aa0b4').replace('#', ''), 16) || 0x9aa0b4; }\n\nconst legendary = (final.cards || []).filter((c) => c.rarity && c.rarity.key === 'legendaire');\nconst epic = (final.cards || []).filter((c) => c.rarity && c.rarity.key === 'epique');\n\nlet content = '';\nlet embeds = [];\nif (legendary.length) {\n content = `\\u{1F31F} ${discordMention} vient de decrocher une carte legendaire (code) !`;\n embeds = [{ color: hexToInt(legendary[0].rarity.colorHex), title: '\\u{1F451} ' + legendary.map((c) => c.name).join(', '), description: `Rarete : **${legendary[0].rarity.name}**` }];\n} else if (epic.length) {\n content = `\\u2728 **${pseudo}** vient d'obtenir une carte epique (code).`;\n embeds = [{ color: hexToInt(epic[0].rarity.colorHex), title: epic.map((c) => c.name).join(', '), description: `Rarete : **${epic[0].rarity.name}**` }];\n}\n\nconst shouldNotify = (legendary.length > 0 || epic.length > 0) && !!config.discordWebhookUrl && !config.discordWebhookUrl.startsWith('CHANGE-MOI');\n\nreturn [{ json: { shouldNotify, webhookUrl: config.discordWebhookUrl, content, embeds } }];"
}
},
{
"id": "if-should-notify",
"name": "If Should Notify",
"type": "n8n-nodes-base.if",
"typeVersion": 2,
"position": [
960,
820
],
"parameters": {
"conditions": {
"options": {
"caseSensitive": true,
"leftValue": "",
"typeValidation": "strict"
},
"conditions": [
{
"leftValue": "={{ $json.shouldNotify }}",
"rightValue": true,
"operator": {
"type": "boolean",
"operation": "true"
}
}
],
"combinator": "and"
},
"options": {}
}
},
{
"id": "notify-discord",
"name": "Notify Discord",
"type": "n8n-nodes-base.httpRequest",
"typeVersion": 4.2,
"position": [
1140,
820
],
"parameters": {
"method": "POST",
"url": "={{ $json.webhookUrl }}",
"sendBody": true,
"contentType": "json",
"specifyBody": "json",
"jsonBody": "={{ JSON.stringify({ content: $json.content, embeds: $json.embeds }) }}",
"options": {}
}
},
{
"id": "sync-users-allpulls-redeem",
"name": "Sync Get Users -> Get All Pulls",
"type": "n8n-nodes-base.code",
"typeVersion": 2,
"position": [
-1080,
740
],
"parameters": {
"mode": "runOnceForAllItems",
"jsCode": "return [{ json: {} }];"
}
},
{
"id": "get-all-pulls-redeem",
"name": "Get All Pulls",
"type": "n8n-nodes-base.grist",
"typeVersion": 1,
"alwaysOutputData": true,
"position": [
-1000,
740
],
"parameters": {
"operation": "getAll",
"docId": "={{ $('Set Config').first().json.docId }}",
"tableId": "Pulls",
"returnAll": true,
"additionalOptions": {}
},
"credentials": {
"gristApi": {
"id": "Yoc8FS9lCXpWBRrk",
"name": "Grist account"
}
}
},
{
"id": "wait-before-recheck",
"name": "Wait Before Recheck",
"type": "n8n-nodes-base.code",
"typeVersion": 2,
"position": [
1400,
260
],
"parameters": {
"mode": "runOnceForAllItems",
"jsCode": "// BUG REEL corrige ici (2026-09-30) : \"Decide Redemption Outcome\" essayait\n// de distinguer la branche carte/booster en lisant $('Insert Redemption\n// Card')/$('Insert Redemption Booster') - mais n8n leve une erreur reelle\n// des qu'on reference un noeud qui n'a PAS execute sur le chemin emprunte\n// (une seule des deux branches tourne jamais par execution), pas juste une\n// liste vide comme je l'avais suppose. On capture donc l'id de la ligne de\n// reclamation ICI (le seul point ou les deux branches se rejoignent), et on\n// determine card/booster via Validate & Prepare (qui tourne TOUJOURS) plus\n// loin plutot que via les noeuds Insert conditionnels.\nconst myRedemptionId = $input.first().json.id;\nawait new Promise((resolve) => setTimeout(resolve, 400));\nreturn [{ json: { myRedemptionId } }];"
}
},
{
"id": "sync-wait-recheck",
"name": "Sync Wait -> Recheck",
"type": "n8n-nodes-base.code",
"typeVersion": 2,
"position": [
1600,
260
],
"parameters": {
"mode": "runOnceForAllItems",
"jsCode": "return $input.all();"
}
},
{
"id": "recheck-code-redemptions",
"name": "Recheck Code Redemptions",
"type": "n8n-nodes-base.grist",
"typeVersion": 1,
"alwaysOutputData": true,
"position": [
1800,
260
],
"parameters": {
"operation": "getAll",
"docId": "={{ $('Validate & Prepare').first().json.docId }}",
"tableId": "CodeRedemptions",
"returnAll": true,
"additionalOptions": {}
},
"credentials": {
"gristApi": {
"id": "Yoc8FS9lCXpWBRrk",
"name": "Grist account"
}
}
},
{
"id": "sync-recheck-decide",
"name": "Sync Recheck -> Decide",
"type": "n8n-nodes-base.code",
"typeVersion": 2,
"position": [
2000,
260
],
"parameters": {
"mode": "runOnceForAllItems",
"jsCode": "return [{ json: {} }];"
}
},
{
"id": "decide-redemption-outcome",
"name": "Decide Redemption Outcome",
"type": "n8n-nodes-base.code",
"typeVersion": 2,
"position": [
2200,
260
],
"parameters": {
"mode": "runOnceForAllItems",
"jsCode": "function refId(v) { return Array.isArray(v) ? v[1] : v; }\nconst prep = $('Validate & Prepare').first().json;\nconst isCard = prep.kind === 'card';\nconst myRedemptionId = $('Wait Before Recheck').first().json.myRedemptionId;\nconst codeRowId = prep.codeRowId;\nconst maxRedemptions = prep.maxRedemptions || 0;\nconst docId = prep.docId;\n\nconst allRedemptions = $('Recheck Code Redemptions').all().map((i) => i.json).filter((r) => r.id != null && refId(r.Code) === codeRowId);\nallRedemptions.sort((a, b) => a.id - b.id);\nconst winners = maxRedemptions ? allRedemptions.slice(0, maxRedemptions) : allRedemptions;\nconst iWon = !maxRedemptions || winners.some((r) => r.id === myRedemptionId);\n\nif (iWon) {\n return [{ json: { rollback: false, isCard, docId } }];\n}\n\nif (isCard) {\n const finalize = $('Finalize Card').first().json;\n return [{ json: { rollback: true, isCard: true, docId, redemptionRowId: myRedemptionId, pullRowIds: finalize.pullRowIds || [] } }];\n}\nreturn [{ json: { rollback: true, isCard: false, docId, redemptionRowId: myRedemptionId, userId: prep.userId, oldBoosterCount: prep.oldBoosterCount } }];"
}
},
{
"id": "if-rollback-needed",
"name": "If Rollback Needed",
"type": "n8n-nodes-base.if",
"typeVersion": 2,
"position": [
2400,
260
],
"parameters": {
"conditions": {
"options": {
"caseSensitive": true,
"leftValue": "",
"typeValidation": "strict"
},
"conditions": [
{
"leftValue": "={{ $json.rollback }}",
"rightValue": true,
"operator": {
"type": "boolean",
"operation": "true"
}
}
],
"combinator": "and"
},
"options": {}
}
},
{
"id": "if-is-card-success",
"name": "If Is Card Success",
"type": "n8n-nodes-base.if",
"typeVersion": 2,
"position": [
2600,
160
],
"parameters": {
"conditions": {
"options": {
"caseSensitive": true,
"leftValue": "",
"typeValidation": "strict"
},
"conditions": [
{
"leftValue": "={{ $('Decide Redemption Outcome').first().json.isCard }}",
"rightValue": true,
"operator": {
"type": "boolean",
"operation": "true"
}
}
],
"combinator": "and"
},
"options": {}
}
},
{
"id": "delete-my-redemption-row",
"name": "Delete My Redemption Row",
"type": "n8n-nodes-base.grist",
"typeVersion": 1,
"position": [
2600,
360
],
"parameters": {
"operation": "delete",
"docId": "={{ $('Decide Redemption Outcome').first().json.docId }}",
"tableId": "CodeRedemptions",
"rowId": "={{ $('Decide Redemption Outcome').first().json.redemptionRowId }}"
},
"credentials": {
"gristApi": {
"id": "Yoc8FS9lCXpWBRrk",
"name": "Grist account"
}
}
},
{
"id": "sync-delete-redemption-rollback",
"name": "Sync Delete Redemption -> Rollback Type",
"type": "n8n-nodes-base.code",
"typeVersion": 2,
"position": [
2800,
360
],
"parameters": {
"mode": "runOnceForAllItems",
"jsCode": "return [{ json: {} }];"
}
},
{
"id": "if-is-card-rollback",
"name": "If Is Card Rollback",
"type": "n8n-nodes-base.if",
"typeVersion": 2,
"position": [
3000,
360
],
"parameters": {
"conditions": {
"options": {
"caseSensitive": true,
"leftValue": "",
"typeValidation": "strict"
},
"conditions": [
{
"leftValue": "={{ $('Decide Redemption Outcome').first().json.isCard }}",
"rightValue": true,
"operator": {
"type": "boolean",
"operation": "true"
}
}
],
"combinator": "and"
},
"options": {}
}
},
{
"id": "expand-pull-deletes",
"name": "Expand Pull Deletes",
"type": "n8n-nodes-base.code",
"typeVersion": 2,
"position": [
3200,
280
],
"parameters": {
"mode": "runOnceForAllItems",
"jsCode": "const d = $('Decide Redemption Outcome').first().json;\nreturn (d.pullRowIds || []).map((id) => ({ json: { docId: d.docId, pullRowId: id } }));"
}
},
{
"id": "delete-pull-rollback",
"name": "Delete Pull (Rollback)",
"type": "n8n-nodes-base.grist",
"typeVersion": 1,
"position": [
3400,
280
],
"parameters": {
"operation": "delete",
"docId": "={{ $json.docId }}",
"tableId": "Pulls",
"rowId": "={{ $json.pullRowId }}"
},
"credentials": {
"gristApi": {
"id": "Yoc8FS9lCXpWBRrk",
"name": "Grist account"
}
}
},
{
"id": "revert-booster-count",
"name": "Revert Booster Count",
"type": "n8n-nodes-base.grist",
"typeVersion": 1,
"position": [
3200,
440
],
"parameters": {
"operation": "update",
"docId": "={{ $('Decide Redemption Outcome').first().json.docId }}",
"tableId": "Users",
"rowId": "={{ $('Decide Redemption Outcome').first().json.userId }}",
"dataToSend": "defineInNode",
"fieldsToSend": {
"properties": [
{
"fieldId": "BoosterCount",
"fieldValue": "={{ $('Decide Redemption Outcome').first().json.oldBoosterCount }}"
}
]
}
},
"credentials": {
"gristApi": {
"id": "Yoc8FS9lCXpWBRrk",
"name": "Grist account"
}
}
},
{
"id": "sync-rollback-format-exhausted",
"name": "Sync Rollback -> Format Exhausted",
"type": "n8n-nodes-base.code",
"typeVersion": 2,
"position": [
3600,
360
],
"parameters": {
"mode": "runOnceForAllItems",
"jsCode": "return [{ json: {} }];"
}
},
{
"id": "format-exhausted",
"name": "Format Exhausted",
"type": "n8n-nodes-base.code",
"typeVersion": 2,
"position": [
3800,
360
],
"parameters": {
"mode": "runOnceForAllItems",
"jsCode": "return [{ json: { error: 'code_exhausted' } }];"
}
},
{
"id": "respond-exhausted",
"name": "Respond Exhausted",
"type": "n8n-nodes-base.respondToWebhook",
"typeVersion": 1.1,
"position": [
4000,
360
],
"parameters": {
"respondWith": "json",
"responseBody": "={{ $json }}",
"options": {
"responseCode": 400
}
}
}
],
"connections": {
"Webhook": {
"main": [
[
{
"node": "Set Config",
"type": "main",
"index": 0
}
]