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

Task model adjustments and cleanup #19

Open
wants to merge 6 commits into
base: improve-auth
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions .changeset/four-beds-leave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"reglement-publish-service": patch
---

Remove obsolete GET `/tasks` endpoint. This endpoint is replaced by a simple mu-cl-resources `/tasks` endpoint
5 changes: 5 additions & 0 deletions .changeset/loud-comics-wink.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"reglement-publish-service": patch
---

Fix: link publication-task to `editor-document` instead of to `documentContainer`
5 changes: 5 additions & 0 deletions .changeset/sixty-apes-enjoy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"reglement-publish-service": patch
---

Ensure endpoint responses following the JSON:API spec
5 changes: 5 additions & 0 deletions .changeset/thirty-years-provide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"reglement-publish-service": minor
---

Remove obsolete `snippet-list-publication-tasks` endpoint and related functions
102 changes: 9 additions & 93 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,6 @@
import { app, errorHandler } from "mu";
import { isAfter } from "date-fns/isAfter";
import Task, {
TASK_STATUS_FAILURE,
TASK_STATUS_RUNNING,
TASK_STATUS_SUCCESS,
TASK_TYPE_REGLEMENT_PUBLISH,
TASK_TYPE_SNIPPET_PUBLISH,
} from "./models/task";
import {
getEditorDocument,
getPublishedVersion,
getSnippetList,
hasPublishedVersion,
} from "./util/common-sparql";
import {
insertPublishedSnippetContainer,
updatePublishedSnippetContainer,
} from "./util/snippet-sparql";
import Task, { JOB_STATUSES, TASK_TYPE_REGLEMENT_PUBLISH } from "./models/task";
import { DECISION_FOLDER, RS_FOLDER } from "./constants";
import Template from "./models/template";
import TemplateVersion from "./models/template-version";
Expand All @@ -36,14 +20,16 @@ app.post("/publish-template/:documentContainerId", async (req, res, next) => {
throw new Error(`Provided document container not found`);
}
publishingTask = await Task.ensure({
involves: documentContainer.uri,
involves: documentContainer.currentVersion.uri,
taskType: TASK_TYPE_REGLEMENT_PUBLISH,
});
res.json({
data: {
id: publishingTask.id,
status: publishingTask.status,
type: publishingTask.type,
attributes: {
status: publishingTask.status,
type: publishingTask.type,
},
},
});
} catch (err) {
Expand All @@ -54,7 +40,7 @@ app.post("/publish-template/:documentContainerId", async (req, res, next) => {
return next(error);
}
try {
await publishingTask.updateStatus(TASK_STATUS_RUNNING);
await publishingTask.updateStatus(JOB_STATUSES.busy);

const templateType =
documentContainer.folder === DECISION_FOLDER
Expand Down Expand Up @@ -84,80 +70,10 @@ app.post("/publish-template/:documentContainerId", async (req, res, next) => {
}
}
await template.setCurrentVersion(templateVersion);
await publishingTask.updateStatus(TASK_STATUS_SUCCESS);
await publishingTask.updateStatus(JOB_STATUSES.success);
} catch (err) {
console.log(err);
publishingTask.updateStatus(TASK_STATUS_FAILURE, err.message);
}
});

app.post("/snippet-list-publication-tasks", async (req, res, next) => {
const documentContainerUuid =
req.body.data.relationships["document-container"].data.id;
const snippetListUuid = req.body.data.relationships["snippet-list"].data.id;

let publishingTask;

try {
const editorDocument = await getEditorDocument(documentContainerUuid);
const documentContainerUri = editorDocument.documentContainer.value;

publishingTask = await Task.ensure({
involves: documentContainerUri,
taskType: TASK_TYPE_SNIPPET_PUBLISH,
});

await publishingTask.updateStatus(TASK_STATUS_RUNNING);
const publishedVersionResults =
await getPublishedVersion(documentContainerUri);
const snippetList = await getSnippetList(snippetListUuid);

if (hasPublishedVersion(publishedVersionResults)) {
await updatePublishedSnippetContainer({
...snippetList,
...editorDocument,
publishedVersionResults,
publishingTaskUri: publishingTask.uri,
});
} else {
await insertPublishedSnippetContainer({
...snippetList,
...editorDocument,
publishingTaskUri: publishingTask.uri,
});
}

await publishingTask.updateStatus(TASK_STATUS_SUCCESS);

res.json({
data: {
id: publishingTask.id,
status: "accepted",
type: publishingTask.type,
},
});
} catch (error) {
console.log(error);
if (publishingTask) {
publishingTask.updateStatus(TASK_STATUS_FAILURE, error.message);
}
next(error);
}
});

app.get("/tasks/:id", async function (req, res) {
const taskId = req.params.id;
const task = await Task.find(taskId);
if (task) {
res.status(200).send({
data: {
id: task.id,
status: task.status,
type: task.type,
},
});
} else {
res.status(404).send(`task with id ${taskId} was not found`);
publishingTask.updateStatus(JOB_STATUSES.failure, err.message);
}
});

Expand Down
19 changes: 9 additions & 10 deletions models/task.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,13 @@ import {
update,
} from "mu";

export const TASK_STATUS_FAILURE =
"http://lblod.data.gift/besluit-publicatie-melding-statuses/failure";
export const TASK_STATUS_CREATED =
"http://lblod.data.gift/besluit-publicatie-melding-statuses/created";
export const TASK_STATUS_SUCCESS =
"http://lblod.data.gift/besluit-publicatie-melding-statuses/success";
export const TASK_STATUS_RUNNING =
"http://lblod.data.gift/besluit-publicatie-melding-statuses/ongoing";
export const JOB_STATUSES = {
scheduled: "http://redpencil.data.gift/id/concept/JobStatus/scheduled",
busy: "http://redpencil.data.gift/id/concept/JobStatus/busy",
success: "http://redpencil.data.gift/id/concept/JobStatus/success",
failed: "http://redpencil.data.gift/id/concept/JobStatus/failed",
canceled: "http://redpencil.data.gift/id/concept/JobStatus/canceled",
};

export const TASK_TYPE_REGLEMENT_PUBLISH =
"regulatory-attachment-publication-tasks";
Expand All @@ -37,7 +36,7 @@ export default class Task {
INSERT DATA {
${sparqlEscapeUri(uri)} a task:Task;
mu:uuid ${sparqlEscapeString(id)};
adms:status ${sparqlEscapeUri(TASK_STATUS_CREATED)};
adms:status ${sparqlEscapeUri(JOB_STATUSES.scheduled)};
task:numberOfRetries ${sparqlEscapeInt(0)};
dct:created ${sparqlEscapeDateTime(created)};
dct:modified ${sparqlEscapeDateTime(created)};
Expand All @@ -54,7 +53,7 @@ export default class Task {
involves,
created,
modified: created,
status: TASK_STATUS_CREATED,
status: JOB_STATUSES.scheduled,
uri,
});
}
Expand Down
80 changes: 0 additions & 80 deletions util/common-sparql.js

This file was deleted.

97 changes: 0 additions & 97 deletions util/snippet-sparql.js

This file was deleted.