-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Add upload model for uploading attachments functionality - Add save, delete and disable file behaviours for uploading attachments functionality - Add fields and translations for uploading attachments - Add views for uploading attachments - Add validation for number of attachments added - Add ui and unit tests
- Loading branch information
1 parent
d49985a
commit 84527e4
Showing
27 changed files
with
710 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
module.exports = superclass => class extends superclass { | ||
locals(req, res) { | ||
const locals = super.locals(req, res); | ||
const images = req.sessionModel.get('images'); | ||
if (images && images.length >= 3) { | ||
// disable file upload if attachment limit reached. | ||
req.form.options.fields['other-info-file-upload'].attributes = [{attribute: 'disabled'}]; | ||
return locals; | ||
} | ||
req.form.options.fields['other-info-file-upload'].attributes = []; | ||
return locals; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
module.exports = superclass => class LimitDocs extends superclass { | ||
validate(req, res, next) { | ||
const images = req.sessionModel.get('images'); | ||
if (images && images.length >= 3 && req.form.values['other-info-file-uploads-add-another'] === 'yes') { | ||
return next({ | ||
'other-info-file-uploads-add-another': new this.ValidationError( | ||
'other-info-file-uploads-add-another', | ||
{ | ||
type: 'tooMany' | ||
} | ||
) | ||
}); | ||
} super.validate(req, res, next); | ||
return next; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
'use strict'; | ||
|
||
module.exports = superclass => class extends superclass { | ||
configure(req, res, next) { | ||
if (req.query.delete) { | ||
const images = req.sessionModel.get('images') || []; | ||
const remaining = images.filter(i => i.id !== req.query.delete); | ||
req.log('info', `Reference: ${req.sessionModel.get('reference')}, Removing image: ${req.query.delete}`); | ||
req.sessionModel.set('images', remaining); | ||
const path = req.baseUrl + req.path; | ||
return res.redirect(path); | ||
} | ||
return super.configure(req, res, next); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
'use strict'; | ||
|
||
const _ = require('lodash'); | ||
const Model = require('../models/file-upload'); | ||
|
||
module.exports = name => superclass => class extends superclass { | ||
process(req) { | ||
if (req.files && req.files[name]) { | ||
// set image name on values for filename extension validation | ||
// N:B validation controller gets values from | ||
// req.form.values and not on req.files | ||
req.form.values[name] = req.files[name].name; | ||
req.log('info', `Reference: ${req.sessionModel.get('reference')}, | ||
Processing image: ${req.form.values[name]}`); | ||
} | ||
super.process.apply(this, arguments); | ||
} | ||
|
||
locals(req, res, next) { | ||
if (!Object.keys(req.form.errors).length) { | ||
req.form.values['other-info-file-upload'] = null; | ||
} | ||
return super.locals(req, res, next); | ||
} | ||
|
||
saveValues(req, res, next) { | ||
const images = req.sessionModel.get('images') || []; | ||
if (req.files && req.files[name]) { | ||
req.log('info', `Reference: ${req.sessionModel.get('reference')}, Saving image: ${req.files[name].name}`); | ||
const image = _.pick(req.files[name], ['name', 'data', 'mimetype']); | ||
const model = new Model(image); | ||
return model.save() | ||
.then(() => { | ||
req.sessionModel.set('images', [...images, model.toJSON()]); | ||
return super.saveValues(req, res, next); | ||
}) | ||
.catch(next); | ||
} | ||
return super.saveValues.apply(this, arguments); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
'use strict'; | ||
|
||
const url = require('url'); | ||
const Model = require('hof').model; | ||
const uuid = require('uuid').v4; | ||
const config = require('../../../config'); | ||
|
||
module.exports = class UploadModel extends Model { | ||
constructor(...args) { | ||
super(...args); | ||
this.set('id', uuid()); | ||
} | ||
|
||
async save() { | ||
const result = await new Promise((resolve, reject) => { | ||
const attributes = { | ||
url: config.upload.hostname | ||
}; | ||
const reqConf = url.parse(this.url(attributes)); | ||
reqConf.formData = { | ||
document: { | ||
value: this.get('data'), | ||
options: { | ||
filename: this.get('name'), | ||
contentType: this.get('mimetype') | ||
} | ||
} | ||
}; | ||
reqConf.method = 'POST'; | ||
this.request(reqConf, (err, data) => { | ||
if (err) { | ||
return reject(err); | ||
} | ||
resolve(data); | ||
}); | ||
}); | ||
this.set({ url: result.url }); | ||
return this.unset('data'); | ||
} | ||
|
||
auth() { | ||
if (!config.keycloak.token) { | ||
// eslint-disable-next-line no-console | ||
console.error('keycloak token url is not defined'); | ||
return Promise.resolve({ | ||
bearer: 'abc123' | ||
}); | ||
} | ||
const tokenReq = { | ||
url: config.keycloak.token, | ||
form: { | ||
username: config.keycloak.username, | ||
password: config.keycloak.password, | ||
grant_type: 'password', | ||
client_id: config.keycloak.clientId, | ||
client_secret: config.keycloak.secret | ||
}, | ||
method: 'POST' | ||
}; | ||
|
||
return new Promise((resolve, reject) => { | ||
this._request(tokenReq, (err, response) => { | ||
const body = JSON.parse(response.body); | ||
|
||
if (err || body.error) { | ||
return reject(err || new Error(`${body.error} - ${body.error_description}`)); | ||
} | ||
|
||
resolve({ | ||
bearer: JSON.parse(response.body).access_token | ||
}); | ||
}); | ||
}); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.