Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | 4x 13x 15x 4x 22x 22x 22x 22x 4x 3x 3x 4x 2x 2x 1x 1x 1x 4x 3x 3x 3x 2x 1x 1x 4x 3x 3x 3x 2x 1x 1x 4x 2x 2x 1x 1x 4x 2x 2x 1x 1x 1x | import type { Transaction } from '../components/transaction-table';
import {
formatDate,
formatDateForAPI,
getCurrentDateISO,
} from '@/lib/date-utils';
import { apiClient } from '@/lib/http-client';
import type {
PaginatedApiResponse,
PaginatedResult,
PaginationParams,
} from '@/lib/pagination';
import { buildPaginationQuery } from '@/lib/pagination';
import type {
TRANSACTION_TYPES,
TRANSACTION_STATUS,
} from '@/constants/transaction';
import {
TRANSACTION_API_ENDPOINTS,
TRANSACTION_DEFAULTS,
TRANSACTION_DEFAULT_SORT,
TRANSACTION_DEFAULT_PAGE_SIZE,
} from '@/constants/transaction';
// Transaction API response type from Strapi
interface TransactionApiResponse {
id: number;
documentId: string;
transactionId: string;
type: typeof TRANSACTION_TYPES.BITCOIN | typeof TRANSACTION_TYPES.CARD;
name: string;
value: string;
return: string;
status:
| typeof TRANSACTION_STATUS.COMPLETED
| typeof TRANSACTION_STATUS.FAILED
| typeof TRANSACTION_STATUS.IN_PROGRESS;
date: string;
user?: any;
createdAt: string;
updatedAt: string;
publishedAt: string;
locale: string;
}
// Transaction-specific pagination parameters
export interface TransactionPaginationParams extends PaginationParams {
status?:
| typeof TRANSACTION_STATUS.COMPLETED
| typeof TRANSACTION_STATUS.FAILED
| typeof TRANSACTION_STATUS.IN_PROGRESS;
type?: typeof TRANSACTION_TYPES.BITCOIN | typeof TRANSACTION_TYPES.CARD;
name?: string;
}
// Transaction-specific pagination result
export interface PaginatedTransactionResult
extends PaginatedResult<Transaction> {
transactions: Transaction[];
}
// Transform API response to Transaction
const transformApiResponse = (
apiData: TransactionApiResponse[]
): Transaction[] => {
console.log('Transforming Transactions API response:', apiData);
return apiData.map(item => ({
id: item.id.toString(),
date: formatDate(item.date), // Format the date consistently
transactionId: item.transactionId,
type: item.type,
name: item.name,
value: item.value,
return: item.return,
status: item.status,
}));
};
// Fetch paginated transactions from backend API
export const fetchTransactionsPaginated = async (
params: TransactionPaginationParams = {}
): Promise<PaginatedTransactionResult> => {
try {
const { sort = TRANSACTION_DEFAULT_SORT, ...otherParams } = params;
const queryParams = buildPaginationQuery({ sort, ...otherParams });
const response: PaginatedApiResponse<TransactionApiResponse> =
await apiClient.get(
`${TRANSACTION_API_ENDPOINTS.BASE}?${queryParams.toString()}`
);
return {
data: transformApiResponse(response.data),
transactions: transformApiResponse(response.data),
pagination: response.meta.pagination,
};
} catch (error) {
console.error('Failed to fetch paginated transactions:', error);
throw new Error(
error instanceof Error ? error.message : 'Failed to load transaction data'
);
}
};
// Legacy method for backward compatibility (fetches all transactions from first page)
export const fetchTransactions = async (): Promise<Transaction[]> => {
try {
const result = await fetchTransactionsPaginated({
pageSize: TRANSACTION_DEFAULT_PAGE_SIZE,
});
return result.transactions;
} catch (error) {
console.error('Failed to fetch transactions:', error);
throw new Error(
error instanceof Error ? error.message : 'Failed to load transaction data'
);
}
};
// Create a new transaction (public API)
export const createTransaction = async (transactionData: {
type: typeof TRANSACTION_TYPES.BITCOIN | typeof TRANSACTION_TYPES.CARD;
name: string;
value: string;
return?: string;
status?:
| typeof TRANSACTION_STATUS.COMPLETED
| typeof TRANSACTION_STATUS.FAILED
| typeof TRANSACTION_STATUS.IN_PROGRESS;
date?: string;
transactionId?: string;
}): Promise<Transaction> => {
try {
// Prepare data with proper date formatting
const submitData = {
type: transactionData.type,
name: transactionData.name,
value: transactionData.value,
date: transactionData.date
? formatDateForAPI(transactionData.date)
: getCurrentDateISO(),
return: transactionData.return || TRANSACTION_DEFAULTS.RETURN,
status: transactionData.status || TRANSACTION_DEFAULTS.STATUS,
// Generate a unique transaction ID if not provided
transactionId:
transactionData.transactionId ||
`${TRANSACTION_DEFAULTS.TRANSACTION_ID_PREFIX}${Date.now()}`,
};
const data: TransactionApiResponse = await apiClient.post(
TRANSACTION_API_ENDPOINTS.BASE,
submitData
);
return transformApiResponse([data])[0];
} catch (error) {
console.error('Failed to create transaction:', error);
throw new Error(
error instanceof Error ? error.message : 'Failed to create transaction'
);
}
};
// Update a transaction
export const updateTransaction = async (
id: string,
updateData: Partial<{
type: typeof TRANSACTION_TYPES.BITCOIN | typeof TRANSACTION_TYPES.CARD;
name: string;
value: string;
return: string;
status:
| typeof TRANSACTION_STATUS.COMPLETED
| typeof TRANSACTION_STATUS.FAILED
| typeof TRANSACTION_STATUS.IN_PROGRESS;
date: string;
}>
): Promise<Transaction> => {
try {
const submitData = {
...updateData,
...(updateData.date && { date: formatDateForAPI(updateData.date) }),
};
const data: TransactionApiResponse = await apiClient.put(
TRANSACTION_API_ENDPOINTS.BY_ID(id),
submitData
);
return transformApiResponse([data])[0];
} catch (error) {
console.error('Failed to update transaction:', error);
throw new Error(
error instanceof Error ? error.message : 'Failed to update transaction'
);
}
};
// Delete a transaction
export const deleteTransaction = async (id: string): Promise<void> => {
try {
await apiClient.delete(TRANSACTION_API_ENDPOINTS.BY_ID(id));
} catch (error) {
console.error('Failed to delete transaction:', error);
throw new Error(
error instanceof Error ? error.message : 'Failed to delete transaction'
);
}
};
// Get a single transaction by ID
export const getTransaction = async (id: string): Promise<Transaction> => {
try {
const data: TransactionApiResponse = await apiClient.get(
TRANSACTION_API_ENDPOINTS.BY_ID(id)
);
return transformApiResponse([data])[0];
} catch (error) {
console.error('Failed to fetch transaction:', error);
throw new Error(
error instanceof Error ? error.message : 'Failed to fetch transaction'
);
}
};
|