From 23d06ae2539402362fa5a1a0fc4f65327daf0950 Mon Sep 17 00:00:00 2001 From: Net <93821485+somnetsak123@users.noreply.github.com> Date: Fri, 14 Jun 2024 15:18:41 +0700 Subject: [PATCH] =?UTF-8?q?feat:=20=20=E0=B8=97=E0=B8=B3=20function=20?= =?UTF-8?q?=E0=B8=95=E0=B9=88=E0=B8=AD=20=20api=20=20Service=20&=20Product?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/stores/product-service/index.ts | 286 ++++++++++++++++++++++++++-- src/stores/product-service/types.ts | 124 +++++++++++- 2 files changed, 394 insertions(+), 16 deletions(-) diff --git a/src/stores/product-service/index.ts b/src/stores/product-service/index.ts index 3c7f45bb..7f1d8fb0 100644 --- a/src/stores/product-service/index.ts +++ b/src/stores/product-service/index.ts @@ -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( + const res = await api.get( `/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( + const res = await api.post( '/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( + const res = await api.put( `/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-group/${groupId}`); + const res = await api.get(`/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( + const res = await api.get( `/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('/product-group', { + const res = await api.post('/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(`/product-group/${groupId}`, { + const res = await api.put(`/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>( + `/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('/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(`/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(`/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>( + `/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('/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>( + `/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/${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(`/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, }; }); diff --git a/src/stores/product-service/types.ts b/src/stores/product-service/types.ts index fa3ec81f..1be71e2b 100644 --- a/src/stores/product-service/types.ts +++ b/src/stores/product-service/types.ts @@ -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; + 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;