Skip to content

Commit

Permalink
🚑 Hotfix for projects
Browse files Browse the repository at this point in the history
  • Loading branch information
naelob committed Apr 25, 2024
1 parent 6da42a1 commit 9bb86dc
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 60 deletions.
24 changes: 8 additions & 16 deletions apps/client-ts/src/components/RootLayout/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ import { useStytchUser } from '@stytch/nextjs';
import useProfile from '@/hooks/useProfile';
import useProfileStore from '@/state/profileStore';
import useProjectStore from '@/state/projectStore';
import useProjects from '@/hooks/useProjects';
import useProjectsByUser from '@/hooks/useProjectsByUser';

const useDeviceSize = () => {

Expand Down Expand Up @@ -41,9 +39,9 @@ export const RootLayout = () => {
const [width, height] = useDeviceSize();
const router = useRouter()
const base = process.env.NEXT_PUBLIC_WEBAPP_DOMAIN;
const [userId, setUserId] = useState("");
const { user } = useStytchUser();
const {data, isLoading, isError, error} = useProfile(user?.user_id!);

if(isLoading) {
console.log("loading profiles");
}
Expand All @@ -53,28 +51,22 @@ export const RootLayout = () => {

const { profile, setProfile } = useProfileStore();

const { idProject, setIdProject } = useProjectStore();
//const { data : projects, isLoading: isloadingProjects } = useProjects();
const {data : projects, isLoading: isloadingProjects} = useProjectsByUser(userId);
useEffect(()=>{
if(projects && projects[0]){
setIdProject(projects[0].id_project);
}
},[projects, setIdProject])
const { setIdProject } = useProjectStore();

useEffect(()=> {
useEffect(()=> {
if(data){
//console.log("data is "+ JSON.stringify(data));
//console.log("data from bundled call is "+ JSON.stringify(data));
setProfile({
id_user: data.id_user,
email: data.email!,
first_name: data.first_name,
last_name: data.last_name,
projects: data.projects
//id_organization: data.id_organization as string,
})
//setUserId(data.id_user);
setIdProject(data.projects[0].id_project)
}
}, [data, setProfile]);
}, [data, setIdProject, setProfile]);


const handlePageChange = (page: string) => {
Expand All @@ -100,7 +92,7 @@ export const RootLayout = () => {
<img src="/logo.png" className='w-14' />
</Link>
</div>
<TeamSwitcher className='w-40 ml-3' />
<TeamSwitcher className='w-40 ml-3' userId={profile?.id_user!}/>
<MainNav
className='flex lg:flex-col mx-auto w-[200px] space-y-0'
onLinkClick={handlePageChange}
Expand Down
38 changes: 9 additions & 29 deletions apps/client-ts/src/components/shared/team-switcher.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,12 @@ import {
import useProjectMutation from "@/hooks/mutations/useProjectMutation"
import { useEffect, useState } from "react"
import useProjectStore from "@/state/projectStore"
import useOrganisationStore from "@/state/organisationStore"
import useProfileStore from "@/state/profileStore"
import useProjects from "@/hooks/useProjects"
import { Skeleton } from "../ui/skeleton"
//import useOrganisations from "@/hooks/useOrganisations"
import { zodResolver } from "@hookform/resolvers/zod"
import { useForm } from "react-hook-form"
import * as z from "zod"
import config from "@/lib/config"
import useProjectsByUser from "@/hooks/useProjectsByUser"
import { Skeleton } from "@/components/ui/skeleton";
import useProfileStore from "@/state/profileStore"


const projectFormSchema = z.object({
Expand All @@ -69,41 +65,25 @@ const projectFormSchema = z.object({

type PopoverTriggerProps = React.ComponentPropsWithoutRef<typeof PopoverTrigger>

interface TeamSwitcherProps extends PopoverTriggerProps {}
interface TeamSwitcherProps extends PopoverTriggerProps {
userId: string;
}

interface ModalObj {
open: boolean;
status?: number; // 0 for org, 1 for project
}

export default function TeamSwitcher({ className }: TeamSwitcherProps) {
export default function TeamSwitcher({ className, userId }: TeamSwitcherProps) {
const [open, setOpen] = useState(false)
const [userId, setUserId] = useState("");

const [showNewDialog, setShowNewDialog] = useState<ModalObj>({
open: false,
})

//const { data : orgs, isLoading: isloadingOrganisations } = useOrganisations();
//const { data : projects, isLoading: isloadingProjects } = useProjects();

const { idProject, setIdProject } = useProjectStore();

const { profile } = useProfileStore();
const {data : projects, isLoading: isloadingProjects} = useProjectsByUser(profile?.id_user!);


useEffect(()=>{
if(projects && projects[0]){
setIdProject(projects[0].id_project);
}
},[projects, setIdProject])

/*useEffect(() => {
if(profile && profile.id_user){
setUserId(profile.id_user)
}
}, [profile])*/
const projects = profile?.projects;

const handleOpenChange = (open: boolean) => {
setShowNewDialog(prevState => ({ ...prevState, open }));
Expand Down Expand Up @@ -139,7 +119,7 @@ export default function TeamSwitcher({ className }: TeamSwitcherProps) {
aria-label="Select a team"
className={cn("w-[250px] justify-between", className)}
>
{projects && projects.length > 0 ? projects[0].name : isloadingProjects ? <Skeleton className="w-[100px] h-[20px] rounded-md" /> : "No projects found"}
{projects && projects.length > 0 ? projects[0].name : "No projects found"}
<CaretSortIcon className="ml-auto h-4 w-4 shrink-0 opacity-50" />
</Button>
</PopoverTrigger>
Expand All @@ -150,7 +130,7 @@ export default function TeamSwitcher({ className }: TeamSwitcherProps) {
<CommandEmpty>Not found.</CommandEmpty>
<CommandGroup key={"projects"} heading={"Projects"}>
{
!isloadingProjects && projects && projects.length > 0 ? projects.map((project) => (
projects && projects.length > 0 ? projects.map((project) => (
<CommandItem
key={project.id_project}
onSelect={() => {
Expand Down
3 changes: 2 additions & 1 deletion apps/client-ts/src/hooks/useProfile.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import config from '@/lib/config';
import { useQuery } from '@tanstack/react-query';
import { users as User } from 'api';
import { projects as Project } from 'api';

const useProfile = (stytchUserId: string) => {
return useQuery({
queryKey: ['profile', stytchUserId],
queryFn: async (): Promise<User> => {
queryFn: async (): Promise<User & {projects: Project[]}> => {
const response = await fetch(`${config.API_URL}/auth/users/${stytchUserId}`);
if (!response.ok) {
throw new Error('Network response was not ok');
Expand Down
10 changes: 6 additions & 4 deletions apps/client-ts/src/state/profileStore.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { create } from 'zustand';
import { projects as Project } from 'api';

interface User {
type User_ = User & { projects: Project[] };
type User = {
id_user: string;
email: string;
first_name: string;
Expand All @@ -9,14 +11,14 @@ interface User {
}

interface ProfileState {
profile: User | null;
setProfile: (profile: User) => void;
profile: User_ | null;
setProfile: (profile: User_) => void;
}


const useProfileStore = create<ProfileState>()((set) => ({
profile: null,
setProfile: (profile_: User) => set({ profile: profile_ }),
setProfile: (profile_: User_) => set({ profile: profile_ }),
}));

export default useProfileStore;
24 changes: 15 additions & 9 deletions packages/api/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,11 @@ model webhooks_reponses {

/// This model or at least one of its fields has comments in the database, and requires an additional setup for migrations: Read more: https://pris.ly/d/database-comments
model connection_strategies {
id_connection_strategy String @id(map: "pk_connection_strategies") @db.Uuid
id_connection_strategy String @id(map: "pk_connection_strategies") @db.Uuid
status Boolean
type String
id_project String? @db.Uuid
id_project String? @db.Uuid
cs_entities cs_entities[]
}

model tcg_collections {
Expand Down Expand Up @@ -170,9 +171,9 @@ model connections {
expiration_timestamp DateTime? @db.Timestamp(6)
created_at DateTime @db.Timestamp(6)
connection_token String?
vertical String
id_project String @db.Uuid
id_linked_user String @db.Uuid
vertical String?
linked_users linked_users @relation(fields: [id_linked_user], references: [id_linked_user], onDelete: NoAction, onUpdate: NoAction, map: "fk_11")
projects projects @relation(fields: [id_project], references: [id_project], onDelete: NoAction, onUpdate: NoAction, map: "fk_9")
Expand Down Expand Up @@ -413,21 +414,26 @@ model crm_users {
}

model cs_attributes {
id_cs_attribute String @id(map: "pk_ct_attributes") @db.Uuid
id_cs_attribute String @id(map: "pk_ct_attributes") @db.Uuid
attribute_slug String
data_type String
id_cs_entity String @db.Uuid
id_cs_entity String @db.Uuid
cs_entities cs_entities @relation(fields: [id_cs_entity], references: [id_cs_entity], onDelete: NoAction, onUpdate: NoAction, map: "fk_cs_entity")
cs_values cs_values[]
}

model cs_entities {
id_cs_entity String @id(map: "pk_ct_entities") @db.Uuid
id_connection_strategy String @db.Uuid
id_cs_entity String @id(map: "pk_ct_entities") @db.Uuid
id_connection_strategy String @db.Uuid
cs_attributes cs_attributes[]
connection_strategies connection_strategies @relation(fields: [id_connection_strategy], references: [id_connection_strategy], onDelete: NoAction, onUpdate: NoAction, map: "fk_cs_id")
}

model cs_values {
id_cs_value String @id(map: "pk_ct_values") @db.Uuid
id_cs_value String @id(map: "pk_ct_values") @db.Uuid
value String
id_cs_attribute String @db.Uuid
id_cs_attribute String @db.Uuid
cs_attributes cs_attributes @relation(fields: [id_cs_attribute], references: [id_cs_attribute], onDelete: NoAction, onUpdate: NoAction, map: "fk_cs_attribute")
}

/// This model or at least one of its fields has comments in the database, and requires an additional setup for migrations: Read more: https://pris.ly/d/database-comments
Expand Down
11 changes: 10 additions & 1 deletion packages/api/src/@core/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,21 @@ export class AuthService {
}
async getUserByStytchId(stytchId: string) {
try {
return await this.prisma.users.findUnique({
const user = await this.prisma.users.findUnique({
where: {
id_stytch: stytchId,
identification_strategy: 'b2c',
},
});
const projects = await this.prisma.projects.findMany({
where: {
id_user: user.id_user,
},
});
return {
...user,
projects: projects,
};
} catch (error) {
handleServiceError(error, this.logger);
}
Expand Down
24 changes: 24 additions & 0 deletions packages/api/swagger/swagger-spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,30 @@
]
}
},
"/projects/{userId}": {
"get": {
"operationId": "getProjectsByUser",
"summary": "Retrieve projects by user",
"parameters": [
{
"name": "userId",
"required": true,
"in": "path",
"schema": {
"type": "string"
}
}
],
"responses": {
"200": {
"description": ""
}
},
"tags": [
"projects"
]
}
},
"/projects/create": {
"post": {
"operationId": "createProject",
Expand Down

0 comments on commit 9bb86dc

Please sign in to comment.