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

Commit 438343d

Browse files
✨ Support for paginated data
1 parent eb4a739 commit 438343d

File tree

6 files changed

+46
-7
lines changed

6 files changed

+46
-7
lines changed

src/controllers/organization.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,8 @@ export class OrganizationController {
299299
res.json(
300300
await getOrganizationMembershipsForUser(
301301
res.locals.token.id,
302-
organizationId
302+
organizationId,
303+
req.params.start
303304
)
304305
);
305306
}

src/crud/data.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { query } from "../helpers/mysql";
2+
import { KeyValue } from "../interfaces/general";
3+
4+
/*
5+
* Get pagination data
6+
*/
7+
export const getPaginatedData = async (
8+
table: string,
9+
conditions?: KeyValue,
10+
index = 0,
11+
itemsPerPage = 5,
12+
primaryKey = "id"
13+
) => {
14+
const data = (await query(
15+
`SELECT * FROM \`${table}\` WHERE ${primaryKey} > ? ${
16+
conditions
17+
? `AND ${Object.keys(conditions)
18+
.map(condition => `${condition} = ?`)
19+
.join(" AND ")}`
20+
: ""
21+
} ORDER BY ${primaryKey} ASC LIMIT ${itemsPerPage}`,
22+
[index, ...(conditions ? Object.values(conditions) : [])]
23+
)) as any[];
24+
return {
25+
data,
26+
has_more: data.length === itemsPerPage,
27+
next: data.length && data[data.length - 1][primaryKey]
28+
};
29+
};

src/crud/membership.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { ErrorCode, CacheCategories } from "../interfaces/enum";
1313
import { deleteItemFromCache, cachedQuery } from "../helpers/cache";
1414
import { getUser } from "./user";
1515
import { Organization } from "../interfaces/tables/organization";
16+
import { getPaginatedData } from "./data";
1617

1718
/*
1819
* Create a new organization membership for a user
@@ -166,8 +167,15 @@ export const getOrganizationMembers = async (organizationId: number) => {
166167
/*
167168
* Get a detailed list of all members in an organization
168169
*/
169-
export const getOrganizationMemberDetails = async (organizationId: number) => {
170-
const members: any = await getOrganizationMembers(organizationId);
170+
export const getOrganizationMemberDetails = async (
171+
organizationId: number,
172+
start?: number
173+
) => {
174+
const members: any = await getPaginatedData(
175+
"memberships",
176+
{ organizationId },
177+
start
178+
);
171179
for await (const member of members) {
172180
member.user = await getUser(member.userId);
173181
}

src/crud/user.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ export const getUserByEmail = async (email: string, secureOrigin = false) => {
9595
*/
9696
export const updateUser = async (id: number, user: KeyValue) => {
9797
user.updatedAt = dateToDateTime(new Date());
98+
user.password = await hash(user.password || "", 8);
9899
user = removeReadOnlyValues(user);
99100
if (user.primaryEmail) {
100101
const originalUser = await getUser(id);

src/rest/auth.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,7 @@ export const updatePassword = async (
150150
) => {
151151
validate(password, ValidationTypes.TEXT);
152152
const userId = (<KeyValue>await verifyToken(token, Tokens.PASSWORD_RESET)).id;
153-
const hashedPassword = await hash(password || "", 8);
154-
await updateUser(userId, { password: hashedPassword });
153+
await updateUser(userId, { password });
155154
await createEvent(
156155
{
157156
userId,

src/rest/organization.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -354,9 +354,10 @@ export const getOrganizationRecentEventsForUser = async (
354354

355355
export const getOrganizationMembershipsForUser = async (
356356
userId: number,
357-
organizationId: number
357+
organizationId: number,
358+
start?: number
358359
) => {
359360
if (await can(userId, Authorizations.READ, "organization", organizationId))
360-
return await getOrganizationMemberDetails(organizationId);
361+
return await getOrganizationMemberDetails(organizationId, start);
361362
throw new Error(ErrorCode.INSUFFICIENT_PERMISSION);
362363
};

0 commit comments

Comments
 (0)