From 44f8ff4f3c15e451f5cdce557820bec682c58563 Mon Sep 17 00:00:00 2001 From: "aji@catalis.app" Date: Sat, 21 Jun 2025 21:47:03 +0700 Subject: [PATCH] perbaikan data issue updated by --- src/routes/backoffice/issue/+page.svelte | 100 +++++++++++++++-------- 1 file changed, 66 insertions(+), 34 deletions(-) diff --git a/src/routes/backoffice/issue/+page.svelte b/src/routes/backoffice/issue/+page.svelte index 379912c..62830c0 100644 --- a/src/routes/backoffice/issue/+page.svelte +++ b/src/routes/backoffice/issue/+page.svelte @@ -110,7 +110,7 @@ type Villa = { id: string; - name: string; + villa_name: string; }; type User = { @@ -123,8 +123,8 @@ onMount(async () => { const { data, error } = await supabase - .from("villas") - .select("id, name"); + .from("vb_villas") + .select("id, villa_name"); if (error) { console.error("Error fetching villas:", error); @@ -164,7 +164,8 @@ follow_up: boolean; need_approval: boolean; created_at: string; - // Optional field to hold the name of the villa + updated_by?: string; + updated_at?: string; }; type issueInsert = { @@ -200,6 +201,10 @@ const columns: columns[] = [ { key: "name", title: "Name" }, { key: "villa_name", title: "Villa Name" }, + { key: "villa_id", title: "Villa ID" }, + { key: "input_by", title: "Input By" }, + { key: "reported_by", title: "Reported By" }, + { key: "reported_date", title: "Reported Date" }, { key: "area_of_villa", title: "Area Of Villa" }, { key: "priority", title: "Priority" }, { key: "issue_type", title: "Issue Type" }, @@ -209,8 +214,8 @@ { key: "reported_date", title: "Reported Date" }, { key: "issue_related_image", title: "Issue Related Image" }, { key: "issue_source", title: "Issue Source" }, - { key: "reported_by", title: "Reported By" }, - { key: "input_by", title: "Input By" }, + { key: "reported_name", title: "Reported By" }, + { key: "inputed_name", title: "Input By" }, { key: "guest_communication", title: "Guest Communication" }, { key: "resolution", title: "Resolution" }, { @@ -220,6 +225,8 @@ { key: "follow_up", title: "Follow Up" }, { key: "need_approval", title: "Need Approval" }, { key: "created_at", title: "Created At" }, + { key: "updated_name", title: "Updated By" }, + { key: "updated_at", title: "Updated At" }, { key: "actions", title: "Actions" }, ]; @@ -232,7 +239,7 @@ limit: number = 10, ) { let query = supabase - .from("vb_issues") + .from("vb_issues_data") .select("*", { count: "exact" }) .order(sort || "created_at", { ascending: order === "asc" }) .range(offset, offset + limit - 1); @@ -260,25 +267,8 @@ return; } - // Ambil semua villa_id unik dari issues - const villaIds = [...new Set(issues.map((i: Issue) => i.villa_id))]; - - const { data: villas, error: villaError } = await supabase - .from("vb_villas") - .select("*") - .in("id", villaIds); - - if (villaError) { - console.error("Error fetching villas:", villaError); - return; - } - // Gabungkan data villa ke dalam setiap issue - allRows = issues.map((issue: Issue) => ({ - ...issue, - villa_name: - villas.find((v) => v.id === issue.villa_id).name || null, - })); + allRows = issues; } let currentPage = offset + 1; @@ -303,11 +293,28 @@ "move_issue", "issue_number", "actions", + "villa_name", + "reported_name", + "inputed_name", + "updated_name", + "updated_at", + "updated_by", + ]; + + const excludedKeysDisplay = [ + "villa_id", + "reported_by", + "input_by", + "updated_by", ]; const formColumns = columns.filter( (col) => !excludedKeys.includes(col.key), ); + const formColumnsDisplay = columns.filter( + (col) => !excludedKeysDisplay.includes(col.key), + ); + function openModal(issue?: Record) { if (issue) { isEditing = true; @@ -352,6 +359,16 @@ } if (isEditing && currentEditingId) { + newIssue = formColumns.reduce( + (acc, col) => { + if (col.key in newIssue) { + acc[col.key] = newIssue[col.key]; + } + return acc; + }, + {} as Record, + ); + const { error } = await supabase .from("vb_issues") .update(newIssue) @@ -365,7 +382,7 @@ } else { const issueInsert: issueInsert = { name: formData.get("name") as string, - villa_id: formData.get("villa_name") as string, + villa_id: formData.get("villa_id") as string, area_of_villa: formData.get("area_of_villa") as string, priority: formData.get("priority") as string, issue_type: formData.get("issue_type") as string, @@ -467,10 +484,23 @@ // insert id issue to project async function moveIssueToProject(issueId: string) { + // get user id from session + const session = await supabase.auth.getSession(); + if (!session) { + console.error("User not authenticated"); + return; + } + + const userId = session.data.session?.user.id; + // update move_issue field in the issue const { error: updateError } = await supabase .from("vb_issues") - .update({ move_issue: "PROJECT" }) + .update({ + move_issue: "PROJECT", + updated_by: userId, + updated_at: new Date().toISOString(), + }) .eq("id", issueId); if (updateError) { @@ -570,7 +600,7 @@ - {#each columns as col} + {#each formColumnsDisplay as col} {#if col.key === "name"} - {#each columns as col} + {#each formColumnsDisplay as col} {#if col.key === "name"}
{#each allRows as row}
Villa Name - {#if $formErrors.villa_name} + {#if $formErrors.villa_id}

- {$formErrors.villa_name} + {$formErrors.villa_id}

{/if}