-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
286 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -195,7 +195,7 @@ query { | |
notifications { | ||
kind | ||
entityId | ||
isSent | ||
status | ||
} | ||
} | ||
``` | ||
|
13 changes: 13 additions & 0 deletions
13
packages/server/prisma/migrations/20230904104915_notification_status/migration.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/* | ||
Warnings: | ||
- You are about to drop the column `isSent` on the `Notification` table. All the data in the column will be lost. | ||
*/ | ||
-- CreateEnum | ||
CREATE TYPE "NotificationStatus" AS ENUM ('PENDING', 'SENT', 'FAILED'); | ||
|
||
-- AlterTable | ||
ALTER TABLE "Notification" DROP COLUMN "isSent", | ||
ADD COLUMN "retryCount" INTEGER NOT NULL DEFAULT 0, | ||
ADD COLUMN "status" "NotificationStatus" NOT NULL DEFAULT 'PENDING'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
import { createNotifications } from './createNotifications' | ||
import { sendNotifications } from './sendNotifications' | ||
import { processNotifications } from './sendNotifications' | ||
|
||
export const run = async () => { | ||
await createNotifications() | ||
await sendNotifications() | ||
await processNotifications() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,58 @@ | ||
import { pick } from 'lodash' | ||
import { Member, Notification } from '@prisma/client' | ||
import { error, info, warn } from 'npmlog' | ||
|
||
import { EMAIL_MAX_RETRY_COUNT } from '@/common/config' | ||
import { prisma } from '@/common/prisma' | ||
import { EmailProvider, createEmailProvider, errorMessage } from '@/common/utils' | ||
|
||
import { createEmailNotifier } from './model/email' | ||
|
||
export const sendNotifications = async (): Promise<any[]> => { | ||
export const processNotifications = async (): Promise<void> => { | ||
let emailProvider: EmailProvider | ||
try { | ||
emailProvider = createEmailProvider() | ||
} catch (err) { | ||
warn('Email notifications', 'Failed to configure email provider with error:', errorMessage(err)) | ||
return [] | ||
return | ||
} | ||
|
||
const notifications = await prisma.notification.findMany({ where: { isSent: false }, include: { member: true } }) | ||
const notifyViaEmail = createEmailNotifier(emailProvider) | ||
const notifications = await prisma.notification.findMany({ where: { status: 'PENDING' }, include: { member: true } }) | ||
await sendNotifications(notifications, emailProvider) | ||
} | ||
|
||
type NotificationWithMember = Notification & { member: Member } | ||
|
||
export const sendNotifications = async ( | ||
notifications: NotificationWithMember[], | ||
emailProvider: EmailProvider | ||
): Promise<void> => { | ||
const notifyViaEmail = createEmailNotifier(emailProvider) | ||
info('Email notifications', 'Attempt to email', notifications.length, 'notifications') | ||
|
||
return Promise.all( | ||
await Promise.all( | ||
notifications.map(async (notification) => { | ||
const { id, retryCount } = notification | ||
|
||
try { | ||
await notifyViaEmail(notification) | ||
return await prisma.notification.update({ where: pick(notification, 'id'), data: { isSent: true } }) | ||
return await prisma.notification.update({ where: { id }, data: { status: 'SENT' } }) | ||
} catch (errData) { | ||
error('Email notification failure', `Failed to email ${notification.id} with error:`, errorMessage(errData)) | ||
if (retryCount >= EMAIL_MAX_RETRY_COUNT) { | ||
error( | ||
'Email notification failure', | ||
`Failed to email ${notification.id} with ${EMAIL_MAX_RETRY_COUNT} retries. Error:`, | ||
errorMessage(errData) | ||
) | ||
return await prisma.notification.update({ where: { id }, data: { status: 'FAILED' } }) | ||
} else { | ||
warn( | ||
'Email notification failure', | ||
`Failed to email ${notification.id}. Will retry. Error:`, | ||
errorMessage(errData) | ||
) | ||
return await prisma.notification.update({ where: { id }, data: { retryCount: retryCount + 1 } }) | ||
} | ||
} | ||
// TODO: update a fail counter instead so it can be retried N time later | ||
await prisma.notification.update({ where: pick(notification, 'id'), data: { isSent: true } }) | ||
}) | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.