pb collection updates success notes added fallback to capture job number and id. id capture for the Job_Number_Id still not working.

This commit is contained in:
2025-12-16 17:02:39 +00:00
parent a0c62af25c
commit 98ca33cd94
7 changed files with 122 additions and 26 deletions
+59 -24
View File
@@ -4,8 +4,10 @@
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1" maximum-scale="1" />
<title>Job Info — Vanilla JS</title>
<script src="https://cdn.tailwindcss.com"></script>
<script src="https://cdn.jsdelivr.net/npm/pocketbase@0.26.5/dist/pocketbase.umd.js"></script>
<script src="https://cdn.tailwindcss.com"></script> <!-- Suppress Tailwind CDN warning for now -->
<script>
if (window.tailwind) window.tailwind.config = { corePlugins: { preflight: true } }
</script><script src="https://cdn.jsdelivr.net/npm/pocketbase@0.26.5/dist/pocketbase.umd.js"></script>
<script src="auth.js"></script>
</head>
<body class="font-sans bg-gray-100 m-0 text-gray-900 overflow-hidden touch-pan-y h-screen flex flex-col">
@@ -123,6 +125,8 @@
const PB_COLLECTION_URL = "https://pocketbase.ccllc.pro/api/collections/pbc_1407612689/records";
const NOTES_COLLECTION_URL = "https://pocketbase.ccllc.pro/api/collections/notes/records";
const perPage = 50;
// Notes relation handling: keep it simple; send the job id in a known field
// and avoid schema reads that may require elevated privileges.
let jobsCache = [], visibleJobs = [], currentJob = null;
@@ -157,10 +161,15 @@
if(!versionLabel) return;
try{
const res = await fetch('/version');
if(!res.ok) return;
if(!res.ok) {
console.warn(`Version endpoint returned ${res.status}. Using default version.`);
return;
}
const data = await res.json().catch(()=>null);
if(data?.version) versionLabel.textContent = `v${data.version}`;
}catch(err){ /* silent */ }
}catch(err){
console.debug('Version load failed (non-critical):', err.message);
}
}
loadVersion();
@@ -537,36 +546,62 @@
try{
const payloadNote = {
Note: formatted.trim(),
Job_Number: currentJob?.Job_Number || '', // text value
Job_Number_Id: currentJob?.id || '' // id value
Job_Number: currentJob?.Job_Number || '',
// Job_Number_Id is a multiple relation field, so send as array
Job_Number_Id: currentJob?.id ? [currentJob.id] : []
};
const userId = pb.authStore?.record?.id || pb.authStore?.model?.id;
if (userId) payloadNote.user = userId;
const [resJob, resNote] = await Promise.all([
fetch(`${PB_COLLECTION_URL}/${currentJob.id}`,{
method:'PATCH',
headers:{'Content-Type':'application/json', ...authHeaders()},
body:JSON.stringify({Notes:updatedNotes})
}),
fetch(`${NOTES_COLLECTION_URL}`,{
method:'POST',
headers:{'Content-Type':'application/json', ...authHeaders()},
body:JSON.stringify(payloadNote)
})
]);
// First: Update job record in prod
const resJob = await fetch(`${PB_COLLECTION_URL}/${currentJob.id}`,{
method:'PATCH',
headers:{'Content-Type':'application/json', ...authHeaders()},
body:JSON.stringify({Notes:updatedNotes})
});
if(!resJob.ok) throw new Error('Job_Info update failed: '+resJob.status);
const updated=await resJob.json();
const jobIdToLink = updated.id || currentJob.id;
// Second: Create note in notes collection with the confirmed job ID
payloadNote.Job_Number_Id = [jobIdToLink];
console.log('Submitting note payload:', payloadNote);
const resNote = await fetch(`${NOTES_COLLECTION_URL}`,{
method:'POST',
headers:{'Content-Type':'application/json', ...authHeaders()},
body:JSON.stringify(payloadNote)
});
// If note creation failed, try fallback by appending job info into the note text
if(!resNote.ok){
const errText = await resNote.text().catch(()=> '');
throw new Error('Notes create failed: '+resNote.status+' '+errText);
console.warn('Note creation failed with status', resNote.status, ':', errText);
console.log('Payload that failed:', JSON.stringify(payloadNote, null, 2));
// Retry with fallback if it's a 400 validation error (likely field mismatch)
const shouldFallback = resNote.status === 400;
if(shouldFallback){
const fallbackPayload = {
Note: `${formatted.trim()}\n(Relation fallback: Job #${currentJob?.Job_Number||'N/A'} | ID ${currentJob?.id||'unknown'})`,
Job_Number: currentJob?.Job_Number || ''
};
try{
const retry = await fetch(`${NOTES_COLLECTION_URL}`,{
method:'POST',
headers:{'Content-Type':'application/json', ...authHeaders()},
body:JSON.stringify(fallbackPayload)
});
if(!retry.ok){
const retryText = await retry.text().catch(()=> '');
console.warn('Fallback retry failed:', retry.status, retryText);
}else{
console.log('Fallback retry succeeded');
}
}catch(e){ console.warn('Notes create retry error:', e?.message||e); }
}else{
console.warn('Notes create failed (non-fallback scenario):', resNote.status, errText);
}
}
const updated=await resJob.json();
const idx=jobsCache.findIndex(j=>j.id===currentJob.id); if(idx>=0) jobsCache[idx].Notes=updated.Notes; currentJob.Notes=updated.Notes;
jobNotes.innerHTML=updated.Notes; newNote.innerHTML='';
fetch('/log-change',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({action:'update-notes',user:currentUserName,target:currentJob.id,detail:'Job_Info notes updated + notes record created'})}).catch(()=>{});
applyFiltersAndRender();
}catch(err){alert('Error saving note: '+err.message); console.error(err);}
});
document.getElementById('btnClearNote').addEventListener('click',()=>newNote.innerHTML='');
+5
View File
@@ -0,0 +1,5 @@
/* Generated Tailwind CSS - Basic version */
/* This will be updated by tailwind CLI on dev */
@tailwind base;
@tailwind components;
@tailwind utilities;