Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API endpoint - PUT: Change user from student to teacher #5

Open
sebgro98 opened this issue Oct 28, 2024 · 3 comments
Open

API endpoint - PUT: Change user from student to teacher #5

sebgro98 opened this issue Oct 28, 2024 · 3 comments
Assignees

Comments

@sebgro98
Copy link

sebgro98 commented Oct 28, 2024

Create a PUT endpoint with the routing ('/changeUserRole') that asynchronously calls a function called changeUserRole. The changeUserRole function should find the user by their ID in the database and update their role from STUDENT to TEACHER.

The prisma model looks like:

model User {
  id            Int       @id @default(autoincrement())
  email         String    @unique
  password      String
  role          Role      @default(STUDENT)
  profile       Profile?
  cohortId      Int?
  cohort        Cohort?   @relation(fields: [cohortId], references: [id])
  posts         Post[]
  deliveryLogs  DeliveryLog[]
}

The user API spec model looks like:

{
  token: "string",
  user: {
    id: 0,
    email: "string",
    cohortId: 0,
    role: "string",
    firstName: "string",
    lastName: "string",
    bio: "string",
    githubUrl: "string"
  }
}

The endpoint returns a response of 201 with the JSON-formatted user if the operation is successful and a response of 401 if the operation fails. The function also returns 401 if the logged in user attempting to change another user's role to teacher is not a teacher using validateTeacherRole which returns false if the logged in user is a student and true if the logged in user is a teacher.

app.put('/changeUserRole', async (req, res) => {
  const { userId } = req.body;
  if(!validateTeacherRole(UserID)){
     res.status(401).json({
      status: 'fail',
      message: "Only a teacher can change a student's role to teacher"
  }
  try {
    const updatedUser = await prisma.user.update({
      where: { id: userId },
      data: { role: 'TEACHER' }
    })
    res.status(201).json({
      status: 'success',
      data: {
        user: updatedUser
      }
    })
  } catch (err) {
    res.status(401).json({
      status: 'fail',
      message: err.message
    })
  }
})
@sebgro98
Copy link
Author

Behöver vi någon validation som kollar att det är en Teacher som gjort requesten?

@sebgro98
Copy link
Author

Approved

1 similar comment
@amos1969
Copy link

Approved

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants