From 265ea35bc4de4c430e0a4dd330acb6676ec5d1d9 Mon Sep 17 00:00:00 2001 From: Methapon Metanipat Date: Tue, 15 Oct 2024 16:26:35 +0700 Subject: [PATCH] chore: clean product store --- src/stores/product-service/index.ts | 185 +++++----------------------- 1 file changed, 32 insertions(+), 153 deletions(-) diff --git a/src/stores/product-service/index.ts b/src/stores/product-service/index.ts index 133dab72..c21cf549 100644 --- a/src/stores/product-service/index.ts +++ b/src/stores/product-service/index.ts @@ -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>( `/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('/product-group', { - ...payload, - }); + const res = await api.post('/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(`/product-group/${groupId}`, { - ...payload, - }); - - if (!res) return false; - - if (res.status === 200) { - return res.data; - } + const res = await api.put( + `/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>( `/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/${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( `/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>( `/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>( `/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(`/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/${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>( `/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; }