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

296 lines
7 KiB
TypeScript
Raw Normal View History

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';
import { BranchContact } from '../branch-contact/types';
2024-08-14 15:30:01 +07:00
import { User } from '../user/types';
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();
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[]>,
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();
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;
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-08-22 14:30:47 +07:00
>(id: string, opts?: Options): Promise<Data | false> {
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
}
const query = params.toString();
const res = await api.get<Data>(
`/branch/${id}${(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) 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[]) {
const { qrCodeImage, imageUrl, ...payload } = branch;
2024-04-04 11:46:25 +07:00
const res = await api.post<
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, {
headers: { 'Content-Type': qrCodeImage.type },
onUploadProgress: (e) => console.log(e),
})
2024-08-22 14:30:47 +07:00
.catch((e) => console.error(e));
}
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, {
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' }>,
qrCodeImage?: File | undefined,
imageHq?: File | undefined,
2024-08-02 11:13:21 +00:00
bank?: BankBook[],
2024-04-03 10:38:35 +07:00
) {
const { ...payload } = data;
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
);
if (qrCodeImage) {
2024-08-22 14:30:47 +07:00
await api
.put(`/branch/${res.data.id}/line-image`, 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 (imageHq) {
2024-08-22 14:30:47 +07:00
await api
.put(`/branch/${res.data.id}/branch-image`, imageHq, {
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-14 15:30:01 +07:00
const res = await api.get<User>(`/branch/${branchId}/admin`, {
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,
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;