Skip to content

Commit

Permalink
Merge pull request #23 from Divyateja04/NoWay
Browse files Browse the repository at this point in the history
Admin inc/dec user karma
  • Loading branch information
thenicekat authored Apr 24, 2024
2 parents 709a4d2 + bb9da5e commit 25001d1
Show file tree
Hide file tree
Showing 5 changed files with 180 additions and 3 deletions.
26 changes: 25 additions & 1 deletion backend/src/routes/admin.route.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Router } from "express";
import { HttpCodes } from "../types/HttpCodes";
import { CustomResponse } from "../types/CustomResponse";
import { getAllUsers, promoteUser, demoteUser, banUser } from "../service/admin.service";
import { getAllUsers, promoteUser, demoteUser, banUser, deltaKarma} from "../service/admin.service";
import prisma from '../db'
import { getAllPosts } from "../service/posts.service";
export const adminRouter = Router();
Expand Down Expand Up @@ -158,6 +158,30 @@ adminRouter.put("/ban/:user", async (req, res) => {
data: ban
}

return res.status(HttpCodes.OK).json(response);
}
})

adminRouter.put("/deltaKarma/:user", async (req, res) => {
const userEmailId = req.params.user;
const amount= req.body.amount as number;
const sign= req.body.sign as boolean;
const fruit = await deltaKarma(userEmailId,amount,sign);

if (fruit.error) {
const response: CustomResponse = {
error: true,
message: fruit.data as string,
data: null
}
return res.status(HttpCodes.INTERNAL_SERVER_ERROR).json(response);
} else {
const response: CustomResponse = {
error: false,
message: "Selected user's karma has been updated",
data: fruit
}

return res.status(HttpCodes.OK).json(response);
}
})
70 changes: 70 additions & 0 deletions backend/src/service/admin.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,74 @@ export const banUser = async (userEmailId: string): Promise<CustomReturn<User>>
data: "Some error occurred while banning user."
}
}
}

//changes Karma of a user
//use sign=false to decrease sign=true to increase
export const deltaKarma =async (userEmailId: string, amount:number, sign:boolean):
Promise<CustomReturn<User>> => {
try {
let user = await prisma.user.findUnique({
where: {
email: userEmailId
}
});

if (!user) return {
error: true,
data: "User does not exist."
}

if (user.role == "admin") {
return {
error: true,
data: "Admin karma cannot be updated."
}
}

if (sign==false) {
if(user.karmaPoints<amount){
return{
error:true,
data:"Stop, Stop! he is already dead."
}
}
const punishedUser= await prisma.user.update({
where: { email: userEmailId },
data: {
karmaPoints: {
decrement: amount
}
}
})
return {
error:false,
data:punishedUser
}
}
else {
const cherishedUser= await prisma.user.update({
where: { email: userEmailId },
data: {
karmaPoints: {
increment: amount
}
}
})
return {
error:false,
data:cherishedUser
}
}
}
catch (err: any) {
logger.error(JSON.stringify({
location: "deltaKarma",
message: err.toString()
}));
return {
error: true,
data: "Some error occurred while changing karma."
}
}
}
Empty file.
84 changes: 82 additions & 2 deletions backend/tests/admin.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { describe, expect } from "@jest/globals";
import { prismaMock } from "./_mockdb";
import { User } from ".prisma/client";
import { getAllUsers, promoteUser, demoteUser, banUser } from "../src/service/admin.service";
import { getAllUsers, promoteUser, demoteUser, banUser, deltaKarma} from "../src/service/admin.service";

const user: User = {
id: "1",
Expand Down Expand Up @@ -33,7 +33,17 @@ const notadmin: User = {
isPublic: true
}

const bu: User = {
const na2: User = {
id: "3",
name: "ben",
email: "[email protected]",
phoneNumber: "9898989898",
karmaPoints: 0,
role: "user",
isPublic: true
}

const bu: User= {
id: "2",
name: "ben",
email: "[email protected]",
Expand All @@ -43,6 +53,16 @@ const bu: User = {
isPublic: true
}

const iku: User = {
id: "2",
name: "ben",
email: "[email protected]",
phoneNumber: "9898989898",
karmaPoints: 12,
role: "user",
isPublic: true
}

describe("Get all users", () => {
it("should get all users", () => {
prismaMock.user.findMany.mockResolvedValue([user]);
Expand Down Expand Up @@ -90,6 +110,7 @@ describe("demote admin to user", () => {
prismaMock.user.findUnique.mockResolvedValue(admin);
prismaMock.user.update.mockResolvedValue(notadmin);


expect(demoteUser(user.email, admin.email)).resolves.toEqual({
error: false,
data: notadmin
Expand All @@ -100,6 +121,7 @@ describe("demote admin to user", () => {
prismaMock.user.findUnique.mockResolvedValue(admin);
prismaMock.user.update.mockRejectedValue(new Error("Some error occurred"));


expect(demoteUser(user.email, admin.email)).resolves.toEqual({
error: true,
data: "Some error occurred while demoting admin to user role"
Expand Down Expand Up @@ -145,4 +167,62 @@ describe("ban user", () => {
data: "Some error occurred while banning user."
})
})
})

describe("increase/decrease user karma", () => {
it("should increase karma", () => {
prismaMock.user.findUnique.mockResolvedValue(na);
prismaMock.user.update.mockResolvedValue(iku);

expect(deltaKarma(na.email,12,true)).resolves.toEqual({
error: false,
data: iku
})
})

it("should decrease karma", () => {
prismaMock.user.findUnique.mockResolvedValue(iku);
prismaMock.user.update.mockResolvedValue(na);

expect(deltaKarma(iku.email,12,false)).resolves.toEqual({
error: false,
data: na
})
})

it("should return error if target karma is negative", () => {
prismaMock.user.findUnique.mockResolvedValue(na);

expect(deltaKarma(na.email,12,false)).resolves.toEqual({
error: true,
data: "Stop, Stop! he is already dead."
})
})

it("should return an error if user does not exist", () => {

expect(banUser("[email protected]")).resolves.toEqual({
error: true,
data: "User does not exist."
})
})

it("should return error if user to be updated is an admin", () => {
prismaMock.user.findUnique.mockResolvedValue(a);

expect(deltaKarma(a.email,12,true)).resolves.toEqual({
error: true,
data: "Admin karma cannot be updated."
})
})

it("should return error if any error occured", () => {
prismaMock.user.findUnique.mockResolvedValue(na);
prismaMock.user.update.mockRejectedValue(new Error("Some error occurred"));

expect(deltaKarma(na.email,12,true)).resolves.toEqual({
error: true,
data: "Some error occurred while changing karma."
})
})
})
3 changes: 3 additions & 0 deletions backend/tests/user.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { prismaMock } from "./_mockdb";
import { getUserByEmail, newUser, getUserById } from "../src/service/user.service";
import { updateUser } from "../src/service/user.service";



describe("Create a new user", () => {
it("should create a new user", () => {
const user = {
Expand Down Expand Up @@ -269,6 +271,7 @@ describe("Fetch user data using id", () => {

prismaMock.user.findUnique.mockResolvedValue(user);


expect(getUserById(user.id, user.email)).resolves.toEqual({
error: true,
data: "User is not public."
Expand Down

0 comments on commit 25001d1

Please sign in to comment.