Use service instead of API to update subscription
This commit is contained in:
@ -8,6 +8,7 @@ import { env } from '../../env'
|
||||
import { redisDataSource } from '../../redis_data_source'
|
||||
import createHttpTaskWithToken from '../../utils/createTask'
|
||||
import { RSSRefreshContext } from './refreshAllFeeds'
|
||||
import { updateSubscription } from '../../services/update_subscription'
|
||||
|
||||
type FolderType = 'following' | 'inbox'
|
||||
|
||||
@ -615,13 +616,12 @@ const processSubscription = async (
|
||||
const nextScheduledAt = scheduledAt + updatePeriodInMs * updateFrequency
|
||||
|
||||
// update subscription lastFetchedAt
|
||||
const updatedSubscription = await sendUpdateSubscriptionMutation(
|
||||
userId,
|
||||
subscriptionId,
|
||||
lastItemFetchedAt,
|
||||
updatedLastFetchedChecksum,
|
||||
new Date(nextScheduledAt)
|
||||
)
|
||||
const updatedSubscription = await updateSubscription(userId, subscriptionId, {
|
||||
id: subscriptionId,
|
||||
lastFetchedAt: lastItemFetchedAt,
|
||||
lastFetchedChecksum: updatedLastFetchedChecksum,
|
||||
scheduledAt: new Date(nextScheduledAt),
|
||||
})
|
||||
console.log('Updated subscription', updatedSubscription)
|
||||
}
|
||||
|
||||
|
||||
@ -50,6 +50,7 @@ import {
|
||||
keysToCamelCase,
|
||||
} from '../../utils/helpers'
|
||||
import { parseFeed, parseOpml, RSS_PARSER_CONFIG } from '../../utils/parser'
|
||||
import { updateSubscription } from '../../services/update_subscription'
|
||||
|
||||
type PartialSubscription = Omit<Subscription, 'newsletterEmail'>
|
||||
|
||||
@ -332,34 +333,7 @@ export const updateSubscriptionResolver = authorized<
|
||||
},
|
||||
})
|
||||
|
||||
const updatedSubscription = await authTrx(async (t) => {
|
||||
const repo = t.getRepository(Subscription)
|
||||
|
||||
// update subscription
|
||||
await t.getRepository(Subscription).save({
|
||||
id: input.id,
|
||||
name: input.name || undefined,
|
||||
description: input.description || undefined,
|
||||
lastFetchedAt: input.lastFetchedAt
|
||||
? new Date(input.lastFetchedAt)
|
||||
: undefined,
|
||||
lastFetchedChecksum: input.lastFetchedChecksum || undefined,
|
||||
status: input.status || undefined,
|
||||
scheduledAt: input.scheduledAt
|
||||
? new Date(input.scheduledAt)
|
||||
: undefined,
|
||||
autoAddToLibrary: input.autoAddToLibrary ?? undefined,
|
||||
isPrivate: input.isPrivate ?? undefined,
|
||||
fetchContent: input.fetchContent ?? undefined,
|
||||
folder: input.folder ?? undefined,
|
||||
})
|
||||
|
||||
return repo.findOneByOrFail({
|
||||
id: input.id,
|
||||
user: { id: uid },
|
||||
})
|
||||
})
|
||||
|
||||
const updatedSubscription = await updateSubscription(uid, input.id, input)
|
||||
return {
|
||||
subscription: updatedSubscription,
|
||||
}
|
||||
|
||||
47
packages/api/src/services/update_subscription.ts
Normal file
47
packages/api/src/services/update_subscription.ts
Normal file
@ -0,0 +1,47 @@
|
||||
import { Subscription } from '../entity/subscription'
|
||||
import { UpdateSubscriptionInput } from '../generated/graphql'
|
||||
import { getRepository } from '../repository'
|
||||
|
||||
const ensureOwns = async (userId: string, subscriptionId: string) => {
|
||||
const repo = getRepository(Subscription)
|
||||
|
||||
const existing = repo.findOneByOrFail({
|
||||
id: subscriptionId,
|
||||
user: { id: userId },
|
||||
})
|
||||
if (!existing) {
|
||||
throw new Error('Can not find subscription being updated.')
|
||||
}
|
||||
}
|
||||
|
||||
export const updateSubscription = async (
|
||||
userId: string,
|
||||
subscriptionId: string,
|
||||
newData: UpdateSubscriptionInput
|
||||
): Promise<Subscription> => {
|
||||
ensureOwns(userId, subscriptionId)
|
||||
|
||||
const repo = getRepository(Subscription)
|
||||
await repo.save({
|
||||
id: subscriptionId,
|
||||
name: newData.name || undefined,
|
||||
description: newData.description || undefined,
|
||||
lastFetchedAt: newData.lastFetchedAt
|
||||
? new Date(newData.lastFetchedAt)
|
||||
: undefined,
|
||||
lastFetchedChecksum: newData.lastFetchedChecksum || undefined,
|
||||
status: newData.status || undefined,
|
||||
scheduledAt: newData.scheduledAt
|
||||
? new Date(newData.scheduledAt)
|
||||
: undefined,
|
||||
autoAddToLibrary: newData.autoAddToLibrary ?? undefined,
|
||||
isPrivate: newData.isPrivate ?? undefined,
|
||||
fetchContent: newData.fetchContent ?? undefined,
|
||||
folder: newData.folder ?? undefined,
|
||||
})
|
||||
|
||||
return (await getRepository(Subscription).findOneByOrFail({
|
||||
id: subscriptionId,
|
||||
user: { id: userId },
|
||||
})) as Subscription
|
||||
}
|
||||
Reference in New Issue
Block a user