Skip to content

Commit

Permalink
Merge pull request #110 from WildCodeSchool/chore/106/enum-in-admin-r…
Browse files Browse the repository at this point in the history
…oles

chore : add enum in admin roles
  • Loading branch information
AntoniSDev authored Oct 10, 2024
2 parents 84a33f8 + 636db22 commit 363669e
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 7 deletions.
21 changes: 18 additions & 3 deletions backend/src/entities/user.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
import { BaseEntity, Column, Entity, OneToMany, PrimaryGeneratedColumn } from "typeorm";
import { Field, ObjectType } from "type-graphql";
import { Field, ObjectType, registerEnumType } from "type-graphql";
import { Reservation } from "./reservation";

export enum Role {
User = "user",
Admin = "admin",
}

// this is used to export the type to the frontend through GraphQL
registerEnumType(Role, {
name: 'Role',
description: 'user role',
})

@ObjectType() //typeGraphQl
@Entity() //typeORM
export class User extends BaseEntity {
Expand All @@ -12,17 +23,21 @@ export class User extends BaseEntity {
@Column({ unique: true })
email: string;

@Field()
@Column()
firstname: string;

@Field()
@Column()
lastname: string;

@Field()
@Column()
hashedPassword: string;

@Column({ default: "USER" })
role: string;
@Field(() => Role)
@Column({ default: Role.User})
role: Role;

@Field(() => [Reservation],{ nullable: true })
@OneToMany(() => Reservation, (reservation) => reservation.user)
Expand Down
4 changes: 2 additions & 2 deletions backend/src/resolvers/UserResolver.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Mutation, Arg, Query, Ctx, ObjectType, Field } from "type-graphql";
import argon2 from "argon2";
import { User } from "../entities/user";
import { Role, User } from "../entities/user";
import jwt from "jsonwebtoken";
import { Context } from "src";

Expand All @@ -19,7 +19,7 @@ class UserInfo {
lastname: string;

@Field({ nullable: true })
role: string;
role: Role;
}

class UserResolver {
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/components/Navbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
import { Input, Button, message} from "antd";
import { Link } from "react-router-dom";
import { UserContext } from "../components/Layout";
import { useLogoutLazyQuery } from "../generated/graphql-types";
import { Role, useLogoutLazyQuery } from "../generated/graphql-types";
import Logo from "../assets/logo.png";
import RangePicker from "./RangePicker";

Expand Down Expand Up @@ -85,7 +85,7 @@ function Navbar() {
>
Bonjour, {userInfo.firstname}
</p>
{userInfo.role === "admin" ? (
{userInfo.role === Role.Admin ? (
<Link
to="/admin"
className="mr-4"
Expand Down
10 changes: 10 additions & 0 deletions frontend/src/generated/graphql-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,20 @@ export type ReservationWithTotal = {
totalPrice: Scalars['Float']['output'];
};

/** user role */
export enum Role {
Admin = 'Admin',
User = 'User'
}

export type User = {
__typename?: 'User';
email: Scalars['String']['output'];
firstname: Scalars['String']['output'];
hashedPassword: Scalars['String']['output'];
lastname: Scalars['String']['output'];
reservations?: Maybe<Array<Reservation>>;
role: Role;
};

export type UserInfo = {
Expand Down

0 comments on commit 363669e

Please sign in to comment.