From 66b167dea55fa18c3d003db985b2f1cedd0279fd Mon Sep 17 00:00:00 2001 From: Methapon Metanipat Date: Tue, 17 Sep 2024 11:41:09 +0700 Subject: [PATCH] refactor: api store --- src/stores/customer/index.ts | 326 +++++++---------------------------- src/stores/utils/index.ts | 107 ++++++++++++ 2 files changed, 169 insertions(+), 264 deletions(-) diff --git a/src/stores/customer/index.ts b/src/stores/customer/index.ts index 6ec939f4..c2ec9032 100644 --- a/src/stores/customer/index.ts +++ b/src/stores/customer/index.ts @@ -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>(); + 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>( `/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 { + >(opts?: Options): Promise { 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( `/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 { + >(opts?: Options): Promise { 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( `/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('/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('/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(`/customer-branch/${id}/attachment`, { - headers: { 'X-Rtid': flowStore.rtid }, - }); + const res = await api.get(`/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(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, - flow?: { - sessionId?: string; - refTransactionId?: string; - transactionId?: string; - }, - ) { + async function editById(id: string, data: Partial) { 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/${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/${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(`/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(`/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('/customer-branch', payload, { - headers: { - 'X-Session-Id': flow?.sessionId, - 'X-Rtid': flow?.refTransactionId || flowStore.rtid, - 'X-Tid': flow?.transactionId, - }, - }); + const res = await api.post('/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( `/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-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-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, }; }); diff --git a/src/stores/utils/index.ts b/src/stores/utils/index.ts index 2abd3358..786b3b7e 100644 --- a/src/stores/utils/index.ts +++ b/src/stores/utils/index.ts @@ -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(`${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(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(api: AxiosInstance, base: string) { + return { + listFile: async (opts: { group: T }) => { + const res = await api.get(`${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(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;