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

Rewrite with tool library #7

Open
wants to merge 14 commits into
base: linting
Choose a base branch
from
Open
9 changes: 9 additions & 0 deletions .babelrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"overrides": [
{ "ignore": "./automatic-submission-flow-tools/node_modules" }
],
"babelrcRoots": [
".",
"./packages/*"
]
}
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "automatic-submission-flow-tools"]
path = automatic-submission-flow-tools
url = [email protected]:lblod/automatic-submission-flow-tools.git
4 changes: 1 addition & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
FROM semtech/mu-javascript-template:1.5.0-beta.4
FROM semtech/mu-javascript-template:feature-node-16-support
LABEL maintainer=info@redpencil

# see https://github.com/mu-semtech/mu-javascript-template for more info
91 changes: 47 additions & 44 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { app, errorHandler } from 'mu';
import bodyParser from 'body-parser';
import { updateTaskStatus } from './lib/submission-task';
import {
getSubmissionByTask,
getSubmissionBySubmissionDocument,
SUBMITABLE_STATUS,
SENT_STATUS,
CONCEPT_STATUS,
} from './lib/submission';
import * as env from './env.js';
import { saveError } from './lib/utils.js';
import * as cts from './automatic-submission-flow-tools/constants.js';
import * as tsk from './automatic-submission-flow-tools/asfTasks.js';
import * as del from './automatic-submission-flow-tools/deltas.js';
import * as err from './automatic-submission-flow-tools/errors.js';
import * as N3 from 'n3';
const { namedNode } = N3.DataFactory;

app.use(errorHandler);
app.use(
Expand All @@ -34,56 +35,58 @@ app.post('/delta', async function (req, res) {

try {
//Don't trust the delta-notifier, filter as best as possible. We just need the task that was created to get started.
const actualTaskUris = req.body
.map((changeset) => changeset.inserts)
.filter((inserts) => inserts.length > 0)
.flat()
.filter((insert) => insert.predicate.value === env.OPERATION_PREDICATE)
.filter((insert) => insert.object.value === env.VALIDATE_OPERATION)
.map((insert) => insert.subject.value);
const actualTasks = del.getSubjects(
req.body,
namedNode(cts.PREDICATE_TABLE.task_operation),
namedNode(cts.OPERATIONS.validate)
);

for (const taskUri of actualTaskUris) {
for (const task of actualTasks) {
const taskUri = task.value;
try {
await updateTaskStatus(taskUri, env.TASK_ONGOING_STATUS);
await tsk.updateStatus(
task,
namedNode(cts.TASK_STATUSES.busy),
namedNode(cts.SERVICES.validateSubmission)
);

const submission = await getSubmissionByTask(taskUri);
const { status, logicalFileUri } = await submission.process();
const resultingStatus = status;

let saveStatus;
switch (resultingStatus) {
case SENT_STATUS:
saveStatus = env.TASK_SUCCESSFUL_SENT_STATUS;
break;
case CONCEPT_STATUS:
saveStatus = env.TASK_SUCCESSFUL_CONCEPT_STATUS;
break;
default:
saveStatus = resultingStatus;
break;
}
const { logicalFileUri } = await submission.process();

await updateTaskStatus(
taskUri,
env.TASK_SUCCESS_STATUS,
undefined, //Potential errorURI
saveStatus,
logicalFileUri
await tsk.updateStatus(
task,
namedNode(cts.TASK_STATUSES.success),
namedNode(cts.SERVICES.validateSubmission),
{ files: [namedNode(logicalFileUri)] }
);
} catch (error) {
const message = `Something went wrong while enriching for task ${taskUri}`;
console.error(`${message}\n`, error.message);
console.error(error);
const errorUri = await saveError({ message, detail: error.message });
await updateTaskStatus(taskUri, env.TASK_FAILURE_STATUS, errorUri);
const errorNode = await err.create(
namedNode(cts.SERVICES.validateSubmission),
message,
error.message
);
await tsk.updateStatus(
task,
namedNode(cts.TASK_STATUSES.failed),
namedNode(cts.SERVICES.validateSubmission),
undefined,
errorNode
);
}
}
} catch (error) {
const message =
'The task for enriching a submission could not even be started or finished due to an unexpected problem.';
console.error(`${message}\n`, error.message);
console.error(error);
await saveError({ message, detail: error.message });
await err.create(
namedNode(cts.SERVICES.validateSubmission),
message,
error.message
);
}
});

Expand All @@ -100,13 +103,13 @@ app.put('/submission-documents/:uuid', async function (req, res, next) {

if (submission) {
try {
if (submission.status == SENT_STATUS) {
if (submission.status == env.SENT_STATUS) {
return res
.status(422)
.send({ title: `Submission ${submission.uri} already submitted` });
} else {
const { additions, removals } = req.body;
await submission.update({ additions, removals });
await submission.update(additions, removals);
return res.status(204).send();
}
} catch (e) {
Expand All @@ -131,21 +134,21 @@ app.post('/submission-documents/:uuid/submit', async function (req, res, next) {

if (submission) {
try {
if (submission.status == SENT_STATUS) {
if (submission.status == env.SENT_STATUS) {
return res
.status(422)
.send({ title: `Submission ${submission.uri} already submitted` });
} else {
await submission.updateStatus(SUBMITABLE_STATUS);
await submission.updateStatus(env.SUBMITABLE_STATUS);
const newStatus = (await submission.process()).status;
if (newStatus == SENT_STATUS) {
if (newStatus == env.SENT_STATUS) {
return res.status(204).send();
} else {
return res.status(400).send({ title: 'Unable to submit form' });
}
}
} catch (error) {
await submission.updateStatus(CONCEPT_STATUS);
await submission.updateStatus(env.CONCEPT_STATUS);
console.log(
`Something went wrong while submitting submission with id ${uuid}`
);
Expand Down
1 change: 1 addition & 0 deletions automatic-submission-flow-tools
57 changes: 9 additions & 48 deletions env.js
Original file line number Diff line number Diff line change
@@ -1,54 +1,15 @@
export const CREATOR =
'http://lblod.data.gift/services/validate-submission-service';

export const TASK_ONGOING_STATUS =
'http://redpencil.data.gift/id/concept/JobStatus/busy';
export const TASK_SUCCESS_STATUS =
'http://redpencil.data.gift/id/concept/JobStatus/success';
export const TASK_FAILURE_STATUS =
'http://redpencil.data.gift/id/concept/JobStatus/failed';

export const TASK_SUCCESSFUL_CONCEPT_STATUS =
'http://lblod.data.gift/automatische-melding-statuses/successful-concept';
export const TASK_SUCCESSFUL_SENT_STATUS =
'http://lblod.data.gift/automatische-melding-statuses/successful-sent';

export const OPERATION_PREDICATE =
'http://redpencil.data.gift/vocabularies/tasks/operation';
export const VALIDATE_OPERATION =
'http://lblod.data.gift/id/jobs/concept/TaskOperation/validate';

export const PREFIX_TABLE = {
meb: 'http://rdf.myexperiment.org/ontologies/base/',
xsd: 'http://www.w3.org/2001/XMLSchema#',
pav: 'http://purl.org/pav/',
dct: 'http://purl.org/dc/terms/',
melding: 'http://lblod.data.gift/vocabularies/automatische-melding/',
lblodBesluit: 'http://lblod.data.gift/vocabularies/besluit/',
adms: 'http://www.w3.org/ns/adms#',
muAccount: 'http://mu.semte.ch/vocabularies/account/',
eli: 'http://data.europa.eu/eli/ontology#',
org: 'http://www.w3.org/ns/org#',
elod: 'http://linkedeconomy.org/ontology#',
nie: 'http://www.semanticdesktop.org/ontologies/2007/01/19/nie#',
prov: 'http://www.w3.org/ns/prov#',
mu: 'http://mu.semte.ch/vocabularies/core/',
foaf: 'http://xmlns.com/foaf/0.1/',
nfo: 'http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#',
ext: 'http://mu.semte.ch/vocabularies/ext/',
http: 'http://www.w3.org/2011/http#',
rpioHttp: 'http://redpencil.data.gift/vocabularies/http/',
dgftSec: 'http://lblod.data.gift/vocabularies/security/',
dgftOauth: 'http://kanselarij.vo.data.gift/vocabularies/oauth-2.0-session/',
wotSec: 'https://www.w3.org/2019/wot/security#',
task: 'http://redpencil.data.gift/vocabularies/tasks/',
asj: 'http://data.lblod.info/id/automatic-submission-job/',
dbpedia: 'http://dbpedia.org/ontology/',
};
export const CONCEPT_STATUS =
'http://lblod.data.gift/concepts/79a52da4-f491-4e2f-9374-89a13cde8ecd';
export const SUBMITABLE_STATUS =
'http://lblod.data.gift/concepts/f6330856-e261-430f-b949-8e510d20d0ff';
export const SENT_STATUS =
'http://lblod.data.gift/concepts/9bd8d86d-bb10-4456-a84e-91e9507c374c';

export const PREFIXES = (() => {
const all = [];
for (const key in PREFIX_TABLE)
all.push(`PREFIX ${key}: <${PREFIX_TABLE[key]}>`);
return all.join('\n');
})();
export const FORM_GRAPH = 'http://data.lblod.info/graphs/semantic-forms';
export const META_GRAPH = 'http://data.lblod.info/graphs/meta';
export const SOURCE_GRAPH = 'http://data.lblod.info/graphs/submission';
157 changes: 0 additions & 157 deletions lib/file-helpers.js

This file was deleted.

Loading