-
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.
Merge pull request #328 from vevcom/chore/events-migrations
Chore/events migrations
- Loading branch information
Showing
11 changed files
with
175 additions
and
25 deletions.
There are no files selected for viewing
Submodule docs
updated
from 3cc94e to 97b8ef
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
121 changes: 121 additions & 0 deletions
121
src/prisma/prismaservice/src/dobbelOmega/migrateEvents.ts
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,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, | ||
} | ||
}) | ||
})) | ||
} |
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
30 changes: 30 additions & 0 deletions
30
src/prisma/prismaservice/src/dobbelOmega/upsertOrderBasedOnDate.ts
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,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 | ||
} |
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