Skip to content

Commit 41f8826

Browse files
committed
fix(byok): route AI config save through backend worker to bypass RLS + Gemini auth fix
1 parent 933cad3 commit 41f8826

1 file changed

Lines changed: 23 additions & 27 deletions

File tree

dashboard/hr.html

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1844,37 +1844,33 @@ <h4>Multi-Platform Job Syndication</h4>
18441844
ai_model: provider === 'cloudflare' ? document.getElementById('cfModelSelect').value : document.getElementById('aiModel').value
18451845
};
18461846

1847-
const db = (typeof getSupabaseClient === 'function' ? getSupabaseClient() : null) || window.SimpaticoDB || window._supabaseClient;
1848-
if (!db) throw new Error('Database connection not available');
1847+
const tenantId = window.SIMPATICO_CONFIG?.tenantId || compId;
1848+
const workerUrl = window.SIMPATICO_CONFIG?.workerUrl || '';
1849+
const authToken = typeof getAuthToken === 'function' ? getAuthToken() : '';
18491850

1850-
const res = await db.from('companies').update(updates).eq('id', compId);
1851-
if (res.error) {
1852-
if (res.error.message.includes('schema cache') || res.error.message.includes('column') || res.error.code === 'PGRST204') {
1853-
console.warn('Supabase schema cache is stale or columns missing. Saving to local cache only.', res.error);
1854-
} else {
1855-
throw new Error(res.error.message);
1856-
}
1851+
// Save via backend worker (uses service key to bypass RLS)
1852+
const headers = { 'Content-Type': 'application/json', 'X-Tenant-ID': tenantId };
1853+
if (authToken) headers['Authorization'] = 'Bearer ' + authToken;
1854+
1855+
const saveRes = await fetch(workerUrl + '/ai/byok-config', {
1856+
method: 'POST',
1857+
headers,
1858+
body: JSON.stringify(updates),
1859+
});
1860+
1861+
if (!saveRes.ok) {
1862+
const errJson = await saveRes.json().catch(() => ({}));
1863+
throw new Error(errJson.error?.message || `Server error ${saveRes.status}`);
18571864
}
1858-
1859-
// Update local cache with Multi-Tenant Isolation
1860-
const tenantId = window.SIMPATICO_CONFIG?.tenantId || compId;
1865+
1866+
const saveData = await saveRes.json();
1867+
console.log('[BYOK] Config saved via backend:', saveData);
1868+
1869+
// Also update local cache for UI state
18611870
localStorage.setItem('simpatico_ai_settings_' + tenantId, JSON.stringify(updates));
1862-
18631871
Object.assign(compData, updates);
18641872
localStorage.setItem('simpatico_company', JSON.stringify(compData));
18651873

1866-
// Invalidate server-side BYOK config cache so changes take effect immediately
1867-
try {
1868-
const workerUrl = window.SIMPATICO_CONFIG?.workerUrl || '';
1869-
const authToken = typeof getAuthToken === 'function' ? getAuthToken() : '';
1870-
const cacheHeaders = { 'Content-Type': 'application/json', 'X-Tenant-ID': tenantId };
1871-
if (authToken) cacheHeaders['Authorization'] = 'Bearer ' + authToken;
1872-
await fetch(workerUrl + '/ai/byok-cache-clear', { method: 'POST', headers: cacheHeaders });
1873-
console.log('[BYOK] Server-side cache cleared');
1874-
} catch (cacheErr) {
1875-
console.warn('[BYOK] Cache clear failed (non-critical):', cacheErr.message);
1876-
}
1877-
18781874
if (typeof showToast === 'function') {
18791875
let modelLabel;
18801876
if (provider === 'cloudflare') {
@@ -1883,10 +1879,10 @@ <h4>Multi-Platform Job Syndication</h4>
18831879
} else {
18841880
modelLabel = updates.ai_model || provider;
18851881
}
1886-
showToast('AI Config saved — Active: ' + modelLabel);
1882+
showToast('AI Config saved — Active: ' + modelLabel + (saveData?.data?.rows_updated ? ' ✓ DB updated' : ''));
18871883
}
18881884
} catch (err) {
1889-
console.error(err);
1885+
console.error('[BYOK] Save failed:', err);
18901886
alert("Failed to save AI settings: " + err.message);
18911887
} finally {
18921888
btn.innerHTML = '<i class="fas fa-save"></i> Save AI Config';

0 commit comments

Comments
 (0)