refactor: api store

This commit is contained in:
Methapon Metanipat 2024-09-17 11:41:09 +07:00
parent 6f54693eb1
commit 66b167dea5
2 changed files with 169 additions and 264 deletions

View file

@ -6,22 +6,27 @@ import {
Customer,
CustomerCreate,
CustomerUpdate,
BranchAttachmentCreate,
BranchAttachment,
CustomerStats,
CustomerBranch,
CustomerBranchCreate,
CustomerType,
} from './types';
import axios from 'axios';
import useFlowStore from '../flow';
import { Employee } from '../employee/types';
import { baseUrl } from '../utils';
import { baseUrl, manageFile } from '../utils';
const useCustomerStore = defineStore('api-customer', () => {
const flowStore = useFlowStore();
const data = ref<Pagination<Customer[]>>();
const fileManager = manageFile<
| 'citizen'
| 'house-registration'
| 'commercial-registration'
| 'vat-registration'
| 'power-of-attorney'
>(api, 'customer-branch');
async function setImage(id: string, image: File) {
await api.put(`/customer/${id}/image`, image, {
headers: { 'Content-Type': image?.type },
@ -29,27 +34,12 @@ const useCustomerStore = defineStore('api-customer', () => {
});
}
async function fetchById(
customerId: string,
flow?: {
sessionId?: string;
refTransactionId?: string;
transactionId?: string;
},
) {
async function fetchById(customerId: string) {
const res = await api.get<
Customer & { branch: CustomerBranch[]; registeredBranchId: string }
>(`/customer/${customerId}`, {
headers: {
'X-Session-Id': flow?.sessionId,
'X-Rtid': flow?.refTransactionId || flowStore.rtid,
'X-Tid': flow?.transactionId,
},
});
>(`/customer/${customerId}`);
if (res && res.status === 200) {
return res.data;
}
if (res && res.status === 200) return res.data;
return false;
}
@ -62,11 +52,6 @@ const useCustomerStore = defineStore('api-customer', () => {
page?: number;
pageSize?: number;
},
flow?: {
sessionId?: string;
refTransactionId?: string;
transactionId?: string;
},
) {
const params = new URLSearchParams();
for (const [k, v] of Object.entries(opts || {})) {
@ -77,13 +62,6 @@ const useCustomerStore = defineStore('api-customer', () => {
const res = await api.get<Pagination<Employee[]>>(
`/customer-branch/${idBranch}/employee${(params && '?'.concat(query)) || ''}`,
{
headers: {
'X-Session-Id': flow?.sessionId,
'X-Rtid': flow?.refTransactionId || flowStore.rtid,
'X-Tid': flow?.transactionId,
},
},
);
if (res.status === 200) {
@ -107,14 +85,7 @@ const useCustomerStore = defineStore('api-customer', () => {
? { customer: Customer }
: unknown))[]
>,
>(
opts?: Options,
flow?: {
sessionId?: string;
refTransactionId?: string;
transactionId?: string;
},
): Promise<Data | false> {
>(opts?: Options): Promise<Data | false> {
const params = new URLSearchParams();
for (const [k, v] of Object.entries(opts || {})) {
@ -125,13 +96,6 @@ const useCustomerStore = defineStore('api-customer', () => {
const res = await api.get<Data>(
`/customer-branch${(params && '?'.concat(query)) || ''}`,
{
headers: {
'X-Session-Id': flow?.sessionId,
'X-Rtid': flow?.refTransactionId || flowStore.rtid,
'X-Tid': flow?.transactionId,
},
},
);
if (!res) return false;
@ -157,14 +121,7 @@ const useCustomerStore = defineStore('api-customer', () => {
? { branch: CustomerBranch[] }
: unknown))[]
>,
>(
opts?: Options,
flow?: {
sessionId?: string;
refTransactionId?: string;
transactionId?: string;
},
): Promise<Data | false> {
>(opts?: Options): Promise<Data | false> {
const params = new URLSearchParams();
for (const [k, v] of Object.entries(opts || {})) {
@ -175,13 +132,6 @@ const useCustomerStore = defineStore('api-customer', () => {
const res = await api.get<Data>(
`/customer${(params && '?'.concat(query)) || ''}`,
{
headers: {
'X-Session-Id': flow?.sessionId,
'X-Rtid': flow?.refTransactionId || flowStore.rtid,
'X-Tid': flow?.transactionId,
},
},
);
if (!res) return false;
@ -192,18 +142,8 @@ const useCustomerStore = defineStore('api-customer', () => {
return false;
}
async function getStatsCustomer(flow?: {
sessionId?: string;
refTransactionId?: string;
transactionId?: string;
}) {
const res = await api.get<CustomerStats>('/customer/type-stats', {
headers: {
'X-Session-Id': flow?.sessionId,
'X-Rtid': flow?.refTransactionId || flowStore.rtid,
'X-Tid': flow?.transactionId,
},
});
async function getStatsCustomer() {
const res = await api.get<CustomerStats>('/customer/type-stats');
if (!res) return false;
@ -219,7 +159,7 @@ const useCustomerStore = defineStore('api-customer', () => {
`/customer-branch/${opts.branchId}/attachment/${opts.filename || opts.file.name}`,
opts.file,
{
headers: { 'X-Rtid': flowStore.rtid, 'Content-Type': opts.file.type },
headers: { 'Content-Type': opts.file.type },
onUploadProgress: (e) => console.log(e),
},
);
@ -239,15 +179,12 @@ const useCustomerStore = defineStore('api-customer', () => {
}
async function listAttachment(id: string) {
const res = await api.get<string[]>(`/customer-branch/${id}/attachment`, {
headers: { 'X-Rtid': flowStore.rtid },
});
const res = await api.get<string[]>(`/customer-branch/${id}/attachment`);
if (res.status >= 400) return false;
return res.data;
}
async function getAttachment(id: string, filename: string, download = false) {
const url = `${baseUrl}/customer-branch/${id}/attachment/${filename}`;
const res = await api.get<string>(url);
@ -267,21 +204,8 @@ const useCustomerStore = defineStore('api-customer', () => {
return res.data;
}
async function fetchImageListById(
id: string,
flow?: {
sessionId?: string;
refTransactionId?: string;
transactionId?: string;
},
) {
const res = await api.get(`/customer/${id}/image`, {
headers: {
'X-Session-Id': flow?.sessionId,
'X-Rtid': flow?.refTransactionId || flowStore.rtid,
'X-Tid': flow?.transactionId,
},
});
async function fetchImageListById(id: string) {
const res = await api.get(`/customer/${id}/image`);
if (!res) return false;
if (res.status === 200) return res.data;
@ -301,22 +225,8 @@ const useCustomerStore = defineStore('api-customer', () => {
return name;
}
async function deleteImageByName(
id: string,
name: string,
flow?: {
sessionId?: string;
refTransactionId?: string;
transactionId?: string;
},
) {
const res = await api.delete(`/customer/${id}/image/${name}`, {
headers: {
'X-Session-Id': flow?.sessionId,
'X-Rtid': flow?.refTransactionId || flowStore.rtid,
'X-Tid': flow?.transactionId,
},
});
async function deleteImageByName(id: string, name: string) {
const res = await api.delete(`/customer/${id}/image/${name}`);
if (!res) return false;
}
@ -327,11 +237,6 @@ const useCustomerStore = defineStore('api-customer', () => {
selectedImage: string;
list: { url: string; imgFile: File | null; name: string }[];
},
flow?: {
sessionId?: string;
refTransactionId?: string;
transactionId?: string;
},
) {
if (data.customerBranch) {
if (data.customerBranch[0].citizenId) {
@ -357,28 +262,18 @@ const useCustomerStore = defineStore('api-customer', () => {
imageUrl: string;
imageUploadUrl: string;
}
>(
'/customer',
{
...payload,
branch: data.customerBranch?.map((v) => ({
...v,
file: undefined,
branchCode: undefined,
id: undefined,
customerId: undefined,
codeCustomer: undefined,
})),
selectedImage: imgList.selectedImage,
},
{
headers: {
'X-Session-Id': flow?.sessionId,
'X-Rtid': flow?.refTransactionId || flowStore.rtid,
'X-Tid': flow?.transactionId,
},
},
);
>('/customer', {
...payload,
branch: data.customerBranch?.map((v) => ({
...v,
file: undefined,
branchCode: undefined,
id: undefined,
customerId: undefined,
codeCustomer: undefined,
})),
selectedImage: imgList.selectedImage,
});
if (imgList.list.length > 0 && res.data.id) {
for (let index = 0; index < imgList.list.length; index++) {
@ -393,15 +288,7 @@ const useCustomerStore = defineStore('api-customer', () => {
return res.data;
}
async function editById(
id: string,
data: Partial<CustomerUpdate>,
flow?: {
sessionId?: string;
refTransactionId?: string;
transactionId?: string;
},
) {
async function editById(id: string, data: Partial<CustomerUpdate>) {
const { customerBranch, image, ...payload } = data;
const res = await api.put<
@ -410,53 +297,30 @@ const useCustomerStore = defineStore('api-customer', () => {
imageUrl: string;
imageUploadUrl: string;
}
>(
`/customer/${id}`,
{
...payload,
branch: data.customerBranch?.map((v) => ({
...v,
file: undefined,
branchCode: undefined,
id: undefined,
customerId: undefined,
codeCustomer: undefined,
createAt: undefined,
createdByUserId: undefined,
statusOrder: undefined,
updatedAt: undefined,
updatedByUserId: undefined,
})),
},
{
headers: {
'X-Session-Id': flow?.sessionId,
'X-Rtid': flow?.refTransactionId || flowStore.rtid,
'X-Tid': flow?.transactionId,
},
},
);
>(`/customer/${id}`, {
...payload,
branch: data.customerBranch?.map((v) => ({
...v,
file: undefined,
branchCode: undefined,
id: undefined,
customerId: undefined,
codeCustomer: undefined,
createAt: undefined,
createdByUserId: undefined,
statusOrder: undefined,
updatedAt: undefined,
updatedByUserId: undefined,
})),
});
if (!res) return false;
return res.data;
}
async function deleteById(
id: string,
flow?: {
sessionId?: string;
refTransactionId?: string;
transactionId?: string;
},
) {
const res = await api.delete<Customer>(`/customer/${id}`, {
headers: {
'X-Session-Id': flow?.sessionId,
'X-Rtid': flow?.refTransactionId || flowStore.rtid,
'X-Tid': flow?.transactionId,
},
});
async function deleteById(id: string) {
const res = await api.delete<Customer>(`/customer/${id}`);
if (!res) return false;
if (res.status === 200) return res.data;
@ -464,21 +328,8 @@ const useCustomerStore = defineStore('api-customer', () => {
return false;
}
async function getBranchById(
id: string,
flow?: {
sessionId?: string;
refTransactionId?: string;
transactionId?: string;
},
) {
const res = await api.get<CustomerBranch>(`/customer-branch/${id}`, {
headers: {
'X-Session-Id': flow?.sessionId,
'X-Rtid': flow?.refTransactionId || flowStore.rtid,
'X-Tid': flow?.transactionId,
},
});
async function getBranchById(id: string) {
const res = await api.get<CustomerBranch>(`/customer-branch/${id}`);
if (!res) return false;
return res.data;
@ -490,11 +341,6 @@ const useCustomerStore = defineStore('api-customer', () => {
customerId: string;
codeCustomer: string;
},
flow?: {
sessionId?: string;
refTransactionId?: string;
transactionId?: string;
},
) {
const { codeCustomer, statusSave, code, file, birthDate, ...payload } =
data;
@ -516,17 +362,9 @@ const useCustomerStore = defineStore('api-customer', () => {
delete data['birthDate'];
}
const res = await api.post<CustomerBranch>('/customer-branch', payload, {
headers: {
'X-Session-Id': flow?.sessionId,
'X-Rtid': flow?.refTransactionId || flowStore.rtid,
'X-Tid': flow?.transactionId,
},
});
const res = await api.post<CustomerBranch>('/customer-branch', payload);
if (!res) return false;
// if (file) await addBranchAttachment(res.data.id, { file });
return res.data;
}
@ -539,11 +377,6 @@ const useCustomerStore = defineStore('api-customer', () => {
codeCustomer: string;
}
>,
flow?: {
sessionId?: string;
refTransactionId?: string;
transactionId?: string;
},
) {
if (data.citizenId) {
delete data['authorizedNameEN'];
@ -573,8 +406,6 @@ const useCustomerStore = defineStore('api-customer', () => {
...payload
} = data;
console.log(payload);
if (!!payload.citizenId) {
delete payload['registerDate'];
delete payload['registerName'];
@ -588,37 +419,15 @@ const useCustomerStore = defineStore('api-customer', () => {
const res = await api.put<CustomerBranch>(
`/customer-branch/${id}`,
payload,
{
headers: {
'X-Session-Id': flow?.sessionId,
'X-Rtid': flow?.refTransactionId || flowStore.rtid,
'X-Tid': flow?.transactionId,
},
},
);
// if (file) await addBranchAttachment(id, { file });
if (!res) return false;
return res.data;
}
async function deleteBranchById(
id: string,
flow?: {
sessionId?: string;
refTransactionId?: string;
transactionId?: string;
},
) {
const res = await api.delete<Customer>(`/customer-branch/${id}`, {
headers: {
'X-Session-Id': flow?.sessionId,
'X-Rtid': flow?.refTransactionId || flowStore.rtid,
'X-Tid': flow?.transactionId,
},
});
async function deleteBranchById(id: string) {
const res = await api.delete<Customer>(`/customer-branch/${id}`);
if (!res) return false;
if (res.status === 200) return res.data;
@ -626,21 +435,8 @@ const useCustomerStore = defineStore('api-customer', () => {
return false;
}
async function fetchListCustomeBranchById(
branchId: string,
flow?: {
sessionId?: string;
refTransactionId?: string;
transactionId?: string;
},
) {
const res = await api.get(`/customer-branch/${branchId}`, {
headers: {
'X-Session-Id': flow?.sessionId,
'X-Rtid': flow?.refTransactionId || flowStore.rtid,
'X-Tid': flow?.transactionId,
},
});
async function fetchListCustomeBranchById(branchId: string) {
const res = await api.get(`/customer-branch/${branchId}`);
if (res && res.status === 200) {
return res.data;
@ -678,6 +474,8 @@ const useCustomerStore = defineStore('api-customer', () => {
getAttachment,
putAttachment,
deleteAttachment,
...fileManager,
};
});

View file

@ -4,6 +4,7 @@ import { ComposerTranslation, useI18n } from 'vue-i18n';
import { defineStore } from 'pinia';
import { Ref, ref } from 'vue';
import { getRole } from 'src/services/keycloak';
import { AxiosInstance } from 'axios';
export const baseUrl = import.meta.env.VITE_API_BASE_URL;
@ -234,4 +235,110 @@ export function resetScrollBar(elementId: string) {
}
}
export function manageAttachment(api: AxiosInstance, base: string) {
return {
listAttachment: async () => {
const res = await api.get<string[]>(`${base}/attachment`);
if (res.status >= 400) return false;
return res.data;
},
getAttachment: async (opts: {
parentId: string;
name: string;
download?: boolean;
}) => {
const url = `/${opts.parentId}/attachment/${opts.name}`;
const res = await api.get<string>(url);
if (opts.download) {
fetch(res.data)
.then(async (res) => await res.blob())
.then((blob) => {
const a = document.createElement('a');
a.download = opts.name;
a.href = window.URL.createObjectURL(blob);
a.click();
a.remove();
});
}
return res.data;
},
putAttachment: async (opts: {
parentId: string;
name: string;
file: File;
}) => {
const res = await api.put(
`/${opts.parentId}/attachment/${opts.name}`,
opts.file,
{
headers: { 'Content-Type': opts.file.type },
onUploadProgress: (e) => console.log(e),
},
);
if (res.status < 400) return true;
return false;
},
delAttachment: async (opts: { parentId: string; name: string }) => {
const res = await api.delete(`/${opts.parentId}/attachment/${opts.name}`);
if (res.status < 400) return true;
return false;
},
};
}
export function manageFile<T extends string>(api: AxiosInstance, base: string) {
return {
listFile: async (opts: { group: T }) => {
const res = await api.get<string[]>(`${base}/file-${opts.group}`);
if (res.status >= 400) return false;
return res.data;
},
getFile: async (opts: {
group: T;
parentId: string;
fileId: string;
download?: boolean;
}) => {
const url = `/${opts.parentId}/file-${opts.group}/${opts.fileId}`;
const res = await api.get<string>(url);
if (opts.download) {
fetch(res.data)
.then(async (res) => await res.blob())
.then((blob) => {
const a = document.createElement('a');
a.download = opts.fileId;
a.href = window.URL.createObjectURL(blob);
a.click();
a.remove();
});
}
return res.data;
},
putFile: async (opts: {
group: T;
parentId: string;
fileId: string;
file: File;
}) => {
const res = await api.put(
`/${opts.parentId}/file-${opts.group}/${opts.fileId}`,
opts.file,
{
headers: { 'Content-Type': opts.file.type },
onUploadProgress: (e) => console.log(e),
},
);
if (res.status < 400) return true;
return false;
},
delFile: async (opts: { group: T; parentId: string; fileId: string }) => {
const res = await api.delete(
`/${opts.parentId}/file-${opts.group}/${opts.fileId}`,
);
if (res.status < 400) return true;
return false;
},
};
}
export default useUtilsStore;