From cc70fe7d7b5817005208c9285da91018e24d94c8 Mon Sep 17 00:00:00 2001 From: Just-a-Pirate Date: Tue, 23 Apr 2024 19:18:35 +0530 Subject: [PATCH 1/2] feat: add deltaKarma api --- backend/src/routes/admin.route.ts | 26 ++++++++++- backend/src/service/admin.service.ts | 68 ++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+), 1 deletion(-) diff --git a/backend/src/routes/admin.route.ts b/backend/src/routes/admin.route.ts index 81b40774..b4d60bab 100644 --- a/backend/src/routes/admin.route.ts +++ b/backend/src/routes/admin.route.ts @@ -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(); @@ -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); } }) \ No newline at end of file diff --git a/backend/src/service/admin.service.ts b/backend/src/service/admin.service.ts index b4077455..6aa2f16f 100644 --- a/backend/src/service/admin.service.ts +++ b/backend/src/service/admin.service.ts @@ -121,4 +121,72 @@ export const banUser = async (userEmailId: string): Promise> data: "Some error occurred while banning user." } } +} + +export const deltaKarma =async (userEmailId: string, amount:number, sign:boolean): +Promise> => { + 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: "An admin cannot be fruit :)." + } + } + + if (sign==false) { + if(user.karmaPoints Date: Wed, 24 Apr 2024 00:34:18 +0530 Subject: [PATCH 2/2] fix: tests --- backend/src/service/admin.service.ts | 6 +- backend/src/service/plot.service.ts | 0 backend/tests/admin.test.ts | 84 +++++++++++++++++++++++++++- backend/tests/user.test.ts | 8 ++- 4 files changed, 90 insertions(+), 8 deletions(-) create mode 100644 backend/src/service/plot.service.ts diff --git a/backend/src/service/admin.service.ts b/backend/src/service/admin.service.ts index 6aa2f16f..6769b4c0 100644 --- a/backend/src/service/admin.service.ts +++ b/backend/src/service/admin.service.ts @@ -123,6 +123,8 @@ export const banUser = async (userEmailId: string): Promise> } } +//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> => { try { @@ -140,7 +142,7 @@ Promise> => { if (user.role == "admin") { return { error: true, - data: "An admin cannot be fruit :)." + data: "Admin karma cannot be updated." } } @@ -148,7 +150,7 @@ Promise> => { if(user.karmaPoints { it("should get all users", () => { prismaMock.user.findMany.mockResolvedValue([user]); @@ -90,7 +110,7 @@ describe("demote admin to user", () => { prismaMock.user.findUnique.mockResolvedValue(a); prismaMock.user.update.mockResolvedValue(na); - expect(demoteUser(a.email)).resolves.toEqual({ + expect(demoteUser(a.email,na2.email)).resolves.toEqual({ error: false, data: na }) @@ -100,7 +120,7 @@ describe("demote admin to user", () => { prismaMock.user.findUnique.mockResolvedValue(a); prismaMock.user.update.mockRejectedValue(new Error("Some error occurred")); - expect(demoteUser(a.email)).resolves.toEqual({ + expect(demoteUser(a.email,na2.email)).resolves.toEqual({ error: true, data: "Some error occurred while demoting admin to user role" }) @@ -145,4 +165,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("random@random.com")).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." + }) + }) }) \ No newline at end of file diff --git a/backend/tests/user.test.ts b/backend/tests/user.test.ts index 59cb065a..6167674a 100644 --- a/backend/tests/user.test.ts +++ b/backend/tests/user.test.ts @@ -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 = { @@ -250,7 +252,7 @@ describe("Fetch user data using id", () => { prismaMock.user.findUnique.mockResolvedValue(user); - expect(getUserById(user.id)).resolves.toEqual({ + expect(getUserById(user.id,user.id)).resolves.toEqual({ error: false, data: user }); @@ -269,7 +271,7 @@ describe("Fetch user data using id", () => { prismaMock.user.findUnique.mockResolvedValue(user); - expect(getUserById(user.id)).resolves.toEqual({ + expect(getUserById(user.id,user.id)).resolves.toEqual({ error: true, data: "User is not public." }); @@ -287,7 +289,7 @@ describe("Fetch user data using id", () => { prismaMock.user.findUnique.mockRejectedValue(new Error("Some error")); - expect(getUserById(user.id)).resolves.toEqual({ + expect(getUserById(user.id,user.id)).resolves.toEqual({ error: true, data: null });