Skip to content

Commit

Permalink
migration: major database migration
Browse files Browse the repository at this point in the history
  • Loading branch information
Sammonster495 committed Aug 28, 2024
1 parent 1e03800 commit 636ee2d
Show file tree
Hide file tree
Showing 38 changed files with 6,922 additions and 7,374 deletions.
13,529 changes: 6,428 additions & 7,101 deletions package-lock.json

Large diffs are not rendered by default.

101 changes: 78 additions & 23 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ generator client {

datasource db {
//Local database
provider = "postgresql"
provider = "cockroachdb"
// Prod
// provider = "cockroachdb"
url = env("DATABASE_URL")
}
Expand All @@ -26,19 +25,22 @@ model User {
accounts Account[]
sessions Session[]
displayName String?
bio String?
phone String?
username String?
usn String?
lightTheme String?
darkTheme String?
links String[]
achivements String[]
displayName String?
bio String?
phone String?
username String?
college String? @default("N.M.A.M. Institute of Technology")
usn String?
lightTheme String?
darkTheme String?
role String? @default("user")
links Links?
organizers Event[]
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
Core Core[]
Team Team[]
}

model Account {
Expand Down Expand Up @@ -72,20 +74,15 @@ model Session {
updatedAt DateTime @updatedAt
}

model VerificationToken {
identifier String
token String
expires DateTime
@@id([identifier, token])
}

model Core {
id String @id @default(cuid())
id String @id @default(cuid())
year String
position String
User User @relation(fields: [userId], references: [id])
post String
image String
quote String?
User User @relation(fields: [userId], references: [id])
order Int
userId String
}

Expand All @@ -95,11 +92,69 @@ model Faculty {
name String
email String
designation String
designation2 String?
image String
about String[]
order Int @unique
published Boolean @default(false)
@@index([name], name: "name_index")
}
}

model Event {
id String @id @default(cuid())
title String
description String?
image String
date DateTime
time String?
reportLink String?
venue String?
category String
participants Team[]
winners Winners[]
organizers User[]
guests String[]
published Boolean @default(false)
}

model Links {
id String @id @default(cuid())
instagram String?
twitter String?
linkedin String?
github String?
custom1 String?
custom2 String?
custom3 String?
custom4 String?
order String[]
user User @relation(fields: [userId], references: [id])
userId String @unique
}

model Team {
id String @id @default(cuid())
user User[]
event Event @relation(fields: [eventId], references: [id])
winner Winners[]
attended Boolean @default(false)
eventId String
}

model Winners {
id String @id @default(cuid())
position Int
teamId String
eventId String
event Event @relation(fields: [eventId], references: [id])
team Team @relation(fields: [teamId], references: [id])
}
2 changes: 1 addition & 1 deletion src/app.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@
<body data-sveltekit-preload-data="hover">
<div style="display: contents">%sveltekit.body%</div>
</body>
</html>
</html>
81 changes: 76 additions & 5 deletions src/lib/auth/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import { DefaultSession, SvelteKitAuth, SvelteKitAuthConfig } from '@auth/svelte
import { PrismaAdapter } from '@auth/prisma-adapter';
import Google from '@auth/sveltekit/providers/google';
import { db } from '$lib/db/db';
import { env } from '$lib/env';
import { AUTH_GOOGLE_ID, AUTH_GOOGLE_SECRET } from '$env/static/private';

const authOptions: SvelteKitAuthConfig = {
adapter: PrismaAdapter(db),
providers: [
Google({
clientId: env.AUTH_GOOGLE_ID,
clientSecret: env.AUTH_GOOGLE_SECRET,
clientId: AUTH_GOOGLE_ID,
clientSecret: AUTH_GOOGLE_SECRET,

authorization: {
params: {
Expand All @@ -21,11 +21,82 @@ const authOptions: SvelteKitAuthConfig = {
})
],
callbacks: {
async session({ session, user, token }) {
async signIn({ user, account }) {
const email = user.email;

// Check if the user already exists in the database
let existingUser = await db.user.findUnique({
where: { email: email as string },
});

if (!existingUser) {
// If the user doesn't exist, create a new user
existingUser = await db.user.create({
data: {
email: email as string,
name: user.name,
image: user.image,
bio: `Hello! I am ${user.name}.`,
accounts: {
create: {
provider: account?.provider as string,
providerAccountId: account?.providerAccountId as string,
type: account?.type as string, // Ensure this matches your schema
access_token: account?.access_token || undefined,
refresh_token: account?.refresh_token || undefined,
expires_at: account?.expires_at || undefined,
token_type: account?.token_type || undefined,
scope: account?.scope || undefined,
id_token: account?.id_token || undefined,
},
},
},
});
} else {
// If the user exists, update their name, image, and account information
existingUser = await db.user.update({
where: { email: email as string },
data: {
name: user.name || existingUser.name,
image: user.image || existingUser.image,
bio: existingUser.bio || `Hello! I am ${user.name}.`,
accounts: {
upsert: {
where: {
provider_providerAccountId: {
provider: account?.provider as string,
providerAccountId: account?.providerAccountId as string,
},
},
update: {
access_token: account?.access_token || undefined,
refresh_token: account?.refresh_token || undefined,
expires_at: account?.expires_at || undefined,
},
create: {
provider: account?.provider as string,
providerAccountId: account?.providerAccountId as string,
type: account?.type as string, // Ensure this matches your schema
access_token: account?.access_token || undefined,
refresh_token: account?.refresh_token || undefined,
expires_at: account?.expires_at || undefined,
token_type: account?.token_type || undefined,
scope: account?.scope || undefined,
id_token: account?.id_token || undefined,
},
},
},
},
});
}

return true; // Continue with the sign-in
},
async session({ session, user }) {
const sess = {} as DefaultSession;

sess.expires = session.expires;
sess.user = user;
sess.user = user

return sess;
}
Expand Down
15 changes: 14 additions & 1 deletion src/lib/auth/stores.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { writable, derived, type Readable } from 'svelte/store';
import { type User, type UserData, type UserProfileData } from './types';

function setUser(data: { session: any }) {
let links: any = null;

function setUser(data: { session: any, links: any }) {
if (data.session === null) {
user.set(null);
} else {
links = data.links;
user.set(data.session.user);
}
}
Expand Down Expand Up @@ -39,6 +42,16 @@ const userProfileData:Readable<UserProfileData | null> = derived(user, ($user, s
bio: $user.bio,
phone: $user.phone,
usn: $user.usn,
college: $user.college,
instagram: links?.instagram,
linkedin: links?.linkedin,
github: links?.github,
twitter: links?.twitter,
custom1: links?.custom1,
custom2: links?.custom2,
custom3: links?.custom3,
custom4: links?.custom4,
order: links?.order || [],
lightTheme: $user.lightTheme,
darkTheme: $user.darkTheme
};
Expand Down
11 changes: 11 additions & 0 deletions src/lib/auth/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ type User = {
bio: string | null;
phone: number | null;
username: string | null;
college: string | null;
usn: string | null;

lightTheme: string | null;
Expand All @@ -32,8 +33,18 @@ type UserProfileData = {
bio: string | null;
phone: number | null;
usn: string | null;
college: string | null;
lightTheme: string | null;
darkTheme: string | null;
instagram: string | null;
linkedin: string | null;
github: string | null;
twitter: string | null;
custom1: string | null;
custom2: string | null;
custom3: string | null;
custom4: string | null;
order: string[]
};

export {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/components/Auth/AuthButton.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
<Popover.Content>
<div class="flex max-w-xs flex-col gap-2">
<div class="px-2 text-lg text-center">
Hello <span class="font-semibold">{$userData.name}</span>
Hello <span class="font-semibold">{$userData?.displayName}</span>
</div>
<a href="/{$userData.username}" class="contents"> <Button class="border bg-transparent text-primary" variant={'secondary'}>Your Public Profile</Button> </a>
<a href="/{$userData.username}/edit" class="contents"><Button class="border bg-transparent text-primary" variant={'secondary'}>Edit Profile</Button></a>
Expand Down
30 changes: 18 additions & 12 deletions src/lib/components/Auth/user-auth-form.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,34 @@
import { Label } from '$lib/components/ui/label';
import { Input } from '$lib/components/ui/input';
import { auth, userData } from '$lib/firebase/firebase';
import { user } from '$lib/firebase/firebase';
// import { auth, userData } from '$lib/firebase/firebase';
// import { user } from '$lib/firebase/firebase';
import { GoogleAuthProvider, signInWithPopup, signOut } from 'firebase/auth';
import { user, userData , userLoaded} from "$lib/auth/stores"
import { signIn, signOut } from "@auth/sveltekit/client"
// import { GoogleAuthProvider, signInWithPopup, signOut } from 'firebase/auth';
import { goto } from '$app/navigation';
import { page } from '$app/stores';
import { browser } from '$app/environment';
let redirect = $page.url.searchParams.get('redirect') ?? '';
async function signInWithGoogle() {
const provider = new GoogleAuthProvider();
await signInWithPopup(auth, provider);
await signIn("google");
}
async function signOutSSR() {
await signOut(auth);
await signOut();
}
$: {
if ($user && browser) {
if ($userData)
goto('/');
else
goto('/create-account');
}
}
</script>

Expand All @@ -35,12 +47,6 @@
<a class="w-full" href="/{$userData.username}/edit"> <Button class="w-full">Account settings</Button></a>
</Card.Footer>
</Card.Root>
{:else if $user}
{#if redirect !== ''}
{goto('/create-account')}
{:else}
{goto('/create-account?redirect=' + redirect)}
{/if}
<!-- <Card.Root class="m-2">
<Card.Content class="mt-4 flex flex-col gap-4">
<h2 class="card-title">Welcome, {$user.displayName}</h2>
Expand Down
8 changes: 4 additions & 4 deletions src/lib/components/Card/FacultyCard.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
export let imageAlt: string = 'profile';
export let name: string;
export let designation: string = 'Professor';
export let designation2: string;
export let about: Array<any> = [];
export let designation2: string | null;
export let about: Array<string> = [];
// export let target: string = '_blank';
</script>

Expand Down Expand Up @@ -37,9 +37,9 @@
<img src={image2} alt={imageAlt} class="aspect-square w-64 object-cover md:flex-shrink-0 lg:w-96" />
</div>

<div class="flex flex-grow flex-col overflow-y-auto px-4">
<div class="flex flex-grow flex-col overflow-y-auto space-y-8 px-4">
{#each about as para}
<p class="pt-2">{para}</p>
<p>{para}</p>
{/each}
</div>
</div>
Expand Down
Loading

0 comments on commit 636ee2d

Please sign in to comment.