diff --git a/index.js b/index.js index c157afc..7e43ffd 100644 --- a/index.js +++ b/index.js @@ -31,3 +31,64 @@ app.post("/add", (req, res) => { const { a, b } = req.body res.send({ result: a + b }) }) + +app.get("/articles", (req, res) => { + 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"), + due_date: record.get("Due Date"), + publish_date: record.get("Publish Date"), + 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() + .map(a => `|${a.title}|${a.in_charge.map(u=>u.name).join(',')}|${a.due_date}|${a.publish_date}|${a.status}|`) + .join("\n")) + res.send({ + response_type: "in_channel", + text: `--- +#### Editorial Calendar + +|Title|In charge|Due Date|Publish Date|Status| +|:----|:--------|:-------|:-----------|:-----| +${articleTable} +---`, + }) + } else { + res.send({ status: "ok", articles: articles.reverse() }) + } + }) +})