diff --git a/src/stores/branch/index.ts b/src/stores/branch/index.ts index 1cad94d5..286564e3 100644 --- a/src/stores/branch/index.ts +++ b/src/stores/branch/index.ts @@ -35,12 +35,19 @@ export type Branch = { const useBranchStore = defineStore('api-branch', () => { const data = ref>(); - async function fetchBranch(opts?: { - page?: number; - pageSize?: number; - zipCode?: string; - query?: string; - }) { + async function fetchBranch( + opts?: { + page?: number; + pageSize?: number; + zipCode?: string; + query?: string; + }, + flow?: { + sessionId: string; + refTransactionId: string; + transactionId: string; + }, + ) { const params = new URLSearchParams(); if (opts?.pageSize && opts?.pageSize > 0) { @@ -54,6 +61,13 @@ const useBranchStore = defineStore('api-branch', () => { const res = await api.get>( `/branch${(params && '?'.concat(query)) || ''}`, + { + headers: { + 'X-Session-Id': flow?.sessionId, + 'X-Rtid': flow?.refTransactionId, + 'X-Tid': flow?.transactionId, + }, + }, ); if (res && res.status === 200) { @@ -63,8 +77,21 @@ const useBranchStore = defineStore('api-branch', () => { return false; } - async function fetchBranchById(id: string) { - const res = await api.get(`/branch/${id}`); + async function fetchBranchById( + id: string, + flow?: { + sessionId: string; + refTransactionId: string; + transactionId: string; + }, + ) { + const res = await api.get(`/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; @@ -73,24 +100,37 @@ const useBranchStore = defineStore('api-branch', () => { 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); + 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, { + headers: { + 'X-Session-Id': flow?.sessionId, + 'X-Rtid': flow?.refTransactionId, + 'X-Tid': flow?.transactionId, + }, + }); if (!res) return false; @@ -116,16 +156,113 @@ const useBranchStore = defineStore('api-branch', () => { provinceId?: string | null; headOfficeId?: string | null; }, + flow?: { + sessionId: string; + refTransactionId: string; + transactionId: string; + }, ) { - const res = await api.patch(`/branch/${id}`, branch); + const res = await api.put(`/branch/${id}`, branch, { + headers: { + 'X-Session-Id': flow?.sessionId, + 'X-Rtid': flow?.refTransactionId, + 'X-Tid': flow?.transactionId, + }, + }); if (!res) return false; return res.data; } - async function deleteBranchById(id: string) { - const res = await api.delete(`/branch/${id}`); + async function deleteBranchById( + id: string, + flow?: { + sessionId: string; + refTransactionId: string; + transactionId: string; + }, + ) { + const res = await api.delete(`/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) }, + }); if (!res) return false; if (res.status === 200) return res.data; @@ -141,6 +278,10 @@ const useBranchStore = defineStore('api-branch', () => { createBranch, editBranchById, deleteBranchById, + + getUser, + addUser, + removeUser, }; });