Files
Account/src/features/profile/api/settingsApi.ts

174 lines
4.7 KiB
TypeScript

import apiClient from '@/lib/apiClient';
import {
type GetProfileApiResponse,
type SaveProfileApiResponse,
type PhoneNumberApiResponse,
type ConfirmPhoneNumberApiResponse,
type SaveSettingsApiResponse,
type DeleteSessionsApiResponse,
type PasswordApiResponse,
type SendEmailCodeApiResponse,
type ConfirmEmailCodeApiResponse,
type ChangeEmailApiResponse,
} from '../types/settingsApiType';
import { type InfoRowData } from '../types/settingsType';
export async function fetchProfile(): Promise<{ data: GetProfileApiResponse }> {
const res = await apiClient.post<GetProfileApiResponse>(
'/Profile/GetProfile',
{},
);
return { data: res.data };
}
export async function saveProfile(payload?: {
data: InfoRowData;
imageUrl: string | null;
}): Promise<{ data: SaveProfileApiResponse }> {
if (!payload) {
throw new Error('Payload for saving profile is missing.');
}
const res = await apiClient.post<SaveProfileApiResponse>(
'/Profile/SaveProfilePersonalInforamtion',
payload,
);
return { data: res.data };
}
export async function sendVerificationCode(payload?: {
phoneNumber: string;
}): Promise<{ data: PhoneNumberApiResponse }> {
if (!payload) {
throw new Error('Payload for sending verification code is missing.');
}
const res = await apiClient.post<PhoneNumberApiResponse>(
'/Profile/SendVerfiyPhoneNumberCode',
payload,
);
return { data: res.data };
}
export async function confirmPhoneNumberCode(payload?: {
phoneNumber: string;
verifyCode: string;
}): Promise<{ data: ConfirmPhoneNumberApiResponse }> {
if (!payload) {
throw new Error('Payload for confirming phone number is missing.');
}
const res = await apiClient.post<ConfirmPhoneNumberApiResponse>(
'/Profile/ConfirmPhoneNumberChangeCode',
payload,
);
return { data: res.data };
}
export async function changePhoneNumber(payload?: {
phoneNumber: string;
}): Promise<{ data: PhoneNumberApiResponse }> {
if (!payload) {
throw new Error('Payload for changing phone number is missing.');
}
const res = await apiClient.post<PhoneNumberApiResponse>(
'/Profile/ChangePhoneNumber',
payload,
);
return { data: res.data };
}
export async function saveSettings(payload?: {
theme: number;
calendarType: number;
language: number;
}): Promise<{ data: SaveSettingsApiResponse }> {
if (!payload) {
throw new Error('Payload for saving settings is missing.');
}
const res = await apiClient.post<SaveSettingsApiResponse>(
'/Profile/SaveSetting',
payload,
);
return { data: res.data };
}
export async function deleteSessions(payload?: {
keys: string[];
}): Promise<{ data: DeleteSessionsApiResponse }> {
if (!payload || payload.keys.length === 0) {
throw new Error('Payload with session keys is missing or empty.');
}
const res = await apiClient.post<DeleteSessionsApiResponse>(
'/Profile/DeleteSessions',
payload,
);
return { data: res.data };
}
export async function addPassword(payload?: {
password: string;
}): Promise<{ data: PasswordApiResponse }> {
if (!payload) {
throw new Error('Payload for adding password is missing.');
}
const res = await apiClient.post<PasswordApiResponse>(
'/Profile/AddPassword',
payload,
);
return { data: res.data };
}
export async function changePassword(payload?: {
oldPassword: string;
newPassword: string;
}): Promise<{ data: PasswordApiResponse }> {
if (!payload) {
throw new Error('Payload for changing password is missing.');
}
const res = await apiClient.post<PasswordApiResponse>(
'/Profile/ChangePassword',
payload,
);
return { data: res.data };
}
// ✅ NEW FUNCTIONS
export async function sendEmailCode(payload?: {
email: string;
}): Promise<{ data: SendEmailCodeApiResponse }> {
if (!payload) {
throw new Error('Payload for sending email code is missing.');
}
const res = await apiClient.post<SendEmailCodeApiResponse>(
'Profile/SendEmailChangeCode',
payload,
);
return { data: res.data };
}
export async function confirmEmailCode(payload?: {
email: string;
verifyCode: string;
}): Promise<{ data: ConfirmEmailCodeApiResponse }> {
if (!payload) {
throw new Error('Payload for confirming email code is missing.');
}
const res = await apiClient.post<ConfirmEmailCodeApiResponse>(
'Profile/ConfirmEmailChangeCode',
payload,
);
return { data: res.data };
}
export async function changeEmail(payload?: {
email: string;
}): Promise<{ data: ChangeEmailApiResponse }> {
if (!payload) {
throw new Error('Payload for changing email is missing.');
}
const res = await apiClient.post<ChangeEmailApiResponse>(
'Profile/ChangeEmail',
payload,
);
return { data: res.data };
}