feat: map database entry to TrainingSession type
Signed-off-by: Vojtěch Mareš <vojtech@mares.cz>
This commit is contained in:
parent
333969a4fa
commit
fe01054742
1 changed files with 51 additions and 1 deletions
52
src/main.mts
52
src/main.mts
|
|
@ -5,6 +5,19 @@ dotenv.config();
|
|||
|
||||
await main();
|
||||
|
||||
type TrainingSession = {
|
||||
name: string;
|
||||
slug: string;
|
||||
location: string;
|
||||
price: number;
|
||||
date?: string;
|
||||
dates?: {
|
||||
start: string;
|
||||
end: string;
|
||||
},
|
||||
signUpFormURL?: string;
|
||||
}
|
||||
|
||||
async function main() {
|
||||
const notion = new Client({
|
||||
auth: process.env.NOTION_KEY as string,
|
||||
|
|
@ -14,7 +27,44 @@ async function main() {
|
|||
|
||||
const pages = await queryDatabase(notion, databaseID);
|
||||
|
||||
console.log(pages);
|
||||
const sessions: TrainingSession[] = []
|
||||
for (const page of pages) {
|
||||
const session = mapPageToTrainingSession(page);
|
||||
|
||||
if (!session) {
|
||||
continue;
|
||||
}
|
||||
|
||||
sessions.push(session);
|
||||
}
|
||||
|
||||
console.log("");
|
||||
console.log("Sessions:");
|
||||
console.log("========");
|
||||
console.log(sessions);
|
||||
}
|
||||
|
||||
function mapPageToTrainingSession(page: any): TrainingSession | null {
|
||||
const props = page.properties;
|
||||
|
||||
const session: TrainingSession = {
|
||||
name: props.Training.title[0].plain_text,
|
||||
slug: props.meta_slug.select.name,
|
||||
location: props.Location.rich_text[0].plain_text,
|
||||
price: props.Price.number,
|
||||
signUpFormURL: props["Sign up form URL"].url,
|
||||
} as TrainingSession;
|
||||
|
||||
if (props.Date.date.end !== null) {
|
||||
session.dates = {
|
||||
start: props.Date.date.start,
|
||||
end: props.Date.date.end,
|
||||
};
|
||||
} else {
|
||||
session.date = props.Date.date.start;
|
||||
}
|
||||
|
||||
return session;
|
||||
}
|
||||
|
||||
async function queryDatabase(notion: Client, databaseID: string) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue