Commit c66cbedd authored by A. Koch's avatar A. Koch

set author on import if missing, use correct profile records when setting...

set author on import if missing, use correct profile records when setting author, rearrange middleware
parent 32005188
......@@ -82,6 +82,12 @@ module.exports.setupArchives = (app, mapService, annotationService) => {
map.title = req.body.title
map.uuid = undefined
}
if (!map.author) {
map.author = {
id: req.user.uuid,
name: req.user.profile.name
}
}
const postRequest = {
body: map,
user: req.user
......@@ -99,6 +105,12 @@ module.exports.setupArchives = (app, mapService, annotationService) => {
annotation.target.id = mappings[annotation.target.id]
annotation.uuid = undefined
}
if (!annotation.author) {
annotation.author = {
id: req.user.uuid,
name: req.user.profile.name
}
}
const postRequest = {
body: annotation,
user: req.user
......@@ -160,15 +172,15 @@ module.exports.createArchive = async (data) => {
module.exports.readArchive = archivePath => {
const results = {}
const getFile = (entry, zipfile) => {
return new Promise((rs, rj) => {
return new Promise((resolve, reject) => {
let data = ''
zipfile.openReadStream(entry, function (err, readStream) {
if (err) return rj(err)
if (err) return reject(err)
readStream.on('data', chunk => {
data += chunk.toString()
})
readStream.on('end', () => rs(data))
readStream.on('error', err => rj(err))
readStream.on('end', () => resolve(data))
readStream.on('error', err => reject(err))
})
})
}
......
......@@ -80,9 +80,6 @@ const setup = async function () {
const addUserUUID = require('./middleware/user')
addUserUUID(app)
const addAuthor = require('./middleware/author')
addAuthor(app)
/**
* Configure Profiles
*/
......@@ -91,6 +88,9 @@ const setup = async function () {
profiles = new Profiles(app)
profiles.on('message', message => winston.debug(message))
const addAuthor = require('./middleware/author')
addAuthor(app, profiles)
/**
* Configure resources
*/
......
const
config = require('config'),
axios = require('axios')
const setup = async function (app) {
const setup = async function (app, profileService) {
app.use(async (req, res, next) => {
const r = {
params: {
id: req.user.uuid
},
user: req.user
}
req.user.profile = await profileService.getHandler(r)
if (req.method.toLowerCase() === 'post') {
if (req.body && req.headers.authorization) {
const profile = await axios.get(`${config.auth.jwt.issuer}userinfo`, {
headers: {
Authorization: req.headers.authorization
}
})
req.body.author = {
id: req.user.uuid,
name: profile.data.name
}
req.body.author = {
id: req.user.uuid,
name: req.user.profile ? req.user.profile.name : undefined
}
}
next()
......
const { ObjectUtil } = require('mbjs-utils')
const setup = async function (app) {
app.use((req, res, next) => {
app.use(async (req, res, next) => {
if (req.user) {
req.user.uuid = ObjectUtil.uuid5(req.user.sub)
}
......
......@@ -14,13 +14,7 @@ class Profiles extends TinyEmitter {
const _this = this
app.get('/profiles/:id', async (req, res) => {
const results = await this.client.find({ user: req.params.id }, req.params)
if (results.length) {
return _this._response(req, res, results[0])
}
send(res, 404)
})
app.get('/profiles/:id', (req, res) => _this.getHandler(req, res))
app.post('/profiles', async (req, res) => {
req.body.uuid = ObjectUtil.uuid4()
......@@ -74,9 +68,26 @@ class Profiles extends TinyEmitter {
})
}
async getHandler (req, res) {
console.log(req.params)
const results = await this._client.find({ user: req.params.id }, req.params)
if (results.length) {
return this._response(req, res, results[0])
}
this._errorResponse(res, 404)
}
_response (req, res, data = {}) {
this.emit('message', { method: req.method, id: data.user })
send(res, 200, data)
this.emit('message', { method: req.method, id: data.id })
if (typeof res === 'function') res({ data })
else if (typeof res === 'undefined') return Promise.resolve({ data })
else send(res, 200, data)
}
_errorResponse (res, code, message = undefined) {
if (typeof res === 'function') res({ error: true, code })
else if (typeof res === 'undefined') return Promise.resolve({ error: true, code })
else send(res, code, message)
}
get client () {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment