Skip to content

Commit

Permalink
Use new creator middleware in scheme service (#122)
Browse files Browse the repository at this point in the history
  • Loading branch information
stefandesu committed Nov 13, 2020
1 parent 165dda6 commit 9a2f61b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 24 deletions.
2 changes: 2 additions & 0 deletions routes/schemes.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ if (config.schemes.update) {
utils.wrappers.async(async (req) => {
return await schemeService.putScheme({
body: req.body,
existing: req.existing,
})
}),
utils.adjust,
Expand All @@ -58,6 +59,7 @@ if (config.schemes.delete) {
utils.wrappers.async(async (req) => {
return await schemeService.deleteScheme({
uri: req.query.uri,
existing: req.existing,
})
}),
(req, res) => res.sendStatus(204),
Expand Down
34 changes: 10 additions & 24 deletions services/schemes.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,18 +161,14 @@ module.exports = class SchemeService {
return isMultiple ? response : response[0]
}

async putScheme({ body }) {
async putScheme({ body, existing }) {
let scheme = body

// Prepare
scheme = await this.prepareAndCheckSchemeForAction(scheme, "update")

const existingScheme = await Scheme.findById(scheme.uri).lean()
if (!existingScheme) {
throw new EntityNotFoundError()
}
if (existingScheme.created) {
scheme.created = existingScheme.created
if (existing.created) {
scheme.created = existing.created
}

// Write scheme to database
Expand All @@ -189,12 +185,16 @@ module.exports = class SchemeService {
return scheme
}

async deleteScheme({ uri }) {
async deleteScheme({ uri, existing }) {
if (!uri) {
throw new MalformedRequestError()
}
const scheme = await this.prepareAndCheckSchemeForAction({ uri }, "delete")
const result = await Scheme.deleteOne({ _id: scheme._id })
if (existing.concepts.length) {
// Disallow deletion
// ? Which error type?
throw new MalformedRequestError(`Concept scheme ${uri} still has concepts in the database and therefore can't be deleted.`)
}
const result = await Scheme.deleteOne({ _id: existing._id })
if (!result.ok) {
throw new DatabaseAccessError()
}
Expand Down Expand Up @@ -232,20 +232,6 @@ module.exports = class SchemeService {
delete scheme.created
}
}
if (action == "delete") {
// Replace scheme with scheme from databas
const uri = scheme.uri
scheme = await Scheme.findById(uri).lean()
if (!scheme) {
throw new EntityNotFoundError(null, uri)
}
// Check if concepts exists
if (scheme.concepts.length) {
// Disallow deletion
// ? Which error type?
throw new MalformedRequestError(`Concept scheme ${uri} still has concepts in the database and therefore can't be deleted.`)
}
}

return scheme
}
Expand Down

0 comments on commit 9a2f61b

Please sign in to comment.