148 lines
3.3 KiB
TypeScript
148 lines
3.3 KiB
TypeScript
|
|
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[]>>();
|
||
|
|
|
||
|
|
async function fetchBranch(opts?: {
|
||
|
|
page?: number;
|
||
|
|
pageSize?: number;
|
||
|
|
zipCode?: string;
|
||
|
|
query?: string;
|
||
|
|
}) {
|
||
|
|
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)) || ''}`,
|
||
|
|
);
|
||
|
|
|
||
|
|
if (res && res.status === 200) {
|
||
|
|
data.value = res.data;
|
||
|
|
return data.value;
|
||
|
|
}
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
async function fetchBranchById(id: string) {
|
||
|
|
const res = await api.get<Branch>(`/branch/${id}`);
|
||
|
|
|
||
|
|
if (!res) return false;
|
||
|
|
if (res.status === 200) return res.data;
|
||
|
|
if (res.status === 204) return null;
|
||
|
|
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
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;
|
||
|
|
}) {
|
||
|
|
const res = await api.post<Branch>('/branch', branch);
|
||
|
|
|
||
|
|
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;
|
||
|
|
},
|
||
|
|
) {
|
||
|
|
const res = await api.patch<Branch>(`/branch/${id}`, branch);
|
||
|
|
|
||
|
|
if (!res) return false;
|
||
|
|
|
||
|
|
return res.data;
|
||
|
|
}
|
||
|
|
|
||
|
|
async function deleteBranchById(id: string) {
|
||
|
|
const res = await api.delete<Branch>(`/branch/${id}`);
|
||
|
|
|
||
|
|
if (!res) return false;
|
||
|
|
if (res.status === 200) return res.data;
|
||
|
|
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
return {
|
||
|
|
data,
|
||
|
|
fetchBranch,
|
||
|
|
fetchBranchById,
|
||
|
|
|
||
|
|
createBranch,
|
||
|
|
editBranchById,
|
||
|
|
deleteBranchById,
|
||
|
|
};
|
||
|
|
});
|
||
|
|
|
||
|
|
export default useBranchStore;
|