initialize git, basic setup for crm

This commit is contained in:
richardtekula
2025-11-18 13:53:28 +01:00
commit da01d586fc
47 changed files with 12776 additions and 0 deletions

69
src/db/schema.js Normal file
View File

@@ -0,0 +1,69 @@
import { pgTable, text, timestamp, boolean, uuid, pgEnum } from 'drizzle-orm/pg-core';
// Role enum
export const roleEnum = pgEnum('role', ['admin', 'member']);
// Users table - hlavná tabuľka používateľov
export const users = pgTable('users', {
id: uuid('id').primaryKey().defaultRandom(),
username: text('username').notNull().unique(),
email: text('email').unique(),
emailPassword: text('email_password'), // Heslo k emailovému účtu (encrypted)
jmapAccountId: text('jmap_account_id'), // JMAP account ID z truemail
firstName: text('first_name'),
lastName: text('last_name'),
password: text('password'), // bcrypt hash (null ak ešte nenastavené)
tempPassword: text('temp_password'), // dočasné heslo (bcrypt hash)
changedPassword: boolean('changed_password').default(false), // či si užívateľ zmenil heslo
role: roleEnum('role').default('member').notNull(),
lastLogin: timestamp('last_login'),
createdAt: timestamp('created_at').defaultNow().notNull(),
updatedAt: timestamp('updated_at').defaultNow().notNull(),
});
// Audit logs - kompletný audit trail všetkých akcií
export const auditLogs = pgTable('audit_logs', {
id: uuid('id').primaryKey().defaultRandom(),
userId: uuid('user_id').references(() => users.id, { onDelete: 'set null' }),
action: text('action').notNull(), // 'login', 'password_change', 'email_linked', 'role_change', atď.
resource: text('resource').notNull(), // 'user', 'auth', atď.
resourceId: text('resource_id'), // ID ovplyvneného zdroja
oldValue: text('old_value'), // JSON string starých hodnôt
newValue: text('new_value'), // JSON string nových hodnôt
ipAddress: text('ip_address'),
userAgent: text('user_agent'),
success: boolean('success').default(true).notNull(),
errorMessage: text('error_message'),
createdAt: timestamp('created_at').defaultNow().notNull(),
});
// Contacts table - ľudia s ktorými komunikujeme cez email
export const contacts = pgTable('contacts', {
id: uuid('id').primaryKey().defaultRandom(),
userId: uuid('user_id').references(() => users.id, { onDelete: 'cascade' }).notNull(),
email: text('email').notNull(),
name: text('name'),
notes: text('notes'),
addedAt: timestamp('added_at').defaultNow().notNull(),
createdAt: timestamp('created_at').defaultNow().notNull(),
updatedAt: timestamp('updated_at').defaultNow().notNull(),
});
// Emails table - uložené emaily z JMAP (iba pre pridané kontakty)
export const emails = pgTable('emails', {
id: uuid('id').primaryKey().defaultRandom(),
userId: uuid('user_id').references(() => users.id, { onDelete: 'cascade' }).notNull(),
contactId: uuid('contact_id').references(() => contacts.id, { onDelete: 'cascade' }),
jmapId: text('jmap_id').unique(),
messageId: text('message_id').unique(),
threadId: text('thread_id'),
inReplyTo: text('in_reply_to'),
from: text('from'),
to: text('to'),
subject: text('subject'),
body: text('body'),
isRead: boolean('is_read').default(false).notNull(),
date: timestamp('date'),
createdAt: timestamp('created_at').defaultNow().notNull(),
updatedAt: timestamp('updated_at').defaultNow().notNull(),
});