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

348 lines
7.6 KiB
TypeScript
Raw Normal View History

import { ref, watch } from 'vue';
2024-04-03 10:38:35 +07:00
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';
2024-04-10 20:28:28 +07:00
import { Branch, BranchCreate } from './types';
import { BranchContact } from '../branch-contact/types';
type BranchId = string;
2024-04-03 10:38:35 +07:00
const useBranchStore = defineStore('api-branch', () => {
const data = ref<Pagination<Branch[]>>({
result: [],
page: 0,
pageSize: 0,
total: 0,
});
const map = ref<Record<BranchId, Branch & { contact?: BranchContact[] }>>({});
2024-04-12 22:08:01 +07:00
const contact: Record<BranchId, BranchContact[]> = {};
watch(data, () => {
data.value.result.forEach((v) => {
map.value[v.id] = v;
});
});
2024-04-03 10:38:35 +07:00
async function fetchList<
Options extends {
2024-04-03 17:47:58 +07:00
page?: number;
pageSize?: number;
zipCode?: string;
query?: string;
tree?: boolean;
filter?: 'head' | 'sub';
2024-04-03 17:47:58 +07:00
},
Data extends Pagination<Branch[]>,
>(
opts?: Options,
2024-04-03 17:47:58 +07:00
flow?: {
sessionId: string;
refTransactionId: string;
transactionId: string;
},
): Promise<Data | false> {
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<Data>(
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 res.data;
2024-04-03 10:38:35 +07:00
}
return false;
}
async function fetchById<
2024-04-12 22:08:01 +07:00
Options extends { includeSubBranch?: boolean; includeContact?: boolean },
Data extends Branch &
2024-04-12 22:08:01 +07:00
(Options['includeSubBranch'] extends true
? { branch: Branch[] }
: unknown) &
(Options['includeContact'] extends true
? { contact: BranchContact[] }
: unknown),
>(
2024-04-03 17:47:58 +07:00
id: string,
opts?: Options,
2024-04-03 17:47:58 +07:00
flow?: {
sessionId: string;
refTransactionId: string;
transactionId: string;
},
): Promise<Data | false> {
const params = new URLSearchParams();
2024-04-12 22:08:01 +07:00
for (const [k, v] of Object.entries(opts || {})) {
v !== undefined && params.append(k, v.toString());
}
const query = params.toString();
const res = await api.get<Data>(
`/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;
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;
2024-04-10 20:28:28 +07:00
}>('/branch/stats', {
2024-04-10 17:41:41 +07:00
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-12 22:08:01 +07:00
async function getContact(
branchId: BranchId,
force = false,
flow?: {
sessionId: string;
refTransactionId: string;
transactionId: string;
},
) {
if (!force && contact[branchId]) return contact[branchId];
const res = await fetchById(branchId, { includeContact: true }, flow);
if (res) {
contact[branchId] = res.contact;
return res.contact;
}
return false;
}
2024-04-03 10:38:35 +07:00
return {
data,
map,
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-12 22:08:01 +07:00
getContact,
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;