Skip to content
This repository has been archived by the owner on Apr 19, 2023. It is now read-only.

Commit

Permalink
✨ Add pagination to data
Browse files Browse the repository at this point in the history
  • Loading branch information
AnandChowdhary committed Jun 1, 2019
1 parent 23144aa commit 207c662
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 24 deletions.
12 changes: 9 additions & 3 deletions src/controllers/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,9 @@ export class UserController {
{ id: [Joi.string().required(), Joi.number().required()] },
{ id }
);
res.json(await getRecentEventsForUser(res.locals.token.id, id));
res.json(
await getRecentEventsForUser(res.locals.token.id, id, req.query.start)
);
}

@Get(":id/memberships")
Expand All @@ -129,7 +131,9 @@ export class UserController {
{ id: [Joi.string().required(), Joi.number().required()] },
{ id }
);
res.json(await getMembershipsForUser(res.locals.token.id, id));
res.json(
await getMembershipsForUser(res.locals.token.id, id, req.query.start)
);
}

@Get(":id/data")
Expand Down Expand Up @@ -230,7 +234,9 @@ export class UserController {
{ id: [Joi.string().required(), Joi.number().required()] },
{ id }
);
res.json(await getAllEmailsForUser(res.locals.token.id, id));
res.json(
await getAllEmailsForUser(res.locals.token.id, id, req.query.start)
);
}

@Put(":id/emails")
Expand Down
12 changes: 1 addition & 11 deletions src/crud/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,9 @@ export const getPaginatedData = async (
} ORDER BY ${primaryKey} ASC LIMIT ${itemsPerPage}`,
[index, ...(conditions ? Object.values(conditions) : [])]
)) as any[];
console.log(
`SELECT * FROM \`${table}\` WHERE ${primaryKey} > ? ${
conditions
? `AND ${Object.keys(conditions)
.map(condition => `${condition} = ?`)
.join(" AND ")}`
: ""
} ORDER BY ${primaryKey} ASC LIMIT ${itemsPerPage}`,
[index, ...(conditions ? Object.values(conditions) : [])]
);
return {
data,
hasMore: data.length === itemsPerPage,
next: data.length && data[data.length - 1][primaryKey]
next: data.length === itemsPerPage && data[data.length - 1][primaryKey]
};
};
24 changes: 24 additions & 0 deletions src/crud/membership.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,30 @@ export const getUserMemberships = async (user: User | number) => {
);
};

/**
* Add organization details to membership
*/
export const addOrganizationToMembership = async (membership: Membership) => {
(membership as any).organization = await getOrganization(
membership.organizationId
);
return membership;
};

/**
* Add organization details to memberships
*/
export const addOrganizationToMemberships = async (
memberships: Membership[]
) => {
for await (const membership of memberships) {
(membership as any).organization = await getOrganization(
membership.organizationId
);
}
return memberships;
};

/**
* Get a detailed object of a user's membership
*/
Expand Down
18 changes: 15 additions & 3 deletions src/rest/email.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,25 @@ import {
import { updateUser } from "../crud/user";
import { can } from "../helpers/authorization";
import { validate } from "../helpers/utils";
import { getPaginatedData } from "../crud/data";
import { addIsPrimaryToEmails } from "../helpers/mysql";

export const getAllEmailsForUser = async (
tokenUserId: number,
userId: number
userId: number,
index?: number,
itemsPerPage?: number
) => {
if (await can(tokenUserId, Authorizations.READ, "user", userId))
return await getUserEmails(userId);
if (await can(tokenUserId, Authorizations.READ, "user", userId)) {
const emails = await getPaginatedData(
"emails",
{ userId },
index,
itemsPerPage
);
emails.data = await addIsPrimaryToEmails(emails.data);
return emails;
}
throw new Error(ErrorCode.INSUFFICIENT_PERMISSION);
};

Expand Down
33 changes: 26 additions & 7 deletions src/rest/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ import {
} from "../crud/user";
import {
deleteAllUserMemberships,
getUserMembershipsDetailed
getUserMembershipsDetailed,
addOrganizationToMemberships
} from "../crud/membership";
import { User } from "../interfaces/tables/user";
import { Locals, KeyValue } from "../interfaces/general";
Expand All @@ -39,6 +40,8 @@ import { authenticator } from "otplib";
import { toDataURL } from "qrcode";
import { SERVICE_2FA } from "../config";
import { compare } from "bcryptjs";
import { getPaginatedData } from "../crud/data";
import { addLocationToEvents } from "../helpers/location";

export const getUserFromId = async (userId: number, tokenUserId: number) => {
if (await can(tokenUserId, Authorizations.READ, "user", userId))
Expand Down Expand Up @@ -132,19 +135,35 @@ export const deleteUserForUser = async (

export const getRecentEventsForUser = async (
tokenUserId: number,
dataUserId: number
dataUserId: number,
index?: number
) => {
if (await can(tokenUserId, Authorizations.READ_SECURE, "user", dataUserId))
return await getUserRecentEvents(dataUserId);
if (await can(tokenUserId, Authorizations.READ_SECURE, "user", dataUserId)) {
const events = await getPaginatedData(
"events",
{ userId: dataUserId },
index
);
events.data = await addLocationToEvents(events.data);
return events;
}
throw new Error(ErrorCode.INSUFFICIENT_PERMISSION);
};

export const getMembershipsForUser = async (
tokenUserId: number,
dataUserId: number
dataUserId: number,
index?: number
) => {
if (await can(tokenUserId, Authorizations.READ, "user", dataUserId))
return await getUserMembershipsDetailed(dataUserId);
if (await can(tokenUserId, Authorizations.READ, "user", dataUserId)) {
const memberships = await getPaginatedData(
"memberships",
{ userId: dataUserId },
index
);
memberships.data = await addOrganizationToMemberships(memberships.data);
return memberships;
}
throw new Error(ErrorCode.INSUFFICIENT_PERMISSION);
};

Expand Down

0 comments on commit 207c662

Please sign in to comment.