mirror of
https://github.com/omnivore-app/omnivore.git
synced 2026-03-11 08:54:26 +00:00
Fix subscriptions issues (#501)
* Replace fetch with axios * Add url in subscription * Remove url in subscription * Unsubscribe all newsletters before deleting that email * Continue unsubscribing others if one got error
This commit is contained in:
parent
4d887b3505
commit
ccb79845bc
2 changed files with 49 additions and 21 deletions
|
|
@ -21,6 +21,7 @@ import { analytics } from '../../utils/analytics'
|
|||
import { env } from '../../env'
|
||||
import { AppDataSource } from '../../server'
|
||||
import { User } from '../../entity/user'
|
||||
import { unsubscribeAll } from '../../services/subscriptions'
|
||||
|
||||
export const createNewsletterEmailResolver = authorized<
|
||||
CreateNewsletterEmailSuccess,
|
||||
|
|
@ -116,6 +117,9 @@ export const deleteNewsletterEmailResolver = authorized<
|
|||
}
|
||||
}
|
||||
|
||||
// unsubscribe all before deleting
|
||||
await unsubscribeAll(newsletterEmail.user.id, newsletterEmail.address)
|
||||
|
||||
const deleted = await deleteNewsletterEmail(args.newsletterEmailId)
|
||||
if (deleted) {
|
||||
return {
|
||||
|
|
|
|||
|
|
@ -2,6 +2,31 @@ import { Subscription } from '../entity/subscription'
|
|||
import { getRepository } from '../entity/utils'
|
||||
import { SubscriptionStatus } from '../generated/graphql'
|
||||
import { sendEmail } from '../utils/sendEmail'
|
||||
import axios from 'axios'
|
||||
|
||||
const sendUnsubscribeEmail = async (
|
||||
unsubscribeMailTo: string,
|
||||
newsletterEmail: string
|
||||
): Promise<void> => {
|
||||
const sent = await sendEmail({
|
||||
to: unsubscribeMailTo,
|
||||
subject: 'Unsubscribe',
|
||||
text: `This message was automatically generated by Omnivore.`,
|
||||
from: newsletterEmail,
|
||||
})
|
||||
|
||||
if (!sent) {
|
||||
throw new Error(`Failed to unsubscribe, email: ${unsubscribeMailTo}`)
|
||||
}
|
||||
}
|
||||
|
||||
const sendUnsubscribeHttpRequest = async (url: string): Promise<void> => {
|
||||
const response = await axios.get(url)
|
||||
|
||||
if (response.status !== 200) {
|
||||
throw new Error(`Failed to unsubscribe, response: ${response.statusText}`)
|
||||
}
|
||||
}
|
||||
|
||||
export const saveSubscription = async (
|
||||
userId: string,
|
||||
|
|
@ -12,13 +37,13 @@ export const saveSubscription = async (
|
|||
): Promise<Subscription> => {
|
||||
const subscription = await getRepository(Subscription).findOneBy({
|
||||
name,
|
||||
newsletterEmail,
|
||||
user: { id: userId },
|
||||
})
|
||||
|
||||
if (subscription) {
|
||||
// if subscription already exists, updates updatedAt
|
||||
subscription.status = SubscriptionStatus.Active
|
||||
subscription.newsletterEmail = newsletterEmail
|
||||
unsubscribeMailTo && (subscription.unsubscribeMailTo = unsubscribeMailTo)
|
||||
unsubscribeHttpUrl && (subscription.unsubscribeHttpUrl = unsubscribeHttpUrl)
|
||||
return getRepository(Subscription).save(subscription)
|
||||
|
|
@ -56,28 +81,27 @@ export const unsubscribe = async (
|
|||
return getRepository(Subscription).save(subscription)
|
||||
}
|
||||
|
||||
const sendUnsubscribeEmail = async (
|
||||
unsubscribeMailTo: string,
|
||||
export const unsubscribeAll = async (
|
||||
userId: string,
|
||||
newsletterEmail: string
|
||||
): Promise<void> => {
|
||||
const sent = await sendEmail({
|
||||
to: unsubscribeMailTo,
|
||||
subject: 'Unsubscribe',
|
||||
text: `This message was automatically generated by Omnivore.`,
|
||||
from: newsletterEmail,
|
||||
})
|
||||
try {
|
||||
const subscriptions = await getRepository(Subscription).find({
|
||||
where: {
|
||||
user: { id: userId },
|
||||
status: SubscriptionStatus.Active,
|
||||
newsletterEmail,
|
||||
},
|
||||
})
|
||||
|
||||
if (!sent) {
|
||||
throw new Error(`Failed to unsubscribe, email: ${unsubscribeMailTo}`)
|
||||
}
|
||||
}
|
||||
|
||||
const sendUnsubscribeHttpRequest = async (url: string): Promise<void> => {
|
||||
const response = await fetch(url, {
|
||||
method: 'GET',
|
||||
})
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`Failed to unsubscribe, response: ${response.statusText}`)
|
||||
for (const subscription of subscriptions) {
|
||||
try {
|
||||
await unsubscribe(subscription)
|
||||
} catch (error) {
|
||||
console.log('Failed to unsubscribe', error)
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.log('Failed to unsubscribe all', error)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue