UNPKG

@bowtie/sls

Version:

Serverless helpers & utilities

233 lines (178 loc) 5.77 kB
const { Document, Audit } = require('../models') const BaseController = require('./BaseController') function getRandomInt(max) { return Math.floor(Math.random() * Math.floor(max)) + 1 } const compareLength = (a, b) => { if (a.length < b.length) return 1 if (b.length < a.length) return -1 return 0 } const compareCreated = (a, b) => { if (a['createdAt'] < b['createdAt']) return 1 if (b['createdAt'] < a['createdAt']) return -1 return 0 } class DocumentsController extends BaseController { constructor () { super({ model: Document, defaultSort: 'createdAt' }) } async audits(event, context) { if (!event.pathParameters || !event.pathParameters['id']) { return this._bad() } const action = 'audits' const authorize = (await this._authorize(action, event, context)) if (authorize !== true) { return authorize } const document = await Document.get(event.pathParameters['id']) if (!document) { return this._not_found() } try { const { headers } = event console.log('Headers', { headers }) const audits = await Audit.scanAll({ documentId: document.id }) audits.sort(compareCreated) return this._ok(audits) } catch(err) { return this._bad(err) } } async download(event, context) { if (!event.pathParameters || !event.pathParameters['id']) { return this._bad() } const action = 'download' const authorize = (await this._authorize(action, event, context)) if (authorize !== true) { return authorize } const access = await this._authorizedAccess(action, event, context) if (!access || !access.user) { return this._unauthorized() } const document = await Document.get(event.pathParameters['id']) if (!document) { return this._not_found() } if (!document.permissions || !document.permissions.allow || !document.permissions.allow.includes(access.permission)) { return this._forbidden() } try { const { headers } = event console.log('Headers', { headers }) let audit = new Audit({ // TODO: Implement current username/login user: access.user.login || access.user.nickname || access.user.name, operation: 'getObject', documentId: document.id, lifetime: document.lifetime }) await audit.saveNotify() const signedUrl = await audit.getSignedUrl() return this._ok({ audit, signedUrl }) } catch(err) { return this._bad(err) } } // async _exec(action, event, context) { // const { // path, // headers = {}, // pathParameters = {}, // requestContext, // resource, // httpMethod, // queryStringParameters = {}, // multiValueQueryStringParameters, // stageVariables, // body, // isOffline // } = event // const authorize = await this._authorize(action, event, context) // if (authorize !== true) { // return authorize // } // let parsedBody = null // try { // if (body) { // parsedBody = JSON.parse(body) // } // const compare = (a, b) => { // if (a[this.defaultSort] < b[this.defaultSort]) return 1 // if (b[this.defaultSort] < a[this.defaultSort]) return -1 // return 0 // } // switch(action) { // case 'index': // const queryParms = {} // if (queryStringParameters) { // // TODO: Expand on this ... enable .contains() .in(), etc ... whitelist allowed fileter params? // // Object.keys(queryStringParameters).forEach(key => queryParms[key] = { eq: queryStringParameters[key] }) // Object.assign(queryParms, queryStringParameters) // } // let result = [] // if (Object.keys(queryParms).length > 0) { // result = await this.model.scanAll(queryParms) // // return this._ok(filtered) // } else { // result = await this.model.scanAll() // // return this._ok(all) // } // // console.log(result) // result.sort(compare) // return this._ok(result) // case 'create': // const newItem = new this.model(parsedBody) // await newItem.saveNotify() // return this._created(newItem) // case 'show': // case 'update': // case 'destroy': // if (!pathParameters['id']) { // return this._bad() // } // const item = await this.model.get(pathParameters['id']) // if (!item) { // return this._not_found() // } // if (action === 'update') { // Object.assign(item, parsedBody) // await item.saveNotify() // } else if (action === 'destroy') { // await item.delete() // } // return this._ok(item) // default: // return this._bad() // } // } catch (err) { // console.error('Caught error:', err) // return this._bad({ message: err.message || err }) // } // } // async index(event, context) { // return await this._exec('index', event, context) // } // async create(event, context) { // return await this._exec('create', event, context) // } // async show(event, context) { // return await this._exec('show', event, context) // } // async update(event, context) { // return await this._exec('update', event, context) // } // async destroy(event, context) { // return await this._exec('destroy', event, context) // } } module.exports = DocumentsController