Files
workspace/apps/go-crm/internal/db/db.go
2026-04-21 17:34:58 -03:00

142 lines
3.7 KiB
Go

package db
import (
"database/sql"
"fmt"
"os"
"path/filepath"
_ "github.com/glebarez/sqlite"
)
const schema = `
CREATE TABLE IF NOT EXISTS accounts (
account_id INTEGER PRIMARY KEY AUTOINCREMENT,
email TEXT UNIQUE NOT NULL,
name TEXT NOT NULL,
password TEXT NOT NULL,
created_at INTEGER NOT NULL
);
CREATE TABLE IF NOT EXISTS sessions (
session_id TEXT PRIMARY KEY,
account_id INTEGER NOT NULL,
expires INTEGER NOT NULL,
FOREIGN KEY (account_id) REFERENCES accounts(account_id)
);
CREATE TABLE IF NOT EXISTS clients (
client_id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
phone TEXT,
email TEXT,
address TEXT,
notes TEXT,
created_at INTEGER NOT NULL
);
CREATE TABLE IF NOT EXISTS customers (
customer_id INTEGER PRIMARY KEY AUTOINCREMENT,
client_id INTEGER NOT NULL,
name TEXT NOT NULL,
phone TEXT,
birth_date TEXT,
instagram TEXT,
created_at INTEGER NOT NULL,
FOREIGN KEY (client_id) REFERENCES clients(client_id)
);
CREATE TABLE IF NOT EXISTS services (
service_id INTEGER PRIMARY KEY AUTOINCREMENT,
client_id INTEGER NOT NULL,
name TEXT NOT NULL,
price REAL,
description TEXT,
duration TEXT,
created_at INTEGER NOT NULL,
FOREIGN KEY (client_id) REFERENCES clients(client_id)
);
CREATE TABLE IF NOT EXISTS scheduling (
schedule_id INTEGER PRIMARY KEY AUTOINCREMENT,
client_id INTEGER NOT NULL,
customer_id INTEGER NOT NULL,
service_id INTEGER NOT NULL,
plan_date TEXT,
time TEXT,
status TEXT DEFAULT 'pending',
created_at INTEGER NOT NULL,
FOREIGN KEY (client_id) REFERENCES clients(client_id),
FOREIGN KEY (customer_id) REFERENCES customers(customer_id),
FOREIGN KEY (service_id) REFERENCES services(service_id)
);
CREATE TABLE IF NOT EXISTS payments (
payment_id INTEGER PRIMARY KEY AUTOINCREMENT,
client_id INTEGER NOT NULL,
customer_id INTEGER NOT NULL,
schedule_id INTEGER,
has_paid INTEGER DEFAULT 0,
amount REAL,
payment_date TEXT,
payment_method TEXT,
created_at INTEGER NOT NULL,
FOREIGN KEY (client_id) REFERENCES clients(client_id),
FOREIGN KEY (customer_id) REFERENCES customers(customer_id),
FOREIGN KEY (schedule_id) REFERENCES scheduling(schedule_id)
);
CREATE TABLE IF NOT EXISTS questions (
question_id INTEGER PRIMARY KEY AUTOINCREMENT,
client_id INTEGER NOT NULL,
customer_id INTEGER NOT NULL,
question TEXT NOT NULL,
timestamp INTEGER NOT NULL,
status TEXT DEFAULT 'pending',
created_at INTEGER NOT NULL,
FOREIGN KEY (client_id) REFERENCES clients(client_id),
FOREIGN KEY (customer_id) REFERENCES customers(customer_id)
);
CREATE TABLE IF NOT EXISTS answers (
answer_id INTEGER PRIMARY KEY AUTOINCREMENT,
client_id INTEGER NOT NULL,
question_id INTEGER NOT NULL,
answer TEXT NOT NULL,
timestamp INTEGER NOT NULL,
status TEXT DEFAULT 'active',
created_at INTEGER NOT NULL,
FOREIGN KEY (client_id) REFERENCES clients(client_id),
FOREIGN KEY (question_id) REFERENCES questions(question_id)
);
`
var dbPath = "data/go-crm.db"
func Init(path string) (*sql.DB, error) {
if path != "" {
dbPath = path
}
dir := filepath.Dir(dbPath)
if err := os.MkdirAll(dir, 0755); err != nil {
return nil, fmt.Errorf("failed to create data directory: %w", err)
}
database, err := sql.Open("sqlite", dbPath)
if err != nil {
return nil, fmt.Errorf("failed to open database: %w", err)
}
if err := database.Ping(); err != nil {
return nil, fmt.Errorf("failed to ping database: %w", err)
}
if _, err := database.Exec(schema); err != nil {
return nil, fmt.Errorf("failed to create schema: %w", err)
}
return database, nil
}