test the login yourself4

This commit is contained in:
Leon Serfaty G
2025-09-01 07:24:29 +00:00
parent fa221bbdc1
commit c5ebbc5f77
8 changed files with 136 additions and 113 deletions
+3 -1
View File
@@ -1,8 +1,9 @@
import { NextRequest, NextResponse } from 'next/server';
import db from '@/lib/db';
import getDb from '@/lib/db';
export async function GET(req: NextRequest) {
const db = getDb();
try {
const stmt = db.prepare('SELECT value FROM settings WHERE key = ?');
const setting = stmt.get('hourly_rate') as { value: string } | undefined;
@@ -20,6 +21,7 @@ export async function GET(req: NextRequest) {
}
export async function POST(req: NextRequest) {
const db = getDb();
try {
const { value } = await req.json();
+3 -1
View File
@@ -1,8 +1,9 @@
import { NextRequest, NextResponse } from 'next/server';
import db from '@/lib/db';
import getDb from '@/lib/db';
export async function GET(req: NextRequest) {
const db = getDb();
try {
const stmt = db.prepare("SELECT key, value FROM settings WHERE key LIKE 'smtp_%'");
const settings = stmt.all() as { key: string, value: string }[];
@@ -18,6 +19,7 @@ export async function GET(req: NextRequest) {
}
export async function POST(req: NextRequest) {
const db = getDb();
try {
const settings = await req.json();
+3 -1
View File
@@ -2,7 +2,7 @@
'use server';
import { z } from 'zod';
import db from '@/lib/db';
import getDb from '@/lib/db';
import { revalidatePath } from 'next/cache';
const emailTemplateSchema = z.object({
@@ -59,6 +59,7 @@ const defaultBody = `
* If no template is found, it creates and returns a default one.
*/
export async function getEmailTemplate(): Promise<{ subject: string; body: string }> {
const db = getDb();
try {
const stmt = db.prepare('SELECT subject, body FROM email_templates WHERE id = ?');
let template = stmt.get(1) as { subject: string; body: string } | undefined;
@@ -90,6 +91,7 @@ export async function updateEmailTemplate(data: { subject: string; body: string
}
const { subject, body } = validation.data;
const db = getDb();
try {
const stmt = db.prepare('UPDATE email_templates SET subject = ?, body = ? WHERE id = ?');
+5 -1
View File
@@ -1,7 +1,7 @@
'use server';
import db from '@/lib/db';
import getDb from '@/lib/db';
import { z } from 'zod';
import { revalidatePath } from 'next/cache';
import { redirect } from 'next/navigation';
@@ -29,6 +29,7 @@ type State = {
}
export async function getFlows(): Promise<Flow[]> {
const db = getDb();
try {
const stmt = db.prepare(
'SELECT id, name, description, path, createdAt, updatedAt FROM flows ORDER BY createdAt DESC'
@@ -42,6 +43,7 @@ export async function getFlows(): Promise<Flow[]> {
}
export async function getFlow(id: number): Promise<Flow | null> {
const db = getDb();
try {
const stmt = db.prepare('SELECT * FROM flows WHERE id = ?');
const flow = stmt.get(id) as Flow | undefined;
@@ -64,6 +66,7 @@ export async function saveFlow(prevState: State, formData: FormData): Promise<St
}
const { id, name, description, path } = validatedFields.data;
const db = getDb();
try {
if (id) {
@@ -120,6 +123,7 @@ export async function saveFlow(prevState: State, formData: FormData): Promise<St
}
export async function deleteFlow(id: number): Promise<{ success: boolean, message: string }> {
const db = getDb();
try {
// Prevent deletion of the default flow (ID 1)
if (id === 1) {
+2 -1
View File
@@ -1,7 +1,7 @@
'use server';
import db from '@/lib/db';
import getDb from '@/lib/db';
export type Lead = {
id: number;
@@ -12,6 +12,7 @@ export type Lead = {
};
export async function getLeads(): Promise<Lead[]> {
const db = getDb();
try {
const stmt = db.prepare(
'SELECT id, name, email, phone, createdAt FROM leads ORDER BY createdAt DESC'
+3 -1
View File
@@ -4,7 +4,7 @@
import { z } from 'zod';
import nodemailer from 'nodemailer';
import { PDFDocument, rgb, StandardFonts } from 'pdf-lib';
import db from '@/lib/db';
import getDb from '@/lib/db';
import { getEmailTemplate } from './email';
import type { FormData } from '@/components/cost-estimator/cost-estimator-form';
@@ -21,6 +21,7 @@ type InputType = z.infer<typeof inputSchema>;
// Helper to get SMTP settings from the database
async function getSmtpSettings() {
const db = getDb();
const stmt = db.prepare("SELECT key, value FROM settings WHERE key LIKE 'smtp_%'");
const settings = stmt.all() as { key: string, value: string }[];
const config = settings.reduce((acc, setting) => {
@@ -117,6 +118,7 @@ async function createEstimatePdf(data: InputType): Promise<Buffer> {
}
async function saveLead(name: string, email: string, phone?: string) {
const db = getDb();
try {
const stmt = db.prepare('INSERT INTO leads (name, email, phone) VALUES (?, ?, ?)');
stmt.run(name, email, phone || null);
+4 -1
View File
@@ -1,7 +1,8 @@
'use server';
import { z } from 'zod';
import db from '@/lib/db';
import getDb from '@/lib/db';
import { revalidatePath } from 'next/cache';
import { auth } from '@/auth';
@@ -21,6 +22,7 @@ type User = {
};
export async function getUserByEmail(email: string): Promise<User | null> {
const db = getDb();
try {
const stmt = db.prepare('SELECT id, name, email, password FROM users WHERE email = ?');
const user = stmt.get(email) as User | undefined;
@@ -64,6 +66,7 @@ export async function updateUser(data: UserFormValues): Promise<{ success: boole
const { name, email, password } = validation.data;
try {
const db = getDb();
const userId = session.user.id;
// Check if the new email is already taken by another user
+28 -21
View File
@@ -1,17 +1,17 @@
import Database from 'better-sqlite3';
// Use a file-based database in development
const db = new Database('local.db');
db.pragma('journal_mode = WAL');
let db: Database.Database;
function initializeDb() {
// Use a file-based database
const newDb = new Database('local.db');
newDb.pragma('journal_mode = WAL');
// --- SCHEMA CREATION ---
// Drop the users table to ensure a clean slate on every start, avoiding schema conflicts.
db.exec('DROP TABLE IF EXISTS users');
// Auth.js tables
db.exec(`
newDb.exec(`
CREATE TABLE IF NOT EXISTS users (
id TEXT NOT NULL PRIMARY KEY,
name TEXT,
@@ -22,7 +22,7 @@ db.exec(`
)
`);
db.exec(`
newDb.exec(`
CREATE TABLE IF NOT EXISTS accounts (
userId TEXT NOT NULL,
type TEXT NOT NULL,
@@ -40,7 +40,7 @@ db.exec(`
)
`);
db.exec(`
newDb.exec(`
CREATE TABLE IF NOT EXISTS sessions (
sessionToken TEXT NOT NULL PRIMARY KEY,
userId TEXT NOT NULL,
@@ -49,7 +49,7 @@ db.exec(`
)
`);
db.exec(`
newDb.exec(`
CREATE TABLE IF NOT EXISTS verification_tokens (
identifier TEXT NOT NULL,
token TEXT NOT NULL,
@@ -58,15 +58,14 @@ db.exec(`
)
`);
db.exec(`
newDb.exec(`
CREATE TABLE IF NOT EXISTS settings (
key TEXT PRIMARY KEY,
value TEXT
)
`);
db.exec(`
newDb.exec(`
CREATE TABLE IF NOT EXISTS email_templates (
id INTEGER PRIMARY KEY,
subject TEXT,
@@ -74,7 +73,7 @@ db.exec(`
)
`);
db.exec(`
newDb.exec(`
CREATE TABLE IF NOT EXISTS leads (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
@@ -84,7 +83,7 @@ db.exec(`
)
`);
db.exec(`
newDb.exec(`
CREATE TABLE IF NOT EXISTS flows (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
@@ -95,15 +94,14 @@ db.exec(`
)
`);
// --- SEEDING LOGIC ---
console.log('Running database checks and seeding if necessary...');
// Seed default user
const userStmt = db.prepare('SELECT id FROM users WHERE email = ?');
const userStmt = newDb.prepare('SELECT id FROM users WHERE email = ?');
const defaultUser = userStmt.get('admin@example.com');
if (!defaultUser) {
const insertUser = db.prepare(
const insertUser = newDb.prepare(
"INSERT INTO users (id, email, password, name) VALUES (?, ?, ?, ?)"
);
// Note: In a real app, hash the password!
@@ -112,10 +110,10 @@ if (!defaultUser) {
}
// Seed default flow
const flowStmt = db.prepare("SELECT id FROM flows WHERE path = ?");
const flowStmt = newDb.prepare("SELECT id FROM flows WHERE path = ?");
const defaultFlow = flowStmt.get('/');
if (!defaultFlow) {
const insertFlow = db.prepare(
const insertFlow = newDb.prepare(
"INSERT INTO flows (name, description, path) VALUES (?, ?, ?)"
);
insertFlow.run('Cost Estimator', 'The main cost estimation tool for clients.', '/');
@@ -123,5 +121,14 @@ if (!defaultFlow) {
}
console.log('Database setup complete.');
return newDb;
}
export default db;
function getDb() {
if (!db) {
db = initializeDb();
}
return db;
}
export default getDb;