2021-02-25 01:36:41 -05:00
|
|
|
const express = require("express")
|
|
|
|
const bodyParser = require("body-parser")
|
2021-02-25 05:05:35 -05:00
|
|
|
const moment = require("moment")
|
2021-02-25 01:49:11 -05:00
|
|
|
const Airtable = require("airtable")
|
2021-02-25 01:36:41 -05:00
|
|
|
|
2021-02-25 01:43:45 -05:00
|
|
|
// Let's load some configuration!
|
|
|
|
require("dotenv").config()
|
|
|
|
|
|
|
|
const port = process.env.SNIPBOT_PORT || 5000
|
|
|
|
|
2021-02-25 01:49:11 -05:00
|
|
|
// Now it's time for setup!
|
|
|
|
Airtable.configure({
|
|
|
|
endpointUrl: process.env.SNIPBOT_AIRTABLE_ENDPOINT,
|
|
|
|
apiKey: process.env.SNIPBOT_AIRTABLE_API_KEY,
|
|
|
|
})
|
|
|
|
|
|
|
|
const base = Airtable.base(process.env.SNIPBOT_AIRTABLE_BASE_ID)
|
|
|
|
|
2021-02-25 01:36:41 -05:00
|
|
|
const app = express()
|
|
|
|
|
|
|
|
app.use(bodyParser.json())
|
|
|
|
|
2021-02-25 01:43:45 -05:00
|
|
|
app.listen(port, () => {
|
|
|
|
console.log(`Server is running on port ${port}.`)
|
2021-02-25 01:36:41 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
app.get("/", (req, res) => {
|
2021-02-25 01:40:09 -05:00
|
|
|
res.send({ status: "ok", message: "woof, woof!" })
|
2021-02-25 01:36:41 -05:00
|
|
|
})
|
2021-02-25 01:37:08 -05:00
|
|
|
|
|
|
|
app.post("/add", (req, res) => {
|
|
|
|
const { a, b } = req.body
|
|
|
|
res.send({ result: a + b })
|
|
|
|
})
|
2021-02-25 02:42:08 -05:00
|
|
|
|
2021-02-25 02:46:05 -05:00
|
|
|
app.post("/edcal", (req, res) => {
|
2021-02-25 02:42:08 -05:00
|
|
|
let articles = []
|
|
|
|
|
|
|
|
base('Articles').select({
|
|
|
|
maxRecords: 10,
|
|
|
|
view: "Calendar",
|
|
|
|
fields: [
|
|
|
|
"Title",
|
|
|
|
"Author",
|
|
|
|
"Due Date",
|
|
|
|
"Publish Date",
|
|
|
|
"In charge",
|
|
|
|
"Status",
|
|
|
|
],
|
|
|
|
filterByFormula: "NOT({Status} = 'Stalled')",
|
|
|
|
sort: [
|
|
|
|
{ field: "Publish Date", direction: "desc" },
|
|
|
|
{ field: "Status", direction: "asc" },
|
|
|
|
],
|
|
|
|
}).eachPage(function page(records, fetchNextPage) {
|
|
|
|
records.forEach((record) => {
|
|
|
|
console.log(`Got ${record.get('Title')}`)
|
|
|
|
articles.push({
|
|
|
|
title: record.get("Title"),
|
|
|
|
author: record.get("Author"),
|
2021-02-25 05:05:35 -05:00
|
|
|
due_date: moment(record.get("Due Date")).format("DD MMM"),
|
|
|
|
publish_date: moment(record.get("Publish Date")).format("DD MMM YYYY"),
|
2021-02-25 02:42:08 -05:00
|
|
|
in_charge: record.get("In charge"),
|
|
|
|
status: record.get("Status"),
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
fetchNextPage()
|
|
|
|
}, function done(err) {
|
|
|
|
if (err) {
|
|
|
|
console.error(err)
|
|
|
|
res.send({ status: "error", "error": "An error occurred :("})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if (req.body.command != 'edcal') {
|
|
|
|
articleTable = (articles
|
|
|
|
.reverse()
|
2021-02-25 05:08:27 -05:00
|
|
|
.map(a => `|${a.title}|${a.in_charge.map(u=>u.name[0]).join(',')}|${a.due_date}|${a.publish_date}|${a.status}|`)
|
2021-02-25 02:42:08 -05:00
|
|
|
.join("\n"))
|
|
|
|
res.send({
|
|
|
|
response_type: "in_channel",
|
2021-02-25 05:11:09 -05:00
|
|
|
text: `
|
|
|
|
#### Editorial Calendar #are
|
2021-02-25 02:42:08 -05:00
|
|
|
|
2021-02-25 05:13:27 -05:00
|
|
|
|:roundpup: Title|PP|Due Date|Publish Date|Status|
|
|
|
|
|:---------------|:-|:-------|:-----------|:-----|
|
2021-02-25 02:42:08 -05:00
|
|
|
${articleTable}
|
2021-02-25 05:11:09 -05:00
|
|
|
`,
|
2021-02-25 02:42:08 -05:00
|
|
|
})
|
|
|
|
} else {
|
|
|
|
res.send({ status: "ok", articles: articles.reverse() })
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|