SmartRoutesauth.ts2 matches
34// Simple email verification token generation
5function generateToken(email: string): string {
6const timestamp = Date.now().toString();
7// In a real app, use a proper JWT or other secure token method
1112// Verify token (simplified)
13export function verifyToken(token: string): string | null {
14try {
15const decoded = atob(token);
SmartRoutesREADME.md3 matches
1# Database Layer
23This directory contains the database setup and query functions for the SmartRoutes application.
45## Files
67- `migrations.ts` - Contains the database schema definitions and migration functions
8- `queries.ts` - Contains typed query functions for interacting with the database
910## Database Schema
SmartRoutesqueries.ts8 matches
3031// User queries
32export async function createUser(email: string, name?: string): Promise<number> {
33const result = await sqlite.execute(
34`INSERT INTO ${USERS_TABLE} (email, name) VALUES (?, ?) RETURNING id`,
38}
3940export async function getUserByEmail(email: string): Promise<User | null> {
41const result = await sqlite.execute(
42`SELECT * FROM ${USERS_TABLE} WHERE email = ?`,
46}
4748export async function updateUserLastLogin(userId: number): Promise<void> {
49await sqlite.execute(
50`UPDATE ${USERS_TABLE} SET last_login = CURRENT_TIMESTAMP WHERE id = ?`,
5455// Traffic report queries
56export async function createTrafficReport(
57userId: number,
58latitude: number,
70}
7172export async function getRecentTrafficReports(
73hours: number = 1,
74limit: number = 100
84}
8586export async function getTrafficReportsNearLocation(
87latitude: number,
88longitude: number,
106107// Location queries
108export async function updateUserLocation(
109userId: number,
110latitude: number,
135}
136137export async function getUserLocation(userId: number): Promise<Location | null> {
138const result = await sqlite.execute(
139`SELECT * FROM ${LOCATIONS_TABLE} WHERE user_id = ?`,
SmartRoutesmigrations.ts1 match
78// Create tables if they don't exist
9export async function runMigrations() {
10// Users table
11await sqlite.execute(`
2728// Tab switching
29function switchTab(tabName) {
30if (tabName === 'jobs') {
31jobsTab.classList.add('tab-active');
5556// Load jobs
57async function loadJobs() {
58const jobs = await fetchJobs();
59renderJobs(jobs, jobListings);
6162// Load chat messages
63async function loadChatMessages() {
64const messages = await fetchChatMessages();
65renderChatMessages(messages, chatMessages);
6768// Initialize the application
69async function initApp() {
70// Try to load username from localStorage
71const savedUsername = localStorage.getItem('username');
1// Chat functionality
23// Fetch chat messages
4export async function fetchChatMessages(limit = 50) {
5try {
6const response = await fetch(`/api/chat?limit=${limit}`);
1617// Send a new chat message
18export async function sendChatMessage(userName, message) {
19try {
20const response = await fetch('/api/chat', {
4243// Format date for display
44function formatDate(dateString) {
45const date = new Date(dateString);
46return date.toLocaleString();
4849// Render chat messages
50export function renderChatMessages(messages, container) {
51if (!messages || messages.length === 0) {
52container.innerHTML = '<div class="text-center text-gray-500">No messages yet. Start the conversation!</div>';
8788// Initialize chat form submission
89export function initChatForm(formElement, userNameInput, onSuccess) {
90const chatSubmitButton = document.getElementById('chat-submit-button');
91const usernameWarning = document.getElementById('username-warning');
92const usernameRequired = document.getElementById('username-required');
93
94// Function to check if username is provided
95function checkUsername() {
96const userName = userNameInput.value.trim();
97const hasUsername = !!userName;
153154// Poll for new messages
155export function startChatPolling(interval, callback) {
156// Initial fetch
157callback();
1// Job posting related functionality
23// Fetch all job postings
4export async function fetchJobs() {
5try {
6const response = await fetch('/api/jobs');
1617// Create a new job posting
18export async function createJob(jobData) {
19try {
20const response = await fetch('/api/jobs', {
3940// Format date for display
41function formatDate(dateString) {
42const date = new Date(dateString);
43return date.toLocaleString();
4546// Render job listings
47export function renderJobs(jobs, container) {
48if (!jobs || jobs.length === 0) {
49container.innerHTML = '<li class="px-4 py-4 sm:px-6 text-gray-500">No job postings available.</li>';
6970// Initialize job form submission
71export function initJobForm(formElement, onSuccess) {
72formElement.addEventListener('submit', async (event) => {
73event.preventDefault();
17}
1819// Helper function to format dates
20export function formatDate(dateString: string): string {
21const date = new Date(dateString);
22return date.toLocaleString();
Jobsqueries.ts4 matches
2021// Job posting queries
22export async function getAllJobs(): Promise<JobPosting[]> {
23const result = await sqlite.execute(
24`SELECT * FROM ${JOB_POSTINGS_TABLE} ORDER BY created_at DESC`
27}
2829export async function createJob(job: Omit<JobPosting, "id" | "created_at">): Promise<JobPosting> {
30const created_at = new Date().toISOString();
31
4142// Chat message queries
43export async function getChatMessages(limit = 50): Promise<ChatMessage[]> {
44const result = await sqlite.execute(
45`SELECT * FROM ${CHAT_MESSAGES_TABLE}
51}
5253export async function createChatMessage(message: Omit<ChatMessage, "id" | "created_at">): Promise<ChatMessage> {
54const created_at = new Date().toISOString();
55
Jobsmigrations.ts1 match
5export const CHAT_MESSAGES_TABLE = "chat_messages_v1";
67export async function runMigrations() {
8// Create job postings table
9await sqlite.execute(`