From 608998f534d71be1f108d84bebb9b5dcb5ed68c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Carlos=20S=C3=A1nchez?= Date: Mon, 27 May 2024 12:09:35 -0600 Subject: [PATCH 1/2] change schema --- components/BannerImagePanel.tsx | 2 +- db/schema.ts | 3 +++ services/user.ts | 45 +++++++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 1 deletion(-) diff --git a/components/BannerImagePanel.tsx b/components/BannerImagePanel.tsx index 4e7fc63..d4df3f5 100644 --- a/components/BannerImagePanel.tsx +++ b/components/BannerImagePanel.tsx @@ -17,7 +17,7 @@ const BannerImagePanel = () => { }; return ( -
+

Select an Image

{images.map((src, index) => ( diff --git a/db/schema.ts b/db/schema.ts index ac9abf8..0b5a11a 100644 --- a/db/schema.ts +++ b/db/schema.ts @@ -25,6 +25,9 @@ export const user = pgTable( department: varchar("department", { length: 64 }), photoUrl: varchar("photo_url", { length: 1024 }), role: userRoleEnum("role").notNull(), + bannerId: varchar("banner_id", { length: 24 }).notNull(), + primaryColor: varchar("primary_color", { length: 8 }), + lightMode: boolean("light_mode").default(true), }, ); diff --git a/services/user.ts b/services/user.ts index 2d759d3..a823bd7 100644 --- a/services/user.ts +++ b/services/user.ts @@ -92,6 +92,9 @@ export const registerUser = async ( role: "EMPLOYEE", department: department, jobTitle: jobTitle, + bannerId: "Banner1.svg", + primaryColor: "#6640D5", + lightMode: true, }) .catch((e) => { const dbError = e as DatabaseError; @@ -122,6 +125,48 @@ export const updateRole = async ({ await db.update(user).set({ role: newRole }).where(eq(user.id, id)).execute(); }; +export const updateBanner = async ({ + id, + bannerId, +}: { + id: string; + bannerId: number; +}) => { + await db + .update(user) + .set({ bannerId: bannerId }) + .where(eq(user.id, id)) + .execute(); +}; + +export const updatePrimaryColor = async ({ + id, + primaryColor, +}: { + id: string; + primaryColor: string; +}) => { + await db + .update(user) + .set({ primaryColor: primaryColor }) + .where(eq(user.id, id)) + .execute(); +}; + +export const updateLightMode = async ({ + id, + lightMode, +}: { + id: string; + lightMode: boolean; +}) => { + await db + .update(user) + .set({ lightMode: lightMode }) + .where(eq(user.id, id)) + .execute(); +}; + export async function getUserTraitsById(id: string) { const res = await db .select({ From d206741bd7dac3a843388a94087f5ebcb4c3ac36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Carlos=20S=C3=A1nchez?= Date: Mon, 27 May 2024 12:11:57 -0600 Subject: [PATCH 2/2] removed schema changes --- db/schema.ts | 3 -- services/user.ts | 120 +---------------------------------------------- 2 files changed, 1 insertion(+), 122 deletions(-) diff --git a/db/schema.ts b/db/schema.ts index 0b5a11a..ac9abf8 100644 --- a/db/schema.ts +++ b/db/schema.ts @@ -25,9 +25,6 @@ export const user = pgTable( department: varchar("department", { length: 64 }), photoUrl: varchar("photo_url", { length: 1024 }), role: userRoleEnum("role").notNull(), - bannerId: varchar("banner_id", { length: 24 }).notNull(), - primaryColor: varchar("primary_color", { length: 8 }), - lightMode: boolean("light_mode").default(true), }, ); diff --git a/services/user.ts b/services/user.ts index a823bd7..8454383 100644 --- a/services/user.ts +++ b/services/user.ts @@ -3,12 +3,11 @@ import { projectMember, user, userRoleEnum, project } from "@/db/schema"; import db from "@/db/drizzle"; import * as schema from "@/db/schema"; -import { eq, not, and, or, asc, sql, ne, gt } from "drizzle-orm"; +import { eq, not, and, or, asc, sql } from "drizzle-orm"; import { alias } from "drizzle-orm/pg-core"; import { auth } from "@/auth"; import bcrypt from "bcrypt"; import { DatabaseError } from "pg"; -import { deprecate } from "util"; export async function getUserInfoById(id: string) { const res = await db @@ -92,9 +91,6 @@ export const registerUser = async ( role: "EMPLOYEE", department: department, jobTitle: jobTitle, - bannerId: "Banner1.svg", - primaryColor: "#6640D5", - lightMode: true, }) .catch((e) => { const dbError = e as DatabaseError; @@ -125,48 +121,6 @@ export const updateRole = async ({ await db.update(user).set({ role: newRole }).where(eq(user.id, id)).execute(); }; -export const updateBanner = async ({ - id, - bannerId, -}: { - id: string; - bannerId: number; -}) => { - await db - .update(user) - .set({ bannerId: bannerId }) - .where(eq(user.id, id)) - .execute(); -}; - -export const updatePrimaryColor = async ({ - id, - primaryColor, -}: { - id: string; - primaryColor: string; -}) => { - await db - .update(user) - .set({ primaryColor: primaryColor }) - .where(eq(user.id, id)) - .execute(); -}; - -export const updateLightMode = async ({ - id, - lightMode, -}: { - id: string; - lightMode: boolean; -}) => { - await db - .update(user) - .set({ lightMode: lightMode }) - .where(eq(user.id, id)) - .execute(); -}; - export async function getUserTraitsById(id: string) { const res = await db .select({ @@ -225,42 +179,6 @@ export async function getCoWorkers(userId: string | null | undefined) { return coworkers; } -const coworker = alias(schema.user, "coworker"); -const pmUser = alias(schema.projectMember, "pmUser"); -const pmCoworker = alias(schema.projectMember, "pmCoworker"); -export async function getCurrentCoworkers(userId: string | null | undefined) { - if (!userId || userId === undefined) { - const session = await auth(); - userId = session?.user?.id; - if (!userId) { - throw new Error("You most be signed in"); - } - } - - const res = await db - .selectDistinct({ - id: coworker.id, - name: coworker.name, - email: coworker.email, - jobTitle: coworker.jobTitle, - department: coworker.department, - photoUrl: coworker.photoUrl, - }) - .from(pmUser) - .leftJoin(pmCoworker, eq(pmUser.projectId, pmCoworker.projectId)) - .leftJoin(coworker, eq(pmCoworker.userId, coworker.id)) - .leftJoin(project, eq(pmUser.projectId, project.id)) - .where( - and( - eq(pmUser.userId, userId), - ne(pmUser.userId, pmCoworker.userId), - gt(project.endDate, sql`CURRENT_TIMESTAMP`), - ), - ); - - return res; -} - export async function getProjectsProfile(userId: string | null | undefined) { if (!userId || userId === undefined) { const session = await auth(); @@ -292,42 +210,6 @@ export async function getProjectsProfile(userId: string | null | undefined) { return res; } -export async function getCurrentProjectsProfile( - userId: string | null | undefined, -) { - if (!userId || userId === undefined) { - const session = await auth(); - userId = session?.user?.id; - if (!userId) { - throw new Error("You most be signed in"); - } - } - - const res = await db - .selectDistinctOn([schema.project.id], { - id: schema.project.id, - name: schema.project.name, - description: schema.project.description, - startDate: schema.project.startDate, - endDate: schema.project.endDate, - }) - .from(schema.projectMember) - .innerJoin( - schema.project, - eq(schema.projectMember.projectId, schema.project.id), - ) - .where( - and( - or( - eq(schema.projectMember.userId, userId), - eq(schema.project.managerId, userId), - ), - gt(project.endDate, sql`CURRENT_TIMESTAMP`), - ), - ); - return res; -} - export async function getAllEmployees() { const res = await db .select({