feat: ทำ function ต่อ api Service & Product

This commit is contained in:
Net 2024-06-14 15:18:41 +07:00
parent 4ad9d4c274
commit 23d06ae253
2 changed files with 394 additions and 16 deletions

View file

@ -1,8 +1,22 @@
import { defineStore } from 'pinia';
import { api } from 'src/boot/axios';
import { Product, ProductCreate, ProductUpdate } from './types';
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');
@ -14,7 +28,7 @@ const useProductServiceStore = defineStore('api-product-service', () => {
}
async function fetchListProductServiceByIdType(groupId: string) {
const res = await api.get<Product & { productGroupId: string }>(
const res = await api.get<ProductGroup & { productGroupId: string }>(
`/product-type/${groupId}`,
);
@ -41,7 +55,7 @@ const useProductServiceStore = defineStore('api-product-service', () => {
const query = params.toString();
const res = await api.get<(Product & { productGroupId: string })[]>(
const res = await api.get<(ProductGroup & { productGroupId: string })[]>(
`/product-type${(params && '?'.concat(query)) || ''}`,
{
headers: {
@ -60,10 +74,13 @@ const useProductServiceStore = defineStore('api-product-service', () => {
return false;
}
async function createProductServiceType(id: string, data: ProductCreate) {
async function createProductServiceType(
id: string,
data: ProductGroupCreate,
) {
const { code, ...payload } = data;
const res = await api.post<ProductCreate & { productGroupId: string }>(
const res = await api.post<ProductGroupCreate & { productGroupId: string }>(
'/product-type',
{
productGroupId: id,
@ -82,11 +99,11 @@ const useProductServiceStore = defineStore('api-product-service', () => {
async function editProductServiceType(
groupId: string,
data: ProductUpdate & { productGroupId: string },
data: ProductGroupUpdate & { productGroupId: string },
) {
const { code, ...payload } = data;
const res = await api.put<ProductUpdate & { productGroupId: string }>(
const res = await api.put<ProductGroupUpdate & { productGroupId: string }>(
`/product-type/${groupId}`,
{
...payload,
@ -114,6 +131,8 @@ const useProductServiceStore = defineStore('api-product-service', () => {
return false;
}
// Product Group
async function fetchStatsProductGroup() {
const res = await api.get('/product-group/stats');
@ -125,7 +144,7 @@ const useProductServiceStore = defineStore('api-product-service', () => {
}
async function fetchListProductServiceById(groupId: string) {
const res = await api.get<Product>(`/product-group/${groupId}`);
const res = await api.get<ProductGroup>(`/product-group/${groupId}`);
if (!res) return false;
@ -150,7 +169,7 @@ const useProductServiceStore = defineStore('api-product-service', () => {
const query = params.toString();
const res = await api.get<Product[]>(
const res = await api.get<ProductGroup[]>(
`/product-group${(params && '?'.concat(query)) || ''}`,
{
headers: {
@ -169,10 +188,10 @@ const useProductServiceStore = defineStore('api-product-service', () => {
return false;
}
async function createProductService(data: ProductCreate) {
async function createProductService(data: ProductGroupCreate) {
const { code, ...payload } = data;
const res = await api.post<ProductCreate>('/product-group', {
const res = await api.post<ProductGroupCreate>('/product-group', {
...payload,
});
@ -185,10 +204,10 @@ const useProductServiceStore = defineStore('api-product-service', () => {
return false;
}
async function editProductService(groupId: string, data: ProductUpdate) {
async function editProductService(groupId: string, data: ProductGroupUpdate) {
const { code, ...payload } = data;
const res = await api.put<ProductUpdate>(`/product-group/${groupId}`, {
const res = await api.put<ProductGroupUpdate>(`/product-group/${groupId}`, {
...payload,
});
@ -213,6 +232,233 @@ const useProductServiceStore = defineStore('api-product-service', () => {
return false;
}
// Product
async function fetchListProduct(
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<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 { code, ...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,
@ -227,6 +473,20 @@ const useProductServiceStore = defineStore('api-product-service', () => {
createProductService,
editProductService,
deleteProductService,
fetchListProduct,
createProduct,
fetchListProductById,
editProduct,
deleteProduct,
fetchStatsService,
fetchListService,
createService,
fetchListServiceByIdWork,
fetchListServiceById,
editService,
deleteService,
};
});

View file

@ -1,6 +1,124 @@
import { Status } from '../types';
import { Pagination } from 'src/stores/types';
export type Product = {
export interface Service {
updatedAt: string;
updateBy: string;
createdAt: string;
createdBy: string;
status: string;
attributes: string;
detail: string;
name: string;
code: string;
id: string;
workOnService: WorkOnService[];
imageUrl: string;
}
export interface WorkOnService {
work: Work;
productId: string;
updatedAt: string;
updateBy: string;
createdAt: string;
createdBy: string;
serviceId: string;
workId: string;
order: number;
}
export interface Work {
updatedAt: string;
updateBy: string;
createdAt: string;
createdBy: string;
status: string;
attributes: string;
name: string;
id: string;
}
export interface ServiceCreate {
workId: string[];
attributes: Attributes;
detail: string;
name: string;
code: string;
}
export interface Attributes {
additional: {
filName: string;
type: AdditionalType;
}[];
}
type AdditionalType = 'string' | 'number' | 'boolean' | Date;
// Product
export interface ResultProductOnWork {
productOnWork: ProductOnWork[];
updatedAt: string;
updateBy: string;
createdAt: string;
createdBy: string;
status: string;
attributes: string;
name: string;
id: string;
}
export interface ProductOnWork {
product: Omit<ProductList, 'imageUrl'>;
updatedAt: string;
updateBy: string;
createdAt: string;
createdBy: string;
productId: string;
workId: string;
order: number;
}
export interface ProductList {
updatedAt: string;
updateBy: string;
createdAt: string;
createdBy: string;
productTypeId: string;
status: string;
serviceCharge: number;
agentPrice: number;
price: number;
process: string;
detail: string;
name: string;
code: string;
id: string;
imageUrl: string;
}
export interface ProductCreate {
serviceCharge: number;
agentPrice: number;
price: number;
process: string;
detail: string;
name: string;
code: string;
}
export interface ProductUpdate {
serviceCharge: number;
agentPrice: number;
price: number;
process: string;
detail: string;
name: string;
}
//
export type ProductGroup = {
id: string;
code: string;
name: string;
@ -13,14 +131,14 @@ export type Product = {
updatedAt: string;
};
export interface ProductCreate {
export interface ProductGroupCreate {
remark: string;
detail: string;
name: string;
code: string;
}
export interface ProductUpdate {
export interface ProductGroupUpdate {
remark: string;
detail: string;
name: string;