Skip to content

Commit

Permalink
Merge pull request #1648 from serlo/change-links-to-course-pages
Browse files Browse the repository at this point in the history
change migration as requested
  • Loading branch information
hugotiburtino authored Jul 3, 2024
2 parents 9542b53 + d3ab169 commit 6aa2247
Showing 1 changed file with 23 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,16 @@ export async function up(db: Database) {
const apiCache = new ApiCache()

const coursePages = await db.runSql<CoursePage[]>(`
WITH RankedPages AS (
SELECT
entity.id AS coursePageId,
ent2.id AS courseId,
ROW_NUMBER() OVER (PARTITION BY ent2.id ORDER BY entity.id) AS page_rank
FROM entity
JOIN entity_link ON entity.id = entity_link.child_id
JOIN entity ent2 ON entity_link.parent_id = ent2.id
JOIN uuid ON entity.id = uuid.id
WHERE entity.type_id = 8
AND uuid.trashed = 0
AND entity.current_revision_id IS NOT NULL
)
SELECT coursePageId, courseId
FROM RankedPages
WHERE page_rank > 1
SELECT
entity.id AS coursePageId,
ent2.id AS courseId
FROM entity
JOIN entity_link ON entity.id = entity_link.child_id
JOIN entity ent2 ON entity_link.parent_id = ent2.id
JOIN uuid ON entity.id = uuid.id
WHERE entity.type_id = 8
AND uuid.trashed = 0
AND entity.current_revision_id IS NOT NULL
`)

await migrateSerloEditorContent({
Expand Down Expand Up @@ -81,12 +75,19 @@ function replaceLinks(object: object, coursePages: CoursePage[]) {

if ((startsWithSlash || containsSerlo) && !isAnAttachment) {
coursePages.forEach((coursePage) => {
const { courseId, coursePageId } = coursePage

object.href = object.href.replace(
coursePageId.toString(),
`${courseId}/${coursePageId}`,
)
const { coursePageId, courseId } = coursePage
const regex = new RegExp(`/${coursePageId}(?:/|$)`)

if (regex.test(object.href)) {
const isFirstPage =
coursePages.find((page) => page.courseId === courseId)
?.coursePageId === coursePageId
if (isFirstPage) {
object.href = `/${courseId}`
} else {
object.href = `/${courseId}#${coursePageId}`
}
}
})
}
}
Expand Down

0 comments on commit 6aa2247

Please sign in to comment.