Bind offer assistant requests and private context to their session

This commit is contained in:
2026-09-06 10:08:03 +03:00
parent 132bd815c6
commit ecc031a822
4 changed files with 114 additions and 21 deletions
+25
View File
@@ -0,0 +1,25 @@
/** Bind the entire multi-step request to its starting account and mounted view. */
export async function requestOfferAssistant({url, body, readToken, isCurrent = () => true, listDocuments, fetcher = fetch}) {
const token = readToken();
const check = () => {
if (!token || readToken() !== token || !isCurrent())
throw new Error('Oturum veya ihale değişti. İşlemi güncel ekrandan yeniden başlatın.');
};
check();
let payload = body;
if (listDocuments) {
const result = await listDocuments();
check();
// An unavailable inventory must not be represented as an empty inventory.
if (!Array.isArray(result.belgeler)) throw new Error('Belge listesi doğrulanamadı. Yeniden deneyin.');
payload = {...body, belgeler: result.belgeler
.filter(b => b.tur === 'firma' || b.ikn === body.ikn)
.map(b => ({ad:b.ad, kategori:b.kategori}))};
}
check();
const response = await fetcher(url, {method:'POST', headers:{'Content-Type':'application/json', Authorization:`Bearer ${token}`}, body:JSON.stringify(payload)});
const data = await response.json().catch(() => ({}));
check();
if (!response.ok || data.success === false) throw new Error(data.error || `HTTP ${response.status}`);
return data;
}