Last active
August 31, 2021 15:59
-
-
Save sueannioanis/52c9219e2c053cbe57c7beaeb969785b to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| import errors from 'errors'; | |
| import { User, Episode } from 'models'; | |
| import { AVAILABILITY, CAMERA, MICROPHONE, NOT_PUBLISHED, PRERELEASE } from 'constants'; | |
| export async function getEpisodeForUser(episodeId: string, userId) { | |
| const episode = await Episode.findOne(episodeId); | |
| const { isAdmin } = User.findOne(userId); | |
| if (episode.isPublished || isAdmin) return toEpisodeEntity(episode); | |
| throw errors.notFound(); | |
| } | |
| function toEpisodeEntity(episode: Episode) { | |
| let availability = AVAILABILITY; | |
| const requiredPermissions = []; | |
| const { requiresCamera, requiresMicrophone, isPublished, releaseDate } = episode; | |
| const preRelease = releaseDate && releaseDate > new Date(); | |
| if (requiresCamera) requiredPermissions.push(CAMERA); | |
| if (requiresMicrophone) requiredPermissions.push(MICROPHONE); | |
| if (!isPublished) { availability = NOT_PUBLISHED; } | |
| if (preRelease) { availability = PRERELEASE; } | |
| return { | |
| ...episode, | |
| availability, | |
| requiredPermissions, | |
| }; | |
| } | |
| type EpisodeEntity = { | |
| availability: string, | |
| episodeId: string; | |
| requiredPermissions: string[], | |
| title: string; | |
| thumbnailKey: string; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment