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-08-22 14:30:47 +07:00
|
|
|
|
2024-04-03 10:38:35 +07:00
|
|
|
import { api } from 'src/boot/axios';
|
2024-08-22 14:30:47 +07:00
|
|
|
import useFlowStore from '../flow';
|
|
|
|
|
import { Pagination } from '../types';
|
2024-08-02 11:13:21 +00:00
|
|
|
import { BankBook, Branch, BranchCreate } from './types';
|
2024-04-12 21:52:16 +07:00
|
|
|
import { BranchContact } from '../branch-contact/types';
|
2024-08-14 15:30:01 +07:00
|
|
|
import { User } from '../user/types';
|
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-06-25 16:05:42 +07:00
|
|
|
const flowStore = useFlowStore();
|
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[]>,
|
2024-08-22 14:30:47 +07:00
|
|
|
>(opts?: Options): Promise<Data | false> {
|
2024-04-03 10:38:35 +07:00
|
|
|
const params = new URLSearchParams();
|
|
|
|
|
|
2024-08-19 11:49:32 +07:00
|
|
|
for (const [k, v] of Object.entries(opts || {})) {
|
2024-08-22 14:30:47 +07:00
|
|
|
if (v !== undefined) params.append(k, v.toString());
|
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-08-22 14:30:47 +07:00
|
|
|
{ headers: { 'X-Rtid': flowStore.rtid } },
|
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-08-22 14:30:47 +07:00
|
|
|
>(id: string, opts?: Options): 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 || {})) {
|
2024-08-22 14:30:47 +07:00
|
|
|
if (v !== undefined) params.append(k, v.toString());
|
2024-04-12 22:08:01 +07:00
|
|
|
}
|
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)) || ''}`,
|
2024-08-22 14:30:47 +07:00
|
|
|
{ headers: { 'X-Rtid': flowStore.rtid } },
|
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-08-22 14:30:47 +07:00
|
|
|
async function create(branch: BranchCreate, bank?: BankBook[]) {
|
2024-04-18 17:29:51 +07:00
|
|
|
const { qrCodeImage, imageUrl, ...payload } = branch;
|
2024-04-18 13:56:22 +07:00
|
|
|
|
2024-04-04 11:46:25 +07:00
|
|
|
const res = await api.post<
|
2024-04-18 17:29:51 +07:00
|
|
|
Branch & {
|
|
|
|
|
qrCodeImageUrl: string;
|
|
|
|
|
qrCodeImageUploadUrl: string;
|
|
|
|
|
imageUploadUrl: string;
|
|
|
|
|
}
|
2024-08-02 11:13:21 +00:00
|
|
|
>(
|
|
|
|
|
'/branch',
|
|
|
|
|
{ ...payload, bank: bank },
|
2024-08-22 14:30:47 +07:00
|
|
|
{ headers: { 'X-Rtid': flowStore.rtid } },
|
2024-08-02 11:13:21 +00:00
|
|
|
);
|
2024-04-03 10:38:35 +07:00
|
|
|
|
2024-08-22 14:30:47 +07:00
|
|
|
if (qrCodeImage) {
|
|
|
|
|
await api
|
|
|
|
|
.put(`/branch/${res.data.id}/line-image`, qrCodeImage, {
|
2024-04-18 13:56:22 +07:00
|
|
|
headers: { 'Content-Type': qrCodeImage.type },
|
|
|
|
|
onUploadProgress: (e) => console.log(e),
|
|
|
|
|
})
|
2024-08-22 14:30:47 +07:00
|
|
|
.catch((e) => console.error(e));
|
|
|
|
|
}
|
2024-04-18 17:29:51 +07:00
|
|
|
|
|
|
|
|
if (imageUrl) {
|
2024-08-22 14:30:47 +07:00
|
|
|
await api
|
2024-08-28 16:33:51 +07:00
|
|
|
.put(`/branch/${res.data.id}/branch-image`, imageUrl, {
|
2024-04-18 17:29:51 +07:00
|
|
|
headers: { 'Content-Type': imageUrl.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 18:07:38 +07:00
|
|
|
data: Partial<BranchCreate & { status: 'ACTIVE' | 'INACTIVE' | 'CREATED' }>,
|
2024-04-18 17:29:51 +07:00
|
|
|
qrCodeImage?: File | undefined,
|
|
|
|
|
imageHq?: File | undefined,
|
2024-08-02 11:13:21 +00:00
|
|
|
bank?: BankBook[],
|
2024-04-03 10:38:35 +07:00
|
|
|
) {
|
2024-04-18 14:57:01 +07:00
|
|
|
const { ...payload } = data;
|
2024-04-18 17:29:51 +07:00
|
|
|
const res = await api.put<
|
|
|
|
|
Branch & { qrCodeImageUploadUrl: string; imageUploadUrl: string }
|
2024-08-02 11:13:21 +00:00
|
|
|
>(
|
|
|
|
|
`/branch/${id}`,
|
|
|
|
|
{ ...payload, bank: bank },
|
|
|
|
|
{
|
2024-08-22 14:30:47 +07:00
|
|
|
headers: { 'X-Rtid': flowStore.rtid },
|
2024-04-03 17:47:58 +07:00
|
|
|
},
|
2024-08-02 11:13:21 +00:00
|
|
|
);
|
2024-04-18 14:57:01 +07:00
|
|
|
|
|
|
|
|
if (qrCodeImage) {
|
2024-08-22 14:30:47 +07:00
|
|
|
await api
|
|
|
|
|
.put(`/branch/${res.data.id}/line-image`, qrCodeImage, {
|
2024-04-18 14:57:01 +07:00
|
|
|
headers: { 'Content-Type': qrCodeImage.type },
|
|
|
|
|
onUploadProgress: (e) => console.log(e),
|
|
|
|
|
})
|
|
|
|
|
.catch((e) => console.error(e));
|
|
|
|
|
}
|
2024-04-03 10:38:35 +07:00
|
|
|
|
2024-04-18 17:29:51 +07:00
|
|
|
if (imageHq) {
|
2024-08-22 14:30:47 +07:00
|
|
|
await api
|
|
|
|
|
.put(`/branch/${res.data.id}/branch-image`, imageHq, {
|
2024-04-18 17:29:51 +07:00
|
|
|
headers: { 'Content-Type': imageHq.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-08-22 14:30:47 +07:00
|
|
|
async function deleteById(id: string) {
|
2024-04-03 17:47:58 +07:00
|
|
|
const res = await api.delete<Branch>(`/branch/${id}`, {
|
|
|
|
|
headers: {
|
2024-08-22 14:30:47 +07:00
|
|
|
'X-Rtid': flowStore.rtid,
|
2024-04-03 17:47:58 +07:00
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!res) return false;
|
|
|
|
|
if (res.status === 200) return res.data;
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-22 14:30:47 +07:00
|
|
|
async function getUser(branchId: string) {
|
2024-04-03 17:47:58 +07:00
|
|
|
const res = await api.get(`/branch/${branchId}/user`, {
|
2024-08-22 14:30:47 +07:00
|
|
|
headers: { 'X-Rtid': flowStore.rtid },
|
2024-04-03 17:47:58 +07:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!res) return false;
|
|
|
|
|
if (res.status === 200) return res.data;
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-22 14:30:47 +07:00
|
|
|
async function getAdmin(branchId: string | string[]) {
|
2024-08-30 09:47:04 +07:00
|
|
|
const res = await api.get<User>(`/branch/${branchId}/manager`, {
|
2024-08-22 14:30:47 +07:00
|
|
|
headers: { 'X-Rtid': flowStore.rtid },
|
2024-08-14 15:30:01 +07:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!res) return false;
|
|
|
|
|
if (res.status === 200) return res.data;
|
|
|
|
|
if (res.status === 204) return null;
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-22 14:30:47 +07:00
|
|
|
async function addUser(branchId: string, userId: string | string[]) {
|
2024-04-03 17:47:58 +07:00
|
|
|
const res = await api.post(
|
|
|
|
|
`/branch/${branchId}/user`,
|
|
|
|
|
{ user: ([] as string[]).concat(userId) },
|
2024-08-22 14:30:47 +07:00
|
|
|
{ headers: { 'X-Rtid': flowStore.rtid } },
|
2024-04-03 17:47:58 +07:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (!res) return false;
|
|
|
|
|
if (res.status === 200) return res.data;
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-22 14:30:47 +07:00
|
|
|
async function removeUser(branchId: string, userId: string | string[]) {
|
2024-04-03 17:47:58 +07:00
|
|
|
const res = await api.delete(`/branch/${branchId}/user`, {
|
2024-08-22 14:30:47 +07:00
|
|
|
headers: { 'X-Rtid': flowStore.rtid },
|
2024-04-03 17:47:58 +07:00
|
|
|
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-08-22 14:30:47 +07:00
|
|
|
async function stats() {
|
2024-04-10 17:41:41 +07:00
|
|
|
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: {
|
2024-08-22 14:30:47 +07:00
|
|
|
'X-Rtid': flowStore.rtid,
|
2024-04-10 17:41:41 +07:00
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!res) return false;
|
|
|
|
|
if (res.status === 200) return res.data;
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-22 14:30:47 +07:00
|
|
|
async function userStats(userType: string) {
|
2024-04-10 17:41:41 +07:00
|
|
|
const res = await api.get(
|
|
|
|
|
`/branch/user-stats${userType ? '?'.concat(userType) : ''}`,
|
|
|
|
|
{
|
2024-08-22 14:30:47 +07:00
|
|
|
headers: { 'X-Rtid': flowStore.rtid },
|
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-08-22 14:30:47 +07:00
|
|
|
async function getContact(branchId: BranchId, force = false) {
|
2024-04-12 22:08:01 +07:00
|
|
|
if (!force && contact[branchId]) return contact[branchId];
|
|
|
|
|
|
2024-08-22 14:30:47 +07:00
|
|
|
const res = await fetchById(branchId, { includeContact: true });
|
2024-04-12 22:08:01 +07:00
|
|
|
|
|
|
|
|
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
|
|
|
|
2024-08-14 15:30:01 +07:00
|
|
|
getAdmin,
|
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;
|