diff --git a/src/controllers/user.ts b/src/controllers/user.ts index 5fcbd4627..0880a22c2 100644 --- a/src/controllers/user.ts +++ b/src/controllers/user.ts @@ -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") @@ -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") @@ -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") diff --git a/src/crud/data.ts b/src/crud/data.ts index 4a75d2a69..2c77dbfd8 100644 --- a/src/crud/data.ts +++ b/src/crud/data.ts @@ -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] }; }; diff --git a/src/crud/membership.ts b/src/crud/membership.ts index dc23beb32..4be596a2e 100644 --- a/src/crud/membership.ts +++ b/src/crud/membership.ts @@ -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 */ diff --git a/src/rest/email.ts b/src/rest/email.ts index e9c23badc..404db29e9 100644 --- a/src/rest/email.ts +++ b/src/rest/email.ts @@ -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); }; diff --git a/src/rest/user.ts b/src/rest/user.ts index 0eaf1fd58..d6665806c 100644 --- a/src/rest/user.ts +++ b/src/rest/user.ts @@ -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"; @@ -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)) @@ -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); };