Move pagination to common, add serialization, update BackendController

This commit is contained in:
Alice Gaudon 2021-06-01 16:04:43 +02:00
parent 7174097388
commit d925237233
5 changed files with 34 additions and 11 deletions

View File

@ -1,6 +1,9 @@
import {WrappingError} from "./Utils.js";
export class Pagination {
public static deserialize(str: string): Pagination {
const data = JSON.parse(str);
return new Pagination(data.page, data.perPage, data.totalCount);
}
export default class Pagination {
public readonly page: number;
public readonly perPage: number;
public readonly totalCount: number;
@ -80,9 +83,13 @@ export default class Pagination {
return pages;
}
public serialize(): string {
return JSON.stringify(this);
}
}
export class PageNotFoundError extends WrappingError {
export class PageNotFoundError extends Error {
public constructor(
public readonly page: number,
) {

View File

@ -1,7 +1,7 @@
import {Request} from "express";
import {PageNotFoundError} from "../common/Pagination.js";
import {NotFoundHttpError} from "../HttpError.js";
import {PageNotFoundError} from "../Pagination.js";
import Model, {ModelType} from "./Model.js";
import ModelComponent from "./ModelComponent.js";
import ModelQuery, {ModelQueryResult, QueryFields} from "./ModelQuery.js";

View File

@ -1,6 +1,6 @@
import {Connection} from "mysql";
import Pagination from "../Pagination.js";
import {Pagination} from "../common/Pagination.js";
import Model from "./Model.js";
import ModelFactory from "./ModelFactory.js";
import ModelRelation, {RelationDatabaseProperties} from "./ModelRelation.js";

View File

@ -6,6 +6,7 @@ import UserApprovedComponent from "../auth/models/UserApprovedComponent.js";
import UserEmail from "../auth/models/UserEmail.js";
import UserNameComponent from "../auth/models/UserNameComponent.js";
import {route} from "../common/Routing.js";
import {Time} from "../common/Time.js";
import MailComponent from "../components/MailComponent.js";
import Controller from "../Controller.js";
import ModelFactory from "../db/ModelFactory.js";
@ -45,7 +46,7 @@ export default class BackendController extends Controller {
this.get('/', this.getIndex, 'backend');
if (User.isApprovalMode()) {
this.get('/accounts-approval', this.getAccountApproval, 'accounts-approval');
this.get('/accounts-approval/:page?', this.getAccountApproval, 'accounts-approval');
this.post('/accounts-approval/approve', this.postApproveAccount, 'approve-account');
this.post('/accounts-approval/reject', this.postRejectAccount, 'reject-account');
}
@ -62,12 +63,16 @@ export default class BackendController extends Controller {
}
protected async getAccountApproval(req: Request, res: Response): Promise<void> {
const accounts = await User.select()
const accounts = await User.paginate(req, 1, User.select()
.where('approved', 0)
.with('mainEmail')
.get();
.with('mainEmail'));
res.render('backend/accounts_approval', {
accounts: accounts,
accounts: accounts.map(account => Object.assign({
mainEmailStr: account.mainEmail.getOrFail()?.email,
created_at_iso: account.created_at?.toISOString(),
created_at_human: account.created_at ? Time.humanizeTimeSince(account.created_at) : '',
}, account)),
pagination: accounts.pagination?.serialize(),
has_user_name_component: ModelFactory.get(User).hasComponent(UserNameComponent),
});
}

View File

@ -1,4 +1,4 @@
import Pagination from "../src/Pagination.js";
import Pagination from "../../src/common/Pagination.js";
describe('Pagination', () => {
const pagination = new Pagination(3, 5, 31);
@ -134,4 +134,15 @@ describe('Pagination', () => {
expect(pagination.nextPages(2)).toStrictEqual([91, 92, -1, 99, 100]);
expect(pagination.nextPages(3)).toStrictEqual([91, 92, 93, -1, 98, 99, 100]);
});
test('Should serialize properly', () => {
expect(new Pagination(5, 6, 1000).serialize()).toStrictEqual('{"page":5,"perPage":6,"totalCount":1000}');
});
test('Should deserialize properly', () => {
const pagination = Pagination.deserialize('{"page":5,"perPage":6,"totalCount":1000}');
expect(pagination.page).toStrictEqual(5);
expect(pagination.perPage).toStrictEqual(6);
expect(pagination.totalCount).toStrictEqual(1000);
});
});