userSettingService.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // web/services/userSettingService.ts
  2. import { API_BASE_URL } from '../utils/constants';
  3. import { AppSettings } from '../types'; // Frontend AppSettings interface
  4. // Assuming backend returns all AppSettings fields, plus id, userId, createdAt, updatedAt
  5. interface UserSettingResponse extends AppSettings {
  6. id: string;
  7. userId: string;
  8. createdAt: Date;
  9. updatedAt: Date;
  10. }
  11. export const userSettingService = {
  12. async get(token: string): Promise<UserSettingResponse> {
  13. const response = await fetch(`${API_BASE_URL}/settings`, {
  14. method: 'GET',
  15. headers: {
  16. 'Content-Type': 'application/json',
  17. Authorization: `Bearer ${token}`,
  18. },
  19. });
  20. if (!response.ok) {
  21. const errorData = await response.json();
  22. throw new Error(errorData.message || 'Failed to fetch user settings');
  23. }
  24. return response.json();
  25. },
  26. async update(token: string, settings: Partial<AppSettings>): Promise<UserSettingResponse> {
  27. const response = await fetch(`${API_BASE_URL}/settings`, {
  28. method: 'PUT',
  29. headers: {
  30. 'Content-Type': 'application/json',
  31. Authorization: `Bearer ${token}`,
  32. },
  33. body: JSON.stringify(settings),
  34. });
  35. if (!response.ok) {
  36. const errorData = await response.json();
  37. throw new Error(errorData.message || 'Failed to update user settings');
  38. }
  39. return response.json();
  40. },
  41. async getGlobal(token: string): Promise<UserSettingResponse> {
  42. const response = await fetch(`${API_BASE_URL}/settings/global`, {
  43. method: 'GET',
  44. headers: {
  45. 'Content-Type': 'application/json',
  46. Authorization: `Bearer ${token}`,
  47. },
  48. });
  49. if (!response.ok) {
  50. const errorData = await response.json();
  51. throw new Error(errorData.message || 'Failed to fetch global settings');
  52. }
  53. return response.json();
  54. },
  55. async getTenant(token: string): Promise<Partial<UserSettingResponse>> {
  56. const response = await fetch(`${API_BASE_URL}/settings/tenant`, {
  57. method: 'GET',
  58. headers: {
  59. 'Content-Type': 'application/json',
  60. Authorization: `Bearer ${token}`,
  61. },
  62. });
  63. if (!response.ok) {
  64. const errorData = await response.json();
  65. throw new Error(errorData.message || 'Failed to fetch tenant settings');
  66. }
  67. return response.json();
  68. },
  69. async updateGlobal(token: string, settings: Partial<AppSettings>): Promise<UserSettingResponse> {
  70. const response = await fetch(`${API_BASE_URL}/settings/global`, {
  71. method: 'PUT',
  72. headers: {
  73. 'Content-Type': 'application/json',
  74. Authorization: `Bearer ${token}`,
  75. },
  76. body: JSON.stringify(settings),
  77. });
  78. if (!response.ok) {
  79. const errorData = await response.json();
  80. throw new Error(errorData.message || 'Failed to update global settings');
  81. }
  82. return response.json();
  83. },
  84. };