chore: clean product store

This commit is contained in:
Methapon Metanipat 2024-10-15 16:26:35 +07:00
parent 8ce8c9a977
commit 265ea35bc4

View file

@ -1,8 +1,6 @@
import { defineStore } from 'pinia';
import { api } from 'src/boot/axios';
import axios from 'axios';
import { Pagination } from 'stores/types';
import {
@ -11,7 +9,6 @@ import {
ProductGroupUpdate,
Product,
ProductCreate,
ProductUpdate,
Service,
ServiceCreate,
ServiceById,
@ -20,11 +17,8 @@ import {
WorkCreate,
} from './types';
import { ref } from 'vue';
import useFlowStore from '../flow';
const useProductServiceStore = defineStore('api-product-service', () => {
const flowStore = useFlowStore();
const workNameItems = ref<
{
id: string;
@ -71,11 +65,6 @@ const useProductServiceStore = defineStore('api-product-service', () => {
const res = await api.get<Pagination<ProductGroup[]>>(
`/product-group${(params && '?'.concat(query)) || ''}`,
{
headers: {
'X-Rtid': flowStore.rtid,
},
},
);
if (!res) return false;
@ -89,9 +78,7 @@ const useProductServiceStore = defineStore('api-product-service', () => {
async function createProductGroup(data: ProductGroupCreate) {
const { code, ...payload } = data;
const res = await api.post<ProductGroupCreate>('/product-group', {
...payload,
});
const res = await api.post<ProductGroupCreate>('/product-group', payload);
if (!res) return false;
@ -108,28 +95,19 @@ const useProductServiceStore = defineStore('api-product-service', () => {
) {
const { code, ...payload } = data;
const res = await api.put<ProductGroupUpdate>(`/product-group/${groupId}`, {
...payload,
});
if (!res) return false;
if (res.status === 200) {
return res.data;
}
const res = await api.put<ProductGroupUpdate>(
`/product-group/${groupId}`,
payload,
);
if (res && res.status < 400) return res.data;
return false;
}
async function deleteProductGroup(groupId: string) {
const res = await api.delete(`/product-group/${groupId}`);
if (!res) return false;
if (res.status === 200) {
return res.data;
}
if (res && res.status < 400) return res.data;
return false;
}
@ -148,11 +126,8 @@ const useProductServiceStore = defineStore('api-product-service', () => {
`/product/stats${(params && '?'.concat(query)) || ''}`,
);
if (!res) return false;
if (res.status === 200) {
return res.data;
}
if (res && res.status < 400) return res.data;
return false;
}
async function fetchListProduct(opts?: {
@ -172,16 +147,9 @@ const useProductServiceStore = defineStore('api-product-service', () => {
const res = await api.get<Pagination<Product[]>>(
`/product${(params && '?'.concat(query)) || ''}`,
{
headers: { 'X-Rtid': flowStore.rtid },
},
);
if (!res) return false;
if (res.status === 200) {
return res.data;
}
if (res && res.status < 400) return res.data;
return false;
}
@ -212,23 +180,15 @@ const useProductServiceStore = defineStore('api-product-service', () => {
}
}
if (!res) return false;
if (res.status === 201) {
return res.data;
}
if (res && res.status < 400) return res.data;
return false;
}
async function fetchListProductById(productId: string) {
const res = await api.get<Product>(`/product/${productId}`);
if (!res) return false;
if (res.status === 200) {
return res.data;
}
if (res && res.status < 400) return res.data;
return false;
}
async function editProduct(
@ -239,15 +199,10 @@ const useProductServiceStore = defineStore('api-product-service', () => {
const res = await api.put<ProductCreate & { imageUploadUrl: string }>(
`/product/${productId}`,
{
...payload,
},
payload,
);
if (res.status === 200) {
return res.data;
}
if (res && res.status < 400) return res.data;
return false;
}
@ -277,11 +232,8 @@ const useProductServiceStore = defineStore('api-product-service', () => {
`/service/stats${(params && '?'.concat(query)) || ''}`,
);
if (!res) return false;
if (res.status === 200) {
return res.data;
}
if (res && res.status < 400) return res.data;
return false;
}
async function fetchListService(opts?: {
@ -302,18 +254,9 @@ const useProductServiceStore = defineStore('api-product-service', () => {
const res = await api.get<Pagination<Service[]>>(
`/service${(params && '?'.concat(query)) || ''}`,
{
headers: {
'X-Rtid': flowStore.rtid,
},
},
);
if (!res) return false;
if (res.status === 200) {
return res.data;
}
if (res && res.status < 400) return res.data;
return false;
}
@ -349,10 +292,7 @@ const useProductServiceStore = defineStore('api-product-service', () => {
if (!res) return false;
if (res.status === 201) {
return res.data;
}
if (res && res.status < 400) return res.data;
return false;
}
@ -370,11 +310,6 @@ const useProductServiceStore = defineStore('api-product-service', () => {
const res = await api.get<Pagination<ServiceById[]>>(
`/service/${serviceId}/work${(params && '?'.concat(query)) || ''}`,
{
headers: {
'X-Rtid': flowStore.rtid,
},
},
);
if (!res) return false;
@ -387,12 +322,8 @@ const useProductServiceStore = defineStore('api-product-service', () => {
async function fetchListServiceById(serviceId: string) {
const res = await api.get<ServiceById>(`/service/${serviceId}`);
if (!res) return false;
if (res.status === 200) {
return res.data;
}
if (res && res.status < 400) return res.data;
return false;
}
async function editService(
@ -412,12 +343,7 @@ const useProductServiceStore = defineStore('api-product-service', () => {
},
);
if (!res) return false;
if (res.status === 200) {
return res.data;
}
if (res && res.status < 400) return res.data;
return false;
}
@ -450,17 +376,9 @@ const useProductServiceStore = defineStore('api-product-service', () => {
const res = await api.get<
Pagination<(Work & { productOnWork: ProductOnWork[] })[]>
>(`/work${(params && '?'.concat(query)) || ''}`, {
headers: {
'X-Rtid': flowStore.rtid,
},
});
if (!res) return false;
if (res.status === 200) {
return res.data;
}
>(`/work${(params && '?'.concat(query)) || ''}`);
if (res && res.status < 400) return res.data;
return false;
}
@ -471,9 +389,7 @@ const useProductServiceStore = defineStore('api-product-service', () => {
...payload,
});
if (!res) return false;
if (res.status === 201) {
if (res && res.status < 400) {
await fetchListOfWork();
return res.data;
}
@ -484,11 +400,8 @@ const useProductServiceStore = defineStore('api-product-service', () => {
async function fetchListWorkById(workId: string) {
const res = await api.get<Work>(`/work/${workId}`);
if (!res) return false;
if (res.status === 200) {
return res.data;
}
if (res && res.status < 200) return res.data;
return false;
}
async function editWork(
@ -501,7 +414,7 @@ const useProductServiceStore = defineStore('api-product-service', () => {
...payload,
});
if (res.status === 200) {
if (res && res.status < 200) {
await fetchListOfWork();
return res.data;
}
@ -536,18 +449,9 @@ const useProductServiceStore = defineStore('api-product-service', () => {
const res = await api.get<Pagination<Product[]>>(
`/work/${workId}/product${(params && '?'.concat(query)) || ''}`,
{
headers: {
'X-Rtid': flowStore.rtid,
},
},
);
if (!res) return false;
if (res.status === 200) {
return res.data;
}
if (res && res.status < 200) return res.data;
return false;
}
@ -561,22 +465,8 @@ const useProductServiceStore = defineStore('api-product-service', () => {
}
}
async function fetchImageListById(
id: string,
type: 'product' | 'service',
flow?: {
sessionId?: string;
refTransactionId?: string;
transactionId?: string;
},
) {
const res = await api.get(`/${type}/${id}/image`, {
headers: {
'X-Session-Id': flow?.sessionId,
'X-Rtid': flow?.refTransactionId || flowStore.rtid,
'X-Tid': flow?.transactionId,
},
});
async function fetchImageListById(id: string, type: 'product' | 'service') {
const res = await api.get(`/${type}/${id}/image`);
if (!res) return false;
if (res.status === 200) return res.data;
@ -605,19 +495,8 @@ const useProductServiceStore = defineStore('api-product-service', () => {
id: string,
name: string,
type: 'product' | 'service',
flow?: {
sessionId?: string;
refTransactionId?: string;
transactionId?: string;
},
) {
const res = await api.delete(`/${type}/${id}/image/${name}`, {
headers: {
'X-Session-Id': flow?.sessionId,
'X-Rtid': flow?.refTransactionId || flowStore.rtid,
'X-Tid': flow?.transactionId,
},
});
const res = await api.delete(`/${type}/${id}/image/${name}`);
if (!res) return false;
}