Skip to content

Commit

Permalink
Merge pull request #1776 from Giveth/feat/stellar_integration
Browse files Browse the repository at this point in the history
fix: reduce timer to 15 mins & update renew draft donation expiration…
  • Loading branch information
Meriem-BM authored Aug 22, 2024
2 parents 09bb7a3 + 97b0cec commit 9bfd0a5
Show file tree
Hide file tree
Showing 6 changed files with 159 additions and 31 deletions.
114 changes: 88 additions & 26 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@
"twitter-api-sdk": "^1.0.9",
"type-graphql": "2.0.0-beta.1",
"typedi": "0.8.0",
"typeorm": "0.3.20"
"typeorm": "0.3.20",
"ws": "^8.18.0"
},
"lint-staged": {
"*.ts": [
Expand Down
14 changes: 10 additions & 4 deletions src/resolvers/draftDonationResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ export class DraftDonationResolver {
: fromAddress.toLowerCase();

const expiresAt = isQRDonation
? new Date(Date.now() + 30 * 60 * 1000)
? new Date(Date.now() + 15 * 60 * 1000)
: undefined;

const draftDonationId = await DraftDonation.createQueryBuilder(
Expand Down Expand Up @@ -418,6 +418,12 @@ export class DraftDonationResolver {
{ status: DRAFT_DONATION_STATUS.FAILED },
);

// Notify clients of new donation
(global as any).notifyDraftDonationFailed({
draftDonationId: id,
expiresAt: draftDonation.expiresAt,
});

return true;
} catch (e) {
logger.error(
Expand All @@ -432,16 +438,16 @@ export class DraftDonationResolver {
@Arg('id', _type => Int) id: number,
): Promise<DraftDonation | null> {
try {
const expiresAt = new Date(Date.now() + 30 * 60 * 1000);
const expiresAt = new Date(Date.now() + 15 * 60 * 1000);
const draftDonation = await DraftDonation.createQueryBuilder(
'draftDonation',
)
.where('draftDonation.id = :id', { id })
.andWhere('draftDonation.isQRDonation = :isQRDonation', {
isQRDonation: true,
})
.andWhere('draftDonation.status = :status', {
status: DRAFT_DONATION_STATUS.PENDING,
.andWhere('draftDonation.status != :status', {
status: DRAFT_DONATION_STATUS.MATCHED,
})
.getOne();

Expand Down
7 changes: 7 additions & 0 deletions src/server/bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ import { runDraftDonationMatchWorkerJob } from '../services/cronJobs/draftDonati
import { runCheckUserSuperTokenBalancesJob } from '../services/cronJobs/checkUserSuperTokenBalancesJob';
import { runCheckPendingRecurringDonationsCronJob } from '../services/cronJobs/syncRecurringDonationsWithNetwork';
import { runCheckQRTransactionJob } from '../services/cronJobs/checkQRTransactionJob';
import { startWebSocketServer } from '../services/ws/webSocketServer';

Resource.validate = validate;

Expand Down Expand Up @@ -291,6 +292,12 @@ export async function bootstrap() {

const httpServer = http.createServer(app);

// Start WebSocket server
const { notifyDonationAdded, notifyDraftDonationFailed } =
startWebSocketServer(httpServer);
(global as any).notifyDonationAdded = notifyDonationAdded;
(global as any).notifyDraftDonationFailed = notifyDraftDonationFailed;

await new Promise<void>((resolve, reject) => {
httpServer
.listen({ port: 4000 }, () => {
Expand Down
6 changes: 6 additions & 0 deletions src/services/cronJobs/checkQRTransactionJob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,12 @@ export async function checkTransactions(
donationId: returnedDonation.id,
});

// Notify clients of new donation
(global as any).notifyDonationAdded({
donationId: returnedDonation.id,
draftDonationId: donation.id,
});

return;
}
}
Expand Down
46 changes: 46 additions & 0 deletions src/services/ws/webSocketServer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import WebSocket from 'ws';
import { logger } from '../../utils/logger';

export function startWebSocketServer(server) {
const wss = new WebSocket.Server({ server });

// Handle WebSocket connections
wss.on('connection', ws => {
ws.on('message', message => {
logger.info(`Received message: ${message}`);
});
});

// Broadcast a message to all connected clients
function broadcastMessage(message) {
wss.clients.forEach(client => {
if (client.readyState === WebSocket.OPEN) {
client.send(message);
}
});
}

// Exported function to be called from cron job
function notifyDonationAdded(donation) {
const message = JSON.stringify({
type: 'new-donation',
data: donation,
});
broadcastMessage(message);
}

// Exported function to to be called when marking draft donation as failed
function notifyDraftDonationFailed(donation) {
const message = JSON.stringify({
type: 'draft-donation-failed',
data: donation,
});
broadcastMessage(message);
}

// Export the notifyDonationAdded function so it can be used externally
return {
notifyDonationAdded,
notifyDraftDonationFailed,
};
}

0 comments on commit 9bfd0a5

Please sign in to comment.