Skip to content

Commit

Permalink
Merge pull request #4764 from Giveth/remove-mongoDB-inserOne
Browse files Browse the repository at this point in the history
changed mogoDb connection and insertOne method
  • Loading branch information
lovelgeorge99 committed Sep 25, 2024
2 parents afa3732 + 3c2bf5b commit 4417654
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 40 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"graphql": "^16.8.1",
"lodash.isequal": "^4.5.0",
"lottie-react": "^2.4.0",
"mongodb": "6.9.0",
"next": "^14.2.3",
"nprogress": "^0.2.0",
"posthog-js": "^1.147.0",
Expand Down
62 changes: 23 additions & 39 deletions pages/api/donation-backup.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { captureException } from '@sentry/nextjs';
import { getNowUnixMS } from '@/helpers/time';
import { SENTRY_URGENT } from '@/configuration';
import { getMongoDB } from '@/lib/mongoDb/db';

const handler = (req, res) => {
const handler = async (req, res) => {
const { body, method, headers } = req;
const now = getNowUnixMS();
body.saveTimestamp = now;
Expand All @@ -13,46 +14,29 @@ const handler = (req, res) => {
}
try {
if (method === 'POST') {
fetch(process.env.MONGO_DONATION_URL, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Access-Control-Request-Headers': '*',
'api-key': process.env.MONGO_DONATION_API_KEY,
},
body: JSON.stringify({
collection: process.env.MONGO_DONATION_COLLECTION,
database: process.env.MONGO_DONATION_DATABASE,
dataSource: process.env.MONGO_DONATION_DATA_SOURCE,
document: body,
}),
})
.then(response => response.json())
.then(data => {
console.log(data);
res.status(200).json({
message: 'Successfully saved',
id: data.insertedId,
});
})
.catch(error => {
captureException(
{
data: body,
authorization: headers.authorization,
error,
},
{
tags: {
section: SENTRY_URGENT,
},
},
);
res.status(200).json('Sent to sentry');
});
console.log('body', body);
const db = await getMongoDB();
const response = await db.collection('failed_donation').insertOne({
...body,
});
res.status(200).json({
message: 'Successfully saved',
id: response.insertedId,
});
}
} catch (error) {
console.log('Error in saving donation to DB', error);
captureException(
{
data: body,
authorization: headers.authorization,
error,
},
{
tags: {
section: SENTRY_URGENT,
},
},
);
res.status(500).json();
}
};
Expand Down
20 changes: 20 additions & 0 deletions src/lib/mongoDb/db.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import * as mongoDB from 'mongodb';

let mongoClient: mongoDB.MongoClient;

export async function connectToMongo() {
const url = process.env.MONGO_DONATION_CONNECTION_URI;
if (!url) {
throw new Error('MONGODB_CONNECTION_URL is not set');
}
if (!mongoClient) {
mongoClient = new mongoDB.MongoClient(url);
await mongoClient.connect();
}
return mongoClient;
}

export async function getMongoDB(): Promise<mongoDB.Db> {
const client = await connectToMongo();
return client.db(process.env.MONGO_DONATION_DATABASE);
}
70 changes: 69 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2102,6 +2102,13 @@
resolved "https://registry.npmjs.org/@mobily/ts-belt/-/ts-belt-3.13.1.tgz"
integrity sha512-K5KqIhPI/EoCTbA6CGbrenM9s41OouyK8A03fGJJcla/zKucsgLbz8HNbeseoLarRPgyWJsUyCYqFhI7t3Ra9Q==

"@mongodb-js/saslprep@^1.1.5":
version "1.1.9"
resolved "https://registry.yarnpkg.com/@mongodb-js/saslprep/-/saslprep-1.1.9.tgz#e974bab8eca9faa88677d4ea4da8d09a52069004"
integrity sha512-tVkljjeEaAhCqTzajSdgbQ6gE6f3oneVwa3iXR6csiEwXXOFsiC6Uh9iAjAhXPtqa/XMDHWjjeNH/77m/Yq2dw==
dependencies:
sparse-bitfield "^3.0.3"

"@motionone/animation@^10.15.1", "@motionone/animation@^10.17.0":
version "10.17.0"
resolved "https://registry.npmjs.org/@motionone/animation/-/animation-10.17.0.tgz"
Expand Down Expand Up @@ -4999,6 +5006,18 @@
resolved "https://registry.npmjs.org/@types/web/-/web-0.0.138.tgz"
integrity sha512-oQD74hl+cNCZdSWIupJCXZ2azTuB3MJ/mrWlgYt+v4pD7/Dr78gl5hKAdieZNf9NrAqwUez79bHtnFVSNSscWA==

"@types/webidl-conversions@*":
version "7.0.3"
resolved "https://registry.yarnpkg.com/@types/webidl-conversions/-/webidl-conversions-7.0.3.tgz#1306dbfa53768bcbcfc95a1c8cde367975581859"
integrity sha512-CiJJvcRtIgzadHCYXw7dqEnMNRjhGZlYK05Mj9OyktqV8uVT8fD2BFOB7S1uwBE3Kj2Z+4UyPmFw/Ixgw/LAlA==

"@types/whatwg-url@^11.0.2":
version "11.0.5"
resolved "https://registry.yarnpkg.com/@types/whatwg-url/-/whatwg-url-11.0.5.tgz#aaa2546e60f0c99209ca13360c32c78caf2c409f"
integrity sha512-coYR071JRaHa+xoEvvYqvnIHaVqaYrLPbsufM9BF63HkwI5Lgmy2QR8Q5K/lYDYo5AK82wOvSOS0UsLTpTG7uQ==
dependencies:
"@types/webidl-conversions" "*"

"@types/wrap-ansi@^3.0.0":
version "3.0.0"
resolved "https://registry.npmjs.org/@types/wrap-ansi/-/wrap-ansi-3.0.0.tgz"
Expand Down Expand Up @@ -6833,6 +6852,11 @@ [email protected]:
dependencies:
node-int64 "^0.4.0"

bson@^6.7.0:
version "6.8.0"
resolved "https://registry.yarnpkg.com/bson/-/bson-6.8.0.tgz#5063c41ba2437c2b8ff851b50d9e36cb7aaa7525"
integrity sha512-iOJg8pr7wq2tg/zSlCCHMi3hMm5JTOxLTagf3zxhcenHsFp+c6uOs6K7W5UE7A4QIJGtqh/ZovFNMP4mOPJynQ==

buffer-alloc-unsafe@^1.1.0:
version "1.1.0"
resolved "https://registry.npmjs.org/buffer-alloc-unsafe/-/buffer-alloc-unsafe-1.1.0.tgz"
Expand Down Expand Up @@ -12263,6 +12287,11 @@ memoize-one@^6.0.0:
resolved "https://registry.npmjs.org/memoize-one/-/memoize-one-6.0.0.tgz"
integrity sha512-rkpe71W0N0c0Xz6QD0eJETuWAJGnJ9afsl1srmwPrI+yBCkge5EycXXbYRyvL29zZVUWQCY7InPRCv3GDXuZNw==

memory-pager@^1.0.2:
version "1.5.0"
resolved "https://registry.yarnpkg.com/memory-pager/-/memory-pager-1.5.0.tgz#d8751655d22d384682741c972f2c3d6dfa3e66b5"
integrity sha512-ZS4Bp4r/Zoeq6+NLJpP+0Zzm0pR8whtGPf1XExKLJBAczGMnSi3It14OiNCStjQjM6NU1okjQGSxgEZN8eBYKg==

memorystream@^0.3.1:
version "0.3.1"
resolved "https://registry.npmjs.org/memorystream/-/memorystream-0.3.1.tgz"
Expand Down Expand Up @@ -12545,6 +12574,23 @@ module-deps@^6.2.3:
through2 "^2.0.0"
xtend "^4.0.0"

mongodb-connection-string-url@^3.0.0:
version "3.0.1"
resolved "https://registry.yarnpkg.com/mongodb-connection-string-url/-/mongodb-connection-string-url-3.0.1.tgz#c13e6ac284ae401752ebafdb8cd7f16c6723b141"
integrity sha512-XqMGwRX0Lgn05TDB4PyG2h2kKO/FfWJyCzYQbIhXUxz7ETt0I/FqHjUeqj37irJ+Dl1ZtU82uYyj14u2XsZKfg==
dependencies:
"@types/whatwg-url" "^11.0.2"
whatwg-url "^13.0.0"

[email protected]:
version "6.9.0"
resolved "https://registry.yarnpkg.com/mongodb/-/mongodb-6.9.0.tgz#743ebfff6b3c14b04ac6e00a55e30d4127d3016d"
integrity sha512-UMopBVx1LmEUbW/QE0Hw18u583PEDVQmUmVzzBRH0o/xtE9DBRA5ZYLOjpLIa03i8FXjzvQECJcqoMvCXftTUA==
dependencies:
"@mongodb-js/saslprep" "^1.1.5"
bson "^6.7.0"
mongodb-connection-string-url "^3.0.0"

moo-color@^1.0.2:
version "1.0.3"
resolved "https://registry.npmjs.org/moo-color/-/moo-color-1.0.3.tgz"
Expand Down Expand Up @@ -13713,7 +13759,7 @@ punycode@^1.3.2, punycode@^1.4.1:
resolved "https://registry.npmjs.org/punycode/-/punycode-1.4.1.tgz"
integrity sha512-jmYNElW7yvO7TV33CjSmvSiE2yco3bV2czu/OzDKdMNVZQWfxCblURLhf+47syQRBntjfLdd/H0egrzIG+oaFQ==

punycode@^2.1.0, punycode@^2.1.1:
punycode@^2.1.0, punycode@^2.1.1, punycode@^2.3.0:
version "2.3.1"
resolved "https://registry.npmjs.org/punycode/-/punycode-2.3.1.tgz"
integrity sha512-vYt7UD1U9Wg6138shLtLOvdAu+8DsC/ilFtEVHcH+wydcSpNE20AfSOduf6MkRFahL5FY7X1oU7nKVZFtfq8Fg==
Expand Down Expand Up @@ -15129,6 +15175,13 @@ source-map@^0.7.4:
resolved "https://registry.npmjs.org/source-map/-/source-map-0.7.4.tgz"
integrity sha512-l3BikUxvPOcn5E74dZiq5BGsTb5yEwhaTSzccU6t4sDOH8NWJCstKO5QT2CvtFoK6F0saL7p9xHAqHOlCPJygA==

sparse-bitfield@^3.0.3:
version "3.0.3"
resolved "https://registry.yarnpkg.com/sparse-bitfield/-/sparse-bitfield-3.0.3.tgz#ff4ae6e68656056ba4b3e792ab3334d38273ca11"
integrity sha512-kvzhi7vqKTfkh0PZU+2D2PIllw2ymqJKujUcyPMd9Y75Nv4nPbGJZXNhxsgdQab2BmlDct1YnfQCguEvHr7VsQ==
dependencies:
memory-pager "^1.0.2"

spdx-correct@^3.0.0:
version "3.2.0"
resolved "https://registry.npmjs.org/spdx-correct/-/spdx-correct-3.2.0.tgz"
Expand Down Expand Up @@ -15881,6 +15934,13 @@ tr46@^3.0.0:
dependencies:
punycode "^2.1.1"

tr46@^4.1.1:
version "4.1.1"
resolved "https://registry.yarnpkg.com/tr46/-/tr46-4.1.1.tgz#281a758dcc82aeb4fe38c7dfe4d11a395aac8469"
integrity sha512-2lv/66T7e5yNyhAAC4NaKe5nVavzuGJQVVtRYLyQ2OI8tsJ61PMLlelehb0wi2Hx6+hT/OJUWZcw8MjlSRnxvw==
dependencies:
punycode "^2.3.0"

tr46@~0.0.3:
version "0.0.3"
resolved "https://registry.npmjs.org/tr46/-/tr46-0.0.3.tgz"
Expand Down Expand Up @@ -17094,6 +17154,14 @@ whatwg-url@^11.0.0:
tr46 "^3.0.0"
webidl-conversions "^7.0.0"

whatwg-url@^13.0.0:
version "13.0.0"
resolved "https://registry.yarnpkg.com/whatwg-url/-/whatwg-url-13.0.0.tgz#b7b536aca48306394a34e44bda8e99f332410f8f"
integrity sha512-9WWbymnqj57+XEuqADHrCJ2eSXzn8WXIW/YSGaZtb2WKAInQ6CHfaUUcTyyver0p8BDg5StLQq8h1vtZuwmOig==
dependencies:
tr46 "^4.1.1"
webidl-conversions "^7.0.0"

whatwg-url@^5.0.0:
version "5.0.0"
resolved "https://registry.npmjs.org/whatwg-url/-/whatwg-url-5.0.0.tgz"
Expand Down

0 comments on commit 4417654

Please sign in to comment.