|
| 1 | +import data from '../../sessionize.json'; |
| 2 | +import { firestore } from '../firebase-config'; |
| 3 | +import { Answer, SessionizeSession } from './types'; |
| 4 | +import { categoryItem, nameToId, notConfirmedSpeaker, questionAnswer } from './utils'; |
| 5 | + |
| 6 | +export const importSessions = async () => { |
| 7 | + const sessions: SessionizeSession[] = convertSessions(); |
| 8 | + const { length } = await save(sessions); |
| 9 | + console.log(`Imported data for ${length} sessions`); |
| 10 | +}; |
| 11 | + |
| 12 | +const cleanTags = (text: string): string[] => { |
| 13 | + return text |
| 14 | + .split(',') |
| 15 | + .map((dirtyTag) => dirtyTag.trim()) |
| 16 | + .filter(Boolean); |
| 17 | +}; |
| 18 | + |
| 19 | +const cleanComplexity = (text: string): string => { |
| 20 | + return text === 'Introductory and overview' ? 'Beginner' : text; |
| 21 | +}; |
| 22 | + |
| 23 | +const convertSpeakerIds = (ids: string[]): string[] => { |
| 24 | + return ids.map((sessionizeId) => { |
| 25 | + const speaker = data.speakers.find(({ id }) => id === sessionizeId); |
| 26 | + if (speaker) { |
| 27 | + return nameToId(speaker.fullName); |
| 28 | + } else { |
| 29 | + throw new Error(`Unable to find speaker with id ${sessionizeId}`); |
| 30 | + } |
| 31 | + }); |
| 32 | +}; |
| 33 | + |
| 34 | +const matchIcon = ({ |
| 35 | + title, |
| 36 | + isServiceSession, |
| 37 | +}: { |
| 38 | + title: string; |
| 39 | + isServiceSession: boolean; |
| 40 | +}): string => { |
| 41 | + if (isServiceSession) { |
| 42 | + switch (true) { |
| 43 | + case title.toLowerCase().includes('break'): |
| 44 | + return 'coffee-break'; |
| 45 | + case title.toLowerCase().includes('closing'): |
| 46 | + case title.toLowerCase().includes('welcome'): |
| 47 | + return 'opening'; |
| 48 | + case title.toLowerCase().includes('lunch'): |
| 49 | + return 'lunch'; |
| 50 | + case title.toLowerCase().includes('party'): |
| 51 | + return 'party'; |
| 52 | + default: |
| 53 | + return ''; |
| 54 | + } |
| 55 | + } |
| 56 | + return ''; |
| 57 | +}; |
| 58 | + |
| 59 | +const getTags = (answers: Answer[], items: number[]): string[] => { |
| 60 | + const isKeynote = categoryItem('Session format', items).toLowerCase() === 'keynote'; |
| 61 | + const tags = [questionAnswer('Tags', answers), isKeynote ? 'keynote' : ''].join(','); |
| 62 | + return cleanTags(tags); |
| 63 | +}; |
| 64 | + |
| 65 | +const convertSessions = (): SessionizeSession[] => { |
| 66 | + const sessions: SessionizeSession[] = []; |
| 67 | + for (const sessionData of data.sessions) { |
| 68 | + if (notConfirmedSpeaker(sessionData.speakers)) { |
| 69 | + console.log(`Skipping session ${sessionData.title}`); |
| 70 | + continue; |
| 71 | + } |
| 72 | + console.log(`Importing session ${sessionData.title}`); |
| 73 | + sessions.push({ |
| 74 | + sessionizeId: sessionData.id, |
| 75 | + complexity: cleanComplexity(categoryItem('Level', sessionData.categoryItems)), |
| 76 | + description: sessionData.description || '', |
| 77 | + icon: matchIcon(sessionData), |
| 78 | + image: '', |
| 79 | + language: 'en', |
| 80 | + speakers: convertSpeakerIds(sessionData.speakers), |
| 81 | + tags: getTags(sessionData.questionAnswers, sessionData.categoryItems), |
| 82 | + title: sessionData.title, |
| 83 | + }); |
| 84 | + } |
| 85 | + return sessions; |
| 86 | +}; |
| 87 | + |
| 88 | +const save = async (sessions: SessionizeSession[]) => { |
| 89 | + if (sessions.length === 0) { |
| 90 | + throw new Error('No sessions found!'); |
| 91 | + } |
| 92 | + console.log(`Importing ${sessions.length} sessions...`); |
| 93 | + |
| 94 | + const collectionRef = firestore.collection('sessions'); |
| 95 | + const { docs } = await collectionRef.get(); |
| 96 | + const batch = firestore.batch(); |
| 97 | + |
| 98 | + sessions.forEach(async (session) => { |
| 99 | + const existingDoc = docs.find((doc) => doc.data().sessionizeId === session.sessionizeId); |
| 100 | + if (existingDoc) { |
| 101 | + batch.set(existingDoc.ref, session); |
| 102 | + } else { |
| 103 | + batch.set(collectionRef.doc(), session); |
| 104 | + } |
| 105 | + }); |
| 106 | + |
| 107 | + return batch.commit(); |
| 108 | +}; |
0 commit comments