File size: 3,222 Bytes
62c3fe0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
//SQLite
const sqlite3 = require('sqlite3').verbose();
const {runQuery, runSelectQuery} = require("./queries");

// Initialize the database by creating tables if they don't exist
async function initializeDatabase() {
    try {
        // Define the queries for table creation
        const tables = {
            subscribers: `CREATE TABLE IF NOT EXISTS subscribers (
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                date TEXT,
                time TEXT,
                email TEXT NOT NULL UNIQUE
            )`,
            applicants: `CREATE TABLE IF NOT EXISTS applicants (
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                date TEXT,
                time TEXT,
                name TEXT NOT NULL,
                email TEXT NOT NULL,
                phone TEXT,
                role TEXT,
                experience TEXT,
                linkedin TEXT,
                resume BLOB,
                filename TEXT NOT NULL
            )`,
            demo_requests: `CREATE TABLE IF NOT EXISTS demo_requests (
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                date TEXT,
                time TEXT,
                name TEXT NOT NULL,
                email TEXT NOT NULL,
                company TEXT,
                product TEXT,
                demo_date TEXT,
                slot TEXT,
                phone TEXT,
                comments TEXT
            )`,
            contact_requests: `CREATE TABLE IF NOT EXISTS contact_requests (
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                date TEXT,
                time TEXT,
                name TEXT NOT NULL,
                email TEXT NOT NULL,
                phone TEXT,
                subject TEXT,
                message TEXT
            )`,
            purchases: `CREATE TABLE IF NOT EXISTS purchases (
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                purchase_date TEXT,
                time TEXT,
                name TEXT NOT NULL,
                email TEXT NOT NULL,
                phone TEXT,
                product TEXT,
                subscription_plan TEXT,
                subscription_type TEXT
            )`,
            jobs: `CREATE TABLE IF NOT EXISTS jobs (
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                title TEXT NOT NULL,
                description TEXT NOT NULL,
                status TEXT CHECK(status IN ('Open', 'Closed')) NOT NULL DEFAULT 'Open',
                vacancies INTEGER NOT NULL DEFAULT 0,
                job_type TEXT CHECK(job_type IN ('Full-Time', 'Part-Time', 'Contract')) NOT NULL,
                location TEXT CHECK(location IN ('On-Site', 'Remote', 'Hybrid')) NOT NULL,
                date_posted TEXT NOT NULL,
                deadline TEXT,
                image_url TEXT
            )`
        };

        // Iterate through each table and create it if necessary
        for (const [table, query] of Object.entries(tables)) {
            await runQuery(query);
            console.log(`Table '${table}' is checked/created successfully.`);
        }
    } catch (err) {
        console.error('Database connection error:', err);
    }
}

module.exports = {initializeDatabase}