Skip to content

Commit

Permalink
Allow links to be in either format
Browse files Browse the repository at this point in the history
  • Loading branch information
cliftonc committed May 26, 2022
1 parent 63f4b65 commit 57d89ec
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 14 deletions.
27 changes: 20 additions & 7 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21151,19 +21151,32 @@ const updateLinks = async (linkDatabaseId, links, { notion }) => {
const existingLinkRows = await notion.databases.query({
database_id: linkDatabaseId
})

const existingLinks = processRows(existingLinkRows)
// Now scan for any that have changed or are new
const linkKeys = Object.keys(links)
for (const link of linkKeys) {
// Lets see if we can find the row
if (existingLinks[link]) {
const linkId = existingLinks[link].id
const linkUrl = existingLinks[link].url
if (links[link] !== linkUrl) { // url has changed
await updateNotionLink(linkId, link, links[link], { notion })
// Check if our links are an object or simple attributes
let newLink
let newLinkUrl
if (links[link].url) {
newLinkUrl = links[link].url
newLink = links[link].title
} else {
newLinkUrl = links[link]
newLink = link
}

// Now lets see if we can find the row
if (existingLinks[newLink]) {
const linkId = existingLinks[newLink].id
const linkUrl = existingLinks[newLink].url

if (newLinkUrl !== linkUrl) { // url has changed
await updateNotionLink(linkId, newLink, newLinkUrl, { notion })
}
} else {
await createNotionLink(linkDatabaseId, link, links[link], { notion })
await createNotionLink(linkDatabaseId, newLink, newLinkUrl, { notion })
}
}
}
Expand Down
27 changes: 20 additions & 7 deletions src/links.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,32 @@ const updateLinks = async (linkDatabaseId, links, { notion }) => {
const existingLinkRows = await notion.databases.query({
database_id: linkDatabaseId
})

const existingLinks = processRows(existingLinkRows)
// Now scan for any that have changed or are new
const linkKeys = Object.keys(links)
for (const link of linkKeys) {
// Lets see if we can find the row
if (existingLinks[link]) {
const linkId = existingLinks[link].id
const linkUrl = existingLinks[link].url
if (links[link] !== linkUrl) { // url has changed
await updateNotionLink(linkId, link, links[link], { notion })
// Check if our links are an object or simple attributes
let newLink
let newLinkUrl
if (links[link].url) {
newLinkUrl = links[link].url
newLink = links[link].title
} else {
newLinkUrl = links[link]
newLink = link
}

// Now lets see if we can find the row
if (existingLinks[newLink]) {
const linkId = existingLinks[newLink].id
const linkUrl = existingLinks[newLink].url

if (newLinkUrl !== linkUrl) { // url has changed
await updateNotionLink(linkId, newLink, newLinkUrl, { notion })
}
} else {
await createNotionLink(linkDatabaseId, link, links[link], { notion })
await createNotionLink(linkDatabaseId, newLink, newLinkUrl, { notion })
}
}
}
Expand Down

0 comments on commit 57d89ec

Please sign in to comment.