Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(egh): Refactor Sanity Schema Types and Add Course Creation Function #343

Merged
merged 3 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { POST_CREATED_EVENT } from '@/inngest/events/post-created'
import { inngest } from '@/inngest/inngest.server'
import { SanityCourseSchema } from '@/lib/sanity-content'
import { createCourse, getSanityCollaborator } from '@/lib/sanity-content-query'
import { loadEggheadInstructorForUser } from '@/lib/users'
import { NonRetriableError } from 'inngest'

export const createCourseInSanity = inngest.createFunction(
{
id: 'create-course-in-sanity',
name: 'Create Course in Sanity',
},
{
event: POST_CREATED_EVENT,
},
async ({ event, step }) => {
const post = await step.run('verify-post', async () => {
const { post } = event.data
if (!post) {
throw new NonRetriableError('Post not found')
}

if (post.fields?.postType !== 'course') {
throw new NonRetriableError('Post is not a course')
}

return post
})

const contributor = await step.run('get-contributor', async () => {
const instructor = await loadEggheadInstructorForUser(post.createdById)

if (!instructor) {
return null
}

const contributor = await getSanityCollaborator(instructor.id)

return contributor
})

const sanityCourse = await step.run('create-course', async () => {
const { fields } = post

const coursePayload = SanityCourseSchema.safeParse({
title: fields?.title,
slug: {
current: fields?.slug,
_type: 'slug',
},
description: fields?.body,
railsCourseId: fields?.railsCourseId,
collaborators: [contributor],
searchIndexingState: 'hidden',
accessLevel: 'pro',
productionProcessState: 'new',
sharedId: post.id,
})

if (!coursePayload.success) {
throw new NonRetriableError('Failed to create course in sanity', {
cause: coursePayload.error.flatten().fieldErrors,
})
}

const course = await createCourse(coursePayload.data)

return course
})
},
)
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@ import { inngest } from '@/inngest/inngest.server'
import { eggheadLessonSchema, getEggheadLesson } from '@/lib/egghead'
import type { EggheadLesson } from '@/lib/egghead'
import {
sanityReferenceSchema,
sanityVersionedSoftwareLibraryObjectSchema,
SanityReferenceSchema,
SoftwareLibraryArrayObjectSchema,
} from '@/lib/sanity-content'
import type {
SanityCollaboratorDocument,
SanityReference,
SanityVersionedSoftwareLibraryObject,
SoftwareLibraryArrayObject,
} from '@/lib/sanity-content'
import {
createSanityLesson,
Expand All @@ -36,7 +35,7 @@ export const syncLessonToSanity = inngest.createFunction(
try {
return await Promise.all(
lesson.topic_list.map(async (library: string) => {
return sanityVersionedSoftwareLibraryObjectSchema.parse(
return SoftwareLibraryArrayObjectSchema.parse(
await getSanitySoftwareLibrary(library),
)
}),
Expand All @@ -46,12 +45,12 @@ export const syncLessonToSanity = inngest.createFunction(
return []
}
},
)) as SanityVersionedSoftwareLibraryObject[]
)) as SoftwareLibraryArrayObject[]

const sanityCollaboratorReferenceObject = (await step.run(
'Get collaborator',
async () => {
return sanityReferenceSchema?.parse(
return SanityReferenceSchema?.parse(
await getSanityCollaborator(lesson.instructor.id),
)
},
Expand Down
2 changes: 2 additions & 0 deletions apps/egghead/src/inngest/inngest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { courseBuilderCoreFunctions } from '@coursebuilder/core/inngest'
import { instructorInviteCreated } from './functions/instructor-invite-created'
import { migrateTipsToPosts } from './functions/migrate-tips-to-posts'
import { notifySlack } from './functions/notify-slack-for-post'
import { createCourseInSanity } from './functions/sanity/create-course-in-sanity'
import { syncLessonToSanity } from './functions/sanity/sync-lesson-to-sanity'
import { syncVideoResourceToSanity } from './functions/sanity/sync-video-resource-to-sanity'
import { syncPostToEgghead } from './functions/sync-post-to-egghead'
Expand All @@ -26,6 +27,7 @@ export const inngestConfig = {
notifySlack,
syncVideoResourceData,
syncPostsToEggheadLessons,
createCourseInSanity,
instructorInviteCreated,
],
}
4 changes: 2 additions & 2 deletions apps/egghead/src/lib/posts-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ import {
updateEggheadLesson,
writeLegacyTaggingsToEgghead,
} from './egghead'
import { sanityLessonDocumentSchema } from './sanity-content'
import { SanityLessonDocumentSchema } from './sanity-content'
import {
replaceSanityLessonResources,
updateSanityLesson,
Expand Down Expand Up @@ -373,7 +373,7 @@ export async function writeNewPostToDatabase(input: {
: null

if (eggheadLessonId) {
const lesson = sanityLessonDocumentSchema.parse({
const lesson = SanityLessonDocumentSchema.parse({
_id: `lesson-${eggheadLessonId}`,
_type: 'lesson',
title,
Expand Down
51 changes: 29 additions & 22 deletions apps/egghead/src/lib/sanity-content-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,19 @@ import {
import {
createSanityReference,
keyGenerator,
sanityCollaboratorDocumentSchema,
sanityLessonDocumentSchema,
sanityReferenceSchema,
SanityCollaboratorSchema,
SanityLessonDocumentSchema,
SanityReferenceSchema,
sanitySoftwareLibraryDocumentSchema,
sanityVersionedSoftwareLibraryObjectSchema,
sanityVideoResourceDocumentSchema,
SanityVideoResourceDocumentSchema,
SoftwareLibraryArrayObjectSchema,
} from '@/lib/sanity-content'
import type {
SanityCollaboratorDocument,
SanityCollaborator,
SanityCourse,
SanityReference,
SanitySoftwareLibraryDocument,
SanityVersionedSoftwareLibraryObject,
SoftwareLibraryArrayObject,
} from '@/lib/sanity-content'
import { sanityWriteClient } from '@/server/sanity-write-client'
import { asc, eq, sql } from 'drizzle-orm'
Expand All @@ -42,7 +43,7 @@ export async function createSanityVideoResource(videoResource: VideoResource) {
const streamUrl =
muxPlaybackId && `https://stream.mux.com/${muxPlaybackId}.m3u8`

const body = sanityVideoResourceDocumentSchema.parse({
const body = SanityVideoResourceDocumentSchema.parse({
_type: 'videoResource',
filename: id,
muxAsset: {
Expand Down Expand Up @@ -224,17 +225,17 @@ export async function updateSanityLesson(
const softwareLibraries = await Promise.all(
eggheadLesson.topic_list.map(async (library: string) => {
console.log('process topic library', library)
return sanityVersionedSoftwareLibraryObjectSchema
.nullable()
.parse(await getSanitySoftwareLibrary(library))
return SoftwareLibraryArrayObjectSchema.nullable().parse(
await getSanitySoftwareLibrary(library),
)
}),
)

const collaboratorData = await getSanityCollaborator(
eggheadLesson.instructor.id,
)

let collaborator = sanityReferenceSchema.nullable().parse(collaboratorData)
let collaborator = SanityReferenceSchema.nullable().parse(collaboratorData)

if (!collaborator) {
const user = await db.query.users.findFirst({
Expand Down Expand Up @@ -264,9 +265,9 @@ export async function updateSanityLesson(

await syncInstructorToSanity(eggheadUserProfile)

collaborator = sanityReferenceSchema
.nullable()
.parse(await getSanityCollaborator(eggheadLesson.instructor.id))
collaborator = SanityReferenceSchema.nullable().parse(
await getSanityCollaborator(eggheadLesson.instructor.id),
)

if (!collaborator) {
throw new Error(
Expand Down Expand Up @@ -299,7 +300,7 @@ export async function updateSanityLesson(
export async function createSanityLesson(
eggheadLesson: EggheadLesson,
collaborator: SanityReference,
softwareLibraries: SanityVersionedSoftwareLibraryObject[],
softwareLibraries: SoftwareLibraryArrayObject[],
) {
const post = PostSchema.nullable().parse(
await db.query.contentResource.findFirst({
Expand Down Expand Up @@ -336,7 +337,7 @@ export async function createSanityLesson(
return updateSanityLesson(eggheadLesson.id, post)
}

const lesson = sanityLessonDocumentSchema.parse({
const lesson = SanityLessonDocumentSchema.parse({
_id: `lesson-${eggheadLesson.id}`,
_type: 'lesson',
title: eggheadLesson.title,
Expand All @@ -363,18 +364,17 @@ export async function createSanityLesson(

export async function getSanityCollaborator(
instructorId: number,
role: SanityCollaboratorDocument['role'] = 'instructor',
role: SanityCollaborator['role'] = 'instructor',
returnReference = true,
) {
const collaboratorData = await sanityWriteClient.fetch(
`*[_type == "collaborator" && eggheadInstructorId == "${instructorId}" && role == "${role}"][0]`,
)

const collaborator = sanityCollaboratorDocumentSchema
.nullable()
.parse(collaboratorData)
const collaborator =
SanityCollaboratorSchema.nullable().parse(collaboratorData)

if (!collaborator) return null
if (!collaborator || !collaborator._id) return null

return returnReference
? createSanityReference(collaborator._id)
Expand Down Expand Up @@ -405,3 +405,10 @@ export async function getSanitySoftwareLibrary(
},
}
}

export const createCourse = async (course: Partial<SanityCourse>) => {
return await sanityWriteClient.create({
_type: 'course',
...course,
})
}
Loading
Loading