feat: เพิ่ม function ต่อ api Product Group

This commit is contained in:
Net 2024-06-11 14:09:32 +07:00
parent 73afca7b57
commit 40aebe30a1

View file

@ -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<ProductGroup>(`/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<ProductGroup>(
`/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<ProductGroupCreate>('/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<ProductGroupUpdate>(`/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;