import { ref, watch } from 'vue'; import { defineStore } from 'pinia'; import { Pagination } from '../types'; import { api } from 'src/boot/axios'; import { BankBook, Branch, BranchCreate } from './types'; import { BranchContact } from '../branch-contact/types'; import axios from 'axios'; import useFlowStore from '../flow'; import { User } from '../user/types'; type BranchId = string; const useBranchStore = defineStore('api-branch', () => { const flowStore = useFlowStore(); const data = ref>({ result: [], page: 0, pageSize: 0, total: 0, }); const map = ref>({}); const contact: Record = {}; watch(data, () => { data.value.result.forEach((v) => { map.value[v.id] = v; }); }); async function fetchList< Options extends { page?: number; pageSize?: number; zipCode?: string; query?: string; tree?: boolean; filter?: 'head' | 'sub'; }, Data extends Pagination, >( 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( `/branch${(params && '?'.concat(query)) || ''}`, { headers: { 'X-Session-Id': flow?.sessionId, 'X-Rtid': flow?.refTransactionId || flowStore.rtid, 'X-Tid': flow?.transactionId, }, }, ); if (res && res.status === 200) { data.value = res.data; return res.data; } return false; } async function fetchById< Options extends { includeSubBranch?: boolean; includeContact?: boolean }, Data extends Branch & (Options['includeSubBranch'] extends true ? { branch: Branch[] } : unknown) & (Options['includeContact'] extends true ? { contact: BranchContact[] } : unknown), >( id: string, 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( `/branch/${id}${(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 create( branch: BranchCreate, bank?: BankBook[], flow?: { sessionId?: string; refTransactionId?: string; transactionId?: string; }, ) { const { qrCodeImage, imageUrl, ...payload } = branch; const res = await api.post< Branch & { qrCodeImageUrl: string; qrCodeImageUploadUrl: string; imageUploadUrl: string; } >( '/branch', { ...payload, bank: bank }, { headers: { 'X-Session-Id': flow?.sessionId, 'X-Rtid': flow?.refTransactionId || flowStore.rtid, 'X-Tid': flow?.transactionId, }, }, ); qrCodeImage && (await axios .put(res.data.qrCodeImageUploadUrl, qrCodeImage, { headers: { 'Content-Type': qrCodeImage.type }, onUploadProgress: (e) => console.log(e), }) .catch((e) => console.error(e))); if (imageUrl) { await axios .put(res.data.imageUploadUrl, imageUrl, { headers: { 'Content-Type': imageUrl.type }, onUploadProgress: (e) => console.log(e), }) .catch((e) => console.error(e)); } if (!res) return false; return res.data; } async function editById( id: string, data: Partial, qrCodeImage?: File | undefined, imageHq?: File | undefined, bank?: BankBook[], flow?: { sessionId?: string; refTransactionId?: string; transactionId?: string; }, ) { const { ...payload } = data; const res = await api.put< Branch & { qrCodeImageUploadUrl: string; imageUploadUrl: string } >( `/branch/${id}`, { ...payload, bank: bank }, { headers: { 'X-Session-Id': flow?.sessionId, 'X-Rtid': flow?.refTransactionId || flowStore.rtid, 'X-Tid': flow?.transactionId, }, }, ); if (qrCodeImage) { await axios .put(res.data.qrCodeImageUploadUrl, qrCodeImage, { headers: { 'Content-Type': qrCodeImage.type }, onUploadProgress: (e) => console.log(e), }) .catch((e) => console.error(e)); } if (imageHq) { await axios .put(res.data.imageUploadUrl, imageHq, { headers: { 'Content-Type': imageHq.type }, onUploadProgress: (e) => console.log(e), }) .catch((e) => console.error(e)); } if (!res) return false; return res.data; } async function deleteById( id: string, flow?: { sessionId?: string; refTransactionId?: string; transactionId?: string; }, ) { const res = await api.delete(`/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 getUser( branchId: string, flow?: { sessionId?: string; refTransactionId?: string; transactionId?: string; }, ) { const res = await api.get(`/branch/${branchId}/user`, { 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 getAdmin( branchId: string | string[], flow?: { sessionId?: string; refTransactionId?: string; transactionId?: string; }, ) { const res = await api.get(`/branch/${branchId}/admin`, { 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 addUser( branchId: string, userId: string | string[], flow?: { sessionId?: string; refTransactionId?: string; transactionId?: string; }, ) { const res = await api.post( `/branch/${branchId}/user`, { user: ([] as string[]).concat(userId) }, { 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 removeUser( branchId: string, userId: string | string[], flow?: { sessionId?: string; refTransactionId?: string; transactionId?: string; }, ) { const res = await api.delete(`/branch/${branchId}/user`, { headers: { 'X-Session-Id': flow?.sessionId, 'X-Rtid': flow?.refTransactionId || flowStore.rtid, 'X-Tid': flow?.transactionId, }, data: { user: ([] as string[]).concat(userId) }, }); if (!res) return false; if (res.status === 200) return res.data; return false; } async function stats(flow?: { sessionId?: string; refTransactionId?: string; transactionId?: string; }) { const res = await api.get<{ hq: number; br: number; }>('/branch/stats', { 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 userStats( userType: string, flow?: { sessionId?: string; refTransactionId?: string; transactionId?: string; }, ) { const res = await api.get( `/branch/user-stats${userType ? '?'.concat(userType) : ''}`, { 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 getContact( branchId: BranchId, force = false, flow?: { sessionId?: string; refTransactionId?: string; transactionId?: string; }, ) { if (!force && contact[branchId]) return contact[branchId]; const res = await fetchById(branchId, { includeContact: true }, flow); if (res) { contact[branchId] = res.contact; return res.contact; } return false; } return { data, map, fetchList, fetchById, create, editById, deleteById, getAdmin, getUser, addUser, removeUser, getContact, stats, userStats, }; }); export default useBranchStore;