refactor phase 6

This commit is contained in:
alorig
2025-11-20 21:47:03 +05:00
parent b0409d965b
commit 45dc0d1fa2
5 changed files with 305 additions and 121 deletions

View File

@@ -1476,32 +1476,108 @@ export interface AccountSettingsResponse {
results: AccountSetting[];
}
export type AccountSettingsErrorType =
| 'ACCOUNT_SETTINGS_API_ERROR'
| 'ACCOUNT_SETTINGS_NOT_FOUND'
| 'ACCOUNT_SETTINGS_VALIDATION_ERROR';
export class AccountSettingsError extends Error {
type: AccountSettingsErrorType;
status?: number;
details?: unknown;
constructor(type: AccountSettingsErrorType, message: string, status?: number, details?: unknown) {
super(message);
this.name = 'AccountSettingsError';
this.type = type;
this.status = status;
this.details = details;
}
}
function buildAccountSettingsError(error: any, fallbackMessage: string): AccountSettingsError {
const status = error?.status;
const response = error?.response;
const details = response || error;
if (status === 404) {
return new AccountSettingsError(
'ACCOUNT_SETTINGS_NOT_FOUND',
'No account settings were found for this account yet.',
status,
details
);
}
if (status === 400 || response?.errors) {
const validationMessage =
response?.error ||
response?.message ||
response?.detail ||
'The account settings request is invalid. Please review the submitted data.';
return new AccountSettingsError(
'ACCOUNT_SETTINGS_VALIDATION_ERROR',
validationMessage,
status,
details
);
}
return new AccountSettingsError(
'ACCOUNT_SETTINGS_API_ERROR',
error?.message || fallbackMessage,
status,
details
);
}
export async function fetchAccountSettings(): Promise<AccountSettingsResponse> {
return fetchAPI('/v1/system/settings/account/');
try {
return await fetchAPI('/v1/system/settings/account/');
} catch (error: any) {
throw buildAccountSettingsError(error, 'Unable to load account settings right now.');
}
}
export async function fetchAccountSetting(key: string): Promise<AccountSetting> {
return fetchAPI(`/v1/system/settings/account/${key}/`);
try {
return await fetchAPI(`/v1/system/settings/account/${key}/`);
} catch (error: any) {
throw buildAccountSettingsError(error, `Account setting "${key}" is not available.`);
}
}
export async function createAccountSetting(data: { key: string; config: Record<string, any>; is_active?: boolean }): Promise<AccountSetting> {
return fetchAPI('/v1/system/settings/account/', {
method: 'POST',
body: JSON.stringify(data),
});
try {
return await fetchAPI('/v1/system/settings/account/', {
method: 'POST',
body: JSON.stringify(data),
});
} catch (error: any) {
throw buildAccountSettingsError(error, 'Unable to create the account setting.');
}
}
export async function updateAccountSetting(key: string, data: Partial<{ config: Record<string, any>; is_active: boolean }>): Promise<AccountSetting> {
return fetchAPI(`/v1/system/settings/account/${key}/`, {
method: 'PUT',
body: JSON.stringify(data),
});
try {
return await fetchAPI(`/v1/system/settings/account/${key}/`, {
method: 'PUT',
body: JSON.stringify(data),
});
} catch (error: any) {
throw buildAccountSettingsError(error, `Unable to update account setting "${key}".`);
}
}
export async function deleteAccountSetting(key: string): Promise<void> {
return fetchAPI(`/v1/system/settings/account/${key}/`, {
method: 'DELETE',
});
try {
await fetchAPI(`/v1/system/settings/account/${key}/`, {
method: 'DELETE',
});
} catch (error: any) {
throw buildAccountSettingsError(error, `Unable to delete account setting "${key}".`);
}
}
// Module Settings