File size: 6,880 Bytes
eb6a2f9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// ─────────────────────────────────────────────────────────────────────────────
//  src/lib/api/studentsapi.ts
//  API layer for My Students page β€” matches actual backend endpoints
// ─────────────────────────────────────────────────────────────────────────────

import { getApiBaseUrl } from './config';

const BASE_URL = getApiBaseUrl();
const TOKEN_KEY = 'authToken';

// ─── Shared helpers ───────────────────────────────────────────────────────────

function getHeaders(): HeadersInit {
  const token =
    localStorage.getItem(TOKEN_KEY) ??
    localStorage.getItem('token') ??
    '';
  return {
    'Content-Type': 'application/json',
    ...(token ? { Authorization: `Bearer ${token}` } : {}),
  };
}

export class StudentsApiError extends Error {
  readonly status: number;

  constructor(status: number, message: string) {
    super(message);
    this.status = status;
    this.name = 'StudentsApiError';
  }
}

async function request<T>(path: string, options?: RequestInit): Promise<T> {
  const res = await fetch(`${BASE_URL}${path}`, {
    ...options,
    headers: { ...getHeaders(), ...options?.headers },
  });

  // 204 No Content β†’ return empty array gracefully
  if (res.status === 204) {
    return [] as unknown as T;
  }

  if (!res.ok) {
    const message = await res.text().catch(() => res.statusText);
    throw new StudentsApiError(
      res.status,
      `[${res.status}] ${path} β†’ ${message}`,
    );
  }

  const text = await res.text();
  if (!text) return [] as unknown as T;

  try {
    return JSON.parse(text) as T;
  } catch {
    return text as unknown as T;
  }
}

// ─── Response Types ───────────────────────────────────────────────────────────

export interface Session {
  id: string;
  date: string;
  duration: number;
  status: 'completed' | 'cancelled' | 'upcoming';
  rating?: number;
  feedback?: string;
  price: number;
  topic: string;
}

export interface Student {
  id: string;
  name: string;
  email: string;
  phone?: string;
  location?: string;
  joinedDate: string;
  totalSessions: number;
  completedSessions: number;
  cancelledSessions: number;
  totalMinutes: number;
  totalSpent: number;
  averageRating: number;
  lastSession: string;
  currentStreak: number;
  progress: number;
  level: string;
  goals: string[];
  sessionHistory?: Session[];
}

export interface StudentsStats {
  totalStudents: number;
  activeLast7Days: number;
  totalSessions: number;
  averageRating: number;
}

// ─── Raw-to-typed normalizer ──────────────────────────────────────────────────
// Maps any backend field naming to our Student shape safely

// eslint-disable-next-line @typescript-eslint/no-explicit-any
function normalizeStudent(raw: any): Student {
  return {
    id: String(
      raw.id ?? raw.studentId ?? raw.userId ?? '',
    ),
    name:
      raw.name ??
      raw.fullName ??
      raw.studentName ??
      '',
    email: raw.email ?? raw.emailAddress ?? '',
    phone: raw.phone ?? raw.phoneNumber ?? undefined,
    location: raw.location ?? raw.city ?? undefined,
    joinedDate:
      raw.joinedDate ?? raw.createdAt ?? new Date().toISOString(),
    totalSessions: raw.totalSessions ?? raw.sessionsCount ?? 0,
    completedSessions: raw.completedSessions ?? raw.completed ?? 0,
    cancelledSessions: raw.cancelledSessions ?? raw.cancelled ?? 0,
    totalMinutes: raw.totalMinutes ?? raw.totalTime ?? 0,
    totalSpent:
      raw.totalSpent ?? raw.totalPaid ?? raw.totalEarnings ?? 0,
    averageRating: raw.averageRating ?? raw.rating ?? 0,
    lastSession:
      raw.lastSession ??
      raw.lastSessionDate ??
      new Date().toISOString(),
    currentStreak: raw.currentStreak ?? raw.streak ?? 0,
    progress: raw.progress ?? raw.progressPercent ?? 0,
    level: raw.level ?? raw.studentLevel ?? 'Beginner',
    goals: Array.isArray(raw.goals) ? raw.goals : [],
    sessionHistory: Array.isArray(raw.sessionHistory)
      ? raw.sessionHistory
      : undefined,
  };
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
async function requestStudents(path: string): Promise<Student[]> {
  const raw = await request<unknown>(path);
  if (!Array.isArray(raw)) return [];
  return raw.map(normalizeStudent);
}

// ─── API Functions β€” exact endpoints from backend docs ────────────────────────

/**
 * GET /api/sheikh/my-students
 * All students the sheikh has had sessions with
 */
export function getAllStudents(): Promise<Student[]> {
  return requestStudents('/api/sheikh/my-students');
}

/**
 * GET /api/sheikh/my-students/active-last-7-days
 * Students active in the past 7 days
 */
export function getActiveStudents(): Promise<Student[]> {
  return requestStudents('/api/sheikh/my-students/active-last-7-days');
}

/**
 * GET /api/sheikh/my-students/top-students
 * Top performing students
 */
export function getTopStudents(): Promise<Student[]> {
  return requestStudents('/api/sheikh/my-students/top-students');
}

/**
 * GET /api/sheikh/my-students/search?keyword=query
 * Search students by keyword
 */
export function searchStudents(keyword: string): Promise<Student[]> {
  return requestStudents(
    `/api/sheikh/my-students/search?keyword=${encodeURIComponent(keyword)}`,
  );
}

/**
 * GET /api/sheikh/my-students/View-Student?id=id
 * Detailed info for a specific student
 */
export async function getStudentDetails(id: number): Promise<Student> {
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  const raw = await request<any>(
    `/api/sheikh/my-students/View-Student?id=${id}`,
  );
  return normalizeStudent(raw);
}

/**
 * GET /api/sheikh/my-students/stats
 * Aggregate statistics for all the sheikh's students
 */
export async function getStudentsStats(): Promise<StudentsStats> {
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  const raw = await request<any>('/api/sheikh/my-students/stats');
  return {
    totalStudents:
      raw.totalStudents ?? raw.total ?? 0,
    activeLast7Days:
      raw.activeLast7Days ?? raw.activeStudents ?? raw.active ?? 0,
    totalSessions:
      raw.totalSessions ?? raw.sessions ?? 0,
    averageRating:
      raw.averageRating ?? raw.avgRating ?? raw.rating ?? 0,
  };
}