refactor: permission product service

This commit is contained in:
Methapon Metanipat 2024-09-10 14:36:36 +07:00
parent 67d360c92b
commit ed692c823d
5 changed files with 48 additions and 372 deletions

View file

@ -18,147 +18,20 @@ import {
Work,
ProductOnWork,
WorkCreate,
ServiceAndProduct,
TreeProduct,
} from './types';
import { computed, ref } from 'vue';
import { ref } from 'vue';
import useFlowStore from '../flow';
import useMyBranchStore from 'stores/my-branch';
const useProductServiceStore = defineStore('api-product-service', () => {
const useMyBranch = useMyBranchStore();
const recordTreeProductType = ref<Record<string, TreeProduct[]>>({});
// Product Type
const flowStore = useFlowStore();
const workNameItems = ref<{ id: string; name: string; isEdit: boolean }[]>(
[],
);
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?: {
page?: number;
pageSize?: number;
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<
Pagination<(ProductGroup & { productGroupId: string })[]>
>(`/product-type${(params && '?'.concat(query)) || ''}`, {
headers: {
'X-Session-Id': flow?.sessionId,
'X-Rtid': flow?.refTransactionId || flowStore.rtid,
'X-Tid': flow?.transactionId,
},
});
if (!res) return false;
if (res.status === 200) {
if (!!opts?.productGroupId) {
recordTreeProductType.value[opts?.productGroupId] = res.data.result;
}
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: Partial<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;
}
const workNameItems = ref<
{
id: string;
name: string;
isEdit: boolean;
}[]
>([]);
// Product Group
@ -182,19 +55,12 @@ const useProductServiceStore = defineStore('api-product-service', () => {
}
}
async function fetchListProductService(
opts?: {
page?: number;
pageSize?: number;
query?: string;
status?: 'CREATED' | 'ACTIVE' | 'INACTIVE';
},
flow?: {
sessionId?: string;
refTransactionId?: string;
transactionId?: string;
},
) {
async function fetchListProductService(opts?: {
page?: number;
pageSize?: number;
query?: string;
status?: 'CREATED' | 'ACTIVE' | 'INACTIVE';
}) {
const params = new URLSearchParams();
for (const [k, v] of Object.entries(opts || {})) {
@ -207,9 +73,7 @@ const useProductServiceStore = defineStore('api-product-service', () => {
`/product-group${(params && '?'.concat(query)) || ''}`,
{
headers: {
'X-Session-Id': flow?.sessionId,
'X-Rtid': flow?.refTransactionId || flowStore.rtid,
'X-Tid': flow?.transactionId,
'X-Rtid': flowStore.rtid,
},
},
);
@ -291,40 +155,25 @@ const useProductServiceStore = defineStore('api-product-service', () => {
}
}
async function fetchListProduct(
opts?: {
query?: string;
page?: number;
pageSize?: number;
productGroupId?: string;
status?: string;
},
flow?: {
sessionId?: string;
refTransactionId?: string;
transactionId?: string;
},
) {
async function fetchListProduct(opts?: {
query?: string;
page?: number;
pageSize?: number;
productGroupId?: string;
status?: string;
}) {
const params = new URLSearchParams();
for (const [k, v] of Object.entries(opts || {})) {
v !== undefined && params.append(k, v.toString());
}
if (useMyBranch.myBranch?.length !== 0 && useMyBranch.myBranch) {
params.append('registeredBranchId', useMyBranch.myBranch[0].id);
}
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 || flowStore.rtid,
'X-Tid': flow?.transactionId,
},
headers: { 'X-Rtid': flowStore.rtid },
},
);
@ -433,39 +282,26 @@ const useProductServiceStore = defineStore('api-product-service', () => {
}
}
async function fetchListService(
opts?: {
query?: string;
page?: number;
pageSize?: number;
productGroupId?: string;
status?: string;
},
flow?: {
sessionId?: string;
refTransactionId?: string;
transactionId?: string;
},
) {
async function fetchListService(opts?: {
query?: string;
page?: number;
pageSize?: number;
productGroupId?: string;
status?: string;
}) {
const params = new URLSearchParams();
for (const [k, v] of Object.entries(opts || {})) {
v !== undefined && params.append(k, v.toString());
}
if (useMyBranch.myBranch?.length !== 0 && useMyBranch.myBranch) {
params.append('registeredBranchId', useMyBranch.myBranch[0].id);
}
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 || flowStore.rtid,
'X-Tid': flow?.transactionId,
'X-Rtid': flowStore.rtid,
},
},
);
@ -508,11 +344,6 @@ const useProductServiceStore = defineStore('api-product-service', () => {
async function fetchListServiceByIdWork(
serviceId: string,
opts?: { query?: string; page?: number; pageSize?: number },
flow?: {
sessionId?: string;
refTransactionId?: string;
transactionId?: string;
},
) {
const params = new URLSearchParams();
@ -526,9 +357,7 @@ const useProductServiceStore = defineStore('api-product-service', () => {
`/service/${serviceId}/work${(params && '?'.concat(query)) || ''}`,
{
headers: {
'X-Session-Id': flow?.sessionId,
'X-Rtid': flow?.refTransactionId || flowStore.rtid,
'X-Tid': flow?.transactionId,
'X-Rtid': flowStore.rtid,
},
},
);
@ -598,19 +427,12 @@ const useProductServiceStore = defineStore('api-product-service', () => {
}
// work
async function fetchListWork(
opts?: {
query?: string;
page?: number;
pageSize?: number;
baseOnly?: boolean;
},
flow?: {
sessionId: string;
refTransactionId: string;
transactionId: string;
},
) {
async function fetchListWork(opts?: {
query?: string;
page?: number;
pageSize?: number;
baseOnly?: boolean;
}) {
const params = new URLSearchParams();
for (const [k, v] of Object.entries(opts || {})) {
@ -623,9 +445,7 @@ const useProductServiceStore = defineStore('api-product-service', () => {
Pagination<(Work & { productOnWork: ProductOnWork[] })[]>
>(`/work${(params && '?'.concat(query)) || ''}`, {
headers: {
'X-Session-Id': flow?.sessionId,
'X-Rtid': flow?.refTransactionId || flowStore.rtid,
'X-Tid': flow?.transactionId,
'X-Rtid': flowStore.rtid,
},
});
@ -698,11 +518,6 @@ const useProductServiceStore = defineStore('api-product-service', () => {
async function fetchListProductByIdWork(
workId: string,
opts?: { query?: string; page?: number; pageSize?: number },
flow?: {
sessionId: string;
refTransactionId: string;
transactionId: string;
},
) {
const params = new URLSearchParams();
@ -716,9 +531,7 @@ const useProductServiceStore = defineStore('api-product-service', () => {
`/work/${workId}/product${(params && '?'.concat(query)) || ''}`,
{
headers: {
'X-Session-Id': flow?.sessionId,
'X-Rtid': flow?.refTransactionId || flowStore.rtid,
'X-Tid': flow?.transactionId,
'X-Rtid': flowStore.rtid,
},
},
);
@ -741,60 +554,8 @@ const useProductServiceStore = defineStore('api-product-service', () => {
}
}
async function fetchListProductAndService(
opts?: {
query?: string;
page?: number;
pageSize?: number;
productGroupId?: string;
status?: 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());
}
if (useMyBranch.myBranch?.length !== 0 && useMyBranch.myBranch) {
params.append('registeredBranchId', useMyBranch.myBranch[0].id);
}
const query = params.toString();
const res = await api.get<Pagination<ServiceAndProduct[]>>(
`/product-service${(params && '?'.concat(query)) || ''}`,
{
headers: {
'X-Session-Id': flow?.sessionId,
'X-Rtid': flow?.refTransactionId || flowStore.rtid,
'X-Tid': flow?.transactionId,
},
},
);
if (!res) return false;
if (res.status === 200) {
return res.data;
}
return false;
}
return {
workNameItems,
recordTreeProductType,
fetchStatsProductType,
fetchListProductServiceByIdType,
fetchListProductServiceType,
createProductServiceType,
editProductServiceType,
deleteProductServiceType,
fetchStatsProductGroup,
fetchListProductServiceById,
@ -826,8 +587,6 @@ const useProductServiceStore = defineStore('api-product-service', () => {
fetchListProductByIdWork,
fetchListOfWork,
fetchListProductAndService,
};
});

View file

@ -1,9 +1,5 @@
import { Status } from '../types';
import { Pagination, UpdatedBy, CreatedBy } from 'stores/types';
export type ServiceAndProduct =
| (Service & { type: 'service' })
| (ProductList & { type: 'product' });
import { UpdatedBy, CreatedBy } from 'stores/types';
export interface TreeProduct {
name: string;
@ -63,7 +59,6 @@ export interface ServiceCreate {
image?: File;
status?: Status;
productGroupId: string;
registeredBranchId: string | null;
}
export interface Attributes {
@ -154,13 +149,11 @@ export interface ProductList {
code: string;
id: string;
imageUrl: string;
registeredBranchId: string;
}
export interface ProductCreate {
expenseType: string;
vatIncluded: boolean;
registeredBranchId: string;
productGroupId: string;
remark: string;
serviceCharge: number;