feat: get contact from branch

This commit is contained in:
Methapon-Frappet 2024-04-12 22:08:01 +07:00
parent b69146ff8a
commit 7068c63086

View file

@ -15,6 +15,7 @@ const useBranchStore = defineStore('api-branch', () => {
total: 0, total: 0,
}); });
const map = ref<Record<BranchId, Branch & { contact?: BranchContact[] }>>({}); const map = ref<Record<BranchId, Branch & { contact?: BranchContact[] }>>({});
const contact: Record<BranchId, BranchContact[]> = {};
watch(data, () => { watch(data, () => {
data.value.result.forEach((v) => { data.value.result.forEach((v) => {
@ -72,9 +73,14 @@ const useBranchStore = defineStore('api-branch', () => {
} }
async function fetchById< async function fetchById<
Options extends { includeSubBranch?: boolean }, Options extends { includeSubBranch?: boolean; includeContact?: boolean },
Data extends Branch & 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, id: string,
opts?: Options, opts?: Options,
@ -86,7 +92,9 @@ const useBranchStore = defineStore('api-branch', () => {
): Promise<Data | false> { ): Promise<Data | false> {
const params = new URLSearchParams(); 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(); const query = params.toString();
@ -294,6 +302,27 @@ const useBranchStore = defineStore('api-branch', () => {
return false; 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 { return {
data, data,
map, map,
@ -308,6 +337,8 @@ const useBranchStore = defineStore('api-branch', () => {
addUser, addUser,
removeUser, removeUser,
getContact,
stats, stats,
userStats, userStats,
}; };