Created
November 1, 2023 17:18
-
-
Save TheBrotherFromASouthernMother/ac373baba1852edd48b395debdfeb196 to your computer and use it in GitHub Desktop.
Sending a "Like" notification (Node + Bull + Redis + Firebase Cloud Messaging)
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
const { getReview } = require('app/services/reviews/get.js'); | |
const { getUser } = require('app/services/users/get.js'); | |
const { Sentry, getEnv } = require('app/config.js'); | |
const moment = require('moment-timezone'); | |
const { errorLogger, infoLogger, warnLogger } = require('lib/logger.js'); | |
const { createPushNotification } = require('app/services/push_notifications/create.js'); | |
const { getContactPreferences } = require('app/services/contact_preferences/get.js'); | |
const isEmpty = require('lodash/isEmpty'); | |
const isNil = require('lodash/isNil'); | |
const get = require('lodash/get'); | |
const truncate = require('lodash/truncate'); | |
const { pushNotificationReceiptQueue } = require('workers/queues.js'); | |
module.exports = async (data, eventInfo) => { | |
let eventId = null; | |
let eventName = null; | |
try { | |
eventId = get(eventInfo, 'id', null); | |
eventName = get(eventInfo, 'name', null); | |
infoLogger(`EVENT ID (${eventId}): starting execution of event consumer for event: ${eventName}`); | |
if (isEmpty(data)) throw new Error(`malformed event consumed for like_created consumer`); | |
const user = await getUser({ id: data.userId }); | |
if (isNil(user)) throw new Error(`cannot create like push notification with missing source user ${data.userId}`); | |
const review = await getReview(data.reviewId); | |
if (isNil(review)) throw new Error(`cannot create like push notification with missing review ${data.reviewId}`); | |
const contactPreferences = await getContactPreferences({ userId: review.userId }); | |
if (!contactPreferences || !contactPreferences.push_notification_contact_consent) { | |
warnLogger( | |
`EVENT ID (${eventId}): warning cannot send push notification to user where contact preferences are falsely. For user with id: ${review.userId}` | |
); | |
return; | |
}; | |
const sourceUserFirstName = user.full_name.split(' ')[0]; | |
const truncatedSourceUserFirstName = truncate(sourceUserFirstName, { | |
length: 30, | |
omission: ' ' | |
}); | |
const truncatedPlaceName = truncate(review.place_name, { | |
length: 50, | |
}); | |
const message = { | |
title: 'The Green Book Project', | |
subtitle: 'Someone liked your review!', | |
body: `${truncatedSourceUserFirstName} liked your review of ${truncatedPlaceName}`, | |
data: {}, | |
userId: review.userId, | |
}; | |
const tickets = await createPushNotification(message); | |
tickets.forEach(ticket => { | |
if (ticket.status === 'ok') { | |
infoLogger(`EVENT ID (${eventId}): push notification created for like (id: ${data.id}). Ticket id: ${ticket.id}`) | |
} else { | |
errorLogger(`EVENT ID (${eventId}): Failed to send push notification for "like". Expo API returned a status of "${ticket.status}" for ticket id: ${ticket.id}.`) | |
} | |
}); | |
const now = moment(); | |
const delayTimeInMinutes = getEnv('PUSH_NOTIFICAION_RECEIPT_DELAY_IN_MINUTES'); | |
const delayedTime = moment().add(delayTimeInMinutes, 'minutes'); | |
const jobOptions = { | |
delay: delayedTime.diff(now, 'seconds') * 1000, // job delay is set in milliseconds | |
} | |
const receiptIds = tickets.map(ticket => ticket.id); | |
pushNotificationReceiptQueue.add({ receiptIds }, jobOptions); | |
infoLogger(`EVENT ID (${eventId}): completed execution of handler for event ${eventName}`) | |
} catch (error) { | |
Sentry.captureException(error) | |
errorLogger(`EVENT ID (${eventId}): ${error.toString()}`, error.stack); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment