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

289 lines
6.3 KiB
TypeScript
Raw Normal View History

2024-04-03 10:38:35 +07:00
import { ref } from 'vue';
import { defineStore } from 'pinia';
import { District, Province, SubDistrict } from '../address';
import { Pagination, Status } from '../types';
import { api } from 'src/boot/axios';
export type Branch = {
subDistrict: SubDistrict | null;
district: District | null;
province: Province | null;
updatedAt: string;
updateBy: string;
createdAt: string;
createdBy: string;
status: Status;
headOfficeId: string | null;
isHeadOffice: boolean;
longitude: string;
latitude: string;
telephoneNo: string;
email: string;
zipCode: string;
subDistrictId: string;
districtId: string;
provinceId: string;
addressEN: string;
addressTH: string;
nameEN: string;
nameTH: string;
taxNo: string;
code: string;
id: string;
};
const useBranchStore = defineStore('api-branch', () => {
const data = ref<Pagination<Branch[]>>();
2024-04-03 17:47:58 +07:00
async function fetchBranch(
opts?: {
page?: number;
pageSize?: number;
zipCode?: string;
query?: string;
},
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);
const query = params.toString();
const res = await api.get<Pagination<Branch[]>>(
`/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;
}
return false;
}
2024-04-03 17:47:58 +07:00
async function fetchBranchById(
id: string,
flow?: {
sessionId: string;
refTransactionId: string;
transactionId: string;
},
) {
const res = await api.get<Branch>(`/branch/${id}`, {
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;
if (res.status === 200) return res.data;
if (res.status === 204) return null;
return false;
}
2024-04-03 17:47:58 +07:00
async function createBranch(
branch: {
code: string;
taxNo: string;
nameEN: string;
nameTH: string;
addressEN: string;
addressTH: string;
zipCode: string;
email: string;
telephoneNo: string;
longitude: string;
latitude: string;
subDistrictId?: string | null;
districtId?: string | null;
provinceId?: string | null;
headOfficeId?: string | null;
},
flow?: {
sessionId: string;
refTransactionId: string;
transactionId: string;
},
) {
const res = await api.post<Branch>('/branch', 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;
}
async function editBranchById(
id: string,
branch: {
code?: string;
taxNo?: string;
nameEN?: string;
nameTH?: string;
addressEN?: string;
addressTH?: string;
zipCode?: string;
email?: string;
telephoneNo?: string;
longitude?: string;
latitude?: string;
subDistrictId?: string | null;
districtId?: string | null;
provinceId?: string | null;
headOfficeId?: string | null;
},
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-03 17:47:58 +07:00
async function deleteBranchById(
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;
}
return {
data,
fetchBranch,
fetchBranchById,
createBranch,
editBranchById,
deleteBranchById,
2024-04-03 17:47:58 +07:00
getUser,
addUser,
removeUser,
2024-04-03 10:38:35 +07:00
};
});
export default useBranchStore;