241 lines
5.3 KiB
TypeScript
241 lines
5.3 KiB
TypeScript
import { ref } from 'vue';
|
|
import { defineStore } from 'pinia';
|
|
import { Pagination } from '../types';
|
|
import { api } from 'src/boot/axios';
|
|
import { User, UserCreate } from './types';
|
|
import axios from 'axios';
|
|
|
|
const useUserStore = defineStore('api-user', () => {
|
|
const data = ref<Pagination<User[]>>();
|
|
|
|
async function fetchList(
|
|
opts?: {
|
|
page?: number;
|
|
pageSize?: number;
|
|
zipCode?: string;
|
|
query?: string;
|
|
},
|
|
flow?: {
|
|
sessionId: string;
|
|
refTransactionId: string;
|
|
transactionId: string;
|
|
},
|
|
) {
|
|
const params = new URLSearchParams();
|
|
|
|
if (opts?.pageSize && opts?.pageSize > 0) {
|
|
params.append('pageSize', `${opts.pageSize}`);
|
|
}
|
|
if (opts?.page && opts.page > 0) params.append('page', `${opts.page}`);
|
|
if (opts?.zipCode) params.append('zipCode', opts.zipCode);
|
|
if (opts?.query) params.append('query', opts.query);
|
|
|
|
const query = params.toString();
|
|
|
|
const res = await api.get<Pagination<User[]>>(
|
|
`/user${(params && '?'.concat(query)) || ''}`,
|
|
{
|
|
headers: {
|
|
'X-Session-Id': flow?.sessionId,
|
|
'X-Rtid': flow?.refTransactionId,
|
|
'X-Tid': flow?.transactionId,
|
|
},
|
|
},
|
|
);
|
|
|
|
if (res && res.status === 200) {
|
|
data.value = res.data;
|
|
return data.value;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
async function fetchById(
|
|
id: string,
|
|
flow?: {
|
|
sessionId: string;
|
|
refTransactionId: string;
|
|
transactionId: string;
|
|
},
|
|
) {
|
|
const res = await api.get<User>(`/user/${id}`, {
|
|
headers: {
|
|
'X-Session-Id': flow?.sessionId,
|
|
'X-Rtid': flow?.refTransactionId,
|
|
'X-Tid': flow?.transactionId,
|
|
},
|
|
});
|
|
|
|
if (!res) return false;
|
|
if (res.status === 200) return res.data;
|
|
if (res.status === 204) return null;
|
|
|
|
return false;
|
|
}
|
|
|
|
async function create(
|
|
data: UserCreate,
|
|
flow?: {
|
|
sessionId: string;
|
|
refTransactionId: string;
|
|
transactionId: string;
|
|
},
|
|
) {
|
|
const { profileImage, ...payload } = data;
|
|
const res = await api.post<User & { profileImageUploadUrl: string }>(
|
|
'/user',
|
|
payload,
|
|
{
|
|
headers: {
|
|
'X-Session-Id': flow?.sessionId,
|
|
'X-Rtid': flow?.refTransactionId,
|
|
'X-Tid': flow?.transactionId,
|
|
},
|
|
},
|
|
);
|
|
|
|
await axios
|
|
.put(res.data.profileImageUploadUrl, profileImage, {
|
|
headers: { 'Content-Type': profileImage.type },
|
|
onUploadProgress: (e) => console.log(e),
|
|
})
|
|
.catch((e) => console.error(e));
|
|
|
|
if (!res) return false;
|
|
|
|
return res.data;
|
|
}
|
|
|
|
async function editById(
|
|
id: string,
|
|
data: Partial<Omit<UserCreate, 'keycloakId'>>,
|
|
flow?: {
|
|
sessionId: string;
|
|
refTransactionId: string;
|
|
transactionId: string;
|
|
},
|
|
) {
|
|
const res = await api.put<User>(`/user/${id}`, data, {
|
|
headers: {
|
|
'X-Session-Id': flow?.sessionId,
|
|
'X-Rtid': flow?.refTransactionId,
|
|
'X-Tid': flow?.transactionId,
|
|
},
|
|
});
|
|
|
|
if (!res) return false;
|
|
|
|
return res.data;
|
|
}
|
|
|
|
async function deleteById(
|
|
id: string,
|
|
flow?: {
|
|
sessionId: string;
|
|
refTransactionId: string;
|
|
transactionId: string;
|
|
},
|
|
) {
|
|
const res = await api.delete<User>(`/user/${id}`, {
|
|
headers: {
|
|
'X-Session-Id': flow?.sessionId,
|
|
'X-Rtid': flow?.refTransactionId,
|
|
'X-Tid': flow?.transactionId,
|
|
},
|
|
});
|
|
|
|
if (!res) return false;
|
|
if (res.status === 200) return res.data;
|
|
|
|
return false;
|
|
}
|
|
|
|
async function getBranch(
|
|
userId: string,
|
|
flow?: {
|
|
sessionId: string;
|
|
refTransactionId: string;
|
|
transactionId: string;
|
|
},
|
|
) {
|
|
const res = await api.get(`/user/${userId}/branch`, {
|
|
headers: {
|
|
'X-Session-Id': flow?.sessionId,
|
|
'X-Rtid': flow?.refTransactionId,
|
|
'X-Tid': flow?.transactionId,
|
|
},
|
|
});
|
|
|
|
if (!res) return false;
|
|
if (res.status === 200) return res.data;
|
|
|
|
return false;
|
|
}
|
|
|
|
async function addBranch(
|
|
userId: string,
|
|
branchId: string | string[],
|
|
flow?: {
|
|
sessionId: string;
|
|
refTransactionId: string;
|
|
transactionId: string;
|
|
},
|
|
) {
|
|
const res = await api.post(
|
|
`/user/${userId}/branch`,
|
|
{ branch: ([] as string[]).concat(branchId) },
|
|
{
|
|
headers: {
|
|
'X-Session-Id': flow?.sessionId,
|
|
'X-Rtid': flow?.refTransactionId,
|
|
'X-Tid': flow?.transactionId,
|
|
},
|
|
},
|
|
);
|
|
|
|
if (!res) return false;
|
|
if (res.status >= 400) return false;
|
|
|
|
return res.data || true;
|
|
}
|
|
|
|
async function removeBranch(
|
|
userId: string,
|
|
branchId: string | string[],
|
|
flow?: {
|
|
sessionId: string;
|
|
refTransactionId: string;
|
|
transactionId: string;
|
|
},
|
|
) {
|
|
const res = await api.delete(`/user/${userId}/branch`, {
|
|
headers: {
|
|
'X-Session-Id': flow?.sessionId,
|
|
'X-Rtid': flow?.refTransactionId,
|
|
'X-Tid': flow?.transactionId,
|
|
},
|
|
data: { branch: ([] as string[]).concat(branchId) },
|
|
});
|
|
|
|
if (!res) return false;
|
|
if (res.status >= 400) return false;
|
|
|
|
return res.data || true;
|
|
}
|
|
|
|
return {
|
|
data,
|
|
fetchList,
|
|
fetchById,
|
|
|
|
create,
|
|
editById,
|
|
deleteById,
|
|
|
|
getBranch,
|
|
addBranch,
|
|
removeBranch,
|
|
};
|
|
});
|
|
|
|
export default useUserStore;
|