Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add api endpoint to get plugin states [PT-187885524] #258

Merged
merged 1 commit into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions functions/src/api/get-plugin-states.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@

import { Request, Response } from "express"
import { getPath, getCollection } from "./helpers/paths"

export default (req: Request, res: Response) => {
const {source, remote_endpoint} = req.query

if (!source) {
return res.error(500, "Missing source in query!")
}
if (typeof source !== "string") {
return res.error(500, "Malformed source")
}
if (!remote_endpoint) {
return res.error(500, "Missing remote_endpoint in query!")
}

return getPath(source, "plugin_states")
.then((path) => {
return getCollection(path)
.where("remote_endpoint", "==", remote_endpoint)
.get()
.then((snapshot) => {
const pluginStates = snapshot.docs.reduce<Record<string,any>>((acc, doc) => {
try {
acc[doc.id] = JSON.parse(doc.data().state)
} catch (e) {
// no-op
}
return acc
}, {})
res.success({"plugin_states": pluginStates})
})
})
.catch((e) => {
console.error(e);
res.error(404, e.toString())
})
}
5 changes: 4 additions & 1 deletion functions/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import moveStudentWork from "./api/move-student-work"
import getResource from "./api/get-resource"
import fakeAnswer from "./api/fake-answer"
import getAnswer from "./api/get-answer"
import getPluginStates from "./api/get-plugin-states"

import {
createSyncDocAfterAnswerWritten,
Expand All @@ -38,7 +39,8 @@ api.get("/", (req, res) => {
"POST import_structure": "Imports the structure, requires a bearer token",
"POST move_student_work": "Moves a students work from one class to another, requires a bearer token.",
"GET resource?source=<SOURCE>&url=<URL>": "Returns a resource under source with given url",
"GET answer?source=<SOURCE>&remote_endpoint=<REMOTE_ENDPOINT>&question_id=<QUESTION_ID>": "Returns the full answer document for a question by a learner"
"GET answer?source=<SOURCE>&remote_endpoint=<REMOTE_ENDPOINT>&question_id=<QUESTION_ID>": "Returns the full answer document for a question by a learner",
"GET plugin_states?source=<SOURCE>&remote_endpoint=<REMOTE_ENDPOINT>": "Returns all the plugin states for a learner's resource"
}
})
})
Expand All @@ -47,6 +49,7 @@ api.post("/import_structure", importStructure)
api.post("/move_student_work", moveStudentWork)
api.get("/resource", getResource)
api.get("/answer", getAnswer)
api.get("/plugin_states", getPluginStates)
// TODO: comment out for final PR
api.get("/fakeAnswer", fakeAnswer)

Expand Down
Loading