diff --git a/src/stores/address/index.ts b/src/stores/address/index.ts new file mode 100644 index 00000000..da89566e --- /dev/null +++ b/src/stores/address/index.ts @@ -0,0 +1,90 @@ +import { ref } from 'vue'; +import { defineStore } from 'pinia'; +import { api } from 'src/boot/axios'; + +export interface SubDistrict { + updatedAt: string; + updateBy: string; + createdAt: string; + createdBy: string; + districtId: string; + zipCode: string; + nameEN: string; + nameTH: string; + id: string; +} + +export interface District { + updatedAt: string; + updateBy: string; + createdAt: string; + createdBy: string; + provinceId: string; + nameEN: string; + nameTH: string; + id: string; +} + +export interface Province { + updatedAt: string; + updateBy: string; + createdAt: string; + createdBy: string; + nameEN: string; + nameTH: string; + id: string; +} + +const useAddressStore = defineStore('api-address', () => { + const province = ref(); + const district = ref>({}); + const subDistrict = ref>({}); + + async function fetchProvince() { + if (province.value) return province.value; + + const res = await api.get(`/address/province`); + + if (!res) return false; + + province.value = res.data; + + return province.value; + } + + async function fetchDistrictByProvinceId(provinceId: string) { + if (district.value[provinceId]) return district.value[provinceId]; + + const res = await api.get( + `/address/province/${provinceId}/district`, + ); + + if (!res) return false; + + district.value[provinceId] = res.data; + + return district.value[provinceId]; + } + + async function fetchSubDistrictByProvinceId(districtId: string) { + if (subDistrict.value[districtId]) return subDistrict.value[districtId]; + + const res = await api.get( + `/address/district/${districtId}/sub-district`, + ); + + if (!res) return false; + + subDistrict.value[districtId] = res.data; + + return subDistrict.value[districtId]; + } + + return { + fetchProvince, + fetchDistrictByProvinceId, + fetchSubDistrictByProvinceId, + }; +}); + +export default useAddressStore; diff --git a/src/stores/branch/index.ts b/src/stores/branch/index.ts new file mode 100644 index 00000000..1cad94d5 --- /dev/null +++ b/src/stores/branch/index.ts @@ -0,0 +1,147 @@ +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>(); + + 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>( + `/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/${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); + + 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/${id}`, branch); + + if (!res) return false; + + return res.data; + } + + async function deleteBranchById(id: string) { + const res = await api.delete(`/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; diff --git a/src/stores/types.ts b/src/stores/types.ts new file mode 100644 index 00000000..42223bb1 --- /dev/null +++ b/src/stores/types.ts @@ -0,0 +1,11 @@ +export type Pagination = { + result: T; + page: number; + pageSize: number; + total: number; +}; + +export enum Status { + CREATED, + USED, +}