main
ts 166 lines 4.18 KB
Raw
1 import { Transaction, Currency, ExchangeRate, Recipient } from "@/types/transaction";
2
3 // Mock data
4 const mockTransactions: Transaction[] = [
5 {
6 id: "tx_001",
7 amount: 250.00,
8 currency: "USD",
9 status: "completed",
10 sender: "you@company.com",
11 recipient: "vendor@supplier.com",
12 recipientName: "Office Supplies Co.",
13 timestamp: new Date(Date.now() - 24 * 60 * 60 * 1000),
14 description: "Office Supplies",
15 fee: 2.50,
16 },
17 {
18 id: "tx_002",
19 amount: 128.50,
20 currency: "USD",
21 status: "completed",
22 sender: "you@company.com",
23 recipient: "restaurant@food.com",
24 recipientName: "City Bistro",
25 timestamp: new Date(Date.now() - 2 * 24 * 60 * 60 * 1000),
26 description: "Client Lunch",
27 fee: 1.29,
28 },
29 {
30 id: "tx_003",
31 amount: 45.99,
32 currency: "USD",
33 status: "processing",
34 sender: "you@company.com",
35 recipient: "store@retail.com",
36 recipientName: "Tech Store",
37 timestamp: new Date(Date.now() - 30 * 60 * 1000),
38 description: "Cable & Adapters",
39 fee: 0.46,
40 },
41 ];
42
43 const mockRecipients: Recipient[] = [
44 {
45 id: "rec_001",
46 name: "Sarah Johnson",
47 email: "sarah.j@company.com",
48 },
49 {
50 id: "rec_002",
51 name: "Michael Chen",
52 email: "m.chen@partner.com",
53 },
54 {
55 id: "rec_003",
56 name: "Office Supplies Co.",
57 email: "vendor@supplier.com",
58 },
59 ];
60
61 // Exchange rates (base USD)
62 const exchangeRates: Record<Currency, number> = {
63 USD: 1,
64 EUR: 0.92,
65 GBP: 0.79,
66 NGN: 1620,
67 KES: 129,
68 GHS: 15.5,
69 };
70
71 class TransactionService {
72 private transactions: Transaction[] = [...mockTransactions];
73 private recipients: Recipient[] = [...mockRecipients];
74
75 async sendMoney(
76 amount: number,
77 currency: Currency,
78 recipient: string,
79 recipientName: string,
80 description?: string
81 ): Promise<Transaction> {
82 // Calculate fee (1% with min $0.50)
83 const fee = Math.max(amount * 0.01, 0.50);
84
85 const transaction: Transaction = {
86 id: `tx_${Date.now()}`,
87 amount,
88 currency,
89 status: "pending",
90 sender: "you@company.com",
91 recipient,
92 recipientName,
93 timestamp: new Date(),
94 description,
95 fee,
96 };
97
98 this.transactions.unshift(transaction);
99
100 // Simulate processing delay
101 await new Promise(resolve => setTimeout(resolve, 1500));
102 transaction.status = "processing";
103
104 await new Promise(resolve => setTimeout(resolve, 2000));
105 transaction.status = "completed";
106
107 return transaction;
108 }
109
110 async getTransactions(): Promise<Transaction[]> {
111 // Simulate network delay
112 await new Promise(resolve => setTimeout(resolve, 300));
113 return [...this.transactions];
114 }
115
116 async getTransaction(id: string): Promise<Transaction | null> {
117 await new Promise(resolve => setTimeout(resolve, 200));
118 return this.transactions.find(tx => tx.id === id) || null;
119 }
120
121 getExchangeRate(from: Currency, to: Currency): ExchangeRate {
122 const rate = exchangeRates[to] / exchangeRates[from];
123 return { from, to, rate };
124 }
125
126 async getRecipients(): Promise<Recipient[]> {
127 await new Promise(resolve => setTimeout(resolve, 200));
128 return [...this.recipients];
129 }
130
131 async addRecipient(name: string, email: string): Promise<Recipient> {
132 await new Promise(resolve => setTimeout(resolve, 300));
133 const newRecipient: Recipient = {
134 id: `rec_${Date.now()}`,
135 name,
136 email,
137 };
138 this.recipients.push(newRecipient);
139 return newRecipient;
140 }
141
142 async updateRecipient(id: string, name: string, email: string): Promise<Recipient> {
143 await new Promise(resolve => setTimeout(resolve, 300));
144 const recipientIndex = this.recipients.findIndex(r => r.id === id);
145 if (recipientIndex === -1) {
146 throw new Error("Recipient not found");
147 }
148 this.recipients[recipientIndex] = { id, name, email };
149 return this.recipients[recipientIndex];
150 }
151
152 async deleteRecipient(id: string): Promise<void> {
153 await new Promise(resolve => setTimeout(resolve, 300));
154 this.recipients = this.recipients.filter(r => r.id !== id);
155 }
156
157 calculateFee(amount: number): number {
158 return Math.max(amount * 0.01, 0.50);
159 }
160
161 getBalance(): number {
162 return 2450.00;
163 }
164 }
165
166 export const transactionService = new TransactionService();