Skip to content

Commit

Permalink
feat(api/applications): ✨ Application Pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
Nudelsuppe42 committed Jan 13, 2025
1 parent fb2c68b commit 70b77af
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 13 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"dash/me",
"frontend/auth",
"dash/components",
"frontend/seo"
"frontend/seo",
"api/applications"
]
}
46 changes: 34 additions & 12 deletions apps/api/src/controllers/ApplicationController.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Application, ApplicationQuestionType, ApplicationStatus } from '@repo/db';
import { Request, Response } from 'express';
import { sendBtWebhook, WebhookType } from '../util/BtWebhooks.js';
import { WebhookType, sendBtWebhook } from '../util/BtWebhooks.js';
import { ERROR_GENERIC, ERROR_NO_PERMISSION, ERROR_VALIDATION } from '../util/Errors.js';

import { validationResult } from 'express-validator';
Expand Down Expand Up @@ -30,17 +30,39 @@ class ApplicationController {

const onlyReview = req.query.review;

let applications = await this.core.getPrisma().application.findMany({
where: {
buildteam: req.query.slug ? { slug: req.params.id } : { id: req.params.id },
status: onlyReview ? { in: [ApplicationStatus.SEND, ApplicationStatus.REVIEWING] } : undefined,
},
include: {
user: { select: { id: true, username: true } },
},
});

res.send(applications);
if (req.query && req.query.page) {
let page = parseInt(req.query.page as string);
let take = parseInt((req.query.take || 10) as string);
let applications = await this.core.getPrisma().application.findMany({
skip: page * take,
take: take,
where: {
buildteam: req.query.slug ? { slug: req.params.id } : { id: req.params.id },
status: onlyReview ? { in: [ApplicationStatus.SEND, ApplicationStatus.REVIEWING] } : undefined,
},
include: {
user: { select: { id: true, username: true } },
},
});
let count = await this.core.getPrisma().application.count({
where: {
buildteam: req.query.slug ? { slug: req.params.id } : { id: req.params.id },
status: onlyReview ? { in: [ApplicationStatus.SEND, ApplicationStatus.REVIEWING] } : undefined,
},
});
res.send({ pages: Math.ceil(count / take), data: applications });
} else {
let applications = await this.core.getPrisma().application.findMany({
where: {
buildteam: req.query.slug ? { slug: req.params.id } : { id: req.params.id },
status: onlyReview ? { in: [ApplicationStatus.SEND, ApplicationStatus.REVIEWING] } : undefined,
},
include: {
user: { select: { id: true, username: true } },
},
});
res.send(applications);
}
}

/**
Expand Down
2 changes: 2 additions & 0 deletions apps/api/src/web/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,8 @@ class Routes {
},
param('id'),
query('review').isBoolean().optional(),
query('page').isNumeric().optional().default(0),
query('take').isNumeric().optional().default(10),
checkUserPermission(this.web.getCore().getPrisma(), 'team.application.list', 'id'),
);
router.addRoute(
Expand Down

0 comments on commit 70b77af

Please sign in to comment.