Skip to content

Commit

Permalink
Modify cloud function for reason
Browse files Browse the repository at this point in the history
  • Loading branch information
cp-nirali-s committed Nov 29, 2024
1 parent 7dfe413 commit a2ecee7
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 12 deletions.
4 changes: 4 additions & 0 deletions functions/lib/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -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}",
Expand Down
4 changes: 4 additions & 0 deletions functions/src/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -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}",
Expand Down
28 changes: 24 additions & 4 deletions functions/src/notifications_service/notifications_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ interface ActivityData {
expense_name?: string;
payer_name?: string;
receiver_name?: string
payment_reason?: string
amount?: number;
}

Expand All @@ -62,6 +63,8 @@ export const onActivityCreate = onDocumentCreated(
return;
}

logger.info('Activity Data:', activityData);

const userId = event.params.userId;
const activityMessage = generateNotificationMessage(activityData);

Expand All @@ -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;

Expand Down Expand Up @@ -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;
Expand Down
23 changes: 15 additions & 8 deletions lint
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit a2ecee7

Please sign in to comment.