UNPKG

@bowtie/sls

Version:

Serverless helpers & utilities

190 lines (145 loc) 4.79 kB
const { Submission } = 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 SubmissionsController extends BaseController { constructor () { super({ model: Submission, defaultSort: 'createdAt' }) } async create(event, context) { console.log('Create Submission Event:', JSON.stringify(event, null, 2)); return await this._exec('create', event, context, false, true) } 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 submission = await Submission.get(event.pathParameters['id']) if (!submission) { return this._not_found() } try { const { headers } = event console.log('Headers', { headers }) const signedUrl = await submission.getSignedUrl('getObject') return this._ok({ 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 = SubmissionsController