Skip to content

Commit

Permalink
fix email example (#144)
Browse files Browse the repository at this point in the history
  • Loading branch information
lao9 authored Jul 9, 2021
1 parent 41f2228 commit eac741f
Showing 1 changed file with 71 additions and 68 deletions.
139 changes: 71 additions & 68 deletions examples/database-email-update/index.js
Original file line number Diff line number Diff line change
@@ -1,94 +1,89 @@
/* ================================================================================
notion-github-sync.
database-update-send-email.
Glitch example: https://glitch.com/edit/#!/notion-github-sync
Glitch example: https://glitch.com/edit/#!/notion-database-email-update
Find the official Notion API client @ https://github.com/makenotion/notion-sdk-js/
================================================================================ */

import { Client } from "@notionhq/client"
import dotenv from "dotenv"
import { Octokit } from "octokit"
import sendgridMail from "@sendgrid/mail"

dotenv.config()
const octokit = new Octokit({ auth: process.env.GITHUB_KEY })
sendgridMail.setApiKey(process.env.SENDGRID_KEY)
const notion = new Client({ auth: process.env.NOTION_KEY })

const database_id = process.env.NOTION_DATABASE_ID

async function syncIssuesWithDatabase() {
console.log("Syncing GitHub Issues with Notion Database")
const issuesInDatabase = await getIssuesFromDatabase()
// A JSON Object to hold all tasks in the Notion database.
let tasksInDatabase = {}

// Get a list of github issues and add them to a local store.
let gitHubIssues = {}
const iterator = octokit.paginate.iterator(octokit.rest.issues.listForRepo, {
owner: process.env.GITHUB_REPO_OWNER,
repo: process.env.GITHUB_REPO_NAME,
per_page: 100,
})
async function findChangesAndSendEmails() {
console.log("Looking for changes in Notion database ")
// Get the tasks currently in the database.
const currTasksInDatabase = await getTasksFromDatabase()

for await (const { data: issues } of iterator) {
for (const issue of issues) {
gitHubIssues[issue.number] = {
id: issue.id,
title: issue.title,
state: issue.state,
comments: issue.comments,
// Iterate over the current tasks and compare them to tasks in our local store (tasksInDatabase).
for (const [key, value] of Object.entries(currTasksInDatabase)) {
const page_id = key
const curr_status = value.Status
// If this task hasn't been seen before.
if (!page_id in tasksInDatabase) {
// Add this task to the local store of all tasks.
tasksInDatabase[page_id] = {
Status: curr_status,
}
} else {
// If the current status is different from the status in the local store.
if (curr_status !== tasksInDatabase[page_id].Status) {
// Change the local store.
tasksInDatabase[page_id] = {
Status: curr_status,
}
// Send an email about this change..
const msg = {
to: process.env.EMAIL_TO_FIELD,
from: process.env.EMAIL_FROM_FIELD,
subject: "Notion Task Status Updated",
text:
"A Notion task's: " +
value.Title +
" status has been updated to " +
curr_status +
".",
}
sendgridMail
.send(msg)
.then(() => {
console.log("Email Sent")
})
.catch(error => {
console.error(error)
})
console.log("Status Changed")
}
}
}
// Run this method every 5 seconds (5000 milliseconds).
setTimeout(main, 5000)
}

// Create new issues or update existing in a Notion Database.
for (const [key, value] of Object.entries(gitHubIssues)) {
const issue_number = key
const issues_details = value
// If the issue does not exist in the database yet, add it to the database.
if (!(issue_number in issuesInDatabase)) {
await notion.request({
path: "pages",
method: "POST",
body: {
parent: { database_id: database_id },
properties: {
State: { name: issues_details.state },
"Issue Number": parseInt(issue_number),
Name: [{ text: { content: issues_details.title } }],
Comments: parseInt(issues_details.comments),
},
},
})
}
// This issue already exists in the database so we want to update the page.
else {
await notion.request({
path: "pages/" + issuesInDatabase[issue_number].page_id,
method: "patch",
body: {
properties: {
State: { name: issues_details.state },
"Issue Number": parseInt(issue_number),
Name: [{ text: { content: issues_details.title } }],
Comments: parseInt(issues_details.comments),
},
},
})
}
}
// Run this function every five minutes.
setTimeout(syncIssuesWithDatabase, 5 * 60 * 1000)
function main() {
findChangesAndSendEmails().catch(console.error)
}

;(async () => {
syncIssuesWithDatabase()
tasksInDatabase = await getTasksFromDatabase()
main()
})()

// Get a paginated list of Tasks currently in a the database.
async function getIssuesFromDatabase() {
const issues = {}
async function getTasksFromDatabase() {
const tasks = {}

async function getPageOfIssues(cursor) {
async function getPageOfTasks(cursor) {
let request_payload = ""
// Create the request payload based on the presence of a start_cursor.
if (cursor == undefined) {
Expand All @@ -109,14 +104,22 @@ async function getIssuesFromDatabase() {
const current_pages = await notion.request(request_payload)

for (const page of current_pages.results) {
issues[page.properties["Issue Number"].number] = {
page_id: page.id,
if (page.properties.Status) {
tasks[page.id] = {
Status: page.properties.Status.select.name,
Title: page.properties.Name.title[0].text.content,
}
} else {
tasks[page.id] = {
Status: "No Status",
Title: page.properties.Name.title[0].text.content,
}
}
}
if (current_pages.has_more) {
await getPageOfIssues(current_pages.next_cursor)
await getPageOfTasks(current_pages.next_cursor)
}
}
await getPageOfIssues()
return issues
await getPageOfTasks()
return tasks
}

0 comments on commit eac741f

Please sign in to comment.