This repository has been archived by the owner on Apr 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 205
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
860ed85
commit bf49a3e
Showing
5 changed files
with
78 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]>", | ||
|
@@ -144,5 +144,5 @@ | |
"setup" | ||
], | ||
"snyk": true, | ||
"staart-version": "1.0.132" | ||
"staart-version": "1.0.133" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |