add unique_strings table and trigger for generating unique user codes

This commit is contained in:
vista-man
2025-04-03 11:08:26 +02:00
parent b56f3fb433
commit 56c545021f

View File

@@ -30,6 +30,27 @@ CREATE TABLE scanned_tickets (
UNIQUE (ticket_id)
);
-- Table for storing unique strings for users
CREATE TABLE unique_strings (
id INT AUTO_INCREMENT PRIMARY KEY,
user_id INT NOT NULL,
unique_code CHAR(6) NOT NULL UNIQUE,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES employees(id) ON DELETE CASCADE
);
-- Trigger to automatically generate a unique 6-character alphanumeric string for each user
DELIMITER $$
CREATE TRIGGER generate_unique_code
AFTER INSERT ON employees
FOR EACH ROW
BEGIN
DECLARE unique_code CHAR(6);
SET unique_code = (SELECT SUBSTRING(CONCAT(MD5(RAND()), MD5(RAND())), 1, 6));
INSERT INTO unique_strings (user_id, unique_code) VALUES (NEW.id, unique_code);
END$$
DELIMITER ;
-- Insert a default employee account (username: admin, password: password)
INSERT INTO employees (username, password_hash)
VALUES ('admin', SHA2('password', 256)); -- Replace with a secure password hashing method