@@ -192,18 +192,42 @@ async function addWorkBooks() {
192192 id : workbook . authorId ,
193193 } ,
194194 } ) ;
195- // Allow undefined to match empty strings
196- const normalizedUrlSlug = workbook . urlSlug || undefined ;
197- const registeredWorkBook = await prisma . workBook . findMany ( {
198- where : {
199- urlSlug : normalizedUrlSlug ,
200- } ,
201- } ) ;
195+
196+ const normalizedUrlSlug = normalizeUrlSlug ( workbook . urlSlug ) ;
197+ // Validate for existing workbook using appropriate criteria
198+ let registeredWorkBook ;
199+
200+ if ( normalizedUrlSlug ) {
201+ // If urlSlug exists, check by urlSlug
202+ registeredWorkBook = await prisma . workBook . findMany ( {
203+ where : {
204+ urlSlug : normalizedUrlSlug ,
205+ } ,
206+ } ) ;
207+ } else {
208+ // If no urlSlug, check by title and authorId to avoid duplicates
209+ registeredWorkBook = await prisma . workBook . findMany ( {
210+ where : {
211+ title : workbook . title ,
212+ authorId : workbook . authorId ,
213+ } ,
214+ } ) ;
215+ }
202216
203217 if ( ! author ) {
204218 console . warn ( 'Not found author id: ' , workbook . authorId , '.' ) ;
205219 } else if ( registeredWorkBook . length >= 1 ) {
206- console . warn ( 'Url slug ' , workbook . urlSlug , ' has already been registered.' ) ;
220+ if ( normalizedUrlSlug ) {
221+ console . warn ( 'Url slug ' , workbook . urlSlug , ' has already been registered.' ) ;
222+ } else {
223+ console . warn (
224+ 'Workbook title "' ,
225+ workbook . title ,
226+ '" by author' ,
227+ workbook . authorId ,
228+ 'has already been registered.' ,
229+ ) ;
230+ }
207231 } else {
208232 await addWorkBook ( workbook , workBookFactory ) ;
209233 console . log ( 'workbook title:' , workbook . title , 'was registered.' ) ;
@@ -217,7 +241,7 @@ async function addWorkBooks() {
217241}
218242
219243async function addWorkBook ( workbook , workBookFactory ) {
220- const urlSlug = workbook . urlSlug !== '' ? workbook . urlSlug : undefined ;
244+ const urlSlug = normalizeUrlSlug ( workbook . urlSlug ) ;
221245
222246 await workBookFactory . create ( {
223247 user : {
@@ -237,6 +261,24 @@ async function addWorkBook(workbook, workBookFactory) {
237261 } ) ;
238262}
239263
264+ /**
265+ * Normalizes a URL slug by converting empty strings and null values to undefined.
266+ *
267+ * @param urlSlug - The URL slug to normalize. Can be a string, null, or undefined.
268+ * @returns The normalized URL slug as a string if it has content, otherwise undefined.
269+ *
270+ * @example
271+ * ```typescript
272+ * normalizeUrlSlug("union-find") // returns "union-find"
273+ * normalizeUrlSlug("") // returns undefined
274+ * normalizeUrlSlug(null) // returns undefined
275+ * normalizeUrlSlug(undefined) // returns undefined
276+ * ```
277+ */
278+ function normalizeUrlSlug ( urlSlug : string | null | undefined ) : string | undefined {
279+ return urlSlug && urlSlug !== '' ? urlSlug : undefined ;
280+ }
281+
240282async function addTags ( ) {
241283 console . log ( 'Start adding tags...' ) ;
242284
0 commit comments