perbaikan data issue updated by

This commit is contained in:
aji@catalis.app
2025-06-21 21:47:03 +07:00
parent f2c8d8c05d
commit 44f8ff4f3c

View File

@@ -110,7 +110,7 @@
type Villa = { type Villa = {
id: string; id: string;
name: string; villa_name: string;
}; };
type User = { type User = {
@@ -123,8 +123,8 @@
onMount(async () => { onMount(async () => {
const { data, error } = await supabase const { data, error } = await supabase
.from("villas") .from("vb_villas")
.select("id, name"); .select("id, villa_name");
if (error) { if (error) {
console.error("Error fetching villas:", error); console.error("Error fetching villas:", error);
@@ -164,7 +164,8 @@
follow_up: boolean; follow_up: boolean;
need_approval: boolean; need_approval: boolean;
created_at: string; created_at: string;
// Optional field to hold the name of the villa updated_by?: string;
updated_at?: string;
}; };
type issueInsert = { type issueInsert = {
@@ -200,6 +201,10 @@
const columns: columns[] = [ const columns: columns[] = [
{ key: "name", title: "Name" }, { key: "name", title: "Name" },
{ key: "villa_name", title: "Villa 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: "area_of_villa", title: "Area Of Villa" },
{ key: "priority", title: "Priority" }, { key: "priority", title: "Priority" },
{ key: "issue_type", title: "Issue Type" }, { key: "issue_type", title: "Issue Type" },
@@ -209,8 +214,8 @@
{ key: "reported_date", title: "Reported Date" }, { key: "reported_date", title: "Reported Date" },
{ key: "issue_related_image", title: "Issue Related Image" }, { key: "issue_related_image", title: "Issue Related Image" },
{ key: "issue_source", title: "Issue Source" }, { key: "issue_source", title: "Issue Source" },
{ key: "reported_by", title: "Reported By" }, { key: "reported_name", title: "Reported By" },
{ key: "input_by", title: "Input By" }, { key: "inputed_name", title: "Input By" },
{ key: "guest_communication", title: "Guest Communication" }, { key: "guest_communication", title: "Guest Communication" },
{ key: "resolution", title: "Resolution" }, { key: "resolution", title: "Resolution" },
{ {
@@ -220,6 +225,8 @@
{ key: "follow_up", title: "Follow Up" }, { key: "follow_up", title: "Follow Up" },
{ key: "need_approval", title: "Need Approval" }, { key: "need_approval", title: "Need Approval" },
{ key: "created_at", title: "Created At" }, { key: "created_at", title: "Created At" },
{ key: "updated_name", title: "Updated By" },
{ key: "updated_at", title: "Updated At" },
{ key: "actions", title: "Actions" }, { key: "actions", title: "Actions" },
]; ];
@@ -232,7 +239,7 @@
limit: number = 10, limit: number = 10,
) { ) {
let query = supabase let query = supabase
.from("vb_issues") .from("vb_issues_data")
.select("*", { count: "exact" }) .select("*", { count: "exact" })
.order(sort || "created_at", { ascending: order === "asc" }) .order(sort || "created_at", { ascending: order === "asc" })
.range(offset, offset + limit - 1); .range(offset, offset + limit - 1);
@@ -260,25 +267,8 @@
return; 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 // Gabungkan data villa ke dalam setiap issue
allRows = issues.map((issue: Issue) => ({ allRows = issues;
...issue,
villa_name:
villas.find((v) => v.id === issue.villa_id).name || null,
}));
} }
let currentPage = offset + 1; let currentPage = offset + 1;
@@ -303,11 +293,28 @@
"move_issue", "move_issue",
"issue_number", "issue_number",
"actions", "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( const formColumns = columns.filter(
(col) => !excludedKeys.includes(col.key), (col) => !excludedKeys.includes(col.key),
); );
const formColumnsDisplay = columns.filter(
(col) => !excludedKeysDisplay.includes(col.key),
);
function openModal(issue?: Record<string, any>) { function openModal(issue?: Record<string, any>) {
if (issue) { if (issue) {
isEditing = true; isEditing = true;
@@ -352,6 +359,16 @@
} }
if (isEditing && currentEditingId) { if (isEditing && currentEditingId) {
newIssue = formColumns.reduce(
(acc, col) => {
if (col.key in newIssue) {
acc[col.key] = newIssue[col.key];
}
return acc;
},
{} as Record<string, any>,
);
const { error } = await supabase const { error } = await supabase
.from("vb_issues") .from("vb_issues")
.update(newIssue) .update(newIssue)
@@ -365,7 +382,7 @@
} else { } else {
const issueInsert: issueInsert = { const issueInsert: issueInsert = {
name: formData.get("name") as string, 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, area_of_villa: formData.get("area_of_villa") as string,
priority: formData.get("priority") as string, priority: formData.get("priority") as string,
issue_type: formData.get("issue_type") as string, issue_type: formData.get("issue_type") as string,
@@ -467,10 +484,23 @@
// insert id issue to project // insert id issue to project
async function moveIssueToProject(issueId: string) { 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 // update move_issue field in the issue
const { error: updateError } = await supabase const { error: updateError } = await supabase
.from("vb_issues") .from("vb_issues")
.update({ move_issue: "PROJECT" }) .update({
move_issue: "PROJECT",
updated_by: userId,
updated_at: new Date().toISOString(),
})
.eq("id", issueId); .eq("id", issueId);
if (updateError) { if (updateError) {
@@ -570,7 +600,7 @@
<table class="min-w-[1000px] divide-y divide-gray-200 text-sm w-max"> <table class="min-w-[1000px] divide-y divide-gray-200 text-sm w-max">
<thead class="bg-gray-100"> <thead class="bg-gray-100">
<tr> <tr>
{#each columns as col} {#each formColumnsDisplay as col}
{#if col.key === "name"} {#if col.key === "name"}
<th <th
class="sticky left-0 px-4 py-3 text-left font-semibold text-gray-700 uppercase tracking-wider whitespace-nowrap" class="sticky left-0 px-4 py-3 text-left font-semibold text-gray-700 uppercase tracking-wider whitespace-nowrap"
@@ -591,7 +621,7 @@
<tbody class="divide-y divide-gray-200 bg-white"> <tbody class="divide-y divide-gray-200 bg-white">
{#each allRows as row} {#each allRows as row}
<tr class="hover:bg-gray-50 transition"> <tr class="hover:bg-gray-50 transition">
{#each columns as col} {#each formColumnsDisplay as col}
{#if col.key === "name"} {#if col.key === "name"}
<td <td
class="sticky left-0 px-4 py-2 font-medium text-blue-600" class="sticky left-0 px-4 py-2 font-medium text-blue-600"
@@ -1048,9 +1078,9 @@
>Villa Name</label >Villa Name</label
> >
<select <select
name="villa_name" name="villa_id"
class="w-full border px-3 py-2 rounded {errorClass( class="w-full border px-3 py-2 rounded {errorClass(
'villa_name', 'villa_id',
)}" )}"
bind:value={newIssue[col.key as keyof Issue]} bind:value={newIssue[col.key as keyof Issue]}
> >
@@ -1058,12 +1088,14 @@
>Select Villa</option >Select Villa</option
> >
{#each dataVilla as villa} {#each dataVilla as villa}
<option value={villa.id}>{villa.name}</option> <option value={villa.id}
>{villa.villa_name}</option
>
{/each} {/each}
</select> </select>
{#if $formErrors.villa_name} {#if $formErrors.villa_id}
<p class="text-red-500 text-xs"> <p class="text-red-500 text-xs">
{$formErrors.villa_name} {$formErrors.villa_id}
</p> </p>
{/if} {/if}
</div> </div>