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

Commit

Permalink
✨ Add admin endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
AnandChowdhary committed Aug 16, 2019
1 parent 860ed85 commit bf49a3e
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 12 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "staart-manager",
"version": "1.0.132",
"version": "1.0.133",
"main": "index.js",
"repository": "[email protected]:AnandChowdhary/staart.git",
"author": "Anand Chowdhary <[email protected]>",
Expand Down Expand Up @@ -144,5 +144,5 @@
"setup"
],
"snyk": true,
"staart-version": "1.0.132"
"staart-version": "1.0.133"
}
14 changes: 11 additions & 3 deletions src/controllers/v1/admin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { Request, Response } from "express";
import { ErrorCode } from "../../interfaces/enum";
import {
getAllOrganizationForUser,
getAllUsersForUser
getAllUsersForUser,
getServerLogsForUser
} from "../../rest/admin";
import {
Get,
Expand All @@ -21,14 +22,21 @@ export class AdminController {
async getOrganizations(req: Request, res: Response) {
const userId = res.locals.token.id;
if (!userId) throw new Error(ErrorCode.MISSING_FIELD);
res.json(await getAllOrganizationForUser(userId));
res.json(await getAllOrganizationForUser(userId, req.query));
}

@Get("users")
async getUsers(req: Request, res: Response) {
const userId = res.locals.token.id;
if (!userId) throw new Error(ErrorCode.MISSING_FIELD);
res.json(await getAllUsersForUser(userId));
res.json(await getAllUsersForUser(userId, req.query));
}

@Get("server-logs")
async getServerLogs(req: Request, res: Response) {
const userId = res.locals.token.id;
if (!userId) throw new Error(ErrorCode.MISSING_FIELD);
res.json(await getServerLogsForUser(userId, req.query));
}

@Get("info")
Expand Down
3 changes: 2 additions & 1 deletion src/helpers/elasticsearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ export const cleanElasticSearchQueryResponse = (response: any) => {
const count = response.hits.total;
const data = response.hits.hits;
const newResponse: any = {
data
data,
count
};
if (count > data.length) {
newResponse.hasMore = true;
Expand Down
1 change: 1 addition & 0 deletions src/helpers/tracking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export const trackUrl = async (req: Request, res: Response) => {
cookies: req.cookies,
headers: req.headers,
url: req.url,
ipCountry: (req.get("cf-ipcountry") || "").toLowerCase(),
...res.locals
};
if (trackingObject.apiKey) {
Expand Down
68 changes: 62 additions & 6 deletions src/rest/admin.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,72 @@
import { can } from "../helpers/authorization";
import { Authorizations, ErrorCode } from "../interfaces/enum";
import { getAllOrganizations } from "../crud/organization";
import { getAllUsers } from "../crud/user";
import { getPaginatedData } from "../crud/data";
import { KeyValue } from "../interfaces/general";
import {
cleanElasticSearchQueryResponse,
elasticSearch
} from "../helpers/elasticsearch";
import ms from "ms";

export const getAllOrganizationForUser = async (tokenUserId: number) => {
export const getAllOrganizationForUser = async (
tokenUserId: number,
query: KeyValue
) => {
if (await can(tokenUserId, Authorizations.READ, "general"))
return await getAllOrganizations();
return await getPaginatedData({
table: "organizations",
...query
});
throw new Error(ErrorCode.INSUFFICIENT_PERMISSION);
};

export const getAllUsersForUser = async (tokenUserId: number) => {
export const getAllUsersForUser = async (
tokenUserId: number,
query: KeyValue
) => {
if (await can(tokenUserId, Authorizations.READ, "general"))
return await getAllUsers();
return await getPaginatedData({
table: "users",
...query
});
throw new Error(ErrorCode.INSUFFICIENT_PERMISSION);
};

/**
* Get an API key
*/
export const getServerLogsForUser = async (
tokenUserId: number,
query: KeyValue
) => {
if (!(await can(tokenUserId, Authorizations.READ, "general")))
throw new Error(ErrorCode.INSUFFICIENT_PERMISSION);
const range: string = query.range || "7d";
const from = query.from ? parseInt(query.from) : 0;
const result = await elasticSearch.search({
index: `staart-logs-*`,
from,
body: {
query: {
bool: {
must: [
{
range: {
date: {
gte: new Date(new Date().getTime() - ms(range))
}
}
}
]
}
},
sort: [
{
date: { order: "desc" }
}
],
size: 10
}
});
return cleanElasticSearchQueryResponse(result);
};

0 comments on commit bf49a3e

Please sign in to comment.