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<*>} * @returns {Promise<*>}
*/ */
export async function getAllMembers(req: Request, res: Response): Promise<any> { 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 { export default abstract class MemberService {
/** /**
* @description get all members * @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 return await dataSource
.getRepository(member) .getRepository(member)
.createQueryBuilder("member") .createQueryBuilder("member")
@ -24,7 +24,9 @@ export default abstract class MemberService {
"membership_last", "membership_last",
"membership_last.memberId = member.id AND membership_last.start = (SELECT MAX(m.start) FROM membership m WHERE m.memberId = member.id)" "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) => { .then((res) => {
return res; return res;
}) })
@ -44,25 +46,15 @@ export default abstract class MemberService {
.createQueryBuilder("member") .createQueryBuilder("member")
.leftJoinAndMapOne( .leftJoinAndMapOne(
"member.firstMembershipEntry", "member.firstMembershipEntry",
(subQuery) => { "member.memberships",
return subQuery "membership_first",
.from(membership, "membership") "membership_first.memberId = member.id AND membership_first.start = (SELECT MIN(m.start) FROM membership m WHERE m.memberId = member.id)"
.where("membership.memberId = member.id")
.orderBy("membership.date", "ASC")
.limit(1);
},
"membership_first"
) )
.leftJoinAndMapOne( .leftJoinAndMapOne(
"member.lastMembershipEntry", "member.lastMembershipEntry",
(subQuery) => { "member.memberships",
return subQuery "membership_last",
.from(membership, "membership") "membership_last.memberId = member.id AND membership_last.start = (SELECT MAX(m.start) FROM membership m WHERE m.memberId = member.id)"
.where("membership.memberId = member.id")
.orderBy("membership.date", "DESC")
.limit(1);
},
"membership_last"
) )
.where("member.id = :id", { id: id }) .where("member.id = :id", { id: id })
.getOneOrFail() .getOneOrFail()