member pagination

This commit is contained in:
Julian Krauser 2024-09-16 18:00:00 +02:00
parent 5b678f6db6
commit 04bab0c97e
2 changed files with 20 additions and 21 deletions

View file

@ -19,9 +19,16 @@ import CommunicationFactory from "../../factory/admin/communication";
* @returns {Promise<*>}
*/
export async function getAllMembers(req: Request, res: Response): Promise<any> {
let members = await MemberService.getAll();
let offset = parseInt((req.query.offset as string) ?? "0");
let count = parseInt((req.query.count as string) ?? "25");
let [members, total] = await MemberService.getAll(offset, count);
res.json(MemberFactory.mapToBase(members));
res.json({
members: MemberFactory.mapToBase(members),
total: total,
offset: offset,
count: count,
});
}
/**

View file

@ -6,9 +6,9 @@ import InternalException from "../exceptions/internalException";
export default abstract class MemberService {
/**
* @description get all members
* @returns {Promise<Array<member>>}
* @returns {Promise<[Array<member>, number]>}
*/
static async getAll(): Promise<Array<member>> {
static async getAll(offset: number = 0, count: number = 25): Promise<[Array<member>, number]> {
return await dataSource
.getRepository(member)
.createQueryBuilder("member")
@ -24,7 +24,9 @@ export default abstract class MemberService {
"membership_last",
"membership_last.memberId = member.id AND membership_last.start = (SELECT MAX(m.start) FROM membership m WHERE m.memberId = member.id)"
)
.getMany()
.offset(offset)
.limit(count)
.getManyAndCount()
.then((res) => {
return res;
})
@ -44,25 +46,15 @@ export default abstract class MemberService {
.createQueryBuilder("member")
.leftJoinAndMapOne(
"member.firstMembershipEntry",
(subQuery) => {
return subQuery
.from(membership, "membership")
.where("membership.memberId = member.id")
.orderBy("membership.date", "ASC")
.limit(1);
},
"membership_first"
"member.memberships",
"membership_first",
"membership_first.memberId = member.id AND membership_first.start = (SELECT MIN(m.start) FROM membership m WHERE m.memberId = member.id)"
)
.leftJoinAndMapOne(
"member.lastMembershipEntry",
(subQuery) => {
return subQuery
.from(membership, "membership")
.where("membership.memberId = member.id")
.orderBy("membership.date", "DESC")
.limit(1);
},
"membership_last"
"member.memberships",
"membership_last",
"membership_last.memberId = member.id AND membership_last.start = (SELECT MAX(m.start) FROM membership m WHERE m.memberId = member.id)"
)
.where("member.id = :id", { id: id })
.getOneOrFail()