294 lines
6.5 KiB
TypeScript
294 lines
6.5 KiB
TypeScript
import { ref } from 'vue';
|
|
import { defineStore } from 'pinia';
|
|
import { Pagination } from '../types';
|
|
import { api } from 'src/boot/axios';
|
|
import { Branch, BranchCreate, BranchWithChildren } from './types';
|
|
|
|
const useBranchStore = defineStore('api-branch', () => {
|
|
const data = ref<Pagination<Branch[]>>();
|
|
|
|
async function fetchList<T extends Branch>(
|
|
opts?: {
|
|
page?: number;
|
|
pageSize?: number;
|
|
zipCode?: string;
|
|
query?: string;
|
|
tree?: boolean;
|
|
filter?: 'head' | 'sub';
|
|
},
|
|
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);
|
|
if (opts?.filter) params.append('filter', opts.filter);
|
|
if (opts?.tree) params.append('tree', 'true');
|
|
|
|
const query = params.toString();
|
|
|
|
const res = await api.get<Pagination<T[]>>(
|
|
`/branch${(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 as Pagination<T[]>;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
async function fetchById<T extends Branch>(
|
|
id: string,
|
|
opts?: { includeSubBranch?: boolean },
|
|
flow?: {
|
|
sessionId: string;
|
|
refTransactionId: string;
|
|
transactionId: string;
|
|
},
|
|
) {
|
|
const params = new URLSearchParams();
|
|
|
|
if (opts?.includeSubBranch) params.append('includeSubBranch', 'true');
|
|
|
|
const query = params.toString();
|
|
|
|
const res = await api.get<T>(
|
|
`/branch/${id}${(params && '?'.concat(query)) || ''}`,
|
|
{
|
|
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(
|
|
branch: BranchCreate,
|
|
flow?: {
|
|
sessionId: string;
|
|
refTransactionId: string;
|
|
transactionId: string;
|
|
},
|
|
) {
|
|
const res = await api.post<
|
|
Branch & { qrCodeImageUrl: string; qrCodeImageUploadUrl: string }
|
|
>('/branch', branch, {
|
|
headers: {
|
|
'X-Session-Id': flow?.sessionId,
|
|
'X-Rtid': flow?.refTransactionId,
|
|
'X-Tid': flow?.transactionId,
|
|
},
|
|
});
|
|
|
|
if (!res) return false;
|
|
|
|
return res.data;
|
|
}
|
|
|
|
async function editById(
|
|
id: string,
|
|
branch: Partial<BranchCreate>,
|
|
flow?: {
|
|
sessionId: string;
|
|
refTransactionId: string;
|
|
transactionId: string;
|
|
},
|
|
) {
|
|
const res = await api.put<Branch>(`/branch/${id}`, branch, {
|
|
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<Branch>(`/branch/${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 getUser(
|
|
branchId: string,
|
|
flow?: {
|
|
sessionId: string;
|
|
refTransactionId: string;
|
|
transactionId: string;
|
|
},
|
|
) {
|
|
const res = await api.get(`/branch/${branchId}/user`, {
|
|
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 addUser(
|
|
branchId: string,
|
|
userId: string | string[],
|
|
flow?: {
|
|
sessionId: string;
|
|
refTransactionId: string;
|
|
transactionId: string;
|
|
},
|
|
) {
|
|
const res = await api.post(
|
|
`/branch/${branchId}/user`,
|
|
{ user: ([] as string[]).concat(userId) },
|
|
{
|
|
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 removeUser(
|
|
branchId: string,
|
|
userId: string | string[],
|
|
flow?: {
|
|
sessionId: string;
|
|
refTransactionId: string;
|
|
transactionId: string;
|
|
},
|
|
) {
|
|
const res = await api.delete(`/branch/${branchId}/user`, {
|
|
headers: {
|
|
'X-Session-Id': flow?.sessionId,
|
|
'X-Rtid': flow?.refTransactionId,
|
|
'X-Tid': flow?.transactionId,
|
|
},
|
|
data: { user: ([] as string[]).concat(userId) },
|
|
});
|
|
|
|
if (!res) return false;
|
|
if (res.status === 200) return res.data;
|
|
|
|
return false;
|
|
}
|
|
|
|
async function stats(flow?: {
|
|
sessionId: string;
|
|
refTransactionId: string;
|
|
transactionId: string;
|
|
}) {
|
|
const res = await api.get<{
|
|
hq: number;
|
|
br: number;
|
|
}>(`/branch/stats`, {
|
|
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 userStats(
|
|
userType: string,
|
|
flow?: {
|
|
sessionId: string;
|
|
refTransactionId: string;
|
|
transactionId: string;
|
|
},
|
|
) {
|
|
const res = await api.get(
|
|
`/branch/user-stats${userType ? '?'.concat(userType) : ''}`,
|
|
{
|
|
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;
|
|
}
|
|
|
|
return {
|
|
data,
|
|
fetchList,
|
|
fetchById,
|
|
|
|
create,
|
|
editById,
|
|
deleteById,
|
|
|
|
getUser,
|
|
addUser,
|
|
removeUser,
|
|
|
|
stats,
|
|
userStats,
|
|
};
|
|
});
|
|
|
|
export default useBranchStore;
|