jws-frontend/src/stores/branch/index.ts

295 lines
6.5 KiB
TypeScript
Raw Normal View History

2024-04-03 10:38:35 +07:00
import { ref } from 'vue';
import { defineStore } from 'pinia';
2024-04-04 11:46:25 +07:00
import { Pagination } from '../types';
2024-04-03 10:38:35 +07:00
import { api } from 'src/boot/axios';
import { Branch, BranchCreate, BranchWithChildren } from './types';
2024-04-03 10:38:35 +07:00
const useBranchStore = defineStore('api-branch', () => {
const data = ref<Pagination<Branch[]>>();
async function fetchList<T extends Branch>(
2024-04-03 17:47:58 +07:00
opts?: {
page?: number;
pageSize?: number;
zipCode?: string;
query?: string;
tree?: boolean;
filter?: 'head' | 'sub';
2024-04-03 17:47:58 +07:00
},
flow?: {
sessionId: string;
refTransactionId: string;
transactionId: string;
},
) {
2024-04-03 10:38:35 +07:00
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);
2024-04-10 14:56:57 +07:00
if (opts?.filter) params.append('filter', opts.filter);
if (opts?.tree) params.append('tree', 'true');
2024-04-03 10:38:35 +07:00
const query = params.toString();
const res = await api.get<Pagination<T[]>>(
2024-04-03 10:38:35 +07:00
`/branch${(params && '?'.concat(query)) || ''}`,
2024-04-03 17:47:58 +07:00
{
headers: {
'X-Session-Id': flow?.sessionId,
'X-Rtid': flow?.refTransactionId,
'X-Tid': flow?.transactionId,
},
},
2024-04-03 10:38:35 +07:00
);
if (res && res.status === 200) {
data.value = res.data;
return data.value as Pagination<T[]>;
2024-04-03 10:38:35 +07:00
}
return false;
}
async function fetchById<T extends Branch>(
2024-04-03 17:47:58 +07:00
id: string,
opts?: { includeSubBranch?: boolean },
2024-04-03 17:47:58 +07:00
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,
},
2024-04-03 17:47:58 +07:00
},
);
2024-04-03 10:38:35 +07:00
if (!res) return false;
if (res.status === 200) return res.data;
if (res.status === 204) return null;
return false;
}
2024-04-04 11:46:25 +07:00
async function create(
branch: BranchCreate,
2024-04-03 17:47:58 +07:00
flow?: {
sessionId: string;
refTransactionId: string;
transactionId: string;
},
) {
2024-04-04 11:46:25 +07:00
const res = await api.post<
Branch & { qrCodeImageUrl: string; qrCodeImageUploadUrl: string }
>('/branch', branch, {
2024-04-03 17:47:58 +07:00
headers: {
'X-Session-Id': flow?.sessionId,
'X-Rtid': flow?.refTransactionId,
'X-Tid': flow?.transactionId,
},
});
2024-04-03 10:38:35 +07:00
if (!res) return false;
return res.data;
}
2024-04-04 11:46:25 +07:00
async function editById(
2024-04-03 10:38:35 +07:00
id: string,
2024-04-04 11:46:25 +07:00
branch: Partial<BranchCreate>,
2024-04-03 17:47:58 +07:00
flow?: {
sessionId: string;
refTransactionId: string;
transactionId: string;
},
2024-04-03 10:38:35 +07:00
) {
2024-04-03 17:47:58 +07:00
const res = await api.put<Branch>(`/branch/${id}`, branch, {
headers: {
'X-Session-Id': flow?.sessionId,
'X-Rtid': flow?.refTransactionId,
'X-Tid': flow?.transactionId,
},
});
2024-04-03 10:38:35 +07:00
if (!res) return false;
return res.data;
}
2024-04-04 11:46:25 +07:00
async function deleteById(
2024-04-03 17:47:58 +07:00
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) },
});
2024-04-03 10:38:35 +07:00
if (!res) return false;
if (res.status === 200) return res.data;
return false;
}
2024-04-10 17:41:41 +07:00
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;
}
2024-04-10 13:59:13 +07:00
async function userStats(
userType: string,
flow?: {
sessionId: string;
refTransactionId: string;
transactionId: string;
},
) {
2024-04-10 17:41:41 +07:00
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,
},
2024-04-10 13:59:13 +07:00
},
2024-04-10 17:41:41 +07:00
);
2024-04-10 13:59:13 +07:00
if (!res) return false;
if (res.status === 200) return res.data;
return false;
}
2024-04-03 10:38:35 +07:00
return {
data,
2024-04-04 11:46:25 +07:00
fetchList,
fetchById,
2024-04-03 10:38:35 +07:00
2024-04-04 11:46:25 +07:00
create,
editById,
deleteById,
2024-04-03 17:47:58 +07:00
getUser,
addUser,
removeUser,
2024-04-10 13:59:13 +07:00
2024-04-10 17:41:41 +07:00
stats,
2024-04-10 13:59:13 +07:00
userStats,
2024-04-03 10:38:35 +07:00
};
});
export default useBranchStore;