11import { PodcastQueryResult } from "@/sanity.types" ;
2- import { sanityFetch } from "@/sanity/lib/fetch" ;
32import { podcastQuery , postQuery } from "@/sanity/lib/queries" ;
43import { isValidSignature , SIGNATURE_HEADER_NAME } from "@sanity/webhook" ;
54import toMarkdown from '@sanity/block-content-to-markdown'
@@ -17,7 +16,7 @@ const sanityWriteClient = createClient({
1716export async function POST ( request : Request ) {
1817 if ( ! secret )
1918 return Response . json (
20- { success : false , error : "Missing Secret PRIVATE_ALGOLIA_WEBOOK_SECRET " } ,
19+ { success : false , error : "Missing Secret PRIVATE_SYNDICATE_WEBOOK_SECRET " } ,
2120 { status : 400 }
2221 ) ;
2322
@@ -42,17 +41,13 @@ export async function POST(request: Request) {
4241 const deleted = sanityDoc . deleted ;
4342 const updated = sanityDoc . updated ;
4443
45- delete sanityDoc . created ;
46- delete sanityDoc . deleted ;
47- delete sanityDoc . updated ;
48-
4944 try {
5045 if ( created ) {
5146 await formatPodcast ( sanityDoc . _type , sanityDoc . slug )
5247 } else if ( updated ) {
5348 await formatPodcast ( sanityDoc . _type , sanityDoc . slug )
5449 } else {
55- await unPublishPodcast ( sanityDoc . _type , sanityDoc . slug )
50+ await unPublishPodcast ( sanityDoc . _type , sanityDoc . _id , sanityDoc . devto )
5651 }
5752 } catch ( e ) {
5853 const error = JSON . stringify ( e ) ;
@@ -108,9 +103,11 @@ ${toMarkdown(podcast.content, { serializers })}`
108103 console . debug ( 'addArticle result:' , response . status ) ;
109104 }
110105
106+ const json = await response . json ( ) ;
107+ console . debug ( "result payload" , JSON . stringify ( json , null , 2 ) ) ;
108+
111109 // Get new devto url and update
112110 if ( response . status >= 200 && response . status <= 299 ) {
113- const json = await response . json ( ) ;
114111 if ( json ?. url && ! podcast ?. devto ) {
115112 console . debug ( 'Article Added to Dev.to' , JSON . stringify ( json , null , 2 ) ) ;
116113 await updateSanity ( podcast . _id , json . url ) ;
@@ -127,29 +124,26 @@ ${toMarkdown(podcast.content, { serializers })}`
127124 }
128125}
129126
130- const unPublishPodcast = async ( _type : string , slug : string ) => {
131- const podcast = await sanityQuery ( _type , slug ) ;
132- if ( ! podcast ?. _id ) {
127+ const unPublishPodcast = async ( _type : string , id : string , devto : string ) => {
128+ if ( ! id ) {
133129 return Response . json ( { success : false , error : "Podcast not found" } , { status : 404 } ) ;
134130 }
135131
136- console . debug ( 'Unpublishing' , { slug : podcast ?. slug , devto : podcast ?. devto } ) ;
132+ console . debug ( 'Unpublishing' , { _type , id , devto } ) ;
137133
138134 try {
139135
140- if ( ! podcast ?. devto ) {
136+ if ( ! devto ) {
141137 return Response . json ( { success : false , error : "DevTo not found" } , { status : 404 } ) ;
142138 }
143- const response = await unpublishArticle ( podcast . _id , podcast . devto ) ;
139+ const response = await unpublishArticle ( devto ) ;
144140
145- // Get new devto url and update
141+ // Remove devto from sanity
146142 if ( response . status >= 200 && response . status <= 299 ) {
147- const unpubResponse = await unpublishArticle ( podcast . _id , podcast . devto ) ;
148- if ( unpubResponse . status === 204 ) {
149- console . debug ( 'Unpublished Article' , podcast . _id , podcast . devto ) ;
150- }
143+ console . debug ( 'removed post from devto' )
144+ return Response . json ( { success : true } , { status : 200 } ) ;
151145 }
152- return Response . json ( { success : true } , { status : 201 } ) ;
146+ return Response . json ( { success : true } , { status : 200 } ) ;
153147 } catch ( error ) {
154148 console . error ( error ) ;
155149 return Response . json (
@@ -193,7 +187,7 @@ const updateArticle = async (devto: string, article: any) => {
193187 } ) ;
194188} ;
195189
196- const unpublishArticle = async ( _id : string , devto : string ) => {
190+ const unpublishArticle = async ( devto : string ) => {
197191 const requestedArticle = await getArticle ( devto ) ;
198192 const json = await requestedArticle . json ( ) ;
199193 return fetch ( `https://dev.to/api/articles/${ json ?. id } /unpublish` , {
@@ -212,14 +206,17 @@ const updateSanity = async (_id: string, url: string) => {
212206const sanityQuery = async ( _type : string , slug : string ) => {
213207 const query = _type === 'podcast' ? podcastQuery : postQuery ;
214208 const [ podcast ] = await Promise . all ( [
215- sanityFetch < PodcastQueryResult > ( {
209+ sanityWriteClient . fetch < PodcastQueryResult > (
216210 query ,
217- params : {
211+ {
218212 slug
219213 } ,
220- stega : false ,
221- perspective : "raw" ,
222- } ) ,
214+ {
215+ stega : false ,
216+ perspective : "raw" ,
217+ useCdn : false
218+ }
219+ ) ,
223220 ] ) ;
224221 return podcast ;
225222}
0 commit comments