2021-07-24 18:23:33 +00:00
|
|
|
import http from '@/api/http';
|
|
|
|
import { User, rawDataToUser } from '@/api/admin/users/getUsers';
|
|
|
|
|
|
|
|
export interface Values {
|
|
|
|
username: string;
|
|
|
|
email: string;
|
|
|
|
password: string;
|
2021-07-25 21:51:39 +00:00
|
|
|
adminRoleId: number | null;
|
2021-09-12 23:13:00 +00:00
|
|
|
rootAdmin: boolean;
|
2021-07-24 18:23:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export default (id: number, values: Partial<Values>, include: string[] = []): Promise<User> => {
|
|
|
|
const data = {};
|
|
|
|
Object.keys(values).forEach(k => {
|
2021-09-12 23:24:31 +00:00
|
|
|
// Don't set password if it is empty.
|
|
|
|
if (k === 'password' && values[k] === '') {
|
|
|
|
return;
|
|
|
|
}
|
2021-07-24 18:23:33 +00:00
|
|
|
// @ts-ignore
|
|
|
|
data[k.replace(/[A-Z]/g, l => `_${l.toLowerCase()}`)] = values[k];
|
|
|
|
});
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
http.patch(`/api/application/users/${id}`, data, { params: { include: include.join(',') } })
|
|
|
|
.then(({ data }) => resolve(rawDataToUser(data)))
|
|
|
|
.catch(reject);
|
|
|
|
});
|
|
|
|
};
|