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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { supabase } from "./supabase";
|
| 2 |
+
import * as local from "./auth";
|
| 3 |
+
import { USE_LOCAL } from "./config";
|
| 4 |
+
|
| 5 |
+
// ββ Users βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 6 |
+
export async function dbGetAllUsers() {
|
| 7 |
+
if (USE_LOCAL) return local.getAllUsers();
|
| 8 |
+
const { data, error } = await supabase.from("qa_users").select("username, role, name, phone").order("username");
|
| 9 |
+
if (error) throw error;
|
| 10 |
+
return data;
|
| 11 |
+
}
|
| 12 |
+
|
| 13 |
+
export async function dbLogin(username, password) {
|
| 14 |
+
if (USE_LOCAL) {
|
| 15 |
+
const res = local.login(username, password);
|
| 16 |
+
if (res.error) return res;
|
| 17 |
+
return { success: true, user: { username: res.session.username, role: res.session.role } };
|
| 18 |
+
}
|
| 19 |
+
const { data, error } = await supabase
|
| 20 |
+
.from("qa_users")
|
| 21 |
+
.select("username, password, role")
|
| 22 |
+
.ilike("username", username.trim())
|
| 23 |
+
.single();
|
| 24 |
+
if (error || !data) return { error: "Invalid username or password." };
|
| 25 |
+
if (data.password !== password) return { error: "Invalid username or password." };
|
| 26 |
+
return { success: true, user: data };
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
export async function dbSignup(username, password, { name = "", phone = "" } = {}) {
|
| 30 |
+
if (USE_LOCAL) return local.signup(username, password, { name, phone });
|
| 31 |
+
const { error } = await supabase.from("qa_users").insert({
|
| 32 |
+
username: username.trim(), password, role: "tester",
|
| 33 |
+
name: name.trim(), phone: phone.trim(),
|
| 34 |
+
});
|
| 35 |
+
if (error) {
|
| 36 |
+
if (error.code === "23505") return { error: "Username already taken." };
|
| 37 |
+
return { error: error.message };
|
| 38 |
+
}
|
| 39 |
+
return { success: true };
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
export async function dbUpdateRole(username, role) {
|
| 43 |
+
if (USE_LOCAL) return local.updateUserRole(username, role);
|
| 44 |
+
const { error } = await supabase.from("qa_users").update({ role }).eq("username", username);
|
| 45 |
+
if (error) return { error: error.message };
|
| 46 |
+
return { success: true };
|
| 47 |
+
}
|
| 48 |
+
|
| 49 |
+
export async function dbResetPassword(username, newPassword) {
|
| 50 |
+
if (USE_LOCAL) return local.adminResetPassword(username, newPassword);
|
| 51 |
+
if (!newPassword || newPassword.length < 6) return { error: "Password must be at least 6 characters." };
|
| 52 |
+
const { error } = await supabase.from("qa_users").update({ password: newPassword }).eq("username", username);
|
| 53 |
+
if (error) return { error: error.message };
|
| 54 |
+
return { success: true };
|
| 55 |
+
}
|
| 56 |
+
|
| 57 |
+
export async function dbDeleteUser(username) {
|
| 58 |
+
if (USE_LOCAL) { local.deleteAccount(username); return; }
|
| 59 |
+
const { error } = await supabase.from("qa_users").delete().eq("username", username);
|
| 60 |
+
if (error) throw error;
|
| 61 |
+
}
|
| 62 |
+
|
| 63 |
+
// ββ Submissions βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 64 |
+
export async function dbGetSubmissions() {
|
| 65 |
+
if (USE_LOCAL) return local.getSubmissions();
|
| 66 |
+
const { data, error } = await supabase
|
| 67 |
+
.from("qa_submissions")
|
| 68 |
+
.select("*")
|
| 69 |
+
.order("end_time", { ascending: false });
|
| 70 |
+
if (error) throw error;
|
| 71 |
+
return data.map(row => ({
|
| 72 |
+
id: row.id,
|
| 73 |
+
type: row.type,
|
| 74 |
+
username: row.username,
|
| 75 |
+
profileName: row.profile_name,
|
| 76 |
+
productId: row.product_id ?? null,
|
| 77 |
+
productName: row.product_name ?? null,
|
| 78 |
+
startTime: row.start_time,
|
| 79 |
+
endTime: row.end_time,
|
| 80 |
+
rows: row.rows,
|
| 81 |
+
review: row.review,
|
| 82 |
+
}));
|
| 83 |
+
}
|
| 84 |
+
|
| 85 |
+
export async function dbSaveSubmission(sub) {
|
| 86 |
+
if (USE_LOCAL) { local.saveSubmission(sub); return; }
|
| 87 |
+
const { error } = await supabase.from("qa_submissions").upsert({
|
| 88 |
+
id: sub.id,
|
| 89 |
+
type: sub.type,
|
| 90 |
+
username: sub.username,
|
| 91 |
+
profile_name: sub.profileName ?? null,
|
| 92 |
+
product_id: sub.productId ?? null,
|
| 93 |
+
product_name: sub.productName ?? null,
|
| 94 |
+
start_time: sub.startTime,
|
| 95 |
+
end_time: sub.endTime,
|
| 96 |
+
rows: sub.rows,
|
| 97 |
+
review: sub.review ?? null,
|
| 98 |
+
});
|
| 99 |
+
if (error) throw error;
|
| 100 |
+
}
|
| 101 |
+
|
| 102 |
+
export async function dbDeleteSubmission(id) {
|
| 103 |
+
if (USE_LOCAL) { local.deleteSubmission(id); return; }
|
| 104 |
+
const { error } = await supabase.from("qa_submissions").delete().eq("id", id);
|
| 105 |
+
if (error) throw error;
|
| 106 |
+
}
|
| 107 |
+
|
| 108 |
+
// ββ Profiles ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 109 |
+
export async function dbGetProfiles() {
|
| 110 |
+
if (USE_LOCAL) return local.getProfiles();
|
| 111 |
+
const { data, error } = await supabase
|
| 112 |
+
.from("qa_profiles")
|
| 113 |
+
.select("*")
|
| 114 |
+
.order("created_at", { ascending: true });
|
| 115 |
+
if (error) throw error;
|
| 116 |
+
return data.map(row => ({
|
| 117 |
+
id: row.id,
|
| 118 |
+
name: row.name,
|
| 119 |
+
createdAt: row.created_at,
|
| 120 |
+
questions: row.questions,
|
| 121 |
+
}));
|
| 122 |
+
}
|
| 123 |
+
|
| 124 |
+
export async function dbSaveProfile(profile) {
|
| 125 |
+
if (USE_LOCAL) { local.saveProfile(profile); return; }
|
| 126 |
+
const { error } = await supabase.from("qa_profiles").upsert({
|
| 127 |
+
id: profile.id,
|
| 128 |
+
name: profile.name,
|
| 129 |
+
created_at: profile.createdAt,
|
| 130 |
+
questions: profile.questions,
|
| 131 |
+
});
|
| 132 |
+
if (error) throw error;
|
| 133 |
+
}
|
| 134 |
+
|
| 135 |
+
export async function dbDeleteProfile(id) {
|
| 136 |
+
if (USE_LOCAL) { local.deleteProfile(id); return; }
|
| 137 |
+
const { error } = await supabase.from("qa_profiles").delete().eq("id", id);
|
| 138 |
+
if (error) throw error;
|
| 139 |
+
}
|
| 140 |
+
|
| 141 |
+
// ββ Products ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 142 |
+
export async function dbGetProducts() {
|
| 143 |
+
if (USE_LOCAL) return local.getProducts();
|
| 144 |
+
const { data, error } = await supabase
|
| 145 |
+
.from("qa_products")
|
| 146 |
+
.select("*")
|
| 147 |
+
.order("created_at", { ascending: true });
|
| 148 |
+
if (error) throw error;
|
| 149 |
+
return data.map(row => ({
|
| 150 |
+
id: row.id,
|
| 151 |
+
name: row.name,
|
| 152 |
+
description: row.description ?? "",
|
| 153 |
+
createdAt: row.created_at,
|
| 154 |
+
}));
|
| 155 |
+
}
|
| 156 |
+
|
| 157 |
+
export async function dbSaveProduct(product) {
|
| 158 |
+
if (USE_LOCAL) { local.saveProduct(product); return; }
|
| 159 |
+
const { error } = await supabase.from("qa_products").upsert({
|
| 160 |
+
id: product.id,
|
| 161 |
+
name: product.name,
|
| 162 |
+
description: product.description ?? "",
|
| 163 |
+
created_at: product.createdAt ?? Date.now(),
|
| 164 |
+
});
|
| 165 |
+
if (error) throw error;
|
| 166 |
+
}
|
| 167 |
+
|
| 168 |
+
export async function dbDeleteProduct(id) {
|
| 169 |
+
if (USE_LOCAL) { local.deleteProduct(id); return; }
|
| 170 |
+
await supabase.from("qa_user_products").delete().eq("product_id", id);
|
| 171 |
+
const { error } = await supabase.from("qa_products").delete().eq("id", id);
|
| 172 |
+
if (error) throw error;
|
| 173 |
+
}
|
| 174 |
+
|
| 175 |
+
// ββ User product access βββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 176 |
+
export async function dbGetUserProducts(username) {
|
| 177 |
+
if (USE_LOCAL) return local.getUserProducts(username);
|
| 178 |
+
const { data, error } = await supabase
|
| 179 |
+
.from("qa_user_products")
|
| 180 |
+
.select("product_id")
|
| 181 |
+
.eq("username", username);
|
| 182 |
+
if (error) throw error;
|
| 183 |
+
return data.map(r => r.product_id);
|
| 184 |
+
}
|
| 185 |
+
|
| 186 |
+
export async function dbSetUserProducts(username, productIds) {
|
| 187 |
+
if (USE_LOCAL) { local.setUserProducts(username, productIds); return; }
|
| 188 |
+
await supabase.from("qa_user_products").delete().eq("username", username);
|
| 189 |
+
if (productIds.length === 0) return;
|
| 190 |
+
const { error } = await supabase.from("qa_user_products").insert(
|
| 191 |
+
productIds.map(pid => ({ username, product_id: pid }))
|
| 192 |
+
);
|
| 193 |
+
if (error) throw error;
|
| 194 |
+
}
|
| 195 |
+
|
| 196 |
+
// ββ Planner tasks βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 197 |
+
export async function dbGetTasks() {
|
| 198 |
+
if (USE_LOCAL) return local.getTasks();
|
| 199 |
+
const { data, error } = await supabase
|
| 200 |
+
.from("qa_tasks")
|
| 201 |
+
.select("*")
|
| 202 |
+
.order("created_at", { ascending: false });
|
| 203 |
+
if (error) throw error;
|
| 204 |
+
return data.map(row => ({
|
| 205 |
+
id: row.id,
|
| 206 |
+
title: row.title,
|
| 207 |
+
description: row.description ?? "",
|
| 208 |
+
status: row.status,
|
| 209 |
+
productId: row.product_id ?? null,
|
| 210 |
+
productName: row.product_name ?? null,
|
| 211 |
+
assignee: row.assignee ?? null,
|
| 212 |
+
createdBy: row.created_by,
|
| 213 |
+
tags: row.tags ?? [],
|
| 214 |
+
images: row.images ?? [],
|
| 215 |
+
createdAt: row.created_at,
|
| 216 |
+
updatedAt: row.updated_at,
|
| 217 |
+
}));
|
| 218 |
+
}
|
| 219 |
+
|
| 220 |
+
export async function dbSaveTask(task) {
|
| 221 |
+
if (USE_LOCAL) { local.saveTask(task); return; }
|
| 222 |
+
const { error } = await supabase.from("qa_tasks").upsert({
|
| 223 |
+
id: task.id,
|
| 224 |
+
title: task.title,
|
| 225 |
+
description: task.description ?? "",
|
| 226 |
+
status: task.status,
|
| 227 |
+
product_id: task.productId ?? null,
|
| 228 |
+
product_name: task.productName ?? null,
|
| 229 |
+
assignee: task.assignee ?? null,
|
| 230 |
+
created_by: task.createdBy,
|
| 231 |
+
tags: task.tags ?? [],
|
| 232 |
+
images: task.images ?? [],
|
| 233 |
+
created_at: task.createdAt,
|
| 234 |
+
updated_at: task.updatedAt,
|
| 235 |
+
});
|
| 236 |
+
if (error) throw error;
|
| 237 |
+
}
|
| 238 |
+
|
| 239 |
+
export async function dbDeleteTask(id) {
|
| 240 |
+
if (USE_LOCAL) { local.deleteTask(id); return; }
|
| 241 |
+
const { error } = await supabase.from("qa_tasks").delete().eq("id", id);
|
| 242 |
+
if (error) throw error;
|
| 243 |
+
}
|
| 244 |
+
|
| 245 |
+
// ββ Tickets βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 246 |
+
export async function dbGetTickets() {
|
| 247 |
+
if (USE_LOCAL) return local.getTickets();
|
| 248 |
+
const { data, error } = await supabase
|
| 249 |
+
.from("qa_tickets")
|
| 250 |
+
.select("*")
|
| 251 |
+
.order("created_at", { ascending: false });
|
| 252 |
+
if (error) throw error;
|
| 253 |
+
return data.map(row => ({
|
| 254 |
+
id: row.id,
|
| 255 |
+
title: row.title,
|
| 256 |
+
description: row.description ?? "",
|
| 257 |
+
status: row.status,
|
| 258 |
+
priority: row.priority,
|
| 259 |
+
productId: row.product_id ?? null,
|
| 260 |
+
productName: row.product_name ?? null,
|
| 261 |
+
reporter: row.reporter,
|
| 262 |
+
assignee: row.assignee ?? null,
|
| 263 |
+
images: row.images ?? [],
|
| 264 |
+
createdAt: row.created_at,
|
| 265 |
+
updatedAt: row.updated_at,
|
| 266 |
+
}));
|
| 267 |
+
}
|
| 268 |
+
|
| 269 |
+
export async function dbSaveTicket(ticket) {
|
| 270 |
+
if (USE_LOCAL) { local.saveTicket(ticket); return; }
|
| 271 |
+
const { error } = await supabase.from("qa_tickets").upsert({
|
| 272 |
+
id: ticket.id,
|
| 273 |
+
title: ticket.title,
|
| 274 |
+
description: ticket.description ?? "",
|
| 275 |
+
status: ticket.status,
|
| 276 |
+
priority: ticket.priority,
|
| 277 |
+
product_id: ticket.productId ?? null,
|
| 278 |
+
product_name: ticket.productName ?? null,
|
| 279 |
+
reporter: ticket.reporter,
|
| 280 |
+
assignee: ticket.assignee ?? null,
|
| 281 |
+
images: ticket.images ?? [],
|
| 282 |
+
created_at: ticket.createdAt,
|
| 283 |
+
updated_at: ticket.updatedAt,
|
| 284 |
+
});
|
| 285 |
+
if (error) throw error;
|
| 286 |
+
}
|
| 287 |
+
|
| 288 |
+
export async function dbDeleteTicket(id) {
|
| 289 |
+
if (USE_LOCAL) { local.deleteTicket(id); return; }
|
| 290 |
+
const { error } = await supabase.from("qa_tickets").delete().eq("id", id);
|
| 291 |
+
if (error) throw error;
|
| 292 |
+
}
|
| 293 |
+
|
| 294 |
+
// ββ Notifications βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 295 |
+
export async function dbGetNotifications(username) {
|
| 296 |
+
if (USE_LOCAL) return local.getNotifications(username);
|
| 297 |
+
const { data, error } = await supabase
|
| 298 |
+
.from("qa_notifications")
|
| 299 |
+
.select("*")
|
| 300 |
+
.eq("to_username", username)
|
| 301 |
+
.order("created_at", { ascending: false });
|
| 302 |
+
if (error) throw error;
|
| 303 |
+
return data.map(row => ({
|
| 304 |
+
id: row.id,
|
| 305 |
+
toUsername: row.to_username,
|
| 306 |
+
message: row.message,
|
| 307 |
+
type: row.type ?? null,
|
| 308 |
+
refId: row.ref_id ?? null,
|
| 309 |
+
refType: row.ref_type ?? null,
|
| 310 |
+
read: row.read,
|
| 311 |
+
createdAt: row.created_at,
|
| 312 |
+
}));
|
| 313 |
+
}
|
| 314 |
+
|
| 315 |
+
export async function dbSaveNotification(notif) {
|
| 316 |
+
if (USE_LOCAL) { local.saveNotification(notif); return; }
|
| 317 |
+
const { error } = await supabase.from("qa_notifications").upsert({
|
| 318 |
+
id: notif.id,
|
| 319 |
+
to_username: notif.toUsername,
|
| 320 |
+
message: notif.message,
|
| 321 |
+
type: notif.type ?? null,
|
| 322 |
+
ref_id: notif.refId ?? null,
|
| 323 |
+
ref_type: notif.refType ?? null,
|
| 324 |
+
read: notif.read ?? false,
|
| 325 |
+
created_at: notif.createdAt,
|
| 326 |
+
});
|
| 327 |
+
if (error) throw error;
|
| 328 |
+
}
|
| 329 |
+
|
| 330 |
+
export async function dbMarkNotificationRead(id) {
|
| 331 |
+
if (USE_LOCAL) { local.markNotificationRead(id); return; }
|
| 332 |
+
const { error } = await supabase.from("qa_notifications").update({ read: true }).eq("id", id);
|
| 333 |
+
if (error) throw error;
|
| 334 |
+
}
|
| 335 |
+
|
| 336 |
+
export async function dbMarkAllNotificationsRead(username) {
|
| 337 |
+
if (USE_LOCAL) { local.markAllNotificationsRead(username); return; }
|
| 338 |
+
const { error } = await supabase.from("qa_notifications").update({ read: true }).eq("to_username", username);
|
| 339 |
+
if (error) throw error;
|
| 340 |
+
}
|
| 341 |
+
|
| 342 |
+
export async function dbClearNotifications(username) {
|
| 343 |
+
if (USE_LOCAL) { local.clearNotifications(username); return; }
|
| 344 |
+
const { error } = await supabase.from("qa_notifications").delete().eq("to_username", username);
|
| 345 |
+
if (error) throw error;
|
| 346 |
+
}
|