jws-frontend/src/stores/product-service/index.ts
2024-06-17 17:58:05 +07:00

502 lines
10 KiB
TypeScript

import { defineStore } from 'pinia';
import { api } from 'src/boot/axios';
import { Pagination } from 'src/stores/types';
import {
ProductGroup,
ProductGroupCreate,
ProductGroupUpdate,
ProductList,
ProductCreate,
ProductUpdate,
Service,
ServiceCreate,
ResultProductOnWork,
} from './types';
const useProductServiceStore = defineStore('api-product-service', () => {
// Product Type
async function fetchStatsProductType() {
const res = await api.get('/product-type/stats');
if (!res) return false;
if (res.status === 200) {
return res.data;
}
}
async function fetchListProductServiceByIdType(groupId: string) {
const res = await api.get<ProductGroup & { productGroupId: string }>(
`/product-type/${groupId}`,
);
if (!res) return false;
if (res.status === 200) {
return res.data;
}
}
async function fetchListProductServiceType(
opts?: {
query?: string;
productGroupId?: string;
status?: 'CREATED' | 'ACTIVE' | 'INACTIVE';
},
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 & { productGroupId: string })[]>(
`/product-type${(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 createProductServiceType(
id: string,
data: ProductGroupCreate,
) {
const { code, ...payload } = data;
const res = await api.post<ProductGroupCreate & { productGroupId: string }>(
'/product-type',
{
productGroupId: id,
...payload,
},
);
if (!res) return false;
if (res.status === 201) {
return res.data;
}
return false;
}
async function editProductServiceType(
groupId: string,
data: ProductGroupUpdate & { productGroupId: string },
) {
const { code, ...payload } = data;
const res = await api.put<ProductGroupUpdate & { productGroupId: string }>(
`/product-type/${groupId}`,
{
...payload,
},
);
if (!res) return false;
if (res.status === 200) {
return res.data;
}
return false;
}
async function deleteProductServiceType(groupId: string) {
const res = await api.delete(`/product-type/${groupId}`);
if (!res) return false;
if (res.status === 200) {
return res.data;
}
return false;
}
// Product Group
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; status?: 'CREATED' | 'ACTIVE' | 'INACTIVE' },
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(data: ProductGroupCreate) {
const { code, ...payload } = data;
const res = await api.post<ProductGroupCreate>('/product-group', {
...payload,
});
if (!res) return false;
if (res.status === 201) {
return res.data;
}
return false;
}
async function editProductService(groupId: string, data: ProductGroupUpdate) {
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;
}
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;
}
// Product
async function fetchListProduct(
opts?: {
query?: string;
page?: number;
pageSize?: number;
productTypeId?: 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<Pagination<ProductList[]>>(
`/product${(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 createProduct(data: ProductCreate) {
const { ...payload } = data;
const res = await api.post<ProductCreate>('/product', {
...payload,
});
if (!res) return false;
if (res.status === 201) {
return res.data;
}
return false;
}
async function fetchListProductById(productId: string) {
const res = await api.get<ProductList>(`/product/${productId}`);
if (!res) return false;
if (res.status === 200) {
return res.data;
}
}
async function editProduct(productId: string, data: ProductUpdate) {
const { ...payload } = data;
const res = await api.put<ProductGroupUpdate>(`/product/${productId}`, {
...payload,
});
if (!res) return false;
if (res.status === 200) {
return res.data;
}
return false;
}
async function deleteProduct(productId: string) {
const res = await api.delete(`/product/${productId}`);
if (!res) return false;
if (res.status === 200) {
return res.data;
}
return false;
}
//
// Service
async function fetchStatsService() {
const res = await api.get('/service/stats');
if (!res) return false;
if (res.status === 200) {
return res.data;
}
}
async function fetchListService(
opts?: { query?: string; page?: number; pageSize?: number },
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<Pagination<Service[]>>(
`/service${(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 createService(data: ServiceCreate) {
const { code, ...payload } = data;
const res = await api.post<ServiceCreate>('/service', {
...payload,
});
if (!res) return false;
if (res.status === 201) {
return res.data;
}
return false;
}
async function fetchListServiceByIdWork(
serviceId: string,
opts?: { query?: string; page?: number; pageSize?: number },
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<Pagination<ResultProductOnWork[]>>(
`/service/${serviceId}/work${(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 fetchListServiceById(serviceId: string) {
const res = await api.get<Service>(`/service/${serviceId}`);
if (!res) return false;
if (res.status === 200) {
return res.data;
}
}
async function editService(serviceId: string, data: ServiceCreate) {
const { ...payload } = data;
const res = await api.put<ServiceCreate>(`/service/${serviceId}`, {
...payload,
});
if (!res) return false;
if (res.status === 200) {
return res.data;
}
return false;
}
async function deleteService(serviceId: string) {
const res = await api.delete(`/service/${serviceId}`);
if (!res) return false;
if (res.status === 200) {
return res.data;
}
return false;
}
return {
fetchStatsProductType,
fetchListProductServiceByIdType,
fetchListProductServiceType,
createProductServiceType,
editProductServiceType,
deleteProductServiceType,
fetchStatsProductGroup,
fetchListProductServiceById,
fetchListProductService,
createProductService,
editProductService,
deleteProductService,
fetchListProduct,
createProduct,
fetchListProductById,
editProduct,
deleteProduct,
fetchStatsService,
fetchListService,
createService,
fetchListServiceByIdWork,
fetchListServiceById,
editService,
deleteService,
};
});
export default useProductServiceStore;