Skip to content

Commit

Permalink
add: pagination and fetch contributions on date intervals
Browse files Browse the repository at this point in the history
  • Loading branch information
sandeshkhadka committed Apr 19, 2024
1 parent 5e9057b commit 1c9debf
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 6 deletions.
37 changes: 36 additions & 1 deletion src/handlers/contribution.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { NextFunction, Request, Response } from "express";
import prisma from "../db.js";
import CustomError from "../modules/errors.js";

export async function approveContribution(
req: Request,
res: Response,
next: NextFunction,
) {
// const userId = req.user!.id;
const username: string = req.user!.username;
const contributionId: string = req.params.id;

Expand Down Expand Up @@ -90,11 +90,15 @@ export async function getContributions(
res: Response,
next: NextFunction,
) {
const page = req.body.page;
const offset = (parseInt(page) - 1) * PAGE_SIZE;
try {
const contributions = await prisma.contribution.findMany({
orderBy: {
createdAt: "desc",
},
skip: offset,
take: PAGE_SIZE,
});

res.status(200);
Expand Down Expand Up @@ -129,7 +133,38 @@ export async function getContributionById(
}
}
}
export async function getContributionByDate(
req: Request,
res: Response,
next: NextFunction,
) {
const startDate = new Date(req.body.startDate).toISOString();
const endDate = new Date(req.body.endDate).toISOString();
try {
const contributions = await prisma.contribution.findMany({
where: {
createdAt: {
gte: startDate,
lte: endDate,
},
},
});

res.status(200);
res.json({ contributions });
} catch (err) {
if (err instanceof CustomError) {
next(err);
} else {
next(
new CustomError(
`Could not get contribution with within ${startDate} and ${endDate}`,
500,
),
);
}
}
}
export async function updateContibutions(
req: Request,
res: Response,
Expand Down
7 changes: 6 additions & 1 deletion src/handlers/task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,13 @@ export async function getTasks(
res: Response,
next: NextFunction,
) {
const page = req.body.page;
const offset = (parseInt(page) - 1) * PAGE_SIZE;
try {
const tasks = await prisma.routineTask.findMany();
const tasks = await prisma.routineTask.findMany({
take: PAGE_SIZE,
skip: offset,
});

res.status(200);
res.json({ tasks });
Expand Down
1 change: 1 addition & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { User } from "./modules/auth.ts";
declare global {
const PAGE_SIZE = 10;
namespace Express {
export interface Request {
user?: User;
Expand Down
26 changes: 22 additions & 4 deletions src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
approveContribution,
createContribution,
deleteContribuion,
getContributionByDate,
getContributionById,
getContributions,
updateContibutions,
Expand Down Expand Up @@ -39,8 +40,13 @@ const ContributionTypes = [
];
const router = Router();
router.use(errHandler);
router.get("/task", getTasks);
router.get("/contribution", getContributions);
router.post(
"/task",
body("page").exists().notEmpty().isNumeric(),
assertAllFieldsPresent,
getTasks,
);

router.get("/users", getAllUsers);
router.get("/rehydrate", rehydrate);
router.get("/task/:id", getTaskById);
Expand All @@ -49,16 +55,28 @@ router.get("/dashboard/contribution/distribution", getContributionDistribution);
router.get("/dashboard/task/pending", getPendingTasks);

router.get("/profile_picture/id/:id", getProfilePicture);
router.post(
"/bydate/contributions",
body("startDate").exists().notEmpty().isDate(),
body("endDate").exists().notEmpty().isDate(),
getContributionByDate,
);

router.post(
"/task",
"/create/task",
body("name").exists().notEmpty(),
body("assignToId").exists().notEmpty(),
assertAllFieldsPresent,
createTask,
);
router.post(
"/contribution",
"/contributions",
body("page").exists().notEmpty().isNumeric(),
assertAllFieldsPresent,
getContributions,
);
router.post(
"/create/contribution",
body(["name", "type", "amount"]).exists(),
body("type").isIn(ContributionTypes),
body("amount").isNumeric(),
Expand Down

0 comments on commit 1c9debf

Please sign in to comment.