2025-12-18 13:50:39 +08:00
|
|
|
/**
|
|
|
|
|
* User API endpoints
|
|
|
|
|
* Handles user profile management and password changes
|
|
|
|
|
*/
|
|
|
|
|
|
2025-12-25 08:41:30 -08:00
|
|
|
import { apiClient } from './client'
|
2026-04-13 00:52:42 +08:00
|
|
|
import type { User, ChangePasswordRequest, NotifyEmailEntry } from '@/types'
|
2025-12-18 13:50:39 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get current user profile
|
|
|
|
|
* @returns User profile data
|
|
|
|
|
*/
|
|
|
|
|
export async function getProfile(): Promise<User> {
|
2025-12-25 08:41:30 -08:00
|
|
|
const { data } = await apiClient.get<User>('/user/profile')
|
|
|
|
|
return data
|
2025-12-23 11:26:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Update current user profile
|
|
|
|
|
* @param profile - Profile data to update
|
|
|
|
|
* @returns Updated user profile data
|
|
|
|
|
*/
|
|
|
|
|
export async function updateProfile(profile: {
|
2025-12-25 08:41:30 -08:00
|
|
|
username?: string
|
2026-04-12 02:48:57 +08:00
|
|
|
balance_notify_enabled?: boolean
|
|
|
|
|
balance_notify_threshold?: number | null
|
2026-04-13 00:52:42 +08:00
|
|
|
balance_notify_extra_emails?: NotifyEmailEntry[]
|
2025-12-23 11:26:22 +08:00
|
|
|
}): Promise<User> {
|
2025-12-25 08:41:30 -08:00
|
|
|
const { data } = await apiClient.put<User>('/user', profile)
|
|
|
|
|
return data
|
2025-12-18 13:50:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Change current user password
|
|
|
|
|
* @param passwords - Old and new password
|
|
|
|
|
* @returns Success message
|
|
|
|
|
*/
|
|
|
|
|
export async function changePassword(
|
|
|
|
|
oldPassword: string,
|
|
|
|
|
newPassword: string
|
|
|
|
|
): Promise<{ message: string }> {
|
|
|
|
|
const payload: ChangePasswordRequest = {
|
|
|
|
|
old_password: oldPassword,
|
2025-12-25 08:41:30 -08:00
|
|
|
new_password: newPassword
|
|
|
|
|
}
|
2025-12-18 13:50:39 +08:00
|
|
|
|
2025-12-25 08:41:30 -08:00
|
|
|
const { data } = await apiClient.put<{ message: string }>('/user/password', payload)
|
|
|
|
|
return data
|
2025-12-18 13:50:39 +08:00
|
|
|
}
|
|
|
|
|
|
2026-04-12 02:48:57 +08:00
|
|
|
/**
|
|
|
|
|
* Send verification code for adding a notify email
|
|
|
|
|
* @param email - Email address to verify
|
|
|
|
|
*/
|
|
|
|
|
export async function sendNotifyEmailCode(email: string): Promise<void> {
|
|
|
|
|
await apiClient.post('/user/notify-email/send-code', { email })
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Verify and add a notify email
|
|
|
|
|
* @param email - Email address to add
|
|
|
|
|
* @param code - Verification code
|
|
|
|
|
*/
|
|
|
|
|
export async function verifyNotifyEmail(email: string, code: string): Promise<void> {
|
|
|
|
|
await apiClient.post('/user/notify-email/verify', { email, code })
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Remove a notify email
|
|
|
|
|
* @param email - Email address to remove
|
|
|
|
|
*/
|
|
|
|
|
export async function removeNotifyEmail(email: string): Promise<void> {
|
|
|
|
|
await apiClient.delete('/user/notify-email', { data: { email } })
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-13 00:52:42 +08:00
|
|
|
/**
|
|
|
|
|
* Toggle a notify email's disabled state
|
|
|
|
|
* @param email - Email address (empty string for primary email placeholder)
|
|
|
|
|
* @param disabled - Whether to disable the email
|
|
|
|
|
*/
|
|
|
|
|
export async function toggleNotifyEmail(email: string, disabled: boolean): Promise<User> {
|
|
|
|
|
const { data } = await apiClient.put<User>('/user/notify-email/toggle', { email, disabled })
|
|
|
|
|
return data
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-18 13:50:39 +08:00
|
|
|
export const userAPI = {
|
|
|
|
|
getProfile,
|
2025-12-23 11:26:22 +08:00
|
|
|
updateProfile,
|
2026-04-12 02:48:57 +08:00
|
|
|
changePassword,
|
|
|
|
|
sendNotifyEmailCode,
|
|
|
|
|
verifyNotifyEmail,
|
2026-04-13 00:52:42 +08:00
|
|
|
removeNotifyEmail,
|
|
|
|
|
toggleNotifyEmail
|
2025-12-25 08:41:30 -08:00
|
|
|
}
|
2025-12-18 13:50:39 +08:00
|
|
|
|
2025-12-25 08:41:30 -08:00
|
|
|
export default userAPI
|