diff --git a/package.json b/package.json index 21bf03f18..6c82b045d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "staart", - "version": "1.0.5", + "version": "1.0.6", "main": "index.js", "repository": "git@github.com:AnandChowdhary/staart.git", "author": "Anand Chowdhary ", diff --git a/src/controllers/organization.ts b/src/controllers/organization.ts index fa76ac9a7..d3a154a32 100644 --- a/src/controllers/organization.ts +++ b/src/controllers/organization.ts @@ -300,7 +300,7 @@ export class OrganizationController { await getOrganizationMembershipsForUser( res.locals.token.id, organizationId, - req.query.start + req.query ) ); } diff --git a/src/controllers/user.ts b/src/controllers/user.ts index 0880a22c2..16d80d732 100644 --- a/src/controllers/user.ts +++ b/src/controllers/user.ts @@ -118,9 +118,7 @@ export class UserController { { id: [Joi.string().required(), Joi.number().required()] }, { id } ); - res.json( - await getRecentEventsForUser(res.locals.token.id, id, req.query.start) - ); + res.json(await getRecentEventsForUser(res.locals.token.id, id, req.query)); } @Get(":id/memberships") @@ -131,9 +129,7 @@ export class UserController { { id: [Joi.string().required(), Joi.number().required()] }, { id } ); - res.json( - await getMembershipsForUser(res.locals.token.id, id, req.query.start) - ); + res.json(await getMembershipsForUser(res.locals.token.id, id, req.query)); } @Get(":id/data") @@ -234,9 +230,7 @@ export class UserController { { id: [Joi.string().required(), Joi.number().required()] }, { id } ); - res.json( - await getAllEmailsForUser(res.locals.token.id, id, req.query.start) - ); + res.json(await getAllEmailsForUser(res.locals.token.id, id, req.query)); } @Put(":id/emails") diff --git a/src/crud/data.ts b/src/crud/data.ts index f74d04e78..b0c9d8eca 100644 --- a/src/crud/data.ts +++ b/src/crud/data.ts @@ -5,22 +5,42 @@ import { dateToDateTime } from "../helpers/utils"; /* * Get pagination data */ -export const getPaginatedData = async ( - table: string, - conditions?: KeyValue, - index = 0, +export const getPaginatedData = async ({ + table, + conditions, + start = 0, itemsPerPage = 5, - primaryKey = "id" -) => { + primaryKey = "id", + q, + search, + sort = "asc" +}: { + table: string; + conditions?: KeyValue; + start?: number; + itemsPerPage?: number; + primaryKey?: string; + q?: string; + search?: string; + sort?: string; +}) => { const data = (await query( - `SELECT * FROM \`${table}\` WHERE ${primaryKey} > ? ${ + `SELECT * FROM \`${table}\` WHERE ${primaryKey} ${ + sort === "asc" ? ">" : "<" + } ? ${ conditions ? `AND ${Object.keys(conditions) .map(condition => `${condition} = ?`) .join(" AND ")}` : "" - } ORDER BY ${primaryKey} ASC LIMIT ${itemsPerPage}`, - [index, ...(conditions ? Object.values(conditions) : [])] + }${ + q && search ? ` AND \`${search}\` LIKE "%?%"` : "" + } ORDER BY ${primaryKey} ${sort.toUpperCase()} LIMIT ${itemsPerPage}`, + [ + sort === "desc" && start == 0 ? 99999999999 : start, + ...(conditions ? Object.values(conditions) : []), + q + ] )) as any[]; return { data, diff --git a/src/crud/membership.ts b/src/crud/membership.ts index 4be596a2e..8333b9c1a 100644 --- a/src/crud/membership.ts +++ b/src/crud/membership.ts @@ -169,13 +169,13 @@ export const getOrganizationMembers = async (organizationId: number) => { */ export const getOrganizationMemberDetails = async ( organizationId: number, - start?: number + query?: KeyValue ) => { - const members: any = await getPaginatedData( - "memberships", - { organizationId }, - start - ); + const members: any = await getPaginatedData({ + table: "memberships", + conditions: { organizationId }, + ...query + }); for await (const member of members.data) { member.user = await getUser(member.userId); } diff --git a/src/rest/email.ts b/src/rest/email.ts index cbfa6968b..b2174d250 100644 --- a/src/rest/email.ts +++ b/src/rest/email.ts @@ -1,4 +1,4 @@ -import { Locals } from "../interfaces/general"; +import { Locals, KeyValue } from "../interfaces/general"; import { createEmail, getEmail, @@ -18,16 +18,14 @@ import { addIsPrimaryToEmails } from "../helpers/mysql"; export const getAllEmailsForUser = async ( tokenUserId: number, userId: number, - index?: number, - itemsPerPage?: number + query: KeyValue ) => { if (await can(tokenUserId, Authorizations.READ, "user", userId)) { - const emails = await getPaginatedData( - "emails", - { userId }, - index, - itemsPerPage - ); + const emails = await getPaginatedData({ + table: "emails", + conditions: { userId }, + ...query + }); emails.data = await addIsPrimaryToEmails(emails.data); return emails; } diff --git a/src/rest/organization.ts b/src/rest/organization.ts index a68319c50..875737be3 100644 --- a/src/rest/organization.ts +++ b/src/rest/organization.ts @@ -23,7 +23,7 @@ import { getOrganizationEvents, getOrganizationRecentEvents } from "../crud/event"; -import { Locals } from "../interfaces/general"; +import { Locals, KeyValue } from "../interfaces/general"; import { can } from "../helpers/authorization"; import { getStripeCustomer, @@ -346,9 +346,9 @@ export const getOrganizationRecentEventsForUser = async ( export const getOrganizationMembershipsForUser = async ( userId: number, organizationId: number, - start?: number + query?: KeyValue ) => { if (await can(userId, Authorizations.READ, "organization", organizationId)) - return await getOrganizationMemberDetails(organizationId, start); + return await getOrganizationMemberDetails(organizationId, query); throw new Error(ErrorCode.INSUFFICIENT_PERMISSION); }; diff --git a/src/rest/user.ts b/src/rest/user.ts index 8729959e3..157a788fa 100644 --- a/src/rest/user.ts +++ b/src/rest/user.ts @@ -115,14 +115,14 @@ export const deleteUserForUser = async ( export const getRecentEventsForUser = async ( tokenUserId: number, dataUserId: number, - index?: number + query: KeyValue ) => { if (await can(tokenUserId, Authorizations.READ_SECURE, "user", dataUserId)) { - const events = await getPaginatedData( - "events", - { userId: dataUserId }, - index - ); + const events = await getPaginatedData({ + table: "events", + conditions: { userId: dataUserId }, + ...query + }); events.data = await addLocationToEvents(events.data); return events; } @@ -132,14 +132,14 @@ export const getRecentEventsForUser = async ( export const getMembershipsForUser = async ( tokenUserId: number, dataUserId: number, - index?: number + query: KeyValue ) => { if (await can(tokenUserId, Authorizations.READ, "user", dataUserId)) { - const memberships = await getPaginatedData( - "memberships", - { userId: dataUserId }, - index - ); + const memberships = await getPaginatedData({ + table: "memberships", + conditions: { userId: dataUserId }, + ...query + }); memberships.data = await addOrganizationToMemberships(memberships.data); return memberships; }