import { ref } from 'vue'; import { defineStore } from 'pinia'; import { Pagination } from '../types'; import { api } from 'src/boot/axios'; 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'; const useCustomerStore = defineStore('api-customer', () => { const flowStore = useFlowStore(); const data = ref>(); async function setImage(id: string, image: File) { await api.put(`/customer/${id}/image`, image, { headers: { 'Content-Type': image?.type }, onUploadProgress: (e) => console.log(e), }); } async function fetchById( customerId: string, flow?: { sessionId?: string; refTransactionId?: string; transactionId?: 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, }, }); if (res && res.status === 200) { return res.data; } return false; } async function fetchBranchEmployee( idBranch: string, opts?: { zipCode?: string; 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>( `/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) { return res; } } async function fetchListCustomeBranch< Options extends { zipCode?: string; customerId?: string; includeCustomer?: boolean; status?: 'CREATED' | 'ACTIVE' | 'INACTIVE'; query?: string; page?: number; pageSize?: number; }, Data extends Pagination< (CustomerBranch & (Options['includeCustomer'] extends true ? { customer: Customer } : unknown))[] >, >( opts?: Options, flow?: { sessionId?: string; refTransactionId?: string; transactionId?: string; }, ): Promise { 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( `/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; if (res.status === 200) { return res.data; } return false; } async function fetchList< Options extends { page?: number; pageSize?: number; query?: string; includeBranch?: boolean; status?: 'CREATED' | 'ACTIVE' | 'INACTIVE'; customerType?: CustomerType; }, Data extends Pagination< (Customer & (Options['includeBranch'] extends true ? { branch: CustomerBranch[] } : unknown))[] >, >( opts?: Options, flow?: { sessionId?: string; refTransactionId?: string; transactionId?: string; }, ): Promise { 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( `/customer${(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; } 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, }, }); if (!res) return false; return res.data; } async function putAttachment(opts: { branchId: string; file: File; filename?: string; }) { const res = await api.put( `/customer-branch/${opts.branchId}/attachment/${opts.filename || opts.file.name}`, opts.file, { headers: { 'X-Rtid': flowStore.rtid, 'Content-Type': opts.file.type }, onUploadProgress: (e) => console.log(e), }, ); if (res.status >= 400) return false; return true; } async function deleteAttachment(id: string, filename: string) { const res = await api.delete( `/customer-branch/${id}/attachment/${filename}`, ); if (res.status >= 400) return false; return true; } async function listAttachment(id: string) { const res = await api.get(`/customer-branch/${id}/attachment`, { headers: { 'X-Rtid': flowStore.rtid }, }); 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); if (download) { fetch(res.data) .then(async (res) => await res.blob()) .then((blob) => { const a = document.createElement('a'); a.download = filename; a.href = window.URL.createObjectURL(blob); a.click(); a.remove(); }); } 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, }, }); if (!res) return false; if (res.status === 200) return res.data; if (res.status === 204) return null; return false; } async function addImageList(file: File, customerId: string, name: string) { await api .put(`/customer/${customerId}/image/${name}`, file, { headers: { 'Content-Type': file.type }, onUploadProgress: (e) => console.log(e), }) .catch((e) => console.error(e)); 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, }, }); if (!res) return false; } async function create( data: CustomerCreate, imgList: { 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) { delete data.customerBranch[0]['authorizedNameEN']; delete data.customerBranch[0]['authorizedName']; delete data.customerBranch[0]['authorizedCapital']; delete data.customerBranch[0]['registerDate']; delete data.customerBranch[0]['registerNameEN']; delete data.customerBranch[0]['registerName']; delete data.customerBranch[0]['legalPersonNo']; } else { delete data.customerBranch[0]['citizenId']; } if (!data.customerBranch[0].birthDate) delete data.customerBranch[0]['birthDate']; } const { customerBranch, image, ...payload } = data; const res = await api.post< Customer & { branch: CustomerBranch[]; 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, }, }, ); if (imgList.list.length > 0 && res.data.id) { for (let index = 0; index < imgList.list.length; index++) { const imgFile = imgList.list[index].imgFile; if (imgFile) await addImageList(imgFile, res.data.id, imgList.list[index].name); } } if (!res) return false; return res.data; } async function editById( id: string, data: Partial, flow?: { sessionId?: string; refTransactionId?: string; transactionId?: string; }, ) { const { customerBranch, image, ...payload } = data; const res = await api.put< Customer & { branch: CustomerBranch[]; 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, }, }, ); 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, }, }); if (!res) return false; if (res.status === 200) return res.data; 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, }, }); if (!res) return false; return res.data; } async function createBranch( data: CustomerBranchCreate & { id?: string; customerId: string; codeCustomer: string; }, flow?: { sessionId?: string; refTransactionId?: string; transactionId?: string; }, ) { const { codeCustomer, statusSave, code, file, birthDate, ...payload } = data; if (data.citizenId) { delete data['authorizedNameEN']; delete data['authorizedName']; delete data['registerDate']; delete data['authorizedCapital']; delete data['registerNameEN']; delete data['registerName']; delete data['legalPersonNo']; } else { delete data['citizenId']; } console.log(data.birthDate); console.log(!data.birthDate); if (!data.birthDate) { 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, }, }); if (!res) return false; // if (file) await addBranchAttachment(res.data.id, { file }); return res.data; } async function editBranchById( id: string, data: Partial< CustomerBranch & { id?: string; customerId: string; codeCustomer: string; } >, flow?: { sessionId?: string; refTransactionId?: string; transactionId?: string; }, ) { if (data.citizenId) { delete data['authorizedNameEN']; delete data['authorizedName']; delete data['registerDate']; delete data['authorizedCapital']; delete data['registerNameEN']; delete data['registerName']; delete data['legalPersonNo']; } else { delete data['citizenId']; } if (!data.birthDate) delete data['birthDate']; const { code, status, codeCustomer, createdAt, createdByUserId, statusOrder, updatedAt, updatedByUserId, file, registerCompanyName, statusSave, ...payload } = data; console.log(payload); if (!!payload.citizenId) { delete payload['registerDate']; delete payload['registerName']; delete payload['registerNameEN']; delete payload['authorizedCapital']; delete payload['legalPersonNo']; } else { delete payload['citizenId']; } 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, }, }); if (!res) return false; if (res.status === 200) return res.data; 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, }, }); if (res && res.status === 200) { return res.data; } return false; } return { data, getStatsCustomer, setImage, fetchListCustomeBranch, fetchById, fetchList, fetchImageListById, addImageList, deleteImageByName, create, editById, deleteById, createBranch, getBranchById, editBranchById, deleteBranchById, fetchListCustomeBranchById, fetchBranchEmployee, listAttachment, getAttachment, putAttachment, deleteAttachment, }; }); export default useCustomerStore;