-
Notifications
You must be signed in to change notification settings - Fork 28
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 #323 from badass-courses/creeland/egg-373-sync-cou…
…rse-builder-video-posts-with-egghead-and-sanity feat: Sync Lessons and Video Resources to Sanity
- Loading branch information
Showing
11 changed files
with
462 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { z } from 'zod' | ||
|
||
export const EGGHEAD_LESSON_CREATED_EVENT = 'egghead/lesson-created' | ||
|
||
export type EggheadLessonCreated = { | ||
name: typeof EGGHEAD_LESSON_CREATED_EVENT | ||
data: EggheadLessonCreatedEvent | ||
} | ||
export const EggheadLessonCreatedEventSchema = z.object({ | ||
id: z.number(), | ||
}) | ||
|
||
export type EggheadLessonCreatedEvent = z.infer< | ||
typeof EggheadLessonCreatedEventSchema | ||
> |
64 changes: 64 additions & 0 deletions
64
apps/egghead/src/inngest/functions/sanity/sync-lesson-to-sanity.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,64 @@ | ||
import { EGGHEAD_LESSON_CREATED_EVENT } from '@/inngest/events/egghead/lesson-created' | ||
import { inngest } from '@/inngest/inngest.server' | ||
import { eggheadLessonSchema, getEggheadLesson } from '@/lib/egghead' | ||
import type { EggheadLesson } from '@/lib/egghead' | ||
import { | ||
sanityCollaboratorDocumentSchema, | ||
sanityCollaboratorReferenceObjectSchema, | ||
sanityVersionedSoftwareLibraryObjectSchema, | ||
} from '@/lib/sanity-content' | ||
import type { | ||
SanityCollaboratorDocument, | ||
SanityCollaboratorReferenceObject, | ||
SanityVersionedSoftwareLibraryObject, | ||
} from '@/lib/sanity-content' | ||
import { | ||
createSanityLesson, | ||
getSanityCollaborator, | ||
getSanitySoftwareLibrary, | ||
} from '@/lib/sanity-content-query' | ||
|
||
export const syncLessonToSanity = inngest.createFunction( | ||
{ | ||
id: 'sync-lesson-to-sanity', | ||
name: 'Sync Lesson to Sanity', | ||
}, | ||
{ | ||
event: EGGHEAD_LESSON_CREATED_EVENT, | ||
}, | ||
async ({ event, step }) => { | ||
const lesson = (await step.run('Get lesson', async () => { | ||
return eggheadLessonSchema.parse(await getEggheadLesson(event.data.id)) | ||
})) as EggheadLesson | ||
|
||
const versionedSoftwareLibraryReferences = (await step.run( | ||
'Get an array of versioned software library references', | ||
async () => { | ||
return await Promise.all( | ||
lesson.topic_list.map(async (library: string) => { | ||
return sanityVersionedSoftwareLibraryObjectSchema.parse( | ||
await getSanitySoftwareLibrary(library), | ||
) | ||
}), | ||
) | ||
}, | ||
)) as SanityVersionedSoftwareLibraryObject[] | ||
|
||
const sanityCollaboratorReferenceObject = (await step.run( | ||
'Get collaborator', | ||
async () => { | ||
return sanityCollaboratorReferenceObjectSchema.parse( | ||
await getSanityCollaborator(lesson.instructor.id), | ||
) | ||
}, | ||
)) as SanityCollaboratorReferenceObject | ||
|
||
const sanityLesson = await step.run('Create lesson in sanity', async () => { | ||
return await createSanityLesson( | ||
lesson, | ||
sanityCollaboratorReferenceObject, | ||
versionedSoftwareLibraryReferences, | ||
) | ||
}) | ||
}, | ||
) |
58 changes: 58 additions & 0 deletions
58
apps/egghead/src/inngest/functions/sanity/sync-video-resource-to-sanity.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,58 @@ | ||
import { courseBuilderAdapter } from '@/db' | ||
import { inngest } from '@/inngest/inngest.server' | ||
import { | ||
createSanityVideoResource, | ||
patchSanityLessonWithVideoResourceReference, | ||
} from '@/lib/sanity-content-query' | ||
import { getResourceOfVideoResource } from '@/lib/video-resource-query' | ||
import { NonRetriableError } from 'inngest' | ||
|
||
import { VIDEO_RESOURCE_CREATED_EVENT } from '@coursebuilder/core/inngest/video-processing/events/event-video-resource' | ||
import type { VideoResource } from '@coursebuilder/core/schemas' | ||
|
||
export const syncVideoResourceToSanity = inngest.createFunction( | ||
{ | ||
id: 'sync-video-resource-to-sanity', | ||
name: 'Sync Video Resource to Sanity', | ||
}, | ||
{ | ||
event: VIDEO_RESOURCE_CREATED_EVENT, | ||
}, | ||
async ({ event, step }) => { | ||
const courseBuilderVideoResource = (await step.run( | ||
'Get video resource', | ||
async () => { | ||
const videoResource = await courseBuilderAdapter.getVideoResource( | ||
event.data.videoResourceId, | ||
) | ||
|
||
if (!videoResource) { | ||
throw new NonRetriableError('Video resource not found') | ||
} | ||
|
||
return videoResource | ||
}, | ||
)) as VideoResource | ||
|
||
const sanityVideoResourceDocument = await step.run( | ||
'Create video resource in sanity', | ||
async () => { | ||
return await createSanityVideoResource(courseBuilderVideoResource) | ||
}, | ||
) | ||
|
||
await step.run( | ||
'Associate video document with lesson via reference', | ||
async () => { | ||
const post = await getResourceOfVideoResource( | ||
courseBuilderVideoResource.id, | ||
) | ||
|
||
return patchSanityLessonWithVideoResourceReference( | ||
post?.fields?.eggheadLessonId, | ||
sanityVideoResourceDocument._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
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.