-
Notifications
You must be signed in to change notification settings - Fork 134
Expand file tree
/
Copy pathold_transactions.ts
More file actions
1191 lines (1102 loc) · 96.8 KB
/
Copy pathold_transactions.ts
File metadata and controls
1191 lines (1102 loc) · 96.8 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
/**
* Transactions routes ΓÇö unified, paginated transaction feed.
*
* ## Merge strategy
* The module fetches `queryLimit` rows from each of five entity tables
* (payments, escrow events, agreement events, employees, milestones) in
* parallel, merges them in application code, sorts by `createdAt` desc +
* `txHash` for stable tie-breaking, then slices for the requested page.
* This guarantees each entity type is represented in the merged feed.
*
* ## Address field contract
* The `address` field in every `TransactionItem` represents the **other
* party** involved in the event, relative to the requesting user. The
* resolution logic differs per entity type and is documented inline in
* the `fetchAndBuildTransactions` merge section.
*
* ## Deduplication
* The main endpoint passes `{ deduplicateAgreementEvents: true }` so that
* agreement events with duplicate `id` values are collapsed to one row.
* The filtered endpoint does not deduplicate.
*
* ## Employee condition mode
* The main endpoint matches employees where the user is **either** the
* employer or the employee. The filtered endpoint restricts to rows where
* the user **is** the employee (`employee-only`).
*/
import { Router } from "express";
import { z } from "zod";
import { db, schema } from "../db/index.js";
import { eq, and, or, desc, gte, lte, inArray, sql, count } from "drizzle-orm";
import { agreementContract } from "../starknet/client.js";
import { toHexString } from "../utils/codec.js";
import { normalizeStarknetAddress as normalizeAddr } from "../utils/address.js";
import { env } from "../config.js";
import {
formatTokenAmount,
getTokenInfo as resolveTokenInfo,
type TokenInfo,
} from "../utils/token-formatting.js";
// ΓöÇΓöÇ Types ΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇ
/**
* Shape of each transaction item in the API response.
*
* Every field is guaranteed present (never `undefined`), though some may hold
* placeholder values (`"-"` for token/amount, `""` for tokenIcon) when the
* information is not available for a given entity type.
*/
interface TransactionItem {
id: string;
type: string;
address: string;
date: string;
time: string;
token: string;
amount: string;
status: "Completed";
tokenIcon: string;
txHash: string;
createdAt: Date;
}
/** Pagination result returned by both transaction endpoints. */
interface TransactionResponse {
transactions: TransactionItem[];
total: number;
hasMore: boolean;
limit: number;
offset: number;
}
/** Optional filters accepted by the condition builder. */
interface TransactionFilters {
startDate?: Date;
endDate?: Date;
eventTypes?: string[];
}
// ΓöÇΓöÇ Router ΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇ
export const transactionsRouter = Router();
export interface TransactionRecord {
id: string;
type: string;
address: string;
date: string;
time: string;
token: string;
amount: string;
status: "Completed";
tokenIcon: string;
txHash: string;
createdAt: Date;
}
export interface TransactionExport {
transactions: TransactionRecord[];
total: number;
hasMore: boolean;
limit: number;
offset: number;
}
/**
* Explicit allowlist of every event-type value that may appear in the
* `eventTypes` query parameter. Values outside this set are rejected before
* they reach any DB call, so callers cannot inject arbitrary strings into
* the `inArray()` filter or probe for unknown table values.
*/
const ALLOWED_EVENT_TYPES = new Set([
// WorkAgreement events
"AgreementCreated",
"AgreementActivated",
"AgreementPaused",
"AgreementResumed",
"AgreementCancelled",
"AgreementCompleted",
"AgreementStatusChange",
"PaymentSent",
"PaymentReceived",
"MilestoneAdded",
"MilestoneApproved",
"MilestoneClaimed",
"EmployeeAdded",
"PayrollClaimed",
"DisputeRaised",
"DisputeResolved",
// PayrollEscrow events
"Funded",
"Released",
"Refunded",
]);
/**
* Emits verbose token-matching and fetch diagnostics only when LOG_LEVEL is set
* to "debug". These lines are noisy on the request hot path and can include
* token addresses, so at the default "info" level, and in production, they stay
* silent: this keeps sensitive routing data out of default-level logs and stops
* the per-request flood that previously ran on every transaction list. Genuine
* failures still use console.error and console.warn so errors stay visible.
*/
function debugLog(...args: unknown[]): void {
if (env.LOG_LEVEL === "debug") {
console.debug(...args);
}
}
// ΓöÇΓöÇ Address formatting ΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇ
/**
* Truncates a Starknet address for display (e.g. `0x1234...5678`).
*
* - Returns the original value unchanged when it is falsy or `"N/A"`.
* - If the normalized address is 10 characters or shorter, returns it whole.
* - Otherwise returns the `0x` prefix + first 6 hex chars + `...` + last 4 hex chars.
*/
function formatAddress(addr: string): string {
if (!addr || addr === "N/A") return addr;
const normalized = normalizeAddr(addr);
if (normalized.length <= 10) return normalized;
return `${normalized.slice(0, 6)}...${normalized.slice(-4)}`;
}
// ΓöÇΓöÇ Token configuration (evaluated once at module load) ΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇ
const STRK_TOKEN_ADDRESS =
env.TOKEN_STRK ||
"0x04718f5a0fc34cc1af16a1cdee98ffb20c31f5cd61d6ab07201858f4287c938d";
const USDC_TOKEN_ADDRESS =
env.TOKEN_USDC ||
"0x053b40a647cedfca6ca84f542a0fe36736031905a9639a7f19a3c1e66bfd5080";
const USDT_TOKEN_ADDRESS =
env.TOKEN_USDT ||
"0x02ab8758891e84b968ff11361789070c6b1af2df618d6d2f4a78b0757573c6eb";
const NORMALIZED_STRK = normalizeAddr(STRK_TOKEN_ADDRESS);
const NORMALIZED_USDC = normalizeAddr(USDC_TOKEN_ADDRESS);
const NORMALIZED_USDT = normalizeAddr(USDT_TOKEN_ADDRESS);
debugLog(`[transactions] Known token addresses configured:`);
debugLog(` - STRK: ${STRK_TOKEN_ADDRESS} (normalized: ${NORMALIZED_STRK})`);
debugLog(` - USDC: ${USDC_TOKEN_ADDRESS} (normalized: ${NORMALIZED_USDC})`);
debugLog(` - USDT: ${USDT_TOKEN_ADDRESS} (normalized: ${NORMALIZED_USDT})`);
// ΓöÇΓöÇ Token helpers ΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇ
/** Resolves token info from an address, emitting debug diagnostics. */
function getTokenInfo(tokenAddress: string | null | undefined): TokenInfo {
if (!tokenAddress) {
return { name: "-", icon: "", decimals: 0, isSTRK: false };
}
return resolveTokenInfo(tokenAddress);
}
/**
* Formats an on-chain token amount for human display.
*
* Contract:
* - Zero, empty, or falsy amounts always return `"-"`.
* - STRK amounts are rendered as `"<whole>.<6-fraction-digits> STRK"`.
* - Non-STRK amounts (USDC/USDT) are rendered as `"$<whole>.<2-fraction-digits>"`.
* - The caller adds a `+` or `-` sign prefix for incoming/outgoing context.
*/
function formatAmount(amount: string | bigint, tokenInfo: TokenInfo): string {
if (!amount || amount === "0" || amount === BigInt(0)) {
return "-";
}
const formattedAmount = formatTokenAmount(amount, tokenInfo.decimals);
if (tokenInfo.isSTRK) {
const [wholePart, fractionalPart = ""] = formattedAmount.split(".");
const fractionalDisplay = fractionalPart.slice(0, 6);
const result = fractionalDisplay
? `${wholePart}.${fractionalDisplay} ${tokenInfo.name}`
: `${wholePart} ${tokenInfo.name}`;
debugLog(`[transactions] formatAmount: STRK result: ${result}`);
return result;
}
const [wholePart, fractionalPart = ""] = formattedAmount.split(".");
const fractionalDisplay = fractionalPart.slice(0, 2).padEnd(2, "0");
return `$${wholePart}${fractionalDisplay ? `.${fractionalDisplay}` : ".00"}`;
}
// ── Token cache (agreement contract → token address) ─────────────────────
const tokenCache = new Map<string, { token: string; timestamp: number }>();
const TOKEN_CACHE_TTL_MS = 5 * 60 * 1000;
/** Fetches the token address for a single agreement from its on-chain contract. */
async function getTokenFromAgreementContract(
agreementContractAddress: string,
agreementId: string,
): Promise<string | null> {
const cacheKey = `${agreementContractAddress}:${agreementId}`;
const cached = tokenCache.get(cacheKey);
if (cached && Date.now() - cached.timestamp < TOKEN_CACHE_TTL_MS) {
debugLog(
`[transactions] Using cached token for agreement ${agreementId}: ${cached.token}`,
);
return cached.token;
}
try {
const c = agreementContract(agreementContractAddress);
const out = await c.get_token(agreementId);
const tokenAddress = toHexString(out);
const normalizedToken = normalizeAddr(tokenAddress);
debugLog(
`[transactions] Successfully fetched token for agreement ${agreementId}:`,
);
debugLog(` - Raw token: ${tokenAddress}`);
debugLog(` - Normalized token: ${normalizedToken}`);
debugLog(` - Token info: ${JSON.stringify(getTokenInfo(normalizedToken))}`);
tokenCache.set(cacheKey, { token: normalizedToken, timestamp: Date.now() });
return normalizedToken;
} catch (error: any) {
console.error(`[transactions] Failed to fetch token for agreement ${agreementId}:`, error?.message);
return null;
}
}
/**
* Batches token fetches from agreement contracts, checking the cache first
* and limiting RPC concurrency to BATCH_SIZE.
*/
async function batchGetTokensFromAgreementContracts(
agreements: Array<{ agreementContractAddress: string; agreementId: string }>,
): Promise<Map<string, string>> {
debugLog(
`[transactions] Batch fetching tokens for ${agreements.length} agreements`,
);
const tokenMap = new Map<string, string>();
const uncachedAgreements: Array<{
agreementContractAddress: string;
agreementId: string;
key: string;
}> = [];
for (const agreement of agreements) {
const cacheKey = `${agreement.agreementContractAddress}:${agreement.agreementId}`;
const cached = tokenCache.get(cacheKey);
if (cached && Date.now() - cached.timestamp < TOKEN_CACHE_TTL_MS) {
tokenMap.set(agreement.agreementId, cached.token);
} else {
uncachedAgreements.push({ ...agreement, key: cacheKey });
}
}
debugLog(
`[transactions] Need to fetch ${uncachedAgreements.length} tokens from contracts (${agreements.length - uncachedAgreements.length} from cache)`,
);
const BATCH_SIZE = 10;
for (let i = 0; i < uncachedAgreements.length; i += BATCH_SIZE) {
const batch = uncachedAgreements.slice(i, i + BATCH_SIZE);
const fetchPromises = batch.map(async (agreement) => {
try {
const token = await getTokenFromAgreementContract(
agreement.agreementContractAddress,
agreement.agreementId,
);
if (token) {
tokenMap.set(agreement.agreementId, token);
} else {
console.warn(
`[transactions] No token returned for agreement ${agreement.agreementId}`,
);
}
} catch (error) {
console.error(`[transactions] Batch fetch error for agreement ${agreement.agreementId}`);
}
});
await Promise.all(fetchPromises);
}
return tokenMap;
}
// ΓöÇΓöÇ Date formatting ΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇ
/**
* Formats a Date into separate date and time display strings.
*
* Date format: `"Mon DD, YYYY"` (e.g. `"Jun 15, 2025"`).
* Time format: `"h:MMAM"` or `"h:MMPM"` (e.g. `"10:30AM"` or `"2:45PM"`).
*/
function formatDate(date: Date) {
const d = new Date(date);
const months = [
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sept", "Oct", "Nov", "Dec",
];
const month = months[d.getMonth()];
const day = d.getDate();
const year = d.getFullYear();
const hours = d.getHours();
const minutes = d.getMinutes();
const ampm = hours >= 12 ? "PM" : "AM";
const hour12 = hours % 12 || 12;
const mins = minutes.toString().padStart(2, "0");
return { date: `${month} ${day}, ${year}`, time: `${hour12}:${mins}${ampm}` };
}
// ΓöÇΓöÇ Event type formatting ΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇ
/**
* Maps internal event-type strings to human-readable labels.
* This is the single source of truth used by both route handlers.
*
* For known event types the mapping is explicit; unrecognised values are
* converted by splitting on capital letters (e.g. `"SomeEvent"` → `"Some Event"`).
*/
function formatEventType(eventType: string): string {
const eventTypeMap: Record<string, string> = {
// WorkAgreement events
AgreementCreated: "Agreement Created",
AgreementActivated: "Agreement Activated",
AgreementPaused: "Agreement Paused",
AgreementResumed: "Agreement Resumed",
AgreementCancelled: "Agreement Cancelled",
AgreementCompleted: "Agreement Completed",
AgreementStatusChange: "Agreement Status Changed",
PaymentSent: "Payment Sent",
PaymentReceived: "Payment Received",
MilestoneAdded: "Milestone Added",
MilestoneApproved: "Milestone Approved",
MilestoneClaimed: "Milestone Claimed",
EmployeeAdded: "Employee Added",
PayrollClaimed: "Payroll Claimed",
DisputeRaised: "Dispute Raised",
DisputeResolved: "Dispute Resolved",
// PayrollEscrow events
Funded: "Agreement Funded",
Released: "Payment Released",
Refunded: "Refund Received",
// Fallback
Unknown: "Unknown Event",
};
return eventTypeMap[eventType] || eventType.replace(/([A-Z])/g, " $1").trim();
}
// ΓöÇΓöÇ Query parameter parsing ΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇ
/**
* Parses limit and offset from the request query, clamping limit to [1, 100].
*
* - Missing or invalid `limit` defaults to 50.
* - Missing or invalid `offset` defaults to 0.
* - Requested values > 100 are silently clamped to 100.
*/
function parsePagination(req: {
query: Record<string, unknown>;
}): { limit: number; offset: number } {
const requestedLimit =
z.coerce.number().int().positive().optional().parse(req.query.limit) || 50;
const limit = Math.min(requestedLimit, 100);
const offset =
z.coerce.number().int().nonnegative().optional().parse(req.query.offset) || 0;
return { limit, offset };
}
/**
* Parses a comma-separated `eventTypes` query parameter into a string array.
*
* - Returns `null` when the parameter is absent, empty, or whitespace-only.
* - Individual values are trimmed; empty segments are discarded.
*/
function parseEventTypes(req: {
query: Record<string, unknown>;
}): string[] | null {
const raw = req.query.eventTypes as string | undefined;
if (!raw) return null;
const parsed = raw
.split(",")
.map((t) => t.trim())
.filter((t) => t.length > 0);
return parsed.length > 0 ? parsed : null;
}
/** Parses optional startDate and endDate from the query string. */
function parseDateFilters(req: {
query: Record<string, unknown>;
}): { startDate?: Date; endDate?: Date } {
const startDate = req.query.startDate
? new Date(req.query.startDate as string)
: undefined;
const endDate = req.query.endDate
? new Date(req.query.endDate as string)
: undefined;
return { startDate, endDate };
}
// ΓöÇΓöÇ Condition builders ΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇ
/**
* Builds WHERE conditions for all five entity tables based on user address
* and optional filters (date range, event types).
*
* The two existing routes have slightly different filtering contracts:
* - `/transactions/:user_address` supports eventTypes but not dates.
* - `/transactions/:user_address/filtered` supports dates but not eventTypes.
*
* Both pass through this builder; callers simply omit the filters they don't
* support so the behaviour stays identical to the pre-refactor code.
*
* @param opts.employeeConditionMode
* - `"employer-or-employee"` (default): matches employees where the user is
* the employer OR the employee (used by the main endpoint).
* - `"employee-only"`: matches employees only where the user IS the employee
* (used by the filtered endpoint).
*/
function buildConditions(
userAddress: string,
filters: TransactionFilters = {},
opts: { employeeConditionMode?: "employer-or-employee" | "employee-only" } = {},
): {
payments: ReturnType<typeof and>;
escrowEvents: ReturnType<typeof and>;
agreementEvents: ReturnType<typeof and>;
employees: ReturnType<typeof and>;
milestones: ReturnType<typeof and>;
} {
const { startDate, endDate, eventTypes } = filters;
const { employeeConditionMode = "employer-or-employee" } = opts;
// -- payments -----------------------------------------------------------
const paymentConds: Array<ReturnType<typeof or> | ReturnType<typeof inArray> | ReturnType<typeof sql> | ReturnType<typeof gte> | ReturnType<typeof lte>> = [
or(eq(schema.payments.from, userAddress), eq(schema.payments.to, userAddress)),
];
if (eventTypes && eventTypes.length > 0) {
const paymentEventTypes = eventTypes.filter(
(et) => et === "PaymentSent" || et === "PaymentReceived",
);
if (paymentEventTypes.length > 0) {
paymentConds.push(inArray(schema.payments.eventType, paymentEventTypes));
} else {
paymentConds.push(sql`FALSE`);
}
}
if (startDate) paymentConds.push(gte(schema.payments.createdAt, startDate));
if (endDate) paymentConds.push(lte(schema.payments.createdAt, endDate));
// -- escrow events ------------------------------------------------------
const escrowConds: Array<ReturnType<typeof or> | ReturnType<typeof inArray> | ReturnType<typeof sql> | ReturnType<typeof gte> | ReturnType<typeof lte>> = [
or(eq(schema.escrowEvents.employer, userAddress), eq(schema.escrowEvents.to, userAddress)),
];
if (eventTypes && eventTypes.length > 0) {
const escrowEventTypes = eventTypes.filter(
(et) => et === "Funded" || et === "Released" || et === "Refunded",
);
if (escrowEventTypes.length > 0) {
escrowConds.push(inArray(schema.escrowEvents.eventType, escrowEventTypes));
} else {
escrowConds.push(sql`FALSE`);
}
}
if (startDate) escrowConds.push(gte(schema.escrowEvents.createdAt, startDate));
if (endDate) escrowConds.push(lte(schema.escrowEvents.createdAt, endDate));
// -- agreement events (joined with agreements) --------------------------
const agreementEventConds: Array<ReturnType<typeof or> | ReturnType<typeof gte> | ReturnType<typeof lte>> = [
or(
eq(schema.agreements.employer, userAddress),
eq(schema.agreements.contributor, userAddress),
),
];
if (startDate)
agreementEventConds.push(gte(schema.agreementEvents.createdAt, startDate));
if (endDate)
agreementEventConds.push(lte(schema.agreementEvents.createdAt, endDate));
// When eventTypes are provided, wrap the agreement-event condition with
// an extra AND that restricts to the requested event types.
const agreementEventCondition: ReturnType<typeof and> =
eventTypes && eventTypes.length > 0
? and(
or(...eventTypes.map((et) => eq(schema.agreementEvents.eventType, et))),
and(...agreementEventConds),
)
: and(...agreementEventConds);
// -- employees (joined with agreements) ---------------------------------
// The main route matches where the user is employer OR employee.
// The filtered route (employee-only mode) matches only where the user IS
// the employee, preserving the original contract.
const employeeUserCond =
employeeConditionMode === "employee-only"
? eq(schema.employees.employeeAddress, userAddress)
: or(
eq(schema.agreements.employer, userAddress),
eq(schema.employees.employeeAddress, userAddress),
);
const employeeConds: Array<ReturnType<typeof or> | ReturnType<typeof eq> | ReturnType<typeof sql> | ReturnType<typeof gte> | ReturnType<typeof lte>> = [
employeeUserCond,
];
if (eventTypes && eventTypes.length > 0) {
if (!eventTypes.includes("EmployeeAdded")) {
employeeConds.push(sql`FALSE`);
}
}
if (startDate) employeeConds.push(gte(schema.employees.createdAt, startDate));
if (endDate) employeeConds.push(lte(schema.employees.createdAt, endDate));
// -- milestones (joined with agreements) --------------------------------
const milestoneConds: Array<ReturnType<typeof or> | ReturnType<typeof sql> | ReturnType<typeof gte> | ReturnType<typeof lte>> = [
or(
eq(schema.agreements.employer, userAddress),
eq(schema.agreements.contributor, userAddress),
),
];
if (eventTypes && eventTypes.length > 0) {
if (!eventTypes.includes("MilestoneAdded")) {
milestoneConds.push(sql`FALSE`);
}
}
if (startDate) milestoneConds.push(gte(schema.milestones.createdAt, startDate));
if (endDate) milestoneConds.push(lte(schema.milestones.createdAt, endDate));
return {
payments: and(...paymentConds),
escrowEvents: and(...escrowConds),
agreementEvents: agreementEventCondition,
employees: and(...employeeConds),
milestones: and(...milestoneConds),
};
}
// ΓöÇΓöÇ Core query and merge logic ΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇ
/**
* Runs all five data queries in parallel, resolves escrow agreement tokens
* from the database and on-chain contracts, then merges, formats, and sorts
* every row into a unified transaction list.
*
* ## Merge contract
* Each entity type is mapped to a `TransactionItem` with the following
* per-type behaviour:
*
* **Agreement events** ΓÇö `type` = formatted via `formatEventType()`.
* `address` = the counterparty (contributor if user is the employer,
* employer otherwise). `token`/`amount`/`tokenIcon` always `"-"`/`-`/`""`.
*
* **Payments** ΓÇö `type` = `"Payment Sent"` or `"Payment Received"`.
* `address` = the other party (`to` for sent, `from` for received).
* `token`/`amount`/`tokenIcon` resolved from the payment's `token` column.
* Amount is prefixed with `+` (received) or `-` (sent).
*
* **Escrow events** ΓÇö `type` = `"Agreement Funded"`, `"Payment Released"`,
* or `"Refund Received"`. `address` = `employer` for Funded, the `to`
* address for Released/Refunded. Tokens resolved from the agreement
* row (DB fallback + on-chain cache). Amount prefixed with `+` (incoming)
* or `-` (outgoing).
*
* **Employee events** ΓÇö Synthetic `type` = `"Employee Added"`.
* `address` = the employee address (if user is employer) or the employer
* address (if user is the employee). `token`/`amount`/`tokenIcon` always
* `"-"`/`-`/`""`.
*
* **Milestone events** ΓÇö Synthetic `type` = `"Milestone Added"`.
* `address` = the contributor (if user is employer) or the employer (if user
* is the contributor). `token`/`amount`/`tokenIcon` always `"-"`/`-`/`""`.
*
* ## Sort contract
* The merged array is sorted by `createdAt` descending, then by `txHash`
* ascending for stable tie-breaking. This sort is applied to the full set
* of fetched rows; the caller then slices for the requested page.
*
* ## Deduplication
* When `opts.deduplicateAgreementEvents` is `true`, agreement events are
* collapsed by their `id` field before merging. The main route enables this;
* the filtered route does not.
*
* @returns The full sorted transaction array and the total count across all
* five sources, so the caller can paginate.
*/
async function fetchAndBuildTransactions(
userAddress: string,
conds: ReturnType<typeof buildConditions>,
queryLimit: number,
opts: { deduplicateAgreementEvents?: boolean } = {},
): Promise<{ allTransactions: TransactionItem[]; total: number }> {
const { deduplicateAgreementEvents = false } = opts;
// Fire all queries in parallel ΓÇö count + data.
const [
paymentsCount,
escrowCount,
agreementEventsCount,
employeesCount,
milestonesCount,
payments,
escrowEvents,
agreementEvents,
employeeEventsData,
milestoneEventsData,
] = await Promise.all([
// ΓöÇΓöÇ counts ΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇ
db
.select({ count: count() })
.from(schema.payments)
.where(conds.payments),
db
.select({ count: count() })
.from(schema.escrowEvents)
.where(conds.escrowEvents),
db
.select({ count: count() })
.from(schema.agreementEvents)
.innerJoin(
schema.agreements,
eq(schema.agreementEvents.agreementId, schema.agreements.id),
)
.where(conds.agreementEvents),
db
.select({ count: count() })
.from(schema.employees)
.leftJoin(
schema.agreements,
eq(schema.employees.agreementId, schema.agreements.id),
)
.where(conds.employees),
db
.select({ count: count() })
.from(schema.milestones)
.leftJoin(
schema.agreements,
eq(schema.milestones.agreementId, schema.agreements.id),
)
.where(conds.milestones),
// ΓöÇΓöÇ data ΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇ
db
.select()
.from(schema.payments)
.where(conds.payments)
.orderBy(desc(schema.payments.createdAt), desc(schema.payments.id))
.limit(queryLimit),
db
.select()
.from(schema.escrowEvents)
.where(conds.escrowEvents)
.orderBy(desc(schema.escrowEvents.createdAt), desc(schema.escrowEvents.id))
.limit(queryLimit),
db
.select({
id: schema.agreementEvents.id,
agreementId: schema.agreementEvents.agreementId,
contractAddress: schema.agreementEvents.contractAddress,
eventType: schema.agreementEvents.eventType,
blockNumber: schema.agreementEvents.blockNumber,
transactionHash: schema.agreementEvents.transactionHash,
createdAt: schema.agreementEvents.createdAt,
employer: schema.agreements.employer,
contributor: schema.agreements.contributor,
token: schema.agreements.token,
})
.from(schema.agreementEvents)
.innerJoin(
schema.agreements,
eq(schema.agreementEvents.agreementId, schema.agreements.id),
)
.where(conds.agreementEvents)
.orderBy(desc(schema.agreementEvents.createdAt), desc(schema.agreementEvents.id))
.limit(queryLimit),
db
.select({
id: schema.employees.id,
agreementId: schema.employees.agreementId,
contractAddress: schema.employees.contractAddress,
blockNumber: schema.employees.blockNumber,
transactionHash: schema.employees.transactionHash,
createdAt: schema.employees.createdAt,
employer: schema.agreements.employer,
contributor: schema.agreements.contributor,
token: schema.agreements.token,
employeeAddress: schema.employees.employeeAddress,
amount: schema.employees.salaryPerPeriod,
})
.from(schema.employees)
.leftJoin(
schema.agreements,
eq(schema.employees.agreementId, schema.agreements.id),
)
.where(conds.employees)
.orderBy(desc(schema.employees.createdAt), desc(schema.employees.id))
.limit(queryLimit),
db
.select({
id: schema.milestones.id,
agreementId: schema.milestones.agreementId,
contractAddress: schema.milestones.contractAddress,
blockNumber: schema.milestones.blockNumber,
transactionHash: schema.milestones.transactionHash,
createdAt: schema.milestones.createdAt,
employer: schema.agreements.employer,
contributor: schema.agreements.contributor,
token: schema.agreements.token,
amount: schema.milestones.amount,
})
.from(schema.milestones)
.leftJoin(
schema.agreements,
eq(schema.milestones.agreementId, schema.agreements.id),
)
.where(conds.milestones)
.orderBy(desc(schema.milestones.createdAt), desc(schema.milestones.id))
.limit(queryLimit),
]);
const total =
Number(paymentsCount[0].count) +
Number(escrowCount[0].count) +
Number(agreementEventsCount[0].count) +
Number(employeesCount[0].count) +
Number(milestonesCount[0].count);
// Optionally deduplicate agreement events by id (used by the main route).
const dedupedAgreementEvents = deduplicateAgreementEvents
? Array.from(new Map(agreementEvents.map((a) => [a.id, a])).values())
: agreementEvents;
// Tag employee / milestone rows with their synthetic event types.
const employeeEvents = employeeEventsData.map((e) => ({
...e,
eventType: "EmployeeAdded" as const,
}));
const milestoneEvents = milestoneEventsData.map((m) => ({
...m,
eventType: "MilestoneAdded" as const,
}));
// ΓöÇΓöÇ Resolve escrow tokens ΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇ
const escrowAgreementIds = [...new Set(escrowEvents.map((e) => e.agreementId))];
const escrowAgreements =
escrowAgreementIds.length > 0
? await db
.select({
id: schema.agreements.id,
token: schema.agreements.token,
contractAddress: schema.agreements.contractAddress,
})
.from(schema.agreements)
.where(inArray(schema.agreements.id, escrowAgreementIds))
: [];
const agreementsForTokenFetch = escrowAgreements
.filter((a) => a.contractAddress)
.map((a) => ({
agreementContractAddress: a.contractAddress!,
agreementId: a.id,
}));
const contractTokenMap =
await batchGetTokensFromAgreementContracts(agreementsForTokenFetch);
const escrowTokenMap = new Map<string, string>();
for (const agreement of escrowAgreements) {
const contractToken = contractTokenMap.get(agreement.id);
const dbToken = agreement.token;
escrowTokenMap.set(agreement.id, contractToken || dbToken);
}
// ΓöÇΓöÇ Merge & format into TransactionItem[] ΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇ
const allTransactions: TransactionItem[] = [
...dedupedAgreementEvents.map((a) => {
const dateTime = formatDate(a.createdAt);
return {
id: a.transactionHash.slice(0, 10),
type: formatEventType(a.eventType),
address: formatAddress(
a.employer === userAddress ? a.contributor || "N/A" : a.employer,
),
date: dateTime.date,
time: dateTime.time,
token: "-",
amount: "-",
status: "Completed" as const,
tokenIcon: "",
txHash: a.transactionHash,
createdAt: a.createdAt,
};
}),
...payments.map((p) => {
const dateTime = formatDate(p.createdAt);
const tokenInfo = getTokenInfo(p.token);
const amountStr = formatAmount(p.amount, tokenInfo);
const isReceived = p.eventType === "PaymentReceived";
const sign = isReceived ? "+" : "-";
const finalAmount = amountStr !== "-" ? `${sign}${amountStr}` : amountStr;
const allTransactions: TransactionRecord[] = [
...uniqueAgreementEvents.map((a) => {
const dateTime = formatDate(a.createdAt);
return {
id: a.transactionHash.slice(0, 10), type: formatEventType(a.eventType),
address: formatAddress(a.employer === userAddress ? a.contributor || "N/A" : a.employer),
date: dateTime.date, time: dateTime.time, token: "-", amount: "-",
status: "Completed" as const, tokenIcon: "", txHash: a.transactionHash, createdAt: a.createdAt,
};
}),
...payments.map((p) => {
const dateTime = formatDate(p.createdAt);
const tokenInfo = getTokenInfo(p.token);
const amountStr = formatAmount(p.amount, tokenInfo);
const isReceived = p.eventType === "PaymentReceived";
const sign = isReceived ? "+" : "-";
const finalAmount = amountStr !== "-" ? `${sign}${amountStr}` : amountStr;
return {
id: e.transactionHash.slice(0, 10),
type:
e.eventType === "Funded"
? "Agreement Funded"
: e.eventType === "Released"
? "Payment Released"
: "Refund Received",
address: formatAddress(
e.eventType === "Funded" ? e.employer : e.to || "",
),
date: dateTime.date,
time: dateTime.time,
token: tokenInfo.name,
amount: finalAmount,
status: "Completed" as const,
tokenIcon: tokenInfo.icon,
txHash: e.transactionHash,
createdAt: e.createdAt,
};
}),
...employeeEvents.map((e) => {
const dateTime = formatDate(e.createdAt);
const address =
e.employer === userAddress
? e.employeeAddress || "N/A"
: e.employer || e.employeeAddress || "N/A";
return {
id: e.transactionHash.slice(0, 10),
type: "Employee Added",
address: formatAddress(address),
date: dateTime.date,
time: dateTime.time,
token: "-",
amount: "-",
status: "Completed" as const,
tokenIcon: "",
txHash: e.transactionHash,
createdAt: e.createdAt,
};
}),
...milestoneEvents.map((m) => {
const dateTime = formatDate(m.createdAt);
const address =
m.employer === userAddress
? m.contributor || "N/A"
: m.employer || "N/A";
return {
id: m.transactionHash.slice(0, 10),
type: "Milestone Added",
address: formatAddress(address),
date: dateTime.date,
time: dateTime.time,
token: "-",
amount: "-",
status: "Completed" as const,
tokenIcon: "",
txHash: m.transactionHash,
createdAt: m.createdAt,
};
}),
].sort((a, b) => {
const timeA = a.createdAt ? new Date(a.createdAt).getTime() : 0;
const timeB = b.createdAt ? new Date(b.createdAt).getTime() : 0;
if (timeB !== timeA) return timeB - timeA;
return a.txHash.localeCompare(b.txHash);
});
return { allTransactions, total };
}
// ΓöÇΓöÇ Response helper ΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇ
/**
* Slices, paginates, and sends the unified transaction list.
*
* The response body always contains:
* - `transactions`: the page of items (`array`, may be empty)
* - `total`: the sum of matching rows across all five source tables
* - `hasMore`: `true` when `total > offset + limit`
* - `limit` / `offset`: the requested parameters (clamped)
*/
function respondPaginated(
res: import("express").Response,
allTransactions: TransactionItem[],
total: number,
limit: number,
offset: number,
): void {
const paginated = allTransactions.slice(offset, offset + limit);
const hasMore = total > offset + limit;
const body: TransactionResponse = {
transactions: paginated,
total,
hasMore,
limit,
offset,
};
res.json(body);
}
// ΓöÇΓöÇ Route: main transaction list (with optional event-type filtering) ΓöÇΓöÇΓöÇΓöÇ
//
// Contract:
// - Accepts `eventTypes` query filter (comma-separated).
// - Deduplicates agreement events by id.
// - Employee condition mode: "employer-or-employee".
// - Does NOT support date-range filtering.
transactionsRouter.get(
"/transactions/:user_address",
async (req, res, next) => {
try {
const userAddress = normalizeAddr(req.params.user_address);
const { limit, offset } = parsePagination(req);
const eventTypes = parseEventTypes(req);
const conds = buildConditions(userAddress, { eventTypes: eventTypes ?? undefined });
const { allTransactions, total } = await fetchAndBuildTransactions(
userAddress,
conds,
offset + limit,
{ deduplicateAgreementEvents: true },
);
respondPaginated(res, allTransactions, total, limit, offset);
} catch (e) {
next(e);
}
},
);
// ΓöÇΓöÇ Route: filtered transaction list (with optional date-range) ΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇΓöÇ
//
// Contract:
// - Accepts `startDate` / `endDate` query filters.
// - Employee condition mode: "employee-only".
// - Does NOT deduplicate agreement events.
// - Does NOT support `eventTypes` filter.
transactionsRouter.get(
"/transactions/:user_address/filtered",
async (req, res, next) => {
try {
const userAddress = normalizeAddr(req.params.user_address);