-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from Divyateja04/adminfunction1
Adminfunction1
- Loading branch information
Showing
10 changed files
with
495 additions
and
171 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import { Router } from "express"; | ||
import { HttpCodes } from "../types/HttpCodes"; | ||
import { CustomResponse } from "../types/CustomResponse"; | ||
import { getAllUsers, promoteUser,demoteUser } from "../service/admin.service"; | ||
import prisma from '../db' | ||
export const adminRouter = Router(); | ||
|
||
adminRouter.get("/home", async(req,res)=>{ | ||
let currUserEmail = req.session.email; | ||
let currUser; | ||
try { | ||
currUser = await prisma.user.findUnique({ | ||
where: { | ||
email: currUserEmail | ||
}, | ||
}); | ||
}catch(error){ | ||
console.log(error); | ||
} | ||
|
||
if(currUser?.role==="admin"){ | ||
const appUsers = await getAllUsers(); | ||
|
||
if (appUsers.error) { | ||
const response: CustomResponse = { | ||
error: true, | ||
message: "Error retrieving users", | ||
data: null | ||
} | ||
return res.status(HttpCodes.INTERNAL_SERVER_ERROR).json(response); | ||
} | ||
|
||
if (typeof appUsers.data != "string") { | ||
// const normalUsers = appUsers?.data?.filter((u: any) => u.role !== 'admin'); | ||
|
||
const response: CustomResponse = { | ||
error: false, | ||
message: "All users retrieved successfully", | ||
data: appUsers.data | ||
} | ||
|
||
return res.status(HttpCodes.OK).json(response); | ||
} else { | ||
return res.status(HttpCodes.INTERNAL_SERVER_ERROR).json(appUsers.data); | ||
} | ||
}else{ | ||
return res.status(HttpCodes.UNAUTHORIZED); | ||
} | ||
}) | ||
|
||
adminRouter.put("/promote/:user", async(req, res) => { | ||
const userId = req.params.user; | ||
const pro = await promoteUser(userId); | ||
|
||
if(pro.error){ | ||
const response: CustomResponse = { | ||
error: true, | ||
message: "Error retrieving users", | ||
data: null | ||
} | ||
return res.status(HttpCodes.INTERNAL_SERVER_ERROR).json(response); | ||
}else{ | ||
const response: CustomResponse = { | ||
error: false, | ||
message: "selected user is now an admin", | ||
data: pro | ||
} | ||
|
||
return res.status(HttpCodes.OK).json(response); | ||
} | ||
}) | ||
|
||
|
||
adminRouter.put("/demote/:user", async(req, res) => { | ||
const userId = req.params.user; | ||
const pro = await demoteUser(userId); | ||
|
||
if(pro.error){ | ||
const response: CustomResponse = { | ||
error: true, | ||
message: "Error retrieving users", | ||
data: null | ||
} | ||
return res.status(HttpCodes.INTERNAL_SERVER_ERROR).json(response); | ||
}else{ | ||
const response: CustomResponse = { | ||
error: false, | ||
message: "selected user is demoted from admin role to user only role", | ||
data: pro | ||
} | ||
|
||
return res.status(HttpCodes.OK).json(response); | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { User } from '@prisma/client' | ||
import { CustomReturn } from '../types/CustomReturn' | ||
import prisma from '../db' | ||
import { logger } from '../utils/logger' | ||
|
||
export const getAllUsers = async (): Promise<CustomReturn<User[]>> => { | ||
try { | ||
let allUsers: User[] = await prisma.user.findMany(); | ||
return { | ||
error: false, | ||
data: allUsers | ||
} | ||
} | ||
catch (error) { | ||
return { | ||
error: true, | ||
data: "Some error occurred while fetching the users" | ||
} | ||
} | ||
} | ||
|
||
export const promoteUser = async(userId: string): Promise<CustomReturn<User>> => { | ||
try{ | ||
const updatedUser = await prisma.user.update({ | ||
where: { email: userId }, | ||
data: { role: 'admin' } // Assuming 'role' is the field representing user roles | ||
}); | ||
|
||
return { | ||
error: false, | ||
data: updatedUser | ||
} | ||
} catch(err: any) { | ||
logger.error(JSON.stringify({ | ||
location: "promoteUser", | ||
message: err.toString() | ||
})); | ||
return { | ||
error: true, | ||
data: "Some error occurred while promoting user to admin role" | ||
} | ||
} | ||
} | ||
|
||
export const demoteUser = async(userId: string): Promise<CustomReturn<User>> => { | ||
try{ | ||
const updatedUser = await prisma.user.update({ | ||
where: { email: userId }, | ||
data: { role: 'user' } // Assuming 'role' is the field representing user roles | ||
}); | ||
|
||
return { | ||
error: false, | ||
data: updatedUser | ||
} | ||
} catch(err: any) { | ||
logger.error(JSON.stringify({ | ||
location: "demoteUser", | ||
message: err.toString() | ||
})); | ||
return { | ||
error: true, | ||
data: "Some error occurred while demoting admin to user role" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import { describe, expect } from "@jest/globals"; | ||
import { prismaMock } from "./_mockdb"; | ||
import { User } from ".prisma/client"; | ||
import { getAllUsers, promoteUser, demoteUser } from "../src/service/admin.service"; | ||
|
||
const user: User = { | ||
id: "1", | ||
name: "ben", | ||
email : "[email protected]", | ||
phoneNumber : "9898989898", | ||
karmaPoints: 0, | ||
role: "admin" | ||
} | ||
|
||
const a: User = { | ||
id: "2", | ||
name: "ben", | ||
email : "[email protected]", | ||
phoneNumber : "9898989898", | ||
karmaPoints: 0, | ||
role: "admin" | ||
} | ||
|
||
const na: User = { | ||
id: "2", | ||
name: "ben", | ||
email : "[email protected]", | ||
phoneNumber : "9898989898", | ||
karmaPoints: 0, | ||
role: "user" | ||
} | ||
|
||
|
||
describe("Get all users", () => { | ||
it("should get all users", () => { | ||
prismaMock.user.findMany.mockResolvedValue([user]); | ||
|
||
expect(getAllUsers()).resolves.toEqual({ | ||
error: false, | ||
data: [user] | ||
}); | ||
}); | ||
|
||
it("should catch any error occurred", () => { | ||
prismaMock.user.findMany.mockRejectedValue(new Error("Some error occurred")); | ||
|
||
expect(getAllUsers()).resolves.toEqual({ | ||
error: true, | ||
data: "Some error occurred while fetching the users" | ||
}); | ||
}); | ||
}) | ||
|
||
describe("promote user to admin", () => { | ||
it("should promote user to admin role", () => { | ||
prismaMock.user.findUnique.mockResolvedValue(na); | ||
prismaMock.user.update.mockResolvedValue(a); | ||
|
||
expect(promoteUser(na.email)).resolves.toEqual({ | ||
error: false, | ||
data: a | ||
}) | ||
}) | ||
|
||
it("should return error if any error occured", () => { | ||
prismaMock.user.findUnique.mockResolvedValue(na); | ||
prismaMock.user.update.mockResolvedValue(a); | ||
|
||
prismaMock.post.create.mockRejectedValue(new Error("Some error occurred")); | ||
|
||
expect(promoteUser(na.email)).resolves.toEqual({ | ||
error: true, | ||
data: "Some error occurred while promoting user to admin role" | ||
}) | ||
}) | ||
}) | ||
|
||
describe("demote admin to user", () => { | ||
it("should demote admin to user", () => { | ||
prismaMock.user.findUnique.mockResolvedValue(a); | ||
prismaMock.user.update.mockResolvedValue(na); | ||
|
||
expect(demoteUser(a.email)).resolves.toEqual({ | ||
error: false, | ||
data: na | ||
}) | ||
}) | ||
|
||
it("should return error if any error occured", () => { | ||
prismaMock.user.findUnique.mockResolvedValue(a); | ||
prismaMock.user.update.mockResolvedValue(na); | ||
|
||
prismaMock.post.create.mockRejectedValue(new Error("Some error occurred")); | ||
|
||
expect(demoteUser(a.email)).resolves.toEqual({ | ||
error: true, | ||
data: "Some error occurred while demoting admin to user role" | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.