diff --git a/src/stores/branch/index.ts b/src/stores/branch/index.ts index 9ce6513e..626782d4 100644 --- a/src/stores/branch/index.ts +++ b/src/stores/branch/index.ts @@ -15,6 +15,7 @@ const useBranchStore = defineStore('api-branch', () => { total: 0, }); const map = ref>({}); + const contact: Record = {}; watch(data, () => { data.value.result.forEach((v) => { @@ -72,9 +73,14 @@ const useBranchStore = defineStore('api-branch', () => { } async function fetchById< - Options extends { includeSubBranch?: boolean }, + Options extends { includeSubBranch?: boolean; includeContact?: boolean }, Data extends Branch & - (Options['includeSubBranch'] extends true ? { branch: [] } : unknown), + (Options['includeSubBranch'] extends true + ? { branch: Branch[] } + : unknown) & + (Options['includeContact'] extends true + ? { contact: BranchContact[] } + : unknown), >( id: string, opts?: Options, @@ -86,7 +92,9 @@ const useBranchStore = defineStore('api-branch', () => { ): Promise { const params = new URLSearchParams(); - if (opts?.includeSubBranch) params.append('includeSubBranch', 'true'); + for (const [k, v] of Object.entries(opts || {})) { + v !== undefined && params.append(k, v.toString()); + } const query = params.toString(); @@ -294,6 +302,27 @@ const useBranchStore = defineStore('api-branch', () => { 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, @@ -308,6 +337,8 @@ const useBranchStore = defineStore('api-branch', () => { addUser, removeUser, + getContact, + stats, userStats, };