Skip to content

Commit

Permalink
Merge pull request #328 from vevcom/chore/events-migrations
Browse files Browse the repository at this point in the history
Chore/events migrations
  • Loading branch information
Paulijuz authored Nov 8, 2024
2 parents aab5541 + f375d3a commit bba5a7e
Show file tree
Hide file tree
Showing 11 changed files with 175 additions and 25 deletions.
2 changes: 1 addition & 1 deletion docs
Submodule docs updated from 3cc94e to 97b8ef
2 changes: 2 additions & 0 deletions src/prisma/prismaservice/src/dobbelOmega/dobbelOmega.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { getLimits } from './migrationLimits'
import migrateOmegaquotes from './migrateOmegaquotes'
import migrateArticles from './migateArticles'
import migrateMailAliases from './migrateMailAlias'
import migrateEvents from './migrateEvents'
import { PrismaClient as PrismaClientVeven } from '@/generated/veven'
import type { PrismaClient as PrismaClientPn } from '@/generated/pn'

Expand All @@ -26,6 +27,7 @@ export default async function dobbelOmega(pnPrisma: PrismaClientPn) {
await migrateOmegaquotes(pnPrisma, vevenPrisma, limits)
await migrateArticles(pnPrisma, vevenPrisma, imageIdMap, limits)
await migrateMailAliases(pnPrisma, vevenPrisma, limits)
await migrateEvents(pnPrisma, vevenPrisma, imageIdMap, limits)

vevenPrisma.$disconnect()
console.log('=======Dobbel Omega ferdig, dagen derpå=======')
Expand Down
19 changes: 2 additions & 17 deletions src/prisma/prismaservice/src/dobbelOmega/migateArticles.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { vevenIdToPnId } from './IdMapper'
import upsertOrderBasedOnDate from './upsertOrderBasedOnDate'
import type { PrismaClient as PrismaClientPn } from '@/generated/pn'
import type { PrismaClient as PrismaClientVeven } from '@/generated/veven'
import type { IdMapper } from './IdMapper'
Expand Down Expand Up @@ -84,23 +85,7 @@ export default async function migrateArticles(
}
})

// The order is assumed to change 1. september, calculate by createdAt
// 1. september 1914 = order 1, 1. september 1915 = order 2, ...
let orderPublished = new Date(article.createdAt).getFullYear() - 1914
if (new Date(article.createdAt).getMonth() < 8) {
orderPublished--
}
await pnPrisma.omegaOrder.upsert({
where: {
order: orderPublished,
},
update: {
order: orderPublished,
},
create: {
order: orderPublished,
}
})
const orderPublished = await upsertOrderBasedOnDate(pnPrisma, article.createdAt)

return {
...articlePn,
Expand Down
121 changes: 121 additions & 0 deletions src/prisma/prismaservice/src/dobbelOmega/migrateEvents.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import { vevenIdToPnId, type IdMapper } from './IdMapper'
import upsertOrderBasedOnDate from './upsertOrderBasedOnDate'
import type { PrismaClient as PrismaClientPn } from '@/generated/pn'
import type { PrismaClient as PrismaClientVeven } from '@/generated/veven'
import type { Limits } from './migrationLimits'

export default async function migrateEvents(
pnPrisma: PrismaClientPn,
vevenPrisma: PrismaClientVeven,
imageIdMap: IdMapper,
limits: Limits
) {
const events = await vevenPrisma.events.findMany({
take: limits.events ? limits.events : undefined,
include: {
Images: true,
Committees: true,
EventRegistrations: true,
}
})
//Make sure no events have same title and order
events.forEach((event) => {
const sameTitle = events.filter(e => e.title === event.title)
if (sameTitle.length > 1) {
sameTitle.forEach((e, i) => {
e.title = `${e.title} (${i + 1})`
})
}
})

await Promise.all(events.map(async event => {
const coverId = vevenIdToPnId(imageIdMap, event.ImageId)
const coverIage = await pnPrisma.cmsImage.create({
data: {
image: coverId ? {
connect: {
id: coverId,
}
} : undefined
}
})
const paragraph = await pnPrisma.cmsParagraph.create({
data: {
contentHtml: event.text || '',
createdAt: event.createdAt,
updatedAt: event.updatedAt,
}
})

const order = await upsertOrderBasedOnDate(pnPrisma, event.createdAt)

await pnPrisma.event.create({
data: {
name: event.title,
order,
createdAt: event.createdAt,
updatedAt: event.updatedAt,
canBeViewdBy: 'ALL',
takesRegistration: !!event.places && (event.places > 0),
places: event.places || 0,
eventStart: event.eventDate ?? event.createdAt,
eventEnd: event.eventDate ?? event.createdAt,
registrationStart: event.registrationStart ?? event.createdAt,
registrationEnd: event.registrationDeadline ?? event.createdAt,
coverImageId: coverIage.id,
cmsParagraphId: paragraph.id,
}
})
}))

const simpleEvents = await vevenPrisma.simpleEvents.findMany({
take: limits.events ? limits.events : undefined,
include: {
Committees: true,
}
})
//Make sure no events have same title and order
simpleEvents.forEach((event) => {
const sameTitle = simpleEvents.filter(e => e.title === event.title)
if (sameTitle.length > 1) {
sameTitle.forEach((e, i) => {
e.title = `${e.title} (${i + 1})`
})
}
})

await Promise.all(simpleEvents.map(async simpleEvent => {
const coverIage = await pnPrisma.cmsImage.create({
data: {
image: undefined
}
})
const paragraph = await pnPrisma.cmsParagraph.create({
data: {
contentHtml: simpleEvent.text || '',
createdAt: simpleEvent.createdAt,
updatedAt: simpleEvent.updatedAt,
}
})

const order = await upsertOrderBasedOnDate(pnPrisma, simpleEvent.createdAt)

await pnPrisma.event.create({
data: {
name: simpleEvent.title,
order,
createdAt: simpleEvent.createdAt,
updatedAt: simpleEvent.updatedAt,
canBeViewdBy: 'ALL',
takesRegistration: false,
places: 0,
eventStart: simpleEvent.eventDate ?? simpleEvent.createdAt,
eventEnd: simpleEvent.eventDate ?? simpleEvent.createdAt,
registrationStart: simpleEvent.createdAt,
registrationEnd: simpleEvent.createdAt,
coverImageId: coverIage.id,
cmsParagraphId: paragraph.id,
}
})
}))
}
10 changes: 8 additions & 2 deletions src/prisma/prismaservice/src/dobbelOmega/migrateImages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,12 @@ export default async function migrateImages(
create: {
purpose: 'IMAGE',
published: true,
regularLevel: {},
adminLevel: {},
regularLevel: {
create: {}
},
adminLevel: {
create: {}
},
}
}
},
Expand All @@ -55,6 +59,7 @@ export default async function migrateImages(
include: {
Ombul: true,
Articles: true,
Events: true,
}
})

Expand All @@ -74,6 +79,7 @@ export default async function migrateImages(
if (!limits.numberOffFullImageCollections) return true
if (image.Ombul.length) return true
if (image.Articles.length) return true
if (image.Events.length) return true
if (image.collectionId === ombulCollection.id) return true
if (image.ImageGroupId && image.ImageGroupId < limits.numberOffFullImageCollections) return true
return false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export default async function migrateMailAliases(
vevenPrisma: PrismaClientVeven,
limits: Limits,
) {
if (limits.mailaliases === 0) return
const aliases = await vevenPrisma.mailAliases.findMany({
take: limits.mailaliases ?? undefined,
include: {
Expand Down
4 changes: 3 additions & 1 deletion src/prisma/prismaservice/src/dobbelOmega/migrationLimits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@ export function getLimits() {
numberOffFullImageCollections: 3,
omegaquotes: null,
articles: 300,
mailaliases: null,
mailaliases: 0,
events: null,
}
const nullObj: { [key in keyof typeof limits]: null } = {
ombul: null,
numberOffFullImageCollections: null,
omegaquotes: null,
articles: null,
mailaliases: null,
events: null,
}

const limitsOn = process.env.MIGRATION_WITH_LIMITS !== 'false'
Expand Down
30 changes: 30 additions & 0 deletions src/prisma/prismaservice/src/dobbelOmega/upsertOrderBasedOnDate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import type { PrismaClient as PrismaClientPn } from '@/generated/pn'

/**
* Veven did not have the consept of omegaOrder. This function infers the order based on a date and
* also upserts the order into the database.
* @param date - Date of thing
*/
export default async function upsertOrderBasedOnDate(
pnPrisma: PrismaClientPn,
date: Date
): Promise<number> {
// The order is assumed to change 1. september, calculate by createdAt
// 1. september 1914 = order 1, 1. september 1915 = order 2, ...
let orderPublished = new Date(date).getFullYear() - 1914
if (new Date(date).getMonth() < 8) {
orderPublished--
}
await pnPrisma.omegaOrder.upsert({
where: {
order: orderPublished,
},
update: {
order: orderPublished,
},
create: {
order: orderPublished,
}
})
return orderPublished
}
2 changes: 1 addition & 1 deletion src/prisma/schema/cms.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ enum SpecialCmsParagraph {
FRONTPAGE_2
FRONTPAGE_3
FRONTPAGE_4
INTEREST_GROUP_GENERAL_INFO
CAREER_INFO
}
Expand Down
8 changes: 5 additions & 3 deletions src/prisma/schema/event.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ model Event {
coverImageId Int
eventTagEvents EventTagEvent[]
eventStart DateTime
eventEnd DateTime
canBeViewdBy EventCanView
eventStart DateTime
eventEnd DateTime
canBeViewdBy EventCanView
hostedByCommitee Committee? @relation(fields: [hostedByCommiteeId], references: [id])
hostedByCommiteeId Int?
takesRegistration Boolean
places Int @default(0)
Expand Down
1 change: 1 addition & 0 deletions src/prisma/schema/group.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ model Committee {
logoImageId Int @unique
paragraph CmsParagraph @relation(fields: [paragraphId], references: [id])
paragraphId Int @unique
events Event[]
}

model InterestGroup {
Expand Down

0 comments on commit bba5a7e

Please sign in to comment.