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