@nzz/q-server
Version:
**Maintainer**: [Franco Gervasi](https://github.com/fgervasi)
26 lines • 12.8 kB
JSON
{
"sourceFile": "plugins/core/base/routes/item.js",
"activeCommit": 0,
"commits": [
{
"activePatchIndex": 2,
"patches": [
{
"date": 1698937487047,
"content": "Index: \n===================================================================\n--- \n+++ \n"
},
{
"date": 1698937536907,
"content": "Index: \n===================================================================\n--- \n+++ \n@@ -24,9 +24,9 @@\n } else {\n reject(\n Boom.badRequest(\n validate.errors\n- .map((error) => {\n+ .map(error => {\n return JSON.stringify(error);\n })\n .join(\"\\n\")\n )\n@@ -40,32 +40,31 @@\n path: \"/item/{id}\",\n method: \"GET\",\n options: {\n auth: {\n- strategy: \"q-auth-azure-then-ld\",\n- mode: \"optional\",\n+ strategies: [\"q-auth\"]\n },\n cors: {\n- credentials: true,\n+ credentials: true\n },\n validate: {\n params: {\n- id: Joi.string().required(),\n- },\n+ id: Joi.string().required()\n+ }\n },\n description: \"gets the item with the given id from the database\",\n- tags: [\"api\", \"editor\"],\n+ tags: [\"api\", \"editor\"]\n },\n- handler: async function (request, h) {\n+ handler: async function(request, h) {\n return request.server.methods.db.item.getById({\n id: request.params.id,\n ignoreInactive: true,\n session: {\n credentials: request.auth.credentials,\n- artifacts: request.auth.artifacts,\n- },\n+ artifacts: request.auth.artifacts\n+ }\n });\n- },\n+ }\n },\n post: {\n path: \"/item\",\n method: \"POST\",\n@@ -74,25 +73,23 @@\n payload: {\n _id: Joi.string().optional(),\n _rev: Joi.forbidden(),\n title: Joi.string().required(),\n- tool: Joi.string().required(),\n+ tool: Joi.string().required()\n },\n options: {\n- allowUnknown: true,\n- },\n+ allowUnknown: true\n+ }\n },\n- auth: {\n- strategy: \"q-auth-azure-then-ld\",\n- },\n+ auth: \"q-auth\",\n cors: {\n- credentials: true,\n+ credentials: true\n },\n description:\n \"stores a new item to the database and returns the id among other things\",\n- tags: [\"api\", \"editor\"],\n+ tags: [\"api\", \"editor\"]\n },\n- handler: async function (request, h) {\n+ handler: async function(request, h) {\n let doc = request.payload;\n let now = new Date();\n \n try {\n@@ -119,22 +116,22 @@\n const res = await request.server.methods.db.item.insert({\n doc,\n session: {\n credentials: request.auth.credentials,\n- artifacts: request.auth.artifacts,\n- },\n+ artifacts: request.auth.artifacts\n+ }\n });\n \n docDiff._id = res.id;\n docDiff._rev = res.rev;\n \n const savedDoc = Object.assign(doc, docDiff);\n request.server.events.emit(\"item.new\", {\n- newItem: savedDoc,\n+ newItem: savedDoc\n });\n \n return docDiff;\n- },\n+ }\n },\n put: {\n path: \"/item\",\n method: \"PUT\",\n@@ -143,23 +140,23 @@\n payload: {\n _id: Joi.string().required(),\n _rev: Joi.string().required(),\n title: Joi.string().required(),\n- tool: Joi.string().required(),\n+ tool: Joi.string().required()\n },\n options: {\n- allowUnknown: true,\n- },\n+ allowUnknown: true\n+ }\n },\n- // auth: \"q-auth-azure-then-ld\",\n+ auth: \"q-auth\",\n cors: {\n- credentials: true,\n+ credentials: true\n },\n description:\n \"updates an existing item to the database and returns the new revision number among other things\",\n- tags: [\"api\", \"editor\"],\n+ tags: [\"api\", \"editor\"]\n },\n- handler: async function (request, h) {\n+ handler: async function(request, h) {\n let doc = request.payload;\n let now = new Date();\n try {\n await validateAgainstSchema(request, doc);\n@@ -179,9 +176,9 @@\n docDiff.updatedBy = doc.updatedBy;\n \n const oldDoc = await request.server.methods.db.item.getById({\n id: request.payload._id,\n- ignoreInactive: true,\n+ ignoreInactive: true\n });\n \n // if the active state change to true, we set activateDate\n let isNewActive = false;\n@@ -207,18 +204,18 @@\n const res = await request.server.methods.db.item.insert({\n doc,\n session: {\n credentials: request.auth.credentials,\n- artifacts: request.auth.artifacts,\n- },\n+ artifacts: request.auth.artifacts\n+ }\n });\n \n docDiff._rev = res.rev;\n const savedDoc = Object.assign(doc, docDiff);\n \n const eventData = {\n newItem: savedDoc,\n- oldItem: oldDoc,\n+ oldItem: oldDoc\n };\n \n if (isNewActive) {\n request.server.events.emit(\"item.activate\", eventData);\n@@ -232,7 +229,7 @@\n \n request.server.events.emit(\"item.update\", eventData);\n \n return docDiff;\n- },\n- },\n+ }\n+ }\n };\n"
},
{
"date": 1701762290012,
"content": "Index: \n===================================================================\n--- \n+++ \n@@ -149,9 +149,8 @@\n options: {\n allowUnknown: true,\n },\n },\n- auth: \"q-auth-azure-then-ld\",\n cors: {\n credentials: true,\n },\n description:\n"
}
],
"date": 1698937487047,
"name": "Commit-0",
"content": "const Boom = require(\"@hapi/boom\");\nconst Joi = require(\"../../../../helper/custom-joi.js\");\nconst Ajv = require(\"ajv\");\nconst ajv = new Ajv({ schemaId: \"id\" });\n// add draft-04 support explicit\najv.addMetaSchema(require(\"ajv/lib/refs/json-schema-draft-04.json\"));\n\nfunction validateAgainstSchema(request, doc) {\n return new Promise(async (resolve, reject) => {\n const response = await request.server.inject(\n `/tools/${doc.tool}/schema.json`\n );\n if (response.statusCode !== 200) {\n reject(\n Boom.internal(`Error occured while fetching schema of tool ${doc.tool}`)\n );\n }\n\n const schema = JSON.parse(response.payload);\n\n const validate = ajv.compile(schema);\n if (validate(doc)) {\n resolve(true);\n } else {\n reject(\n Boom.badRequest(\n validate.errors\n .map((error) => {\n return JSON.stringify(error);\n })\n .join(\"\\n\")\n )\n );\n }\n });\n}\n\nmodule.exports = {\n get: {\n path: \"/item/{id}\",\n method: \"GET\",\n options: {\n auth: {\n strategy: \"q-auth-azure-then-ld\",\n mode: \"optional\",\n },\n cors: {\n credentials: true,\n },\n validate: {\n params: {\n id: Joi.string().required(),\n },\n },\n description: \"gets the item with the given id from the database\",\n tags: [\"api\", \"editor\"],\n },\n handler: async function (request, h) {\n return request.server.methods.db.item.getById({\n id: request.params.id,\n ignoreInactive: true,\n session: {\n credentials: request.auth.credentials,\n artifacts: request.auth.artifacts,\n },\n });\n },\n },\n post: {\n path: \"/item\",\n method: \"POST\",\n options: {\n validate: {\n payload: {\n _id: Joi.string().optional(),\n _rev: Joi.forbidden(),\n title: Joi.string().required(),\n tool: Joi.string().required(),\n },\n options: {\n allowUnknown: true,\n },\n },\n auth: {\n strategy: \"q-auth-azure-then-ld\",\n },\n cors: {\n credentials: true,\n },\n description:\n \"stores a new item to the database and returns the id among other things\",\n tags: [\"api\", \"editor\"],\n },\n handler: async function (request, h) {\n let doc = request.payload;\n let now = new Date();\n\n try {\n await validateAgainstSchema(request, doc);\n } catch (err) {\n return err;\n }\n\n // docDiff is used to store all the changed properties\n // to send them back to Q Editor for it to merge it with\n // the existing item state\n let docDiff = {};\n\n doc.createdDate = now.toISOString();\n docDiff.createdDate = doc.createdDate;\n doc.createdBy = request.auth.credentials.name;\n docDiff.createdBy = doc.createdBy;\n\n doc.updatedDate = now.toISOString();\n docDiff.updatedDate = doc.updatedDate;\n doc.updatedBy = request.auth.credentials.name;\n docDiff.updatedBy = doc.updatedBy;\n\n const res = await request.server.methods.db.item.insert({\n doc,\n session: {\n credentials: request.auth.credentials,\n artifacts: request.auth.artifacts,\n },\n });\n\n docDiff._id = res.id;\n docDiff._rev = res.rev;\n\n const savedDoc = Object.assign(doc, docDiff);\n request.server.events.emit(\"item.new\", {\n newItem: savedDoc,\n });\n\n return docDiff;\n },\n },\n put: {\n path: \"/item\",\n method: \"PUT\",\n options: {\n validate: {\n payload: {\n _id: Joi.string().required(),\n _rev: Joi.string().required(),\n title: Joi.string().required(),\n tool: Joi.string().required(),\n },\n options: {\n allowUnknown: true,\n },\n },\n // auth: \"q-auth-azure-then-ld\",\n cors: {\n credentials: true,\n },\n description:\n \"updates an existing item to the database and returns the new revision number among other things\",\n tags: [\"api\", \"editor\"],\n },\n handler: async function (request, h) {\n let doc = request.payload;\n let now = new Date();\n try {\n await validateAgainstSchema(request, doc);\n } catch (err) {\n throw Boom.badRequest(err);\n }\n\n // docDiff is used to store all the changed properties\n // to send them back to Q Editor for it to merge it with\n // the existing item state\n let docDiff = {};\n\n doc.updatedDate = now.toISOString();\n docDiff.updatedDate = doc.updatedDate;\n\n doc.updatedBy = request.auth.credentials.name;\n docDiff.updatedBy = doc.updatedBy;\n\n const oldDoc = await request.server.methods.db.item.getById({\n id: request.payload._id,\n ignoreInactive: true,\n });\n\n // if the active state change to true, we set activateDate\n let isNewActive = false;\n if (doc.active === true && oldDoc.active === false) {\n doc.activateDate = now.toISOString();\n docDiff.activateDate = doc.activateDate;\n isNewActive = true;\n }\n\n // if the active state change to false, we set activateDate\n let isNewInactive = false;\n if (doc.active === false && oldDoc.active === true) {\n doc.deactivateDate = now.toISOString();\n docDiff.deactivateDate = doc.deactivateDate;\n isNewInactive = true;\n }\n\n let isDeleted = false;\n if (doc._deleted === true) {\n isDeleted = true;\n }\n\n const res = await request.server.methods.db.item.insert({\n doc,\n session: {\n credentials: request.auth.credentials,\n artifacts: request.auth.artifacts,\n },\n });\n\n docDiff._rev = res.rev;\n const savedDoc = Object.assign(doc, docDiff);\n\n const eventData = {\n newItem: savedDoc,\n oldItem: oldDoc,\n };\n\n if (isNewActive) {\n request.server.events.emit(\"item.activate\", eventData);\n }\n if (isNewInactive) {\n request.server.events.emit(\"item.deactivate\", eventData);\n }\n if (isDeleted) {\n request.server.events.emit(\"item.delete\", eventData);\n }\n\n request.server.events.emit(\"item.update\", eventData);\n\n return docDiff;\n },\n },\n};\n"
}
]
}