11import { cmd } from "./cmd"
22import * as prompts from "./clack"
33import { UI } from "../ui"
4- import { irisFetch , requireAuth , handleApiError , printDivider , printKV , dim , bold , success , highlight } from "./iris-api"
4+ import { irisFetch , requireAuth , requireUserId , handleApiError , printDivider , printKV , dim , bold , success , highlight } from "./iris-api"
5+ import { scrapeInstagramPost } from "./platform-content"
56import { existsSync , mkdirSync , writeFileSync , readFileSync } from "fs"
67import { join , basename } from "path"
78
@@ -298,7 +299,11 @@ const PullCommand = cmd({
298299 if ( ! ok ) { spinner . stop ( "Failed" , 1 ) ; prompts . outro ( "Done" ) ; return }
299300
300301 const data = ( await res . json ( ) ) as any
301- const entity = data ?. data ?? data
302+ // getProgram wraps the record as { data: { program: {...} } } — unwrap to the
303+ // bare program object, else the filename (undefined-program.json), packages
304+ // fetch, and push all break on the missing top-level id/name.
305+ let entity = data ?. data ?? data
306+ if ( entity && typeof entity . program === "object" && entity . program ) entity = entity . program
302307
303308 // Also fetch packages
304309 const pkgRes = await irisFetch ( `/api/v1/programs/${ entity . id } /packages` )
@@ -367,14 +372,17 @@ const PushCommand = cmd({
367372 spinner . start ( `Pushing ${ basename ( filepath ) } …` )
368373
369374 const entity = JSON . parse ( readFileSync ( filepath , "utf-8" ) )
375+ // Tolerate the wrapped shape too, in case the file was pulled before the unwrap fix.
376+ const p = ( entity && typeof entity . program === "object" && entity . program ) ? entity . program : entity
370377 const payload : Record < string , unknown > = {
371- name : entity . name , slug : entity . slug , description : entity . description ,
372- active : entity . active , tier : entity . tier , bloq_id : entity . bloq_id ,
373- base_price : entity . base_price , has_paid_membership : entity . has_paid_membership ,
374- allow_free_enrollment : entity . allow_free_enrollment ,
375- membership_features : entity . membership_features ,
376- custom_fields : entity . custom_fields ,
377- enrollment_form_config : entity . enrollment_form_config ,
378+ name : p . name , slug : p . slug , description : p . description ,
379+ active : p . active , tier : p . tier , bloq_id : p . bloq_id ,
380+ image_url : p . image_url , icon : p . icon ,
381+ base_price : p . base_price , has_paid_membership : p . has_paid_membership ,
382+ allow_free_enrollment : p . allow_free_enrollment ,
383+ membership_features : p . membership_features ,
384+ custom_fields : p . custom_fields ,
385+ enrollment_form_config : p . enrollment_form_config ,
378386 }
379387 for ( const k of Object . keys ( payload ) ) { if ( payload [ k ] === undefined ) delete payload [ k ] }
380388
@@ -813,6 +821,77 @@ const VerifyCertCommand = cmd({
813821 } ,
814822} )
815823
824+ // ============================================================================
825+ // cover <id> — set a program's cover image from an IG post / image URL (one-shot)
826+ // ============================================================================
827+
828+ const CoverCommand = cmd ( {
829+ command : "cover <id>" ,
830+ describe : "view or set a program's cover image (from an Instagram post or image URL)" ,
831+ builder : ( yargs ) =>
832+ yargs
833+ . positional ( "id" , { describe : "program ID" , type : "number" , demandOption : true } )
834+ . option ( "ig" , { describe : "Instagram post URL — scrape its flyer & use as the cover" , type : "string" } )
835+ . option ( "url" , { describe : "direct image URL to use as the cover" , type : "string" } )
836+ . option ( "user-id" , { describe : "user ID for the IG scrape (or IRIS_USER_ID env)" , type : "number" } ) ,
837+ async handler ( args ) {
838+ UI . empty ( )
839+ prompts . intro ( `◈ Program Cover #${ args . id } ` )
840+
841+ // No source → show the current cover.
842+ if ( ! args . ig && ! args . url ) {
843+ const res = await irisFetch ( `/api/v1/programs/${ args . id } ` )
844+ if ( ! ( await handleApiError ( res , "Get program" ) ) ) { prompts . outro ( "Done" ) ; return }
845+ const data = ( await res . json ( ) ) as any
846+ let p = data ?. data ?? data
847+ if ( p && typeof p . program === "object" && p . program ) p = p . program
848+ printDivider ( )
849+ printKV ( "Program" , p ?. name ?? args . id )
850+ printKV ( "Cover" , p ?. image_url || dim ( "(none)" ) )
851+ printDivider ( )
852+ prompts . outro ( dim ( `iris programs cover ${ args . id } --ig <url> | --url <image-url>` ) )
853+ return
854+ }
855+
856+ const token = await requireAuth ( )
857+ if ( ! token ) { prompts . outro ( "Done" ) ; return }
858+
859+ let coverUrl = args . url as string | undefined
860+
861+ // IG source → scrape the post and use its durable (re-hosted) flyer.
862+ if ( args . ig ) {
863+ const userId = await requireUserId ( args [ "user-id" ] as number | undefined )
864+ if ( ! userId ) { prompts . outro ( "Done" ) ; return }
865+ const spinner = prompts . spinner ( )
866+ spinner . start ( "Scraping Instagram post…" )
867+ const scraped = await scrapeInstagramPost ( args . ig as string , userId )
868+ if ( ! scraped || ! scraped . flyerUrl ) {
869+ spinner . stop ( "No flyer found in that post" , 1 )
870+ prompts . outro ( "Done" )
871+ return
872+ }
873+ spinner . stop ( success ( "Got flyer" ) )
874+ coverUrl = scraped . flyerUrl
875+ }
876+
877+ const spinner = prompts . spinner ( )
878+ spinner . start ( "Setting cover…" )
879+ const res = await irisFetch ( `/api/v1/programs/${ args . id } ` , {
880+ method : "PUT" ,
881+ body : JSON . stringify ( { image_url : coverUrl } ) ,
882+ } )
883+ const ok = await handleApiError ( res , "Set cover" )
884+ if ( ! ok ) { spinner . stop ( "Failed" , 1 ) ; prompts . outro ( "Done" ) ; return }
885+ spinner . stop ( `${ success ( "✓" ) } Cover set` )
886+
887+ printDivider ( )
888+ printKV ( "Program" , args . id )
889+ printKV ( "Cover" , coverUrl )
890+ printDivider ( )
891+ prompts . outro ( dim ( `iris programs get ${ args . id } ` ) )
892+ } ,
893+ } )
894+
816895// ============================================================================
817896// Root command
818897// ============================================================================
@@ -827,6 +906,7 @@ export const PlatformProgramsCommand = cmd({
827906 . command ( GetCommand )
828907 . command ( CreateCommand )
829908 . command ( UpdateCommand )
909+ . command ( CoverCommand )
830910 . command ( PullCommand )
831911 . command ( PushCommand )
832912 . command ( DiffCommand )
0 commit comments