-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3a1670d
commit 60d62ce
Showing
7 changed files
with
363 additions
and
16 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { Speaker } from '../../types' | ||
import firebase from 'firebase-admin' | ||
|
||
const { FieldValue } = firebase.firestore | ||
|
||
export class SpeakerDao { | ||
public static async doesSpeakerExist( | ||
firebaseApp: firebase.app.App, | ||
eventId: string, | ||
speakerId: string | ||
): Promise<boolean | Speaker> { | ||
const db = firebaseApp.firestore() | ||
|
||
// 1. Check if the session exist | ||
const snapshot = await db.collection(`events/${eventId}/speakers`).doc(speakerId).get() | ||
const existingSessionData = snapshot.data() | ||
if (!existingSessionData) { | ||
return false | ||
} | ||
return existingSessionData as Speaker | ||
} | ||
|
||
public static async createSpeaker( | ||
firebaseApp: firebase.app.App, | ||
eventId: string, | ||
speaker: Partial<Speaker> & { | ||
id: string | ||
} | ||
): Promise<any> { | ||
const db = firebaseApp.firestore() | ||
|
||
await db | ||
.collection(`events/${eventId}/speakers`) | ||
.doc(speaker.id) | ||
.set({ | ||
...speaker, | ||
updatedAt: FieldValue.serverTimestamp(), | ||
}) | ||
return speaker | ||
} | ||
|
||
public static async updateSpeaker( | ||
firebaseApp: firebase.app.App, | ||
eventId: string, | ||
speaker: Partial<Speaker> & { | ||
id: string | ||
} | ||
): Promise<any> { | ||
const db = firebaseApp.firestore() | ||
|
||
// 1. Check if the session exist | ||
const existingSpeakerData = await SpeakerDao.doesSpeakerExist(firebaseApp, eventId, speaker.id) | ||
if (!existingSpeakerData) { | ||
throw new Error('Speaker not found') | ||
} | ||
|
||
await db | ||
.collection(`events/${eventId}/speakers`) | ||
.doc(speaker.id) | ||
.set({ | ||
...speaker, | ||
updatedAt: FieldValue.serverTimestamp(), | ||
}) | ||
} | ||
|
||
public static async updateOrCreateSpeaker( | ||
firebaseApp: firebase.app.App, | ||
eventId: string, | ||
speaker: Partial<Speaker> & { id: string } | ||
): Promise<any> { | ||
const existingSpeakerData = await SpeakerDao.doesSpeakerExist(firebaseApp, eventId, speaker.id) | ||
if (!existingSpeakerData) { | ||
await SpeakerDao.createSpeaker(firebaseApp, eventId, speaker) | ||
} else { | ||
await SpeakerDao.updateSpeaker(firebaseApp, eventId, speaker) | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
export const randomColor = (mode: 'colorful' | 'random' = 'colorful') => { | ||
if (mode === 'colorful') { | ||
// Generate random values for the red, green, and blue components | ||
const red = Math.floor(Math.random() * 256) | ||
const green = Math.floor(Math.random() * 256) | ||
const blue = Math.floor(Math.random() * 256) | ||
|
||
// Convert the values to hexadecimal format | ||
const redHex = red.toString(16).padStart(2, '0') | ||
const greenHex = green.toString(16).padStart(2, '0') | ||
const blueHex = blue.toString(16).padStart(2, '0') | ||
|
||
// Concatenate the hexadecimal values to form a color in the "#RRGGBB" format | ||
return `#${redHex}${greenHex}${blueHex}` | ||
} | ||
|
||
return '#' + Math.floor(Math.random() * 16777215).toString(16) | ||
} |
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
Oops, something went wrong.