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

Commit e6bc33b

Browse files
✨ Org events and data export
1 parent 582c5e3 commit e6bc33b

File tree

5 files changed

+117
-4
lines changed

5 files changed

+117
-4
lines changed

src/crud/event.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,34 @@ export const getUserRecentEvents = async (userId: number) => {
5050
));
5151
};
5252

53+
/*
54+
* Get all security events for a user
55+
*/
56+
export const getOrganizationEvents = async (organizationId: number) => {
57+
return <Event[]>(
58+
await cachedQuery(
59+
CacheCategories.ORGANIZATION_EVENT,
60+
organizationId,
61+
`SELECT * FROM events WHERE organizationId = ?`,
62+
[organizationId]
63+
)
64+
);
65+
};
66+
67+
/*
68+
* Get the 10 most recent security events for a user
69+
*/
70+
export const getOrganizationRecentEvents = async (organizationId: number) => {
71+
return await addLocationToEvents(<Event[]>(
72+
await cachedQuery(
73+
CacheCategories.ORGANIZATION_RECENT_EVENTS,
74+
organizationId,
75+
`SELECT * FROM events WHERE organizationId = ? ORDER BY id DESC LIMIT 10`,
76+
[organizationId]
77+
)
78+
));
79+
};
80+
5381
/*
5482
* Delete all security events for a user
5583
*/

src/interfaces/enum.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,9 @@ export enum CacheCategories {
9090
USER_VERIFIED_EMAILS = "user-verified-emails",
9191
EMAIL = "email",
9292
USER_EVENT = "user-event",
93+
ORGANIZATION_EVENT = "organization-event",
9394
USER_RECENT_EVENTS = "user-recent-events",
95+
ORGANIZATION_RECENT_EVENTS = "organization-recent-events",
9496
USER_MEMBERSHIPS = "user-memberships",
9597
USER_MEMBERSHIP_ORGANIZATION = "user-membership-org",
9698
ORGANIZATION_MEMBERSHIPS = "memberships",

src/rest/organization.ts

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,20 @@ import {
88
import { InsertResult } from "../interfaces/mysql";
99
import {
1010
createMembership,
11-
deleteAllOrganizationMemberships
11+
deleteAllOrganizationMemberships,
12+
getOrganizationMemberDetails
1213
} from "../crud/membership";
1314
import {
1415
MembershipRole,
1516
ErrorCode,
1617
EventType,
1718
Authorizations
1819
} from "../interfaces/enum";
19-
import { createEvent } from "../crud/event";
20+
import {
21+
createEvent,
22+
getOrganizationEvents,
23+
getOrganizationRecentEvents
24+
} from "../crud/event";
2025
import { Locals } from "../interfaces/general";
2126
import { can } from "../helpers/authorization";
2227
import {
@@ -273,3 +278,52 @@ export const createOrganizationSourceForUser = async (
273278
}
274279
throw new Error(ErrorCode.INSUFFICIENT_PERMISSION);
275280
};
281+
282+
export const getAllOrganizationDataForUser = async (
283+
userId: number,
284+
organizationId: number
285+
) => {
286+
if (
287+
await can(
288+
userId,
289+
Authorizations.READ_SECURE,
290+
"organization",
291+
organizationId
292+
)
293+
) {
294+
const organization = await getOrganization(organizationId);
295+
const memberships = await getOrganizationMemberDetails(organizationId);
296+
const events = await getOrganizationEvents(organizationId);
297+
let billing = {};
298+
let subscriptions = {};
299+
let invoices = {};
300+
let sources = {};
301+
if (organization.stripeCustomerId) {
302+
billing = await getStripeCustomer(organization.stripeCustomerId);
303+
subscriptions = await getStripeSubscriptions(
304+
organization.stripeCustomerId
305+
);
306+
invoices = await getStripeInvoices(organization.stripeCustomerId);
307+
sources = await getStripeSources(organization.stripeCustomerId);
308+
}
309+
return {
310+
organization,
311+
memberships,
312+
events,
313+
billing,
314+
subscriptions,
315+
invoices,
316+
sources
317+
};
318+
}
319+
throw new Error(ErrorCode.INSUFFICIENT_PERMISSION);
320+
};
321+
322+
export const getOrganizationRecentEventsForUser = async (
323+
userId: number,
324+
organizationId: number
325+
) => {
326+
if (await can(userId, Authorizations.READ, "organization", organizationId))
327+
return await getOrganizationRecentEvents(organizationId);
328+
throw new Error(ErrorCode.INSUFFICIENT_PERMISSION);
329+
};

src/routes/index.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ import {
2929
routeOrganizationSourceGet,
3030
routeOrganizationSourcesPut,
3131
routeOrganizationSourceUpdate,
32-
routeOrganizationSourceDelete
32+
routeOrganizationSourceDelete,
33+
routeOrganizationDataGet,
34+
routeOrganizationRecentEventsGet
3335
} from "./organizations";
3436
import { authHandler } from "../helpers/middleware";
3537
import {
@@ -196,6 +198,16 @@ const routesOrganization = (app: Application) => {
196198
authHandler,
197199
asyncHandler(routeOrganizationSourceDelete)
198200
);
201+
app.get(
202+
"/organizations/:id/data",
203+
authHandler,
204+
asyncHandler(routeOrganizationDataGet)
205+
);
206+
app.get(
207+
"/organizations/:id/events",
208+
authHandler,
209+
asyncHandler(routeOrganizationRecentEventsGet)
210+
);
199211
};
200212

201213
const routesMembership = (app: Application) => {

src/routes/organizations.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ import {
1313
getOrganizationSourceForUser,
1414
createOrganizationSourceForUser,
1515
updateOrganizationSourceForUser,
16-
deleteOrganizationSourceForUser
16+
deleteOrganizationSourceForUser,
17+
getAllOrganizationDataForUser,
18+
getOrganizationRecentEventsForUser
1719
} from "../rest/organization";
1820
import { ErrorCode } from "../interfaces/enum";
1921

@@ -181,3 +183,18 @@ export const routeOrganizationSourcesPut = async (
181183
)
182184
);
183185
};
186+
187+
export const routeOrganizationDataGet = async (req: Request, res: Response) => {
188+
res.json(
189+
await getAllOrganizationDataForUser(res.locals.token.id, req.params.id)
190+
);
191+
};
192+
193+
export const routeOrganizationRecentEventsGet = async (
194+
req: Request,
195+
res: Response
196+
) => {
197+
res.json(
198+
await getOrganizationRecentEventsForUser(res.locals.token.id, req.params.id)
199+
);
200+
};

0 commit comments

Comments
 (0)