diff --git a/frontend/index.html b/frontend/index.html
index ea4b6ed..92a1978 100644
--- a/frontend/index.html
+++ b/frontend/index.html
@@ -1900,36 +1900,55 @@
const currentUserName = getDisplayName();
const noteType = isManagerNote ? 'manager' : 'job';
const typeLabel = isManagerNote ? '[MANAGER NOTE]' : '';
- const formatted = `${typeLabel}[${currentUserName} - ${timestamp}] [Job #${job?.Job_Number||'N/A'}] ${html}`;
+ const formatted = `${typeLabel}[${currentUserName} - ${timestamp}] ${html}`;
try {
+ // If it's a job note, first update Job_Info_Prod.Notes field
+ let jobRecordId = job?.id || '';
+
+ if(!isManagerNote) {
+ // Fetch the latest notes from PocketBase to ensure we have the full chain
+ const fetchLatest = await fetch(`${PB_COLLECTION_URL}/${job.id}`, {
+ headers: authHeaders()
+ });
+
+ if(fetchLatest.ok) {
+ const latestJob = await fetchLatest.json();
+ const currentNotes = latestJob.Notes || '';
+ const updatedNotes = formatted + '\n\n' + currentNotes;
+
+ const resJob = await fetch(`${PB_COLLECTION_URL}/${job.id}`, {
+ method: 'PATCH',
+ headers: {'Content-Type': 'application/json', ...authHeaders()},
+ body: JSON.stringify({Notes: updatedNotes})
+ });
+
+ if(!resJob.ok) {
+ console.error('Job_Info update failed:', resJob.status);
+ throw new Error('Failed to update Job_Info: ' + resJob.status);
+ }
+
+ const updated = await resJob.json();
+ jobRecordId = updated.id || job.id;
+
+ // Update both current job object and the passed job parameter
+ currentJob.Notes = updatedNotes;
+ job.Notes = updatedNotes;
+ console.log('Updated Job_Info_Prod.Notes for job:', jobRecordId);
+ } else {
+ throw new Error('Failed to fetch latest job data: ' + fetchLatest.status);
+ }
+ }
+
+ // Create payload - only include job_info_id, not Job_Number_Id array
const payloadNote = {
Note: formatted.trim(),
Job_Number: job?.Job_Number || '',
- Job_Number_Id: job?.id ? [job.id] : [],
+ job_info_id: jobRecordId,
type: noteType
};
- // If it's a job note, also update Job_Info_Prod.Notes field
- if(!isManagerNote) {
- const updatedNotes = formatted + '\n\n' + (job.Notes || '');
- const resJob = await fetch(`${PB_COLLECTION_URL}/${job.id}`, {
- method: 'PATCH',
- headers: {'Content-Type': 'application/json', ...authHeaders()},
- body: JSON.stringify({Notes: updatedNotes})
- });
-
- if(!resJob.ok) {
- console.error('Job_Info update failed:', resJob.status);
- throw new Error('Failed to update Job_Info: ' + resJob.status);
- }
-
- const updated = await resJob.json();
- payloadNote.Job_Number_Id = [updated.id || job.id];
-
- // Update current job object
- currentJob.Notes = updatedNotes;
- }
+ console.log('Submitting note payload:', payloadNote);
// Create note in notes collection with type
const resNote = await fetch(`${NOTES_COLLECTION_URL}`, {
@@ -1947,6 +1966,9 @@
} else {
throw new Error('Failed to save note: ' + resNote.status);
}
+ } else {
+ const savedNote = await resNote.json();
+ console.log('Note created successfully in Notes collection:', savedNote);
}
// Clear editor
@@ -1968,8 +1990,8 @@
if(!job?.id) return;
try {
- // Fetch all notes for this job
- const filter = `Job_Number_Id?~"${job.id}"`;
+ // Fetch all notes for this job using job_info_id
+ const filter = `job_info_id="${job.id}"`;
const resp = await fetch(`${NOTES_COLLECTION_URL}?filter=${encodeURIComponent(filter)}&sort=-created`, {
headers: authHeaders()
});