feat: 新增会计应用核心TypeScript类型定义和设置服务
This commit is contained in:
@@ -13,15 +13,43 @@ import type {
|
|||||||
UpdateDefaultAccountsInput,
|
UpdateDefaultAccountsInput,
|
||||||
} from '../types';
|
} from '../types';
|
||||||
|
|
||||||
|
// Helper to map backend snake_case to frontend camelCase
|
||||||
|
function mapSettingsResponse(data: any): UserSettings {
|
||||||
|
return {
|
||||||
|
id: data.id,
|
||||||
|
userId: data.user_id,
|
||||||
|
preciseTimeEnabled: data.precise_time_enabled,
|
||||||
|
iconLayout: data.icon_layout,
|
||||||
|
imageCompression: data.image_compression,
|
||||||
|
showReimbursementBtn: data.show_reimbursement_btn,
|
||||||
|
showRefundBtn: data.show_refund_btn,
|
||||||
|
currentLedgerId: data.current_ledger_id,
|
||||||
|
createdAt: data.created_at,
|
||||||
|
updatedAt: data.updated_at,
|
||||||
|
// Add these just in case they are returned in the main object
|
||||||
|
defaultExpenseAccountId: data.default_expense_account_id,
|
||||||
|
defaultIncomeAccountId: data.default_income_account_id,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function mapDefaultAccountsResponse(data: any): DefaultAccountsSettings {
|
||||||
|
return {
|
||||||
|
defaultExpenseAccountId: data.default_expense_account_id,
|
||||||
|
defaultIncomeAccountId: data.default_income_account_id,
|
||||||
|
defaultExpenseAccount: data.default_expense_account,
|
||||||
|
defaultIncomeAccount: data.default_income_account,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get user settings
|
* Get user settings
|
||||||
*/
|
*/
|
||||||
export async function getSettings(): Promise<UserSettings> {
|
export async function getSettings(): Promise<UserSettings> {
|
||||||
const response = await api.get<ApiResponse<UserSettings>>('/settings');
|
const response = await api.get<ApiResponse<any>>('/settings');
|
||||||
if (!response.data) {
|
if (!response.data) {
|
||||||
throw new Error(response.error || 'Failed to get settings');
|
throw new Error(response.error || 'Failed to get settings');
|
||||||
}
|
}
|
||||||
return response.data;
|
return mapSettingsResponse(response.data);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -37,11 +65,11 @@ export async function updateSettings(data: Partial<UserSettings>): Promise<UserS
|
|||||||
if (data.showRefundBtn !== undefined) payload.show_refund_btn = data.showRefundBtn;
|
if (data.showRefundBtn !== undefined) payload.show_refund_btn = data.showRefundBtn;
|
||||||
if (data.currentLedgerId !== undefined) payload.current_ledger_id = data.currentLedgerId;
|
if (data.currentLedgerId !== undefined) payload.current_ledger_id = data.currentLedgerId;
|
||||||
|
|
||||||
const response = await api.put<ApiResponse<UserSettings>>('/settings', payload);
|
const response = await api.put<ApiResponse<any>>('/settings', payload);
|
||||||
if (!response.data) {
|
if (!response.data) {
|
||||||
throw new Error(response.error || 'Failed to update settings');
|
throw new Error(response.error || 'Failed to update settings');
|
||||||
}
|
}
|
||||||
return response.data;
|
return mapSettingsResponse(response.data);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -49,11 +77,11 @@ export async function updateSettings(data: Partial<UserSettings>): Promise<UserS
|
|||||||
* Validates: Requirements 11.1, 11.3
|
* Validates: Requirements 11.1, 11.3
|
||||||
*/
|
*/
|
||||||
export async function getDefaultAccounts(): Promise<DefaultAccountsSettings> {
|
export async function getDefaultAccounts(): Promise<DefaultAccountsSettings> {
|
||||||
const response = await api.get<ApiResponse<DefaultAccountsSettings>>('/settings/default-accounts');
|
const response = await api.get<ApiResponse<any>>('/settings/default-accounts');
|
||||||
if (!response.data) {
|
if (!response.data) {
|
||||||
throw new Error(response.error || 'Failed to get default accounts');
|
throw new Error(response.error || 'Failed to get default accounts');
|
||||||
}
|
}
|
||||||
return response.data;
|
return mapDefaultAccountsResponse(response.data);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -72,14 +100,14 @@ export async function updateDefaultAccounts(
|
|||||||
payload.default_income_account_id = data.defaultIncomeAccountId;
|
payload.default_income_account_id = data.defaultIncomeAccountId;
|
||||||
}
|
}
|
||||||
|
|
||||||
const response = await api.put<ApiResponse<DefaultAccountsSettings>>(
|
const response = await api.put<ApiResponse<any>>(
|
||||||
'/settings/default-accounts',
|
'/settings/default-accounts',
|
||||||
payload
|
payload
|
||||||
);
|
);
|
||||||
if (!response.data) {
|
if (!response.data) {
|
||||||
throw new Error(response.error || 'Failed to update default accounts');
|
throw new Error(response.error || 'Failed to update default accounts');
|
||||||
}
|
}
|
||||||
return response.data;
|
return mapDefaultAccountsResponse(response.data);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -72,6 +72,11 @@ export interface UserSettings {
|
|||||||
currentLedgerId?: number;
|
currentLedgerId?: number;
|
||||||
createdAt: string;
|
createdAt: string;
|
||||||
updatedAt: string;
|
updatedAt: string;
|
||||||
|
// Default accounts
|
||||||
|
defaultExpenseAccountId?: number | null;
|
||||||
|
defaultIncomeAccountId?: number | null;
|
||||||
|
defaultExpenseAccount?: Account | null;
|
||||||
|
defaultIncomeAccount?: Account | null;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Tag Interface
|
// Tag Interface
|
||||||
|
|||||||
Reference in New Issue
Block a user