Monorepo for Aesthetic.Computer
aesthetic.computer
1// Reports API for false.work builds
2// GET: Fetch all reports
3// POST: Save a new report
4// Requires password authentication via Authorization header
5
6import { connect } from "../../backend/database.mjs";
7import { respond } from "../../backend/http.mjs";
8
9const BUILDS_PASSWORD = process.env.BUILDS_PASSWORD;
10
11export async function handler(event, context) {
12 if (event.httpMethod === "OPTIONS") {
13 return respond(204, "");
14 }
15
16 // Verify password
17 const authHeader = event.headers.authorization || event.headers.Authorization;
18 const password = authHeader?.replace("Bearer ", "");
19
20 if (!password || password !== BUILDS_PASSWORD) {
21 return respond(401, { error: "Unauthorized" });
22 }
23
24 try {
25 const database = await connect();
26 const reports = database.db.collection("false.work-reports");
27
28 // GET - Fetch all reports
29 if (event.httpMethod === "GET") {
30 const allReports = await reports
31 .find({})
32 .sort({ createdAt: -1 })
33 .toArray();
34
35 await database.disconnect();
36 return respond(200, { reports: allReports });
37 }
38
39 // POST - Save a new report
40 if (event.httpMethod === "POST") {
41 const body = JSON.parse(event.body);
42 const {
43 buildPlatform,
44 buildVersion,
45 tester,
46 summary,
47 lowHangingFruit,
48 onTheSide,
49 biggerThoughts
50 } = body;
51
52 if (!buildPlatform || !buildVersion) {
53 await database.disconnect();
54 return respond(400, { error: "Missing buildPlatform or buildVersion" });
55 }
56
57 const report = {
58 buildPlatform,
59 buildVersion,
60 tester: tester || "Anonymous",
61 summary: summary || "",
62 lowHangingFruit: lowHangingFruit || [],
63 onTheSide: onTheSide || [],
64 biggerThoughts: biggerThoughts || [],
65 createdAt: new Date(),
66 updatedAt: new Date(),
67 deleted: false
68 };
69
70 const result = await reports.insertOne(report);
71 await database.disconnect();
72
73 return respond(201, {
74 success: true,
75 reportId: result.insertedId,
76 report
77 });
78 }
79
80 await database.disconnect();
81 return respond(405, { error: "Method not allowed" });
82
83 } catch (err) {
84 console.error("Reports API error:", err);
85 return respond(500, { error: "Failed to process request" });
86 }
87}