diff --git a/src/stores/product-service/index.ts b/src/stores/product-service/index.ts index a7ec3b51..59deabeb 100644 --- a/src/stores/product-service/index.ts +++ b/src/stores/product-service/index.ts @@ -1,5 +1,112 @@ import { defineStore } from 'pinia'; +import { api } from 'src/boot/axios'; +import { ProductGroup, ProductGroupCreate, ProductGroupUpdate } from './types'; const useProductServiceStore = defineStore('api-product-service', () => { - return {}; + async function fetchStatsProductGroup() { + const res = await api.get('/product-group/stats'); + + if (!res) return false; + + if (res.status === 200) { + return res.data; + } + } + + async function fetchListProductServiceById(groupId: string) { + const res = await api.get(`/product-group/${groupId}`); + + if (!res) return false; + + if (res.status === 200) { + return res.data; + } + } + + async function fetchListProductService( + opts?: { query?: string }, + flow?: { + sessionId: string; + refTransactionId: string; + transactionId: string; + }, + ) { + const params = new URLSearchParams(); + + for (const [k, v] of Object.entries(opts || {})) { + v !== undefined && params.append(k, v.toString()); + } + + const query = params.toString(); + + const res = await api.get( + `/product-group${(params && '?'.concat(query)) || ''}`, + { + 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 createProductService() { + const res = await api.post('/product-group'); + + if (!res) return false; + + if (res.status === 200) { + return res.data; + } + + return false; + } + + async function editProductService( + groupId: string, + opts: { data: ProductGroupUpdate }, + ) { + const res = await api.put(`/product-group/${groupId}`, { + ...opts.data, + }); + + if (!res) return false; + + if (res.status === 200) { + return res.data; + } + + return false; + } + + async function deleteProductService(groupId: string) { + const res = await api.delete(`/product-group/${groupId}`); + + if (!res) return false; + + if (res.status === 200) { + return res.data; + } + + return false; + } + + return { + fetchStatsProductGroup, + fetchListProductServiceById, + fetchListProductService, + createProductService, + editProductService, + deleteProductService, + }; }); + +export default useProductServiceStore;