generated from UoaWDCC/ssr-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.
Refactor: Changed owens function to read off database
- Loading branch information
Showing
8 changed files
with
181 additions
and
81 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
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
This file was deleted.
Oops, something went wrong.
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,43 @@ | ||
import { PrismaClient } from '@prisma/client'; | ||
|
||
// Initialize Prisma Client | ||
const prisma = new PrismaClient(); | ||
|
||
type TeamDescriptions = { | ||
name: string; | ||
description?: string; | ||
tags?: string[]; // Array of tag names | ||
}; | ||
|
||
/** | ||
* Fetches team descriptions with their tags from the database. | ||
* @returns {Promise<TeamDescriptions[]>} An array of team descriptions with tags. | ||
*/ | ||
export async function serveTeamDescriptions(): Promise<TeamDescriptions[]> { | ||
let teamDescriptions: TeamDescriptions[] = []; // Explicitly type the result variable | ||
|
||
try { | ||
// Fetch team descriptions with their related tags | ||
const descriptions = await prisma.teamDescription.findMany({ | ||
include: { | ||
tags: true, // Fetch related tags | ||
}, | ||
}); | ||
|
||
// Map the results to the TeamDescriptions type | ||
teamDescriptions = descriptions.map((team) => ({ | ||
name: team.name, | ||
description: team.description || undefined, | ||
tags: team.tags.map((tag) => tag.name) || undefined, // Convert tag objects to an array of tag names | ||
})); | ||
|
||
} catch (err) { | ||
console.error("Error fetching team descriptions:", err); | ||
teamDescriptions = []; // Fallback to an empty array in case of error | ||
} finally { | ||
// Close Prisma Client connection | ||
await prisma.$disconnect(); | ||
} | ||
|
||
return teamDescriptions; | ||
} |
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,72 @@ | ||
import { Client } from "@notionhq/client"; | ||
import { PrismaClient } from '@prisma/client'; // Import PrismaClient | ||
import type { teamRow } from "../types/teamRow"; | ||
|
||
type TeamDescriptions = { | ||
name: string; | ||
description?: string; | ||
tags?: string[]; | ||
}; | ||
|
||
export async function updateTeamsDescriptions(): Promise<void> { | ||
const NOTION_TOKEN = process.env.NOTION_TOKEN || import.meta.env.NOTION_TOKEN; | ||
const NOTION_TEAMS_ID = process.env.NOTION_TEAMS_ID || import.meta.env.NOTION_TEAMS_ID; | ||
if (!NOTION_TOKEN || !NOTION_TEAMS_ID) throw new Error("Missing secret(s)"); | ||
|
||
const notion = new Client({ auth: NOTION_TOKEN }); | ||
const prisma = new PrismaClient(); | ||
|
||
const query = await notion.databases.query({ | ||
database_id: NOTION_TEAMS_ID, | ||
sorts: [{ | ||
property: 'Name', | ||
direction: 'ascending' | ||
}] | ||
}); | ||
|
||
const teampages = query.results as teamRow[]; | ||
|
||
const teams: TeamDescriptions[] = teampages.map((row) => { | ||
return { | ||
name: row.properties.Name.title[0] ? row.properties.Name.title[0].plain_text : "", | ||
description: row.properties.Description.rich_text[0] ? row.properties.Description.rich_text[0].plain_text : "", | ||
tags: row.properties.Tags.multi_select?.map(tag => tag.name) || [], // Use optional chaining and provide default | ||
}; | ||
}); | ||
|
||
// Store teams in the Prisma database | ||
for (const team of teams) { | ||
// Ensure tags is an array to avoid 'undefined' error | ||
const tagConnections = await Promise.all( | ||
(team.tags || []).map(async (tagName) => { // Use a fallback to an empty array if tags is undefined | ||
const tag = await prisma.teamTag.upsert({ | ||
where: { name: tagName }, | ||
create: { name: tagName }, | ||
update: {}, | ||
}); | ||
return { id: tag.id }; // Return the tag id for connecting | ||
}) | ||
); | ||
|
||
// Upsert the team description | ||
await prisma.teamDescription.upsert({ | ||
where: { name: team.name }, | ||
create: { | ||
name: team.name, | ||
description: team.description, | ||
tags: { | ||
connect: tagConnections, // Connect the tags | ||
}, | ||
}, | ||
update: { | ||
description: team.description, | ||
tags: { | ||
set: tagConnections, // Update the tags | ||
}, | ||
}, | ||
}); | ||
} | ||
|
||
console.log("Team descriptions uploaded to Prisma database."); | ||
await prisma.$disconnect(); | ||
} |
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,5 @@ | ||
export type TeamDescriptions = { | ||
name: string; | ||
description?: string; | ||
tags?: string[]; | ||
} |
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