From a2ecee7f7bca5a5b4f3caf4e29d5ab67f9445476 Mon Sep 17 00:00:00 2001 From: NiraliSonani Date: Fri, 29 Nov 2024 19:04:46 +0530 Subject: [PATCH] Modify cloud function for reason --- functions/lib/locales/en.json | 4 +++ functions/src/locales/en.json | 4 +++ .../notifications_service.ts | 28 ++++++++++++++++--- lint | 23 +++++++++------ 4 files changed, 47 insertions(+), 12 deletions(-) diff --git a/functions/lib/locales/en.json b/functions/lib/locales/en.json index 87da7eac7..f8e175194 100644 --- a/functions/lib/locales/en.json +++ b/functions/lib/locales/en.json @@ -12,9 +12,13 @@ "expense_deleted": "Expense deleted: {expenseName} \n{amountMessage}", "expense_restored": "Expense restored: {expenseName} \n{amountMessage}", "transaction_added": "{payerName} paid {receiverName} {amountMessage}", + "transaction_added_with_reason": "{payerName} paid {receiverName} {amountMessage} for \"{paymentReason}\"", "transaction_updated": "Payment updated: {payerName} paid {receiverName} {amountMessage}", + "transaction_updated_with_reason": "Payment updated: {payerName} paid {receiverName} {amountMessage} for \"{paymentReason}\"", "transaction_deleted": "Payment deleted: {payerName} paid {receiverName} {amountMessage}", + "transaction_deleted_with_reason": "Payment deleted: {payerName} paid {receiverName} {amountMessage} for \"{paymentReason}\"", "transaction_restored": "Payment restored: {payerName} paid {receiverName} {amountMessage}", + "transaction_restored_with_reason": "Payment restored: {payerName} paid {receiverName} {amountMessage} for \"{paymentReason}\"", "new_activity": "New activity detected", "owe": "You owe {amount}", "getBack": "You get back {amount}", diff --git a/functions/src/locales/en.json b/functions/src/locales/en.json index 0892cd513..a382cd53a 100644 --- a/functions/src/locales/en.json +++ b/functions/src/locales/en.json @@ -12,9 +12,13 @@ "expense_deleted": "Expense deleted: {expenseName} \n{amountMessage}", "expense_restored": "Expense restored: {expenseName} \n{amountMessage}", "transaction_added": "{payerName} paid {receiverName} {amountMessage}", + "transaction_added_with_reason": "{payerName} paid {receiverName} {amountMessage} for \"{paymentReason}\"", "transaction_updated": "Payment updated: {payerName} paid {receiverName} {amountMessage}", + "transaction_updated_with_reason": "Payment updated: {payerName} paid {receiverName} {amountMessage} for \"{paymentReason}\"", "transaction_deleted": "Payment deleted: {payerName} paid {receiverName} {amountMessage}", + "transaction_deleted_with_reason": "Payment deleted: {payerName} paid {receiverName} {amountMessage} for \"{paymentReason}\"", "transaction_restored": "Payment restored: {payerName} paid {receiverName} {amountMessage}", + "transaction_restored_with_reason": "Payment restored: {payerName} paid {receiverName} {amountMessage} for \"{paymentReason}\"", "new_activity": "New activity detected", "owe": "You owe {amount}", "getBack": "You get back {amount}", diff --git a/functions/src/notifications_service/notifications_service.ts b/functions/src/notifications_service/notifications_service.ts index b51df42de..b0bb9891b 100644 --- a/functions/src/notifications_service/notifications_service.ts +++ b/functions/src/notifications_service/notifications_service.ts @@ -39,6 +39,7 @@ interface ActivityData { expense_name?: string; payer_name?: string; receiver_name?: string + payment_reason?: string amount?: number; } @@ -62,6 +63,8 @@ export const onActivityCreate = onDocumentCreated( return; } + logger.info('Activity Data:', activityData); + const userId = event.params.userId; const activityMessage = generateNotificationMessage(activityData); @@ -81,6 +84,7 @@ function generateNotificationMessage(activityData: ActivityData) { const actionUserName = activityData.action_user_name; const payerName = activityData.payer_name ?? messages.someone; const receiverName = activityData.receiver_name ?? messages.someone; + const paymentReason = activityData.payment_reason; const groupName = activityData.group_name; const previousGroupName = activityData.previous_group_name ?? messages.unknown; @@ -122,16 +126,32 @@ function generateNotificationMessage(activityData: ActivityData) { return messages.expense_restored.replace("{expenseName}", expenseName).replace("{amountMessage}", amountMessage); case 'transaction_added': - return messages.transaction_added.replace("{payerName}", payerName).replace("{receiverName}", receiverName).replace("{amountMessage}", formatCurrency(Math.abs(amount))); + if (paymentReason != null && paymentReason.trim().length > 0) { + return messages.transaction_added_with_reason.replace("{payerName}", payerName).replace("{receiverName}", receiverName).replace("{amountMessage}", formatCurrency(Math.abs(amount))).replace("{paymentReason}", paymentReason); + } else { + return messages.transaction_added.replace("{payerName}", payerName).replace("{receiverName}", receiverName).replace("{amountMessage}", formatCurrency(Math.abs(amount))); + } case 'transaction_updated': - return messages.transaction_updated.replace("{payerName}", payerName).replace("{receiverName}", receiverName).replace("{amountMessage}", formatCurrency(Math.abs(amount))); + if (paymentReason != null && paymentReason.trim().length > 0) { + return messages.transaction_updated_with_reason.replace("{payerName}", payerName).replace("{receiverName}", receiverName).replace("{amountMessage}", formatCurrency(Math.abs(amount))).replace("{paymentReason}", paymentReason); + } else { + return messages.transaction_updated.replace("{payerName}", payerName).replace("{receiverName}", receiverName).replace("{amountMessage}", formatCurrency(Math.abs(amount))); + } case 'transaction_deleted': - return messages.transaction_deleted.replace("{payerName}", payerName).replace("{receiverName}", receiverName).replace("{amountMessage}", formatCurrency(Math.abs(amount))); + if (paymentReason != null && paymentReason.trim().length > 0) { + return messages.transaction_deleted_with_reason.replace("{payerName}", payerName).replace("{receiverName}", receiverName).replace("{amountMessage}", formatCurrency(Math.abs(amount))).replace("{paymentReason}", paymentReason); + } else { + return messages.transaction_deleted.replace("{payerName}", payerName).replace("{receiverName}", receiverName).replace("{amountMessage}", formatCurrency(Math.abs(amount))); + } case 'transaction_restored': - return messages.transaction_restored.replace("{payerName}", payerName).replace("{receiverName}", receiverName).replace("{amountMessage}", formatCurrency(Math.abs(amount))); + if (paymentReason != null && paymentReason.trim().length > 0) { + return messages.transaction_restored_with_reason.replace("{payerName}", payerName).replace("{receiverName}", receiverName).replace("{amountMessage}", formatCurrency(Math.abs(amount))).replace("{paymentReason}", paymentReason); + } else { + return messages.transaction_restored.replace("{payerName}", payerName).replace("{receiverName}", receiverName).replace("{amountMessage}", formatCurrency(Math.abs(amount))); + } default: return messages.new_activity; diff --git a/lint b/lint index 126c90061..b9c2ae4dc 100644 --- a/lint +++ b/lint @@ -6,13 +6,20 @@ Running command: npm --prefix "$RESOURCE_DIR" run build > tsc ✔ functions: Finished running predeploy script. -Running command: npm --prefix "$RESOURCE_DIR" run lint +i functions: preparing codebase default for deployment +i functions: ensuring required API cloudfunctions.googleapis.com is enabled... +i functions: ensuring required API cloudbuild.googleapis.com is enabled... +i artifactregistry: ensuring required API artifactregistry.googleapis.com is enabled... +✔ functions: required API cloudbuild.googleapis.com is enabled +✔ functions: required API cloudfunctions.googleapis.com is enabled +✔ artifactregistry: required API artifactregistry.googleapis.com is enabled +⚠ functions: package.json indicates an outdated version of firebase-functions. Please upgrade using npm install --save firebase-functions@latest in your functions directory. +i functions: Loading and analyzing source code for codebase default to determine what to deploy +Serving at port 8638 -> lint -> eslint --ext .js,.ts . - -Running command: npm --prefix "$RESOURCE_DIR" run build - -> build -> tsc +{"severity":"DEBUG","message":"Firebase app already initialized"} +i extensions: ensuring required API firebaseextensions.googleapis.com is enabled... +✔ extensions: required API firebaseextensions.googleapis.com is enabled +i functions: preparing functions directory for uploading... +i functions: packaged /Users/NiraliSonani/Desktop/Splito/functions (97.65 KB) for uploading