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