generated from UoaWDCC/react-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
UADS-53 feat: Integrated CMS with the Socials Component
- Loading branch information
1 parent
d42a607
commit 874ae8f
Showing
5 changed files
with
125 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { Request, Response } from "express"; | ||
import { Client } from "@notionhq/client"; | ||
import { config } from "dotenv"; | ||
config(); | ||
|
||
const notionSecret = process.env.NOTION_SECRET; | ||
const socialID = process.env.SOCIAL_DB_ID; | ||
|
||
const notion = new Client({ | ||
auth: notionSecret, | ||
}); | ||
|
||
type Row = { | ||
Name: { | ||
id: string; | ||
title: { | ||
text: { | ||
content: string; | ||
}; | ||
}[]; | ||
}; | ||
Link: { | ||
id: string; | ||
url: string; | ||
}; | ||
}; | ||
|
||
type rowsStructured = { | ||
name: string; | ||
link: string; | ||
}[]; | ||
|
||
const getSocials = async (req: Request, res: Response) => { | ||
if (!notionSecret || !socialID) { | ||
throw new Error("Missing creds"); | ||
} | ||
|
||
const query = await notion.databases.query({ | ||
database_id: socialID, | ||
}); | ||
|
||
// @ts-ignore | ||
const rows = query.results.map((res) => res.properties) as Row[]; | ||
|
||
const rowsStructured: rowsStructured = rows.map((row) => ({ | ||
name: row.Name.title[0].text.content, | ||
link: row.Link.url, | ||
})); | ||
|
||
const orderedRowsStructured = rowsStructured.reverse(); | ||
|
||
res.status(200).json(orderedRowsStructured); | ||
}; | ||
|
||
export { getSocials }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { Router } from "express"; | ||
import { getSocials } from "../controllers/SocialController"; | ||
|
||
const socialRoutes = Router(); | ||
|
||
socialRoutes.get("/", getSocials); | ||
|
||
export default socialRoutes; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters