]> git.digitality.be Git - pdw25-26/commitdiff
Mobile : Mobile aligné sur /api/price/current, /api/signal/current, /api/wallets...
authorThibaud Moustier <thibaudmoustier0@gmail.com>
Sat, 28 Feb 2026 21:08:05 +0000 (22:08 +0100)
committerThibaud Moustier <thibaudmoustier0@gmail.com>
Sat, 28 Feb 2026 21:08:05 +0000 (22:08 +0100)
Wallette/mobile/src/services/api/dashboardApi.ts
Wallette/mobile/src/services/api/walletApi.ts

index f37605fcf5b30ed4c5f032590f76c8a009f28ee5..0cb2dbcd50f079613ef3c23483230f6954b530b8 100644 (file)
@@ -1,11 +1,8 @@
 import type { DashboardSummary, TradeDecision, AlertLevel } from "../../types/DashboardSummary";
-import type { Alert } from "../../types/Alert";
-
 import { loadSession } from "../../utils/sessionStorage";
 import { loadSettings } from "../../utils/settingsStorage";
-
+import { apiGet } from "./http";
 import { getCurrentPrice } from "./priceApi";
-import { getAlertHistory } from "./alertsApi";
 
 function safeDecision(action: any): TradeDecision {
   const a = String(action ?? "HOLD").toUpperCase();
@@ -19,6 +16,12 @@ function safeLevel(level: any): AlertLevel {
   return "INFO";
 }
 
+/**
+ * dashboardApi (contrat Stéphane - STRICT)
+ * ---------------------------------------
+ * - GET /api/price/current?pair=BTC/EUR
+ * - GET /api/signal/current?userId=...&pair=BTC/EUR
+ */
 export async function getDashboardSummary(): Promise<DashboardSummary> {
   const session = await loadSession();
   const userId = session?.userId;
@@ -27,30 +30,28 @@ export async function getDashboardSummary(): Promise<DashboardSummary> {
   const settings = await loadSettings();
   const currency: "EUR" | "USD" = settings.currency === "USD" ? "USD" : "EUR";
 
+  // Dashboard strict : BTC seulement (align web). Adaptable plus tard.
   const pair = `BTC/${currency}`;
 
+  // 1) Prix (service prix)
   const price = await getCurrentPrice(pair);
 
-  let decision: TradeDecision = "HOLD";
-  let alertLevel: AlertLevel = "INFO";
-  let confidence = 0;
-  let reason = "Aucune alerte récente.";
-  let timestamp = price.timestampMs;
+  // 2) Signal (service stratégies)
+  const sig = await apiGet<any>(
+    `/signal/current?userId=${encodeURIComponent(userId)}&pair=${encodeURIComponent(pair)}`
+  );
 
-  try {
-    const events = await getAlertHistory(10);
-    const last: Alert | undefined = events[0];
+  const decision = safeDecision(sig?.action ?? sig?.decision);
+  const alertLevel = safeLevel(sig?.alertLevel ?? sig?.level ?? sig?.criticality);
+  const confidence = typeof sig?.confidence === "number" ? sig.confidence : 0;
+  const reason = String(sig?.reason ?? sig?.message ?? "—");
 
-    if (last) {
-      decision = safeDecision(last.action);
-      alertLevel = safeLevel(last.alertLevel);
-      confidence = typeof last.confidence === "number" ? last.confidence : 0;
-      reason = String(last.reason ?? last.message ?? "—");
-      timestamp = typeof last.timestamp === "number" ? last.timestamp : timestamp;
-    }
-  } catch {
-    // non bloquant
-  }
+  const timestamp =
+    typeof sig?.timestamp === "number"
+      ? sig.timestamp
+      : typeof sig?.timestamp_ms === "number"
+      ? sig.timestamp_ms
+      : price.timestampMs;
 
   return {
     pair,
index 7cf5c7deecb6ee2998fa2f6333b87e2ef5082a31..2f1a4a2dade52c4edaec48d7b2fb67b14c74d684 100644 (file)
@@ -2,7 +2,14 @@ import { apiGet } from "./http";
 import { loadSession } from "../../utils/sessionStorage";
 import type { PortfolioState } from "../../models/Portfolio";
 
-type WalletListItem = { id?: string; walletId?: string; _id?: string };
+/**
+ * walletApi (contrat Stéphane - STRICT)
+ * ------------------------------------
+ * - GET /api/wallets?userId=...
+ * - GET /api/wallets/:id
+ * - GET /api/wallets/:id/events
+ */
+
 type WalletListResponse = any;
 
 function pickWalletId(list: any): string | null {
@@ -10,11 +17,12 @@ function pickWalletId(list: any): string | null {
     Array.isArray(list) ? list :
     Array.isArray(list?.wallets) ? list.wallets :
     Array.isArray(list?.items) ? list.items :
+    Array.isArray(list?.data) ? list.data :
     null;
 
   if (!arr || arr.length === 0) return null;
-  const first = arr[0];
 
+  const first = arr[0];
   return (
     (typeof first?.walletId === "string" && first.walletId) ||
     (typeof first?.id === "string" && first.id) ||
@@ -48,18 +56,44 @@ function extractAssets(details: any): { symbol: string; quantity: number }[] {
   return [];
 }
 
-export async function getPortfolio(): Promise<PortfolioState> {
+export async function getPrimaryWalletId(): Promise<string | null> {
   const session = await loadSession();
   const userId = session?.userId;
-  if (!userId) throw new Error("Session absente : impossible de charger le portefeuille.");
+  if (!userId) throw new Error("Session absente : impossible de charger les wallets.");
 
   const list = await apiGet<WalletListResponse>(`/wallets?userId=${encodeURIComponent(userId)}`);
-  const walletId = pickWalletId(list);
+  return pickWalletId(list);
+}
 
+export async function getPortfolio(): Promise<PortfolioState> {
+  const walletId = await getPrimaryWalletId();
   if (!walletId) return { assets: [], updatedAtMs: Date.now() };
 
   const details = await apiGet<any>(`/wallets/${encodeURIComponent(walletId)}`);
   const assets = extractAssets(details);
 
   return { assets, updatedAtMs: Date.now() };
+}
+
+export type WalletEvent = {
+  id?: string;
+  type?: string;       // BUY/SELL
+  symbol?: string;     // BTC
+  quantity?: number;
+  price?: number;
+  timestamp?: number;
+};
+
+export async function getWalletEvents(limit = 50): Promise<WalletEvent[]> {
+  const walletId = await getPrimaryWalletId();
+  if (!walletId) return [];
+
+  const data = await apiGet<any>(
+    `/wallets/${encodeURIComponent(walletId)}/events?limit=${encodeURIComponent(String(limit))}`
+  );
+
+  if (Array.isArray(data)) return data as WalletEvent[];
+  if (Array.isArray(data?.events)) return data.events as WalletEvent[];
+  if (Array.isArray(data?.items)) return data.items as WalletEvent[];
+  return [];
 }
\ No newline at end of file