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

Commit

Permalink
✨ Support for search, sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
AnandChowdhary committed Jun 15, 2019
1 parent 6195047 commit 9a3114b
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 50 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "staart",
"version": "1.0.5",
"version": "1.0.6",
"main": "index.js",
"repository": "[email protected]:AnandChowdhary/staart.git",
"author": "Anand Chowdhary <[email protected]>",
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/organization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ export class OrganizationController {
await getOrganizationMembershipsForUser(
res.locals.token.id,
organizationId,
req.query.start
req.query
)
);
}
Expand Down
12 changes: 3 additions & 9 deletions src/controllers/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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")
Expand Down Expand Up @@ -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")
Expand Down
38 changes: 29 additions & 9 deletions src/crud/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
12 changes: 6 additions & 6 deletions src/crud/membership.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
16 changes: 7 additions & 9 deletions src/rest/email.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Locals } from "../interfaces/general";
import { Locals, KeyValue } from "../interfaces/general";
import {
createEmail,
getEmail,
Expand All @@ -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;
}
Expand Down
6 changes: 3 additions & 3 deletions src/rest/organization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
};
24 changes: 12 additions & 12 deletions src/rest/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand Down

0 comments on commit 9a3114b

Please sign in to comment.