update create article api

This commit is contained in:
Hongbo Wu
2024-02-06 15:28:50 +08:00
parent 250d9089b3
commit a37bfab348
2 changed files with 25 additions and 71 deletions

View File

@ -340,29 +340,12 @@ export const createArticleResolver = authorized<
}
}
let libraryItemToReturn: LibraryItem
const existingLibraryItem = await findLibraryItemByUrl(
libraryItemToSave.originalUrl,
uid
// create new item in database
const libraryItemToReturn = await createOrUpdateLibraryItem(
libraryItemToSave,
uid,
pubsub
)
articleSavingRequestId = existingLibraryItem?.id || articleSavingRequestId
if (articleSavingRequestId) {
// update existing item's state from processing to succeeded
libraryItemToReturn = await updateLibraryItem(
articleSavingRequestId,
libraryItemToSave as QueryDeepPartialEntity<LibraryItem>,
uid,
pubsub
)
} else {
// create new item in database
libraryItemToReturn = await createOrUpdateLibraryItem(
libraryItemToSave,
uid,
pubsub
)
}
await createAndSaveLabelsInLibraryItem(
libraryItemToReturn.id,
@ -371,14 +354,6 @@ export const createArticleResolver = authorized<
rssFeedUrl
)
log.info(
'item created in database',
libraryItemToReturn.id,
libraryItemToReturn.originalUrl,
libraryItemToReturn.slug,
libraryItemToReturn.title
)
return {
user,
created: true,

View File

@ -16,12 +16,7 @@ import {
libraryItemToArticleSavingRequest,
} from '../utils/helpers'
import { logger } from '../utils/logger'
import {
countByCreatedAt,
createOrUpdateLibraryItem,
findLibraryItemByUrl,
updateLibraryItem,
} from './library_item'
import { countByCreatedAt, createOrUpdateLibraryItem } from './library_item'
interface PageSaveRequest {
userId: string
@ -112,42 +107,26 @@ export const createPageSaveRequest = async ({
}
url = cleanUrl(url)
// look for existing library item
let libraryItem = await findLibraryItemByUrl(url, userId)
if (!libraryItem) {
logger.info('libraryItem does not exist', { url })
// create processing item
libraryItem = await createOrUpdateLibraryItem(
{
id: articleSavingRequestId,
user: { id: userId },
readableContent: SAVING_CONTENT,
itemType: PageType.Unknown,
slug: generateSlug(url),
title: url,
originalUrl: url,
state: LibraryItemState.Processing,
publishedAt,
folder,
subscription,
savedAt,
},
userId,
pubsub
)
}
// reset state to processing
if (libraryItem.state !== LibraryItemState.Processing) {
libraryItem = await updateLibraryItem(
libraryItem.id,
{
state: LibraryItemState.Processing,
},
userId,
pubsub
)
}
// create processing item
const libraryItem = await createOrUpdateLibraryItem(
{
id: articleSavingRequestId,
user: { id: userId },
readableContent: SAVING_CONTENT,
itemType: PageType.Unknown,
slug: generateSlug(url),
title: url,
originalUrl: url,
state: LibraryItemState.Processing,
publishedAt,
folder,
subscription,
savedAt,
},
userId,
pubsub
)
// get priority by checking rate limit if not specified
priority = priority || (await getPriorityByRateLimit(userId))