feat: CRUD ProductType

This commit is contained in:
Net 2024-06-12 15:37:17 +07:00
parent 069dcce9b6
commit a61b3e550e
3 changed files with 450 additions and 242 deletions

View file

@ -1,8 +1,119 @@
import { defineStore } from 'pinia';
import { api } from 'src/boot/axios';
import { ProductGroup, ProductGroupCreate, ProductGroupUpdate } from './types';
import { Product, ProductCreate, ProductUpdate } from './types';
const useProductServiceStore = defineStore('api-product-service', () => {
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<Product & { productGroupId: string }>(
`/product-type/${groupId}`,
);
if (!res) return false;
if (res.status === 200) {
return res.data;
}
}
async function fetchListProductServiceType(
opts?: { query?: string; productGroupId?: 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<(Product & { 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: ProductCreate) {
const { code, ...payload } = data;
const res = await api.post<ProductCreate & { productGroupId: string }>(
'/product-type',
{
productGroupId: id,
...payload,
},
);
if (!res) return false;
if (res.status === 200) {
return res.data;
}
return false;
}
async function editProductServiceType(
groupId: string,
data: ProductUpdate & { productGroupId: string },
) {
const { code, ...payload } = data;
const res = await api.put<ProductUpdate & { 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;
}
async function fetchStatsProductGroup() {
const res = await api.get('/product-group/stats');
@ -14,7 +125,7 @@ const useProductServiceStore = defineStore('api-product-service', () => {
}
async function fetchListProductServiceById(groupId: string) {
const res = await api.get<ProductGroup>(`/product-group/${groupId}`);
const res = await api.get<Product>(`/product-group/${groupId}`);
if (!res) return false;
@ -39,7 +150,7 @@ const useProductServiceStore = defineStore('api-product-service', () => {
const query = params.toString();
const res = await api.get<ProductGroup[]>(
const res = await api.get<Product[]>(
`/product-group${(params && '?'.concat(query)) || ''}`,
{
headers: {
@ -58,26 +169,26 @@ const useProductServiceStore = defineStore('api-product-service', () => {
return false;
}
async function createProductService(data: ProductGroupCreate) {
async function createProductService(data: ProductCreate) {
const { code, ...payload } = data;
const res = await api.post<ProductGroupCreate>('/product-group', {
const res = await api.post<ProductCreate>('/product-group', {
...payload,
});
if (!res) return false;
if (res.status === 200) {
if (res.status === 201) {
return res.data;
}
return false;
}
async function editProductService(groupId: string, data: ProductGroupUpdate) {
async function editProductService(groupId: string, data: ProductUpdate) {
const { code, ...payload } = data;
const res = await api.put<ProductGroupUpdate>(`/product-group/${groupId}`, {
const res = await api.put<ProductUpdate>(`/product-group/${groupId}`, {
...payload,
});
@ -103,6 +214,13 @@ const useProductServiceStore = defineStore('api-product-service', () => {
}
return {
fetchStatsProductType,
fetchListProductServiceByIdType,
fetchListProductServiceType,
createProductServiceType,
editProductServiceType,
deleteProductServiceType,
fetchStatsProductGroup,
fetchListProductServiceById,
fetchListProductService,

View file

@ -1,6 +1,6 @@
import { Status } from '../types';
export type ProductGroup = {
export type Product = {
id: string;
code: string;
name: string;
@ -13,14 +13,14 @@ export type ProductGroup = {
updatedAt: string;
};
export interface ProductGroupCreate {
export interface ProductCreate {
remark: string;
detail: string;
name: string;
code: string;
}
export interface ProductGroupUpdate {
export interface ProductUpdate {
remark: string;
detail: string;
name: string;