Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/37-export-rule-editor-rules-to-a…
Browse files Browse the repository at this point in the history
…-spreadsheet' into dev
  • Loading branch information
gerrycampion committed Dec 18, 2023
2 parents f724235 + 39aa876 commit 1b0ee28
Show file tree
Hide file tree
Showing 23 changed files with 21,388 additions and 227 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Install Node.js version **16**: https://nodejs.org/en/download/releases
3. Install static web app node libraries by running the following commands:
```
npm install -g @azure/static-web-apps-cli
npm install -g azure-functions-core-tools@4 --unsafe-perm true
```
4. Using VSCODE, install the "Azure Functions" extension.
5. Create a local.settings.json in the API folder to support local development of the API. It should contain the following values which provide information for the staticwebapp config and the Microsoft Graph API for Users' name resolution:
Expand Down Expand Up @@ -57,6 +58,7 @@ Install Node.js version **16**: https://nodejs.org/en/download/releases
"COSMOS_KEY": <COSMOS_KEY>,
"COSMOS_DATABASE": <COSMOS DB Name>,
"COSMOS_CONTAINER": <COSMOS Container Name>,
"COSMOS_HISTORY_CONTAINER": <COSMOS History Container Name>
```
Optional env variable to ignore unauthorized https connections when cosmosdb is running in a local emulator:
Expand Down
2 changes: 0 additions & 2 deletions api/delete_rule/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import { STORAGE_PROVIDER } from "../providers/BaseStorage";
import handle_response from "../utils/handle_response";
import { formatYAML, unpublish } from "../utils/json_yaml";
import { getUser } from "../utils/SWAUtils";

export default async (context, req) => {
const ruleBefore = await STORAGE_PROVIDER.getRule(context.bindingData.id);
const ruleAfter = {
...ruleBefore,
creator: { id: getUser(req).userId },
content: formatYAML(unpublish(ruleBefore.content)),
};
await handle_response(context, async () =>
STORAGE_PROVIDER.deleteRule(ruleAfter)
Expand Down
18 changes: 18 additions & 0 deletions api/get_history/function.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"bindings": [
{
"authLevel": "anonymous",
"type": "httpTrigger",
"direction": "in",
"name": "req",
"methods": ["get"],
"route": "history/{id}"
},
{
"type": "http",
"direction": "out",
"name": "res"
}
],
"scriptFile": "../dist/get_history/index.js"
}
13 changes: 13 additions & 0 deletions api/get_history/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { STORAGE_PROVIDER } from "../providers/BaseStorage";
import { addUsernamesToRule } from "../providers/BaseUsers";
import handle_response from "../utils/handle_response";

export default async (context, req) => {
await handle_response(context, async () => {
const rule = await STORAGE_PROVIDER.getHistory(context.bindingData.id);
await addUsernamesToRule(rule);
return {
body: rule,
};
});
};
5 changes: 1 addition & 4 deletions api/get_rule/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ import handle_response from "../utils/handle_response";

export default async (context, req) => {
await handle_response(context, async () => {
const rule = await STORAGE_PROVIDER.getRule(
context.bindingData.id,
context.bindingData.version
);
const rule = await STORAGE_PROVIDER.getRule(context.bindingData.id);
await addUsernamesToRule(rule);
return {
body: rule,
Expand Down
8 changes: 0 additions & 8 deletions api/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 11 additions & 7 deletions api/providers/BaseUsers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,18 @@ export async function usernameQueryToUserid(query: IQuery) {

export async function addUsernamesToRules(rules: IRule[]) {
// Get list of all unique userids in the rules list and get matching usernames from the Users provider
const users = await USERS_PROVIDER.getUsersByIds([
...new Set(rules.map((rule) => rule.creator.id)),
]);
const ids = new Set<string>();
for (const rule of rules) {
rule.creator = users[rule.creator.id] ?? {
id: rule.creator.id,
name: null,
};
if ("creator.id" in rule) {
ids.add(rule["creator.id"] as string);
}
}

const users = await USERS_PROVIDER.getUsersByIds([...ids]);
for (const rule of rules) {
if (users[rule["creator.id"]]) {
rule["creator.name"] = users[rule["creator.id"]].name;
}
}
}

Expand Down
Loading

0 comments on commit 1b0ee28

Please sign in to comment.