diff --git a/src/app/page.tsx b/src/app/page.tsx index a49d99e..72d514c 100644 --- a/src/app/page.tsx +++ b/src/app/page.tsx @@ -1,7 +1,14 @@ +import { syncUsers } from "@/db/db"; import { SignInButton, SignedOut } from "@clerk/nextjs"; +import { auth, currentUser } from "@clerk/nextjs/server"; import React from "react"; -const page = () => { +const page = async () => { + const user = await currentUser(); + if (user) { + syncUsers(user); + } + return (
{title}
+{title}
+{time}
{description}
} diff --git a/src/db/db.ts b/src/db/db.ts index ec08fbf..85d0a7f 100644 --- a/src/db/db.ts +++ b/src/db/db.ts @@ -2,7 +2,9 @@ import { drizzle } from "drizzle-orm/postgres-js"; import postgres from "postgres"; import * as schema from "./schema"; import "dotenv/config"; -import { sql } from "drizzle-orm"; +import { eq, sql } from "drizzle-orm"; +import { User } from "@clerk/nextjs/server"; +import { UsersTable } from "./schema"; // const connectionString = process.env.DATABASE_URL as string; const connectionString = @@ -13,4 +15,40 @@ export const client = postgres(connectionString, { prepare: false }); export const db = drizzle(client, { schema }); -// Query Calls +// DB Calls +export const syncUsers = async ({ + id, + firstName, + lastName, + primaryEmailAddress, + primaryPhoneNumber, +}: User) => { + const existingUser = await db.query.UsersTable.findFirst({ + where: (UsersTable, { eq }) => eq(UsersTable.userId, id), + }); + + if (primaryEmailAddress == null) { + throw new Error("No email address for user"); + } + + if (!existingUser) { + await db + .insert(UsersTable) + .values({ + userId: id, + firstName: firstName, + lastName: lastName, + emailAddress: primaryEmailAddress.emailAddress, + phoneNumber: primaryPhoneNumber?.phoneNumber, + }) + .onConflictDoUpdate({ + target: UsersTable.userId, + set: { + firstName: firstName, + lastName: lastName, + emailAddress: primaryEmailAddress.emailAddress, + phoneNumber: primaryPhoneNumber?.phoneNumber, + }, + }); + } +}; diff --git a/src/db/migrations/0000_cute_exiles.sql b/src/db/migrations/0000_cute_exiles.sql deleted file mode 100644 index 55b89a4..0000000 --- a/src/db/migrations/0000_cute_exiles.sql +++ /dev/null @@ -1,69 +0,0 @@ -DO $$ BEGIN - CREATE TYPE "public"."eventVariant" AS ENUM('earnings', 'conference', 'split', 'dividend', 'econonmic', 'default'); -EXCEPTION - WHEN duplicate_object THEN null; -END $$; ---> statement-breakpoint -DO $$ BEGIN - CREATE TYPE "public"."themeMode" AS ENUM('light', 'dark', 'system'); -EXCEPTION - WHEN duplicate_object THEN null; -END $$; ---> statement-breakpoint -CREATE TABLE IF NOT EXISTS "dates" ( - "date" timestamp with time zone PRIMARY KEY NOT NULL, - CONSTRAINT "dates_date_unique" UNIQUE("date") -); ---> statement-breakpoint -CREATE TABLE IF NOT EXISTS "events" ( - "eventId" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, - "title" text NOT NULL, - "companyName" text NOT NULL, - "variant" "eventVariant" NOT NULL, - "time" timestamp with time zone NOT NULL, - "date" timestamp NOT NULL, - CONSTRAINT "events_companyName_unique" UNIQUE("companyName") -); ---> statement-breakpoint -CREATE TABLE IF NOT EXISTS "symbols" ( - "symbol" text PRIMARY KEY NOT NULL, - "companyName" text NOT NULL, - CONSTRAINT "symbols_symbol_unique" UNIQUE("symbol"), - CONSTRAINT "symbols_companyName_unique" UNIQUE("companyName") -); ---> statement-breakpoint -CREATE TABLE IF NOT EXISTS "userPreferences" ( - "userId" text PRIMARY KEY NOT NULL, - "theme" "themeMode" DEFAULT 'dark' NOT NULL, - "timezone" text DEFAULT 'America/New_York' NOT NULL, - "currencyUnits" text DEFAULT 'USD' NOT NULL, - CONSTRAINT "userPreferences_userId_unique" UNIQUE("userId") -); ---> statement-breakpoint -CREATE TABLE IF NOT EXISTS "users" ( - "userId" text PRIMARY KEY NOT NULL, - "firstName" text, - "lastName" text, - "emailAddress" text NOT NULL, - "phoneNumber" text, - CONSTRAINT "users_userId_unique" UNIQUE("userId"), - CONSTRAINT "users_emailAddress_unique" UNIQUE("emailAddress") -); ---> statement-breakpoint -DO $$ BEGIN - ALTER TABLE "events" ADD CONSTRAINT "events_companyName_symbols_symbol_fk" FOREIGN KEY ("companyName") REFERENCES "public"."symbols"("symbol") ON DELETE no action ON UPDATE no action; -EXCEPTION - WHEN duplicate_object THEN null; -END $$; ---> statement-breakpoint -DO $$ BEGIN - ALTER TABLE "events" ADD CONSTRAINT "events_date_dates_date_fk" FOREIGN KEY ("date") REFERENCES "public"."dates"("date") ON DELETE no action ON UPDATE no action; -EXCEPTION - WHEN duplicate_object THEN null; -END $$; ---> statement-breakpoint -DO $$ BEGIN - ALTER TABLE "userPreferences" ADD CONSTRAINT "userPreferences_userId_users_userId_fk" FOREIGN KEY ("userId") REFERENCES "public"."users"("userId") ON DELETE no action ON UPDATE no action; -EXCEPTION - WHEN duplicate_object THEN null; -END $$; diff --git a/src/db/migrations/meta/0000_snapshot.json b/src/db/migrations/meta/0000_snapshot.json deleted file mode 100644 index 47c2d5c..0000000 --- a/src/db/migrations/meta/0000_snapshot.json +++ /dev/null @@ -1,296 +0,0 @@ -{ - "id": "5f48754b-8b16-4f50-9273-9fa024fcbaf1", - "prevId": "00000000-0000-0000-0000-000000000000", - "version": "7", - "dialect": "postgresql", - "tables": { - "public.dates": { - "name": "dates", - "schema": "", - "columns": { - "date": { - "name": "date", - "type": "timestamp with time zone", - "primaryKey": true, - "notNull": true - } - }, - "indexes": {}, - "foreignKeys": {}, - "compositePrimaryKeys": {}, - "uniqueConstraints": { - "dates_date_unique": { - "name": "dates_date_unique", - "nullsNotDistinct": false, - "columns": [ - "date" - ] - } - } - }, - "public.events": { - "name": "events", - "schema": "", - "columns": { - "eventId": { - "name": "eventId", - "type": "uuid", - "primaryKey": true, - "notNull": true, - "default": "gen_random_uuid()" - }, - "title": { - "name": "title", - "type": "text", - "primaryKey": false, - "notNull": true - }, - "companyName": { - "name": "companyName", - "type": "text", - "primaryKey": false, - "notNull": true - }, - "variant": { - "name": "variant", - "type": "eventVariant", - "typeSchema": "public", - "primaryKey": false, - "notNull": true - }, - "time": { - "name": "time", - "type": "timestamp with time zone", - "primaryKey": false, - "notNull": true - }, - "date": { - "name": "date", - "type": "timestamp", - "primaryKey": false, - "notNull": true - } - }, - "indexes": {}, - "foreignKeys": { - "events_companyName_symbols_symbol_fk": { - "name": "events_companyName_symbols_symbol_fk", - "tableFrom": "events", - "tableTo": "symbols", - "columnsFrom": [ - "companyName" - ], - "columnsTo": [ - "symbol" - ], - "onDelete": "no action", - "onUpdate": "no action" - }, - "events_date_dates_date_fk": { - "name": "events_date_dates_date_fk", - "tableFrom": "events", - "tableTo": "dates", - "columnsFrom": [ - "date" - ], - "columnsTo": [ - "date" - ], - "onDelete": "no action", - "onUpdate": "no action" - } - }, - "compositePrimaryKeys": {}, - "uniqueConstraints": { - "events_companyName_unique": { - "name": "events_companyName_unique", - "nullsNotDistinct": false, - "columns": [ - "companyName" - ] - } - } - }, - "public.symbols": { - "name": "symbols", - "schema": "", - "columns": { - "symbol": { - "name": "symbol", - "type": "text", - "primaryKey": true, - "notNull": true - }, - "companyName": { - "name": "companyName", - "type": "text", - "primaryKey": false, - "notNull": true - } - }, - "indexes": {}, - "foreignKeys": {}, - "compositePrimaryKeys": {}, - "uniqueConstraints": { - "symbols_symbol_unique": { - "name": "symbols_symbol_unique", - "nullsNotDistinct": false, - "columns": [ - "symbol" - ] - }, - "symbols_companyName_unique": { - "name": "symbols_companyName_unique", - "nullsNotDistinct": false, - "columns": [ - "companyName" - ] - } - } - }, - "public.userPreferences": { - "name": "userPreferences", - "schema": "", - "columns": { - "userId": { - "name": "userId", - "type": "text", - "primaryKey": true, - "notNull": true - }, - "theme": { - "name": "theme", - "type": "themeMode", - "typeSchema": "public", - "primaryKey": false, - "notNull": true, - "default": "'dark'" - }, - "timezone": { - "name": "timezone", - "type": "text", - "primaryKey": false, - "notNull": true, - "default": "'America/New_York'" - }, - "currencyUnits": { - "name": "currencyUnits", - "type": "text", - "primaryKey": false, - "notNull": true, - "default": "'USD'" - } - }, - "indexes": {}, - "foreignKeys": { - "userPreferences_userId_users_userId_fk": { - "name": "userPreferences_userId_users_userId_fk", - "tableFrom": "userPreferences", - "tableTo": "users", - "columnsFrom": [ - "userId" - ], - "columnsTo": [ - "userId" - ], - "onDelete": "no action", - "onUpdate": "no action" - } - }, - "compositePrimaryKeys": {}, - "uniqueConstraints": { - "userPreferences_userId_unique": { - "name": "userPreferences_userId_unique", - "nullsNotDistinct": false, - "columns": [ - "userId" - ] - } - } - }, - "public.users": { - "name": "users", - "schema": "", - "columns": { - "userId": { - "name": "userId", - "type": "text", - "primaryKey": true, - "notNull": true - }, - "firstName": { - "name": "firstName", - "type": "text", - "primaryKey": false, - "notNull": false - }, - "lastName": { - "name": "lastName", - "type": "text", - "primaryKey": false, - "notNull": false - }, - "emailAddress": { - "name": "emailAddress", - "type": "text", - "primaryKey": false, - "notNull": true - }, - "phoneNumber": { - "name": "phoneNumber", - "type": "text", - "primaryKey": false, - "notNull": false - } - }, - "indexes": {}, - "foreignKeys": {}, - "compositePrimaryKeys": {}, - "uniqueConstraints": { - "users_userId_unique": { - "name": "users_userId_unique", - "nullsNotDistinct": false, - "columns": [ - "userId" - ] - }, - "users_emailAddress_unique": { - "name": "users_emailAddress_unique", - "nullsNotDistinct": false, - "columns": [ - "emailAddress" - ] - } - } - } - }, - "enums": { - "public.eventVariant": { - "name": "eventVariant", - "schema": "public", - "values": [ - "earnings", - "conference", - "split", - "dividend", - "econonmic", - "default" - ] - }, - "public.themeMode": { - "name": "themeMode", - "schema": "public", - "values": [ - "light", - "dark", - "system" - ] - } - }, - "schemas": {}, - "_meta": { - "columns": {}, - "schemas": {}, - "tables": {} - } -} \ No newline at end of file diff --git a/src/db/migrations/meta/_journal.json b/src/db/migrations/meta/_journal.json index c3c0500..f04877e 100644 --- a/src/db/migrations/meta/_journal.json +++ b/src/db/migrations/meta/_journal.json @@ -1,13 +1 @@ -{ - "version": "7", - "dialect": "postgresql", - "entries": [ - { - "idx": 0, - "version": "7", - "when": 1718598866339, - "tag": "0000_cute_exiles", - "breakpoints": true - } - ] -} \ No newline at end of file +{"version":"7","dialect":"postgresql","entries":[]} \ No newline at end of file