Skip to content

Commit

Permalink
#74 introduced firestore migration in order to introduce speakers in …
Browse files Browse the repository at this point in the history
…existing events
  • Loading branch information
fcamblor committed Aug 30, 2024
1 parent c88deee commit 4894d7f
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import {getAllEventsDocs} from "../services/event-utils";
import {db} from "../../../firebase";
import {getEventTalks} from "../services/talk-utils";
import {detailedTalksToSpeakersLineup} from "../../../models/Event";


export async function introduceEventSpeakersAndFixTalkUndefinedTags(): Promise<"OK"|"Error"> {
const eventDocs = await getAllEventsDocs({includePrivateSpaces: true})
await Promise.all([
...eventDocs.map(async eventDoc => {
try {
const eventTalks = await getEventTalks(eventDoc.ref, eventDoc.id);

// Migrating event talks with empty tags in it
const migratedEventTalks = await Promise.all(eventTalks.map(async eventTalk => {
if(!eventTalk.tags) {
await db.doc(`${eventDoc.ref.path}/talks/${eventTalk.id}`).update("tags", [])
return {
...eventTalk,
tags: []
};
} else {
return eventTalk;
}
}))

const lineupSpeakers = detailedTalksToSpeakersLineup(migratedEventTalks);

const eventSpeakersDoc = db.doc(`${eventDoc.ref.path}/speakers-allInOne/self`)
await eventSpeakersDoc.set({ lineupSpeakers });
console.log(`Event speakers created for event id ${eventDoc.id}`)
} catch (err) {
console.error(`Error during Event speaker creation for ${eventDoc.id}: ${err}`)
}
}),
])

return "OK"
}
13 changes: 10 additions & 3 deletions cloud/functions/src/functions/firestore/services/talk-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ import {logPerf} from "../../http/utils";
import {getTimeslottedTalks} from "./schedule-utils";
import {firestore} from "firebase-admin";
import QuerySnapshot = firestore.QuerySnapshot;
import {Talk} from "../../../../../../shared/daily-schedule.firestore";
import {DetailedTalk, Talk} from "../../../../../../shared/daily-schedule.firestore";
import stringSimilarity from "string-similarity-js";
import {EventRecordingConfig} from "../../../../../../shared/conference-descriptor.firestore";
import {resolvedEventFirestorePath} from "../../../../../../shared/utilities/event-utils";
import DocumentReference = firestore.DocumentReference;
import {match, P} from "ts-pattern";


type PerTalkPublicUserIdFeedbackRating = {
Expand Down Expand Up @@ -77,9 +79,14 @@ export async function getTalksDetailsWithRatings(maybeSpaceToken: string|undefin
})
}

export async function getEventTalks(maybeSpaceToken: string|undefined, eventId: string) {
export async function getEventTalks(maybeSpaceToken: DocumentReference|string|undefined, eventId: string) {
return logPerf(`getEventTalks(${maybeSpaceToken}, ${eventId})`, async () => {
const talkSnapshots = await db.collection(`${resolvedEventFirestorePath(eventId, maybeSpaceToken)}/talks`).get() as QuerySnapshot<Talk>
const eventPath = match(maybeSpaceToken)
.with(P.nullish, () => resolvedEventFirestorePath(eventId, undefined))
.with(P.string, (spaceToken) => resolvedEventFirestorePath(eventId, spaceToken))
.otherwise(ref => ref.path)

const talkSnapshots = await db.collection(`${eventPath}/talks`).get() as QuerySnapshot<DetailedTalk>

return talkSnapshots.docs.map(snap => snap.data());
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const MIGRATIONS: Migration[] = [
{ name: "introduceFormattingsFeature", exec: async () => (await import("../firestore/migrations/019-introduceFormattingsFeature")).introduceFormattingsFeature() },
{ name: "cleaningUnusedFirestoreDocs", exec: async () => (await import("../firestore/migrations/020-cleaningUnusedFirestoreDocs")).cleaningUnusedFirestoreDocs() },
{ name: "introduceEventVisibility", exec: async () => (await import("../firestore/migrations/021-introduceEventVisibility")).introduceEventVisibility() },
{ name: "introduceEventSpeakersAndFixTalkUndefinedTags", exec: async () => (await import("../firestore/migrations/022-introduceEventSpeakersAndFixTalkUndefinedTags")).introduceEventSpeakersAndFixTalkUndefinedTags() },
];

export type MigrationResult = "OK"|"Error";
Expand Down

0 comments on commit 4894d7f

Please sign in to comment.