File size: 2,499 Bytes
3dbfa56
 
 
 
6320560
3dbfa56
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6320560
3dbfa56
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import express, { Request, Response } from "express";
import { Pool } from "pg";
import fs from "fs";
import path from "path";
import "dotenv/config";

const app = express();
const port = 3000;

app.use(express.json());
app.use(express.static("public"));

let caCert: string;
try {
  caCert = fs.readFileSync(path.join(__dirname, "ca.pem"), "utf8");
  console.log("CA certificate loaded successfully.");
} catch (error: any) {
  console.error("Failed to load CA certificate:", error.message);
  process.exit(1);
}

const pool = new Pool({
  connectionString: `postgres://${process.env.UNAME}:${process.env.PWD}@postgres-bs-bebb.l.aivencloud.com:21759/defaultdb`,
  ssl: {
    ca: caCert,
    rejectUnauthorized: true,
    checkServerIdentity: () => undefined,
  },
});

interface Entry {
  sl_no: number;
  work_completed: string;
  from_time: string;
  to_time: string;
}

interface SaveRequestBody {
  date: string;
  technology: string;
  entries: Entry[];
}

pool.connect((err, client, release) => {
  if (err) {
    console.error("Database connection error:", err.message);
    process.exit(1);
  }
  console.log("Successfully connected to PostgreSQL database.");
  release();
});

app.post(
  "/save",
  async (req: Request<{}, {}, SaveRequestBody>, res: Response) => {
    const { date, technology, entries } = req.body;
    const client = await pool.connect();
    try {
      await client.query("BEGIN");
      for (const entry of entries) {
        await client.query(
          "INSERT INTO daily_reports (report_date, technology, sl_no, work_completed, from_time, to_time) VALUES ($1, $2, $3, $4, $5, $6)",
          [
            date,
            technology,
            entry.sl_no,
            entry.work_completed,
            entry.from_time,
            entry.to_time,
          ]
        );
      }
      await client.query("COMMIT");
      res.json({ success: true });
    } catch (error: any) {
      await client.query("ROLLBACK");
      res.status(500).json({ error: error.message });
    } finally {
      client.release();
    }
  }
);

app.get("/fetch", async (req: Request, res: Response) => {
  const { date } = req.query;
  try {
    const result = await pool.query(
      "SELECT * FROM daily_reports WHERE report_date = $1 ORDER BY sl_no ASC",
      [date]
    );
    res.json(result.rows);
  } catch (error: any) {
    res.status(500).json({ error: error.message });
  }
});

app.listen(port, () => {
  console.log(`Server running on http://localhost:${port}`);
});