Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: settings api [WTEL-4024] #549

Merged
merged 1 commit into from
Nov 9, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 51 additions & 19 deletions src/modules/settings/api/settings.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,71 @@
import { EndpointPatcherApiConsumer } from 'webitel-sdk/esm2015/api-consumers';
import instance, { config } from '../../../app/api/old/instance';
import instance from '../../../app/api/instance';
import applyTransform, {
camelToSnake,
notify,
snakeToCamel
} from '@webitel/ui-sdk/src/api/transformers';

const baseUrl = 'users';
const itemPatcher = new EndpointPatcherApiConsumer({ baseUrl, instance });

export const changePassword = ({ id, changes }) => itemPatcher.patchItem({
id,
changes,
});
export const getWebPhone = async () => {

export const changeWebPhone = async (changes) => {
const url = 'user/settings/phone';

try {
config.errors.silent = true;
return await instance.put(url, changes);
const response = await instance.get(url);
return applyTransform(response.data, [
snakeToCamel(),
]);
} catch (err) {
throw err;
} finally {
config.errors.silent = false;
throw applyTransform(err, [
notify,
]);
}
};

export const getWebPhone = async () => {
export const changeWebPhone = async (changes) => {

const item = applyTransform(changes, [
camelToSnake(),
]);

const url = 'user/settings/phone';

try {
config.errors.silent = true;
return await instance.get(url);
const response = await instance.put(url, item);
return applyTransform(response.data, [
snakeToCamel(),
]);
} catch (err) {
throw err;
} finally {
config.errors.silent = false;
throw applyTransform(err, [
notify,
]);
}
};

const patchItem = async ({ changes, id }) => {
const body = applyTransform(changes, [
camelToSnake(),
]);
const url = `${baseUrl}/${id}`;
try {
const response = await instance.patch(url, body);
return applyTransform(response.data, [
snakeToCamel(),
]);
} catch (err) {
throw applyTransform(err, [
notify,
]);
}
};

export const changePassword = ({ id, changes }) => patchItem({
id,
changes,
});


export default {
changePassword,
changeWebPhone,
Expand Down