@@ -3,6 +3,7 @@ import { ServerClient } from 'postmark'
3
3
import { getContentWithComponentByDirectorySlug } from "./content-handlers" ; // Use this instead of loadContent
4
4
import { ExtendedMetadata } from '@/types'
5
5
import { emailLogger as logger } from '@/utils/logger' // Import centralized email logger
6
+ import { getContentUrlFromObject } from './content-url'
6
7
7
8
// Define the MessageSendingResponse interface based on Postmark API response
8
9
interface MessageSendingResponse {
@@ -100,15 +101,6 @@ interface SendFreeChaptersEmailInput {
100
101
} [ ] ;
101
102
}
102
103
103
- // Function to get the content URL based on content type
104
- const getContentUrl = ( type : string , slug : string ) => {
105
- // Remove any leading slashes from the slug
106
- const cleanSlug = slug . replace ( / ^ \/ + / , '' ) ;
107
-
108
- // For all content types, use the /blog/ path since we're only selling blog content
109
- return `/blog/${ cleanSlug } ` ;
110
- } ;
111
-
112
104
/**
113
105
* Extract preview content from a product's MDX content using server-side approach
114
106
* @param productSlug The slug of the product
@@ -121,12 +113,10 @@ const getContentUrl = (type: string, slug: string) => {
121
113
*/
122
114
async function extractPreviewContent ( productSlug : string ) : Promise < PreviewContentResult | null > {
123
115
// Remove any leading slashes from the productSlug
124
- const normalizedSlug = productSlug . replace ( / ^ \/ + / , '' ) ;
125
-
116
+ const normalizedSlug = productSlug . replace ( / ^ \/ + / , '' ) ;
126
117
// For blog posts, the content type is 'blog'
127
118
// This assumes all products are blog posts - adjust if you have different content types
128
119
const contentType = 'blog' ;
129
-
130
120
logger . info ( `Extracting preview content for: ${ contentType } /${ normalizedSlug } ` ) ;
131
121
// Use getContentWithComponentByDirectorySlug to load content + metadata
132
122
const result = await getContentWithComponentByDirectorySlug ( contentType , normalizedSlug ) ;
@@ -136,13 +126,11 @@ async function extractPreviewContent(productSlug: string): Promise<PreviewConten
136
126
}
137
127
// Access the metadata via result.content
138
128
const { content : metadata } = result ;
139
-
140
- // Create a simple HTML preview with a link to the content
141
129
logger . info ( `Creating preview content...` ) ;
142
130
try {
143
131
// Get the base URL
144
132
const baseUrl = process . env . NEXT_PUBLIC_SITE_URL || 'http://localhost:3000' ;
145
- const productUrl = `${ baseUrl } ${ getContentUrl ( contentType , normalizedSlug ) } ` ;
133
+ const productUrl = `${ baseUrl } ${ getContentUrlFromObject ( metadata ) } ` ;
146
134
147
135
// Create a simple HTML preview with the description and a link
148
136
const title = typeof metadata . title === 'string'
@@ -201,22 +189,29 @@ const sendFreeChaptersEmail = async (
201
189
logger . debug ( `[CONFIG] Base URL: ${ baseUrl } ` ) ;
202
190
203
191
// Normalize the slug and construct the product URL
204
- const normalizedSlug = input . ProductSlug . replace ( / ^ \/ + / , '' ) ;
205
- const productUrl = `${ baseUrl } ${ getContentUrl ( 'article' , normalizedSlug ) } ` ;
192
+ const normalizedSlug = input . ProductSlug . replace ( / ^ \/ + / , '' ) ;
193
+ let productUrl = '' ;
194
+ const result = await getContentWithComponentByDirectorySlug ( 'blog' , normalizedSlug ) ;
195
+ if ( result && result . content ) {
196
+ productUrl = `${ baseUrl } ${ getContentUrlFromObject ( result . content ) } ` ;
197
+ } else {
198
+ logger . warn ( `Could not load content for slug: ${ input . ProductSlug } to generate product URL.` ) ;
199
+ productUrl = `${ baseUrl } /blog/${ normalizedSlug } ` ; // fallback
200
+ }
206
201
logger . debug ( `[CONFIG] Product URL: ${ productUrl } ` ) ;
207
202
208
203
// Extract preview content from the actual product
209
204
logger . info ( `[PROCESS] Starting content extraction...` ) ;
210
205
211
- const result = await extractPreviewContent ( normalizedSlug ) ;
206
+ const previewResult = await extractPreviewContent ( normalizedSlug ) ;
212
207
213
- if ( ! result ) {
208
+ if ( ! previewResult ) {
214
209
logger . warn ( `No content found for slug: ${ input . ProductSlug } ` ) ;
215
210
return null ;
216
211
}
217
212
218
213
// The result from extractPreviewContent already contains the correct metadata
219
- const { previewContent, metadata } = result ;
214
+ const { previewContent, metadata } = previewResult ;
220
215
221
216
logger . debug ( `[DEBUG] Result extracted content length: ${ previewContent . length } characters` ) ;
222
217
0 commit comments