Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added some notification processor #296

Merged
merged 4 commits into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions apps/web/public/service-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,11 @@ self.addEventListener('notificationclick', async function (event) {
clients.openWindow(`/chat?id=${globalPay.friendshipId}`)
}
console.log("accepted");
} else {
clients.openWindow("/");
}
if(event.action === "reject") {
if(globalPay.friendshipId) {
clients.openWindow(`/chat?id=${globalPay.friendshipId}`)
}
console.log("rejected");
}
}, false);
16 changes: 13 additions & 3 deletions backend/backend-common/src/db/friendship.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,10 +199,11 @@ export const acceptFriendship = async (
export const putFriendshipStatus = async (
clientId: string,
id: string,
status: FriendshipStatusEnum
status: FriendshipStatusEnum,
): Promise<{
status: dbResStatus,
friendshipStatus?: FriendshipStatus
to?: string,
}> => {
const response = await chain("mutation")({
update_friendship: [{
Expand All @@ -219,14 +220,23 @@ export const putFriendshipStatus = async (
}, {
returning: {
id: true,
status: true
status: true,
client1: {
username: true,
id: true
},
client2: {
username: true,
id: true
}
}
}]
}, { operationName: "putFriendshipStatus" });
if (response.update_friendship?.returning[0].id) {
return {
status: dbResStatus.Ok,
friendshipStatus: response.update_friendship?.returning[0].status as FriendshipStatus
friendshipStatus: response.update_friendship?.returning[0].status as FriendshipStatus,
to: response.update_friendship?.returning[0].client1.id === clientId ? response.update_friendship?.returning[0].client2.username as string : response.update_friendship?.returning[0].client1.username as string
}
}
return {
Expand Down
47 changes: 47 additions & 0 deletions backend/notif-worker/src/db/txn.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { Chain } from "@paybox/zeus";
import { HASURA_URL, JWT } from "../config";
import { HASURA_ADMIN_SERCRET, Network, NotifSubType, dbResStatus } from "@paybox/common";

const chain = Chain(HASURA_URL, {
headers: {
Authorization: `Bearer ${JWT}`,
"x-hasura-admin-secret": HASURA_ADMIN_SERCRET,
},
});

/**
*
* @param id
* @param clientId
* @returns
*/
export const getTxnDetails = async (
id: string,
username?: string
): Promise<{
status: dbResStatus,
network?: Network,
amount?: number,
}> => {
const response = await chain("query")({
transactions: [{
limit: 1,
where: {
id: { _eq: id }
}
}, {
network: true,
amount: true,
}]
}, { operationName: "getTxnDetails" });
if (Array.isArray(response.transactions)) {
return {
status: dbResStatus.Ok,
network: response.transactions[0].network as Network,
amount: response.transactions[0].amount as number
}
}
return {
status: dbResStatus.Error
}
}
16 changes: 8 additions & 8 deletions backend/notif-worker/src/kafka/consumer.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Consumer } from "kafkajs";
import { kafka } from "..";
import { NotifTopics } from "@paybox/common";
import {notifyFriendRequest, notifyFriendRequestAccepted} from "../processes";
import {notifyFriendRequest, notifyFriendRequestAccepted, notifyFriendRequestRejected, notifyPaid, notifyReceiveTxn} from "../processes";

export class ConsumerWorker {
private consumer!: Consumer;
Expand Down Expand Up @@ -52,22 +52,22 @@ export class ConsumerWorker {
break;

case NotifTopics.FriendRequestRejected:
//TODO: NOTIFY THE FRIEND REQUEST REJECTED
await notifyFriendRequestRejected(payload.to, payload.from, payload.friendshipId)
console.log("Friend Request Rejected");
break;

case NotifTopics.TxnAccept:
//Todo: NOTIFY THE TRANSACTION ACCEPTED
await notifyReceiveTxn(payload.to, payload.from, payload.txnId)
console.log("Transaction Accepted");
break;

case NotifTopics.TxnReject:
//Todo: NOTIFY THE TRANSACTION REJECTED
console.log("Transaction Rejected");
break;
// case NotifTopics.TxnReject:
// //Todo: NOTIFY THE TRANSACTION REJECTED
// console.log("Transaction Rejected");
// break;

case NotifTopics.Paid:
//Todo: NOTIFY THE TRANSACTION PAID
await notifyPaid(payload.to, payload.from, payload.txnId)
console.log("Transaction Paid");
break;

Expand Down
118 changes: 118 additions & 0 deletions backend/notif-worker/src/processes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { NotifTopics, dbResStatus } from "@paybox/common";
import { getClientFriendship } from "./db/friendship";
import { getUsername } from "./db/client";
import { notify } from "./notifier";
import { getTxnDetails } from "./db/txn";

/**
*
Expand Down Expand Up @@ -40,6 +41,13 @@ export const notifyFriendRequest = async (
});
};

/**
*
* @param to
* @param from
* @param friendshipId
* @returns
*/
export const notifyFriendRequestAccepted = async (
to: string,
from: string,
Expand All @@ -62,4 +70,114 @@ export const notifyFriendRequestAccepted = async (
friendshipId
}
});
}

/**
*
* @param to
* @param from
* @param friendshipId
* @returns
*/
export const notifyFriendRequestRejected = async (
to: string,
from: string,
friendshipId: string
) => {
const { status, username } = await getUsername(from);
if (status === dbResStatus.Error || !username) {
return;
}

await notify({
to,
body: `Friend Request Rejected by ${username}`,
title: `Friend Request Rejected`,
href: `/popup.html#/notifications?title="Notifications"&props=%7B%7D&nav=tab`,
image: "https://img.freepik.com/free-psd/3d-illustration-person-with-sunglasses_23-2149436188.jpg?size=338&ext=jpg&ga=GA1.1.735520172.1711238400&semt=ais",
tag: NotifTopics.FriendRequestRejected,
vibrate: [200, 100, 200],
payload: {
friendshipId
}
});
}

/**
*
* @param to
* @param from
* @param txnId
* @returns
*/
export const notifyReceiveTxn = async (
to: string,
from: string,
txnId: string
) => {
const { status, username } = await getUsername(from);
if (status === dbResStatus.Error || !username) {
return;
}

const { amount, network, status: txnDetailsStatus } = await getTxnDetails(txnId, to);
if (txnDetailsStatus === dbResStatus.Error || !amount || !network) {
return;
}

await notify({
to,
body: `Received ${amount} ${network} from ${username}`,
title: `Transaction Received`,
href: getTxnHref(txnId),
image: "https://img.freepik.com/free-psd/3d-illustration-person-with-sunglasses_23-2149436188.jpg?size=338&ext=jpg&ga=GA1.1.735520172.1711238400&semt=ais",
tag: NotifTopics.TxnAccept,
vibrate: [200, 100, 200],
payload: {
txnId
}
});
}

const getTxnHref = (txnId?: string,) => {
if (!txnId) {
return undefined;
}
return `/popup.html#/txn?props=%7B"id"%3A"${txnId}`;
};

/**
*
* @param notifyTo
* @param paidTo
* @param txnId
* @returns
*/
export const notifyPaid = async (
notifyTo: string,
paidTo: string,
txnId: string
) => {
const { status, username } = await getUsername(paidTo);
if (status === dbResStatus.Error || !username) {
return;
}

const { amount, network, status: txnDetailsStatus } = await getTxnDetails(txnId);
if (txnDetailsStatus === dbResStatus.Error || !amount || !network) {
return;
}

await notify({
to: notifyTo,
body: `Paid ${amount} ${network} to ${username}`,
title: `Transaction Paid`,
href: getTxnHref(txnId),
image: "https://img.freepik.com/free-psd/3d-illustration-person-with-sunglasses_23-2149436188.jpg?size=338&ext=jpg&ga=GA1.1.735520172.1711238400&semt=ais",
tag: NotifTopics.Paid,
vibrate: [200, 100, 200],
payload: {
txnId
}
});
}
17 changes: 17 additions & 0 deletions backend/ws/src/routes/friendship.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,23 @@ friendshipRouter.put('/', async (req, res) => {
.json({ msg: "Database Error", status: responseStatus.Error });
}

if(query.friendshipStatus === FriendshipStatusEnum.Rejected) {
await NotifWorker.getInstance().publishOne({
topic: "notif",
message: [{
key: id,
value: JSON.stringify({
from: id,
to: query.to,
friendshipId,
type: NotifTopics.FriendRequestRejected,
}),
partition: 0
}],

})
}

return res
.status(200)
.json({
Expand Down
Loading