2024-04-12 21:52:16 +07:00
|
|
|
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';
|
2024-04-12 21:52:16 +07:00
|
|
|
import { BranchContact } from '../branch-contact/types';
|
2024-04-18 13:56:22 +07:00
|
|
|
import axios from 'axios';
|
2024-04-12 21:52:16 +07:00
|
|
|
|
|
|
|
|
type BranchId = string;
|
2024-04-03 10:38:35 +07:00
|
|
|
|
|
|
|
|
const useBranchStore = defineStore('api-branch', () => {
|
2024-04-12 21:52:16 +07:00
|
|
|
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[]> = {};
|
2024-04-12 21:52:16 +07:00
|
|
|
|
|
|
|
|
watch(data, () => {
|
|
|
|
|
data.value.result.forEach((v) => {
|
|
|
|
|
map.value[v.id] = v;
|
|
|
|
|
});
|
|
|
|
|
});
|
2024-04-03 10:38:35 +07:00
|
|
|
|
2024-04-12 21:52:16 +07:00
|
|
|
async function fetchList<
|
|
|
|
|
Options extends {
|
2024-04-03 17:47:58 +07:00
|
|
|
page?: number;
|
|
|
|
|
pageSize?: number;
|
|
|
|
|
zipCode?: string;
|
|
|
|
|
query?: string;
|
2024-04-09 15:46:33 +07:00
|
|
|
tree?: boolean;
|
|
|
|
|
filter?: 'head' | 'sub';
|
2024-04-03 17:47:58 +07:00
|
|
|
},
|
2024-04-12 21:52:16 +07:00
|
|
|
Data extends Pagination<Branch[]>,
|
|
|
|
|
>(
|
|
|
|
|
opts?: Options,
|
2024-04-03 17:47:58 +07:00
|
|
|
flow?: {
|
|
|
|
|
sessionId: string;
|
|
|
|
|
refTransactionId: string;
|
|
|
|
|
transactionId: string;
|
|
|
|
|
},
|
2024-04-12 21:52:16 +07:00
|
|
|
): 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);
|
2024-04-09 15:46:33 +07:00
|
|
|
if (opts?.tree) params.append('tree', 'true');
|
2024-04-03 10:38:35 +07:00
|
|
|
|
|
|
|
|
const query = params.toString();
|
|
|
|
|
|
2024-04-12 21:52:16 +07:00
|
|
|
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;
|
2024-04-12 21:52:16 +07:00
|
|
|
return res.data;
|
2024-04-03 10:38:35 +07:00
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-12 21:52:16 +07:00
|
|
|
async function fetchById<
|
2024-04-12 22:08:01 +07:00
|
|
|
Options extends { includeSubBranch?: boolean; includeContact?: boolean },
|
2024-04-12 21:52:16 +07:00
|
|
|
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-12 21:52:16 +07:00
|
|
|
>(
|
2024-04-03 17:47:58 +07:00
|
|
|
id: string,
|
2024-04-12 21:52:16 +07:00
|
|
|
opts?: Options,
|
2024-04-03 17:47:58 +07:00
|
|
|
flow?: {
|
|
|
|
|
sessionId: string;
|
|
|
|
|
refTransactionId: string;
|
|
|
|
|
transactionId: string;
|
|
|
|
|
},
|
2024-04-12 21:52:16 +07:00
|
|
|
): Promise<Data | false> {
|
2024-04-10 09:47:18 +07:00
|
|
|
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());
|
|
|
|
|
}
|
2024-04-10 09:47:18 +07:00
|
|
|
|
|
|
|
|
const query = params.toString();
|
|
|
|
|
|
2024-04-12 21:52:16 +07:00
|
|
|
const res = await api.get<Data>(
|
2024-04-10 09:47:18 +07:00
|
|
|
`/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-10 09:47:18 +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-18 13:56:22 +07:00
|
|
|
const { qrCodeImage, ...payload } = branch;
|
|
|
|
|
|
2024-04-04 11:46:25 +07:00
|
|
|
const res = await api.post<
|
|
|
|
|
Branch & { qrCodeImageUrl: string; qrCodeImageUploadUrl: string }
|
2024-04-18 13:56:22 +07:00
|
|
|
>('/branch', payload, {
|
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
|
|
|
|
2024-04-18 13:56:22 +07:00
|
|
|
qrCodeImage &&
|
|
|
|
|
(await axios
|
|
|
|
|
.put(res.data.qrCodeImageUploadUrl, qrCodeImage, {
|
|
|
|
|
headers: { 'Content-Type': qrCodeImage.type },
|
|
|
|
|
onUploadProgress: (e) => console.log(e),
|
|
|
|
|
})
|
|
|
|
|
.catch((e) => console.error(e)));
|
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-18 14:57:01 +07:00
|
|
|
data: Partial<BranchCreate & { status: 'ACTIVE' | 'INACTIVE' }>,
|
|
|
|
|
qrCodeImage?: File,
|
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-18 14:57:01 +07:00
|
|
|
const { ...payload } = data;
|
|
|
|
|
const res = await api.put<Branch & { qrCodeImageUploadUrl: string }>(
|
|
|
|
|
`/branch/${id}`,
|
|
|
|
|
payload,
|
|
|
|
|
{
|
|
|
|
|
headers: {
|
|
|
|
|
'X-Session-Id': flow?.sessionId,
|
|
|
|
|
'X-Rtid': flow?.refTransactionId,
|
|
|
|
|
'X-Tid': flow?.transactionId,
|
|
|
|
|
},
|
2024-04-03 17:47:58 +07:00
|
|
|
},
|
2024-04-18 14:57:01 +07:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (qrCodeImage) {
|
|
|
|
|
await axios
|
|
|
|
|
.put(res.data.qrCodeImageUploadUrl, qrCodeImage, {
|
|
|
|
|
headers: { 'Content-Type': qrCodeImage.type },
|
|
|
|
|
onUploadProgress: (e) => console.log(e),
|
|
|
|
|
})
|
|
|
|
|
.catch((e) => console.error(e));
|
|
|
|
|
}
|
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,
|
2024-04-12 21:52:16 +07:00
|
|
|
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;
|