Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ce230c7
commit 3af62e2
Showing
6 changed files
with
346 additions
and
4 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
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,40 @@ | ||
const ObjectsToCsv = require('objects-to-csv') | ||
const dbConnect = require('./helpers/dbConnect') | ||
|
||
// Initialize outside of handler so it can be cached between invokations | ||
let connection | ||
|
||
// TODO: Netlify secret | ||
const apiToken = process.env.ALONZO_FACUET_TOKEN | ||
|
||
exports.handler = async function (event, context) { | ||
// Authenticate the incoming request | ||
if (event.headers.auth !== apiToken) return requestError('Unauthorized', 401) | ||
|
||
// Connect to DB and intitialize the model | ||
context.callbackWaitsForEmptyEventLoop = false | ||
connection = await dbConnect(connection) | ||
|
||
// Load the model | ||
const Address = connection.model('Address') | ||
|
||
// Get the addresses from the database | ||
let addresses = await Address.find().select('-__v') | ||
|
||
// remove mongo magic from the returned array | ||
addresses = addresses.map((addr) => addr.toObject()) | ||
|
||
// Convert array to csv string | ||
addresses = await new ObjectsToCsv(addresses).toString() | ||
|
||
// Return the save adddress object and 201 status | ||
return { | ||
statusCode: 201, | ||
body: addresses | ||
} | ||
} | ||
|
||
const requestError = (message, status = 400) => ({ | ||
statusCode: status, | ||
body: message | ||
}) |
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,25 @@ | ||
const mongoose = require('mongoose') | ||
|
||
// TODO: netlify secret | ||
const uri = process.env.MONGO_URI | ||
|
||
module.exports = async function (connection) { | ||
if (!connection) { | ||
connection = await mongoose.createConnection(uri, { | ||
bufferCommands: false, // Disable mongoose buffering | ||
bufferMaxEntries: 0, // and MongoDB driver buffering | ||
useNewUrlParser: true, | ||
useUnifiedTopology: true | ||
}) | ||
|
||
connection.model( | ||
'Address', | ||
new mongoose.Schema({ | ||
address: String, | ||
created: { type: Date, default: Date.now() } | ||
}) | ||
) | ||
} | ||
|
||
return connection | ||
} |
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,40 @@ | ||
const dbConnect = require('./helpers/dbConnect') | ||
|
||
// Initialize outside of handler so it can be cached between invokations | ||
let connection | ||
|
||
exports.handler = async function (event, context) { | ||
// Grab the address from the request body | ||
let address | ||
try { | ||
address = JSON.parse(event.body).address | ||
} catch (e) { | ||
return requestError('Malformed JSON') | ||
} | ||
|
||
// Validate the provided address | ||
if (!address || address.length !== 103) { | ||
return requestError('Please provide a valid ADA receive address') | ||
} | ||
|
||
// Connect to DB and intitialize the model | ||
context.callbackWaitsForEmptyEventLoop = false | ||
connection = await dbConnect(connection) | ||
|
||
// Load the model | ||
const Address = connection.model('Address') | ||
|
||
// Save the address to the database | ||
const savedAddress = await new Address({ address }).save() | ||
|
||
// Return the save adddress object and 201 status | ||
return { | ||
statusCode: 201, | ||
body: JSON.stringify(savedAddress) | ||
} | ||
} | ||
|
||
const requestError = (message, status = 400) => ({ | ||
statusCode: status, | ||
body: message | ||
}) |
Oops, something went wrong.