Skip to content

Instantly share code, notes, and snippets.

@catalinpit
Created May 27, 2021 06:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save catalinpit/5f419e72a6b56bf0bd51e1f1b700f1c0 to your computer and use it in GitHub Desktop.
Save catalinpit/5f419e72a6b56bf0bd51e1f1b700f1c0 to your computer and use it in GitHub Desktop.
src/controllers/controllers.js
const { notion } = require('../notion/client');
const getAllApplications = async (req, res) => {
const data = await notion.databases.query({
database_id: process.env.DB_ID
});
const pages = data.results.map(page => {
return {
id: page.id,
created: page.created_time,
updated: page.last_edited_time,
company: page.properties.Company.title[0].plain_text,
position: page.properties.Position.select.name,
status: page.properties.Status.select.name,
deadline: page.properties['Next Deadline'].date.start,
jobDescription: page.properties['Job Description'].url,
comments: page.properties.Comments.rich_text[0].plain_text
}
});
return pages;
};
const getApplication = async (req, res) => {
const page = await notion.pages.retrieve({
page_id: req.params.id
});
return {
id: page.id,
created: page.created_time,
updated: page.last_edited_time,
company: page.properties.Company.title[0].plain_text,
position: page.properties.Position.select.name,
status: page.properties.Status.select.name,
deadline: page.properties['Next Deadline'].date.start,
jobDescription: page.properties['Job Description'].url,
comments: page.properties.Comments.rich_text[0].plain_text
};
};
const addApplication = async (req, res) => {
const newPage = await notion.pages.create({
parent: {
database_id: process.env.DB_ID
},
properties: {
Company: {
title: [
{
text: {
content: req.body.company
}
}
]
},
Position: {
select: {
name: req.body.position
}
},
Status: {
select: {
name: req.body.status
}
},
'Next Deadline': {
date: {
start: req.body.deadline
}
},
'Job Description': {
url: req.body.url
},
Comments: {
rich_text: [
{
text: {
content: req.body.comments
}
}
]
}
}
});
return newPage;
};
const updateApplication = async (req, res) => {
const updatedPage = await notion.pages.update({
page_id: req.params.id,
properties: {
Company: {
title: [
{
text: {
content: req.body.company
}
}
]
},
Position: {
select: {
name: req.body.position
}
},
Status: {
select: {
name: req.body.status
}
},
'Next Deadline': {
date: {
start: req.body.deadline
}
},
'Job Description': {
url: req.body.url
},
Comments: {
rich_text: [
{
text: {
content: req.body.comments
}
}
]
}
}
});
return updatedPage;
};
const filterApplications = async (req, res) => {
const data = await notion.databases.query({
database_id: process.env.DB_ID,
filter: {
property: 'Company',
text: {
contains: req.query.company || ''
}
}
});
const pages = data.results.map(page => {
return {
id: page.id,
created: page.created_time,
updated: page.last_edited_time,
company: page.properties.Company.title[0].plain_text,
position: page.properties.Position.select.name,
status: page.properties.Status.select.name,
deadline: page.properties['Next Deadline'].date.start,
jobDescription: page.properties['Job Description'].url,
comments: page.properties.Comments.rich_text[0].plain_text
}
});
return pages;
};
module.exports = {
getAllApplications,
getApplication,
addApplication,
updateApplication,
filterApplications
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment