-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path2-async-await.ts
More file actions
132 lines (102 loc) · 3.59 KB
/
2-async-await.ts
File metadata and controls
132 lines (102 loc) · 3.59 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
// In-memory data
type AccountRecord = {
id: string;
isActive: boolean;
};
type BalanceRecord = {
id: string;
amount: number;
};
type LimitRecord = {
id: string;
dailyLimit: number;
transferredToday: number;
};
const ACCOUNTS: Record<string, AccountRecord> = {
A: { id: 'A', isActive: true },
B: { id: 'B', isActive: true }
};
const BALANCES: Record<string, BalanceRecord> = {
A: { id: 'A', amount: 1000 },
B: { id: 'B', amount: 300 }
};
const LIMITS: Record<string, LimitRecord> = {
A: { id: 'A', dailyLimit: 500, transferredToday: 0 },
B: { id: 'B', dailyLimit: 1000, transferredToday: 0 }
};
// Data access layer
type GetById = { id: string };
const getAccount = async ({ id }: GetById): Promise<AccountRecord | null> => {
return ACCOUNTS[id] ? { ...ACCOUNTS[id] } : null;
};
const getBalance = async ({ id }: GetById): Promise<BalanceRecord | null> => {
return BALANCES[id] ? { ...BALANCES[id] } : null;
};
const getLimit = async ({ id }: GetById): Promise<LimitRecord | null> => {
return LIMITS[id] ? { ...LIMITS[id] } : null;
};
type SaveBalance = { id: string; amount: number };
const saveBalance = async (params: SaveBalance): Promise<void> => {
const { id, amount } = params;
if (!BALANCES[id]) throw new Error('Balance not found to save');
BALANCES[id] = { id, amount };
};
type SaveLimit = { id: string; transferred: number };
const saveLimit = async (params: SaveLimit): Promise<void> => {
const { id, transferred } = params;
if (!LIMITS[id]) throw new Error('Limit not found to update');
LIMITS[id].transferredToday = transferred;
};
// Transaction Script
type Transfer = { fromId: string; toId: string; amount: number };
const transferFunds = async (transfer: Transfer): Promise<void> => {
const { fromId, toId, amount } = transfer;
if (fromId === toId) throw new Error('Same account transfer is invalid');
if (amount <= 0) throw new Error('Amount must be positive');
if (!ACCOUNTS[fromId] || !ACCOUNTS[toId]) {
throw new Error('Invalid account id');
}
const [from, to] = await Promise.all([
getAccount({ id: fromId }),
getAccount({ id: toId })
]);
if (!from || !to) throw new Error('Account not found');
if (!from.isActive) throw new Error('Source account is inactive');
if (!to.isActive) throw new Error('Destination account is inactive');
const [fromBal, toBal] = await Promise.all([
getBalance({ id: fromId }),
getBalance({ id: toId })
]);
if (!fromBal || !toBal) throw new Error('Balance not found');
const limit = await getLimit({ id: fromId });
if (!limit) throw new Error('Transfer limit not configured');
const feeRate = 0.02;
const fee = Math.ceil(amount * feeRate);
const total = amount + fee;
if (fromBal.amount < total) throw new Error('Insufficient funds');
if (limit.transferredToday + amount > limit.dailyLimit) {
throw new Error('Daily transfer limit exceeded');
}
if (fee > 100) throw new Error('Excessive transaction fee');
if (fee === 0 && amount < 50) {
throw new Error('Small transfers must have minimum fee');
}
const newFrom = fromBal.amount - total;
const newTo = toBal.amount + amount;
const newTransferred = limit.transferredToday + amount;
await Promise.all([
saveBalance({ id: fromId, amount: newFrom }),
saveBalance({ id: toId, amount: newTo }),
saveLimit({ id: fromId, transferred: newTransferred }),
]);
};
// Usage
const main = async () => {
console.log({ ACCOUNTS });
console.log({ BALANCES });
console.log({ LIMITS });
await transferFunds({ fromId: 'A', toId: 'B', amount: 200 });
console.log({ BALANCES });
console.log({ LIMITS });
};
main();