-
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
35b7573
commit ec9a670
Showing
2 changed files
with
54 additions
and
0 deletions.
There are no files selected for viewing
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,51 @@ | ||
import Punishment from "../../models/Punishment"; | ||
import Controller from "../Controller"; | ||
import RequireAuth from "../middleware/RequireAuth"; | ||
import ValidatorError from "../middleware/ValidatorError"; | ||
import Request from "../Request"; | ||
|
||
export default class HistoryController extends Controller { | ||
globalMiddleware(): Function[] { | ||
return [RequireAuth, ValidatorError]; | ||
} | ||
|
||
async index(request: Request) { | ||
if (!request.user?.guilds.includes(request.params.id)) { | ||
return this.response({ error: "You don't have permission to access history of this guild." }, 403); | ||
} | ||
|
||
const queryLimit = parseInt((request.query.limit as string) ?? '0'); | ||
const limit = request.query.limit ? (queryLimit < 1 || queryLimit > 20 ? 20 : queryLimit) : 20; | ||
const maxPages = Math.ceil((await Punishment.count({ guild_id: request.params.id })) / limit); | ||
const page = request.query.page ? parseInt(request.query.page as string) : 1; | ||
|
||
if (maxPages < page) { | ||
return this.response({ error: "That page does not exist" }, 404); | ||
} | ||
|
||
const offset = (page - 1) * limit; | ||
|
||
const data = (await Punishment.find({ guild_id: request.params.id }).skip(offset).limit(limit)); | ||
const newData = []; | ||
|
||
for await (const row of data) { | ||
let user = { id: row.user_id }; | ||
|
||
try { | ||
user = this.client.users.cache.get(row.user_id) || await this.client.users.fetch(row.user_id); | ||
} | ||
catch (e) { | ||
console.log(e); | ||
} | ||
|
||
const newRow = { | ||
...(row.toJSON()), | ||
user | ||
}; | ||
|
||
newData.push(newRow); | ||
} | ||
|
||
return newData; | ||
} | ||
} |
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