From d0dbc92d14b453c6a61ceecb67fffb8c4abac4f7 Mon Sep 17 00:00:00 2001 From: Victor Zheng Date: Fri, 12 Apr 2024 23:56:26 -0700 Subject: [PATCH] fix seed data --- README.md | 51 +++++----- backend/auth/middleware.js | 10 +- backend/aws/s3.js | 6 -- backend/package-lock.json | 186 +++++++++++++++++++++++-------------- backend/package.json | 3 +- backend/server.js | 1 - backend/utils/seedData.js | 70 ++++++++++++-- 7 files changed, 212 insertions(+), 115 deletions(-) diff --git a/README.md b/README.md index 82c350c..03502c3 100644 --- a/README.md +++ b/README.md @@ -19,29 +19,20 @@ create a `.env` file in the "backend" folder Please contact Victor or Anson if you do not know how to set it up ``` -ATLAS_URI= -MAILJET_API_KEY= -MAILJET_SECRET_KEY= -FINANCE_EMAIL= -EMAIL_RECIPIENTS= -CLIENT_URL=http://localhost:3000 -``` - -In order to run the cron that updates the db from the WATO members spreadsheet, please contact Victor or Anson for the following variables you will add in your `.env` - -``` -GOOGLE_SHEET_ID= -SHEET_TAB_ID= -SERVICE_ACCOUNT_EMAIL= -SERVICE_ACCOUNT_PRIVATE_KEY= +WATO_FINANCE_ATLAS_URI= +WATO_FINANCE_MAILJET_API_KEY= +WATO_FINANCE_MAILJET_SECRET_KEY= +WATO_FINANCE_FINANCE_EMAIL= +WATO_FINANCE_EMAIL_RECIPIENTS= +WATO_FINANCE_CLIENT_URL=http://localhost:3000 ``` In order to upload attachments, AWS credentials are needed. To access a role that is already set up with policies on WATonomous' AWS account, please contact Victor. ``` -AWS_SECRET_ACCESS_KEY= -AWS_ACCESS_KEY_ID= -AWS_FINANCE_BUCKET_NAME= +WATO_FINANCE_AWS_SECRET_ACCESS_KEY= +WATO_FINANCE_AWS_ACCESS_KEY_ID= +WATO_FINANCE_AWS_FINANCE_BUCKET_NAME= ``` To start the server, run @@ -71,13 +62,17 @@ In the production environment this file is mounted automatically based on https: ### Generating seeding data -To generate seed data, in the terminal in the backend directory, run: +To generate seed data, in the terminal in the backend directory, you must first add an additional environment variable to `backend/.env`. Note that this is the same firebase api key as the web app, instructions can be found later in this README. + +`WATO_FINANCE_FIREBASE_API_KEY=` + +run: ``` -npm run seeddata +npm run utils/seedData.js [--prod] ``` -Note that this step is not optional due to the fact we need to inject wato cash's id as it is a special case for a funding item. +Note that this step is not optional due to the fact we need to inject wato cash's id as it is a special case for a funding item. If you pass in --prod, it will only create the wato cash fund, and no dummy data will be created. --- @@ -99,13 +94,13 @@ Note that this step is not optional due to the fact we need to inject wato cash' 3. Look for the `firebaseConfig` constant and fill in the following info in the `.env` file you create in the "frontend" folder ``` -REACT_APP_API_KEY= -REACT_APP_AUTH_DOMAIN= -REACT_APP_PROJECT_ID= -REACT_APP_STORAGE_BUCKET= -REACT_APP_MESSAGING_SENDER_ID= -REACT_APP_APP_ID= -REACT_APP_BACKEND_URL=http://localhost:5000 +REACT_APP_WATO_FINANCE_API_KEY= +REACT_APP_WATO_FINANCE_AUTH_DOMAIN= +REACT_APP_WATO_FINANCE_PROJECT_ID= +REACT_APP_WATO_FINANCE_STORAGE_BUCKET= +REACT_APP_WATO_FINANCE_MESSAGING_SENDER_ID= +REACT_APP_WATO_FINANCE_APP_ID= +REACT_APP_WATO_FINANCE_BACKEND_URL=http://localhost:5000 ``` ### Connect Backend to Project diff --git a/backend/auth/middleware.js b/backend/auth/middleware.js index 8bb9655..0f6d02c 100644 --- a/backend/auth/middleware.js +++ b/backend/auth/middleware.js @@ -11,7 +11,8 @@ const { authAdmin } = require('./auth') const authenticateUser = async (req, res, next) => { const bearerToken = req.headers.authorization?.split(' ') if (!bearerToken || bearerToken.length !== 2) { - return res.status(401).json({ error: `No user token provided` }) + res.status(401).send({ error: `No user token provided` }) + return } const token = bearerToken[1] try { @@ -44,6 +45,11 @@ const authenticateUser = async (req, res, next) => { } } + // special case for seed token, we don't have a user id attached to it + if (decodedToken.uid === 'seed-token') { + next() + return + } const { isDirector, isAdmin, isTeamCaptain, isReporter } = await getAuthRoles( decodedToken.uid, @@ -56,7 +62,7 @@ const authenticateUser = async (req, res, next) => { req.user.isReporter = isReporter next() } catch (err) { - res.status(401).json({ error: `Invalid user token: ${err}` }) + res.status(401).send({ error: `Invalid user token: ${err}` }) return } } diff --git a/backend/aws/s3.js b/backend/aws/s3.js index 06b1384..919ee2c 100644 --- a/backend/aws/s3.js +++ b/backend/aws/s3.js @@ -5,12 +5,6 @@ const { } = require('@aws-sdk/client-s3') const { getSignedUrl } = require('@aws-sdk/s3-request-presigner') -const { HttpRequest } = require('@aws-sdk/protocol-http') -const { formatUrl } = require('@aws-sdk/util-format-url') - -const { S3RequestPresigner } = require('@aws-sdk/s3-request-presigner') -const { Hash } = require('@aws-sdk/hash-node') - const s3Client = new S3Client({ region: 'us-east-2', }) diff --git a/backend/package-lock.json b/backend/package-lock.json index 8640efa..2c0d846 100644 --- a/backend/package-lock.json +++ b/backend/package-lock.json @@ -29,7 +29,8 @@ "node-cron": "^3.0.2", "node-fetch": "^2.6.7", "node-mailjet": "^5.1.1", - "unique-names-generator": "^4.7.1" + "unique-names-generator": "^4.7.1", + "yargs": "^17.7.2" } }, "node_modules/@aws-crypto/crc32": { @@ -2434,6 +2435,44 @@ "node": ">=6" } }, + "node_modules/@grpc/proto-loader/node_modules/cliui": { + "version": "7.0.4", + "resolved": "https://registry.npmjs.org/cliui/-/cliui-7.0.4.tgz", + "integrity": "sha512-OcRE68cOsVMXp1Yvonl/fzkQOyjLSu/8bhPDfQt0e0/Eb283TKP20Fs2MqoPsr9SwA595rRCA+QMzYc9nBP+JQ==", + "optional": true, + "dependencies": { + "string-width": "^4.2.0", + "strip-ansi": "^6.0.0", + "wrap-ansi": "^7.0.0" + } + }, + "node_modules/@grpc/proto-loader/node_modules/yargs": { + "version": "16.2.0", + "resolved": "https://registry.npmjs.org/yargs/-/yargs-16.2.0.tgz", + "integrity": "sha512-D1mvvtDG0L5ft/jGWkLpG1+m0eQxOfaBvTNELraWj22wSVUMWxZUvYgJYcKh6jGGIkJFhH4IZPQhR4TKpc8mBw==", + "optional": true, + "dependencies": { + "cliui": "^7.0.2", + "escalade": "^3.1.1", + "get-caller-file": "^2.0.5", + "require-directory": "^2.1.1", + "string-width": "^4.2.0", + "y18n": "^5.0.5", + "yargs-parser": "^20.2.2" + }, + "engines": { + "node": ">=10" + } + }, + "node_modules/@grpc/proto-loader/node_modules/yargs-parser": { + "version": "20.2.9", + "resolved": "https://registry.npmjs.org/yargs-parser/-/yargs-parser-20.2.9.tgz", + "integrity": "sha512-y11nGElTIV+CT3Zv9t7VKl+Q3hTQoT9a1Qzezhhl6Rp21gJ/IVTW7Z3y9EWXhuUBC2Shnf+DX0antecpAwSP8w==", + "optional": true, + "engines": { + "node": ">=10" + } + }, "node_modules/@panva/asn1.js": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/@panva/asn1.js/-/asn1.js-1.0.0.tgz", @@ -3260,7 +3299,6 @@ "version": "5.0.1", "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-5.0.1.tgz", "integrity": "sha512-quJQXlTSUGL2LH9SUXo8VwsY4soanhgo6LNSm84E1LBcE8s3O0wpdiRzyR9z/ZZJMlMWv37qOOb9pdJlMUEKFQ==", - "optional": true, "engines": { "node": ">=8" } @@ -3269,7 +3307,6 @@ "version": "4.3.0", "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-4.3.0.tgz", "integrity": "sha512-zbB9rCJAT1rbjiVDb2hqKFHNYLxgtk8NURxZ3IZwD3F6NtxbXZQCnnSi1Lkx+IDohdPlFp222wVALIheZJQSEg==", - "optional": true, "dependencies": { "color-convert": "^2.0.1" }, @@ -3569,21 +3606,22 @@ } }, "node_modules/cliui": { - "version": "7.0.4", - "resolved": "https://registry.npmjs.org/cliui/-/cliui-7.0.4.tgz", - "integrity": "sha512-OcRE68cOsVMXp1Yvonl/fzkQOyjLSu/8bhPDfQt0e0/Eb283TKP20Fs2MqoPsr9SwA595rRCA+QMzYc9nBP+JQ==", - "optional": true, + "version": "8.0.1", + "resolved": "https://registry.npmjs.org/cliui/-/cliui-8.0.1.tgz", + "integrity": "sha512-BSeNnyus75C4//NQ9gQt1/csTXyo/8Sb+afLAkzAptFuMsod9HFokGNudZpi/oQV73hnVK+sR+5PVRMd+Dr7YQ==", "dependencies": { "string-width": "^4.2.0", - "strip-ansi": "^6.0.0", + "strip-ansi": "^6.0.1", "wrap-ansi": "^7.0.0" + }, + "engines": { + "node": ">=12" } }, "node_modules/color-convert": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-2.0.1.tgz", "integrity": "sha512-RRECPsj7iu/xb5oKYcsFHSppFNnsj/52OVTRKb4zP5onXwVF3zVmmToNcOfGC+CRDpfK/U584fMg38ZHCaElKQ==", - "optional": true, "dependencies": { "color-name": "~1.1.4" }, @@ -3594,8 +3632,7 @@ "node_modules/color-name": { "version": "1.1.4", "resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.4.tgz", - "integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==", - "optional": true + "integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==" }, "node_modules/combined-stream": { "version": "1.0.8", @@ -3799,8 +3836,7 @@ "node_modules/emoji-regex": { "version": "8.0.0", "resolved": "https://registry.npmjs.org/emoji-regex/-/emoji-regex-8.0.0.tgz", - "integrity": "sha512-MSjYzcWNOA0ewAHpz0MxpYFvwg6yjy1NG3xteoqz644VCo/RPgnr1/GGt+ic3iJTzQ8Eu3TdM14SawnVUmGE6A==", - "optional": true + "integrity": "sha512-MSjYzcWNOA0ewAHpz0MxpYFvwg6yjy1NG3xteoqz644VCo/RPgnr1/GGt+ic3iJTzQ8Eu3TdM14SawnVUmGE6A==" }, "node_modules/encodeurl": { "version": "1.0.2", @@ -3838,7 +3874,6 @@ "version": "3.1.1", "resolved": "https://registry.npmjs.org/escalade/-/escalade-3.1.1.tgz", "integrity": "sha512-k0er2gUkLf8O0zKJiAhmkTnJlTvINGv7ygDNPbeIsX/TJjGJZHuh9B2UxbsaEkmlEo9MfhrSzmhIlhRlI2GXnw==", - "optional": true, "engines": { "node": ">=6" } @@ -4250,7 +4285,6 @@ "version": "2.0.5", "resolved": "https://registry.npmjs.org/get-caller-file/-/get-caller-file-2.0.5.tgz", "integrity": "sha512-DyFP3BM/3YHTQOCUL/w0OZHR0lpKeGrxotcHWcqNEdnltqFwXVfhEBQ94eIo34AfQpo0rGki4cyIiftY06h2Fg==", - "optional": true, "engines": { "node": "6.* || 8.* || >= 10.*" } @@ -4704,7 +4738,6 @@ "version": "3.0.0", "resolved": "https://registry.npmjs.org/is-fullwidth-code-point/-/is-fullwidth-code-point-3.0.0.tgz", "integrity": "sha512-zymm5+u+sCsSWyD9qNaejV3DFvhCKclKdizYaJUuHA83RLjb7nSuGnddCHGv0hk+KY7BMAlsWeK4Ueg6EV6XQg==", - "optional": true, "engines": { "node": ">=8" } @@ -5721,7 +5754,6 @@ "version": "2.1.1", "resolved": "https://registry.npmjs.org/require-directory/-/require-directory-2.1.1.tgz", "integrity": "sha512-fGxEI7+wsG9xrvdjsrlmL22OMTTiHRwAMroiEeMgq8gzoLC/PQr7RsRDSTLUg/bZAZtF+TVIkHc6/4RIKrui+Q==", - "optional": true, "engines": { "node": ">=0.10.0" } @@ -6071,7 +6103,6 @@ "version": "4.2.3", "resolved": "https://registry.npmjs.org/string-width/-/string-width-4.2.3.tgz", "integrity": "sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==", - "optional": true, "dependencies": { "emoji-regex": "^8.0.0", "is-fullwidth-code-point": "^3.0.0", @@ -6085,7 +6116,6 @@ "version": "6.0.1", "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-6.0.1.tgz", "integrity": "sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A==", - "optional": true, "dependencies": { "ansi-regex": "^5.0.1" }, @@ -6401,7 +6431,6 @@ "version": "7.0.0", "resolved": "https://registry.npmjs.org/wrap-ansi/-/wrap-ansi-7.0.0.tgz", "integrity": "sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q==", - "optional": true, "dependencies": { "ansi-styles": "^4.0.0", "string-width": "^4.1.0", @@ -6458,7 +6487,6 @@ "version": "5.0.8", "resolved": "https://registry.npmjs.org/y18n/-/y18n-5.0.8.tgz", "integrity": "sha512-0pfFzegeDWJHJIAmTLRP2DwHjdF5s7jo9tuztdQxAhINCdvS+3nGINqPd00AphqJR/0LhANUS6/+7SCb98YOfA==", - "optional": true, "engines": { "node": ">=10" } @@ -6469,30 +6497,28 @@ "integrity": "sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A==" }, "node_modules/yargs": { - "version": "16.2.0", - "resolved": "https://registry.npmjs.org/yargs/-/yargs-16.2.0.tgz", - "integrity": "sha512-D1mvvtDG0L5ft/jGWkLpG1+m0eQxOfaBvTNELraWj22wSVUMWxZUvYgJYcKh6jGGIkJFhH4IZPQhR4TKpc8mBw==", - "optional": true, + "version": "17.7.2", + "resolved": "https://registry.npmjs.org/yargs/-/yargs-17.7.2.tgz", + "integrity": "sha512-7dSzzRQ++CKnNI/krKnYRV7JKKPUXMEh61soaHKg9mrWEhzFWhFnxPxGl+69cD1Ou63C13NUPCnmIcrvqCuM6w==", "dependencies": { - "cliui": "^7.0.2", + "cliui": "^8.0.1", "escalade": "^3.1.1", "get-caller-file": "^2.0.5", "require-directory": "^2.1.1", - "string-width": "^4.2.0", + "string-width": "^4.2.3", "y18n": "^5.0.5", - "yargs-parser": "^20.2.2" + "yargs-parser": "^21.1.1" }, "engines": { - "node": ">=10" + "node": ">=12" } }, "node_modules/yargs-parser": { - "version": "20.2.9", - "resolved": "https://registry.npmjs.org/yargs-parser/-/yargs-parser-20.2.9.tgz", - "integrity": "sha512-y11nGElTIV+CT3Zv9t7VKl+Q3hTQoT9a1Qzezhhl6Rp21gJ/IVTW7Z3y9EWXhuUBC2Shnf+DX0antecpAwSP8w==", - "optional": true, + "version": "21.1.1", + "resolved": "https://registry.npmjs.org/yargs-parser/-/yargs-parser-21.1.1.tgz", + "integrity": "sha512-tVpsJW7DdjecAiFpbIB1e3qxIQsE6NoPc5/eTdrbbIC4h0LVsWhnoa3g+m2HclBIujHzsxZ4VJVA+GUuc2/LBw==", "engines": { - "node": ">=10" + "node": ">=12" } }, "node_modules/yocto-queue": { @@ -8571,6 +8597,40 @@ "long": "^4.0.0", "protobufjs": "^7.0.0", "yargs": "^16.2.0" + }, + "dependencies": { + "cliui": { + "version": "7.0.4", + "resolved": "https://registry.npmjs.org/cliui/-/cliui-7.0.4.tgz", + "integrity": "sha512-OcRE68cOsVMXp1Yvonl/fzkQOyjLSu/8bhPDfQt0e0/Eb283TKP20Fs2MqoPsr9SwA595rRCA+QMzYc9nBP+JQ==", + "optional": true, + "requires": { + "string-width": "^4.2.0", + "strip-ansi": "^6.0.0", + "wrap-ansi": "^7.0.0" + } + }, + "yargs": { + "version": "16.2.0", + "resolved": "https://registry.npmjs.org/yargs/-/yargs-16.2.0.tgz", + "integrity": "sha512-D1mvvtDG0L5ft/jGWkLpG1+m0eQxOfaBvTNELraWj22wSVUMWxZUvYgJYcKh6jGGIkJFhH4IZPQhR4TKpc8mBw==", + "optional": true, + "requires": { + "cliui": "^7.0.2", + "escalade": "^3.1.1", + "get-caller-file": "^2.0.5", + "require-directory": "^2.1.1", + "string-width": "^4.2.0", + "y18n": "^5.0.5", + "yargs-parser": "^20.2.2" + } + }, + "yargs-parser": { + "version": "20.2.9", + "resolved": "https://registry.npmjs.org/yargs-parser/-/yargs-parser-20.2.9.tgz", + "integrity": "sha512-y11nGElTIV+CT3Zv9t7VKl+Q3hTQoT9a1Qzezhhl6Rp21gJ/IVTW7Z3y9EWXhuUBC2Shnf+DX0antecpAwSP8w==", + "optional": true + } } }, "@panva/asn1.js": { @@ -9255,14 +9315,12 @@ "ansi-regex": { "version": "5.0.1", "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-5.0.1.tgz", - "integrity": "sha512-quJQXlTSUGL2LH9SUXo8VwsY4soanhgo6LNSm84E1LBcE8s3O0wpdiRzyR9z/ZZJMlMWv37qOOb9pdJlMUEKFQ==", - "optional": true + "integrity": "sha512-quJQXlTSUGL2LH9SUXo8VwsY4soanhgo6LNSm84E1LBcE8s3O0wpdiRzyR9z/ZZJMlMWv37qOOb9pdJlMUEKFQ==" }, "ansi-styles": { "version": "4.3.0", "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-4.3.0.tgz", "integrity": "sha512-zbB9rCJAT1rbjiVDb2hqKFHNYLxgtk8NURxZ3IZwD3F6NtxbXZQCnnSi1Lkx+IDohdPlFp222wVALIheZJQSEg==", - "optional": true, "requires": { "color-convert": "^2.0.1" } @@ -9487,13 +9545,12 @@ } }, "cliui": { - "version": "7.0.4", - "resolved": "https://registry.npmjs.org/cliui/-/cliui-7.0.4.tgz", - "integrity": "sha512-OcRE68cOsVMXp1Yvonl/fzkQOyjLSu/8bhPDfQt0e0/Eb283TKP20Fs2MqoPsr9SwA595rRCA+QMzYc9nBP+JQ==", - "optional": true, + "version": "8.0.1", + "resolved": "https://registry.npmjs.org/cliui/-/cliui-8.0.1.tgz", + "integrity": "sha512-BSeNnyus75C4//NQ9gQt1/csTXyo/8Sb+afLAkzAptFuMsod9HFokGNudZpi/oQV73hnVK+sR+5PVRMd+Dr7YQ==", "requires": { "string-width": "^4.2.0", - "strip-ansi": "^6.0.0", + "strip-ansi": "^6.0.1", "wrap-ansi": "^7.0.0" } }, @@ -9501,7 +9558,6 @@ "version": "2.0.1", "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-2.0.1.tgz", "integrity": "sha512-RRECPsj7iu/xb5oKYcsFHSppFNnsj/52OVTRKb4zP5onXwVF3zVmmToNcOfGC+CRDpfK/U584fMg38ZHCaElKQ==", - "optional": true, "requires": { "color-name": "~1.1.4" } @@ -9509,8 +9565,7 @@ "color-name": { "version": "1.1.4", "resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.4.tgz", - "integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==", - "optional": true + "integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==" }, "combined-stream": { "version": "1.0.8", @@ -9679,8 +9734,7 @@ "emoji-regex": { "version": "8.0.0", "resolved": "https://registry.npmjs.org/emoji-regex/-/emoji-regex-8.0.0.tgz", - "integrity": "sha512-MSjYzcWNOA0ewAHpz0MxpYFvwg6yjy1NG3xteoqz644VCo/RPgnr1/GGt+ic3iJTzQ8Eu3TdM14SawnVUmGE6A==", - "optional": true + "integrity": "sha512-MSjYzcWNOA0ewAHpz0MxpYFvwg6yjy1NG3xteoqz644VCo/RPgnr1/GGt+ic3iJTzQ8Eu3TdM14SawnVUmGE6A==" }, "encodeurl": { "version": "1.0.2", @@ -9711,8 +9765,7 @@ "escalade": { "version": "3.1.1", "resolved": "https://registry.npmjs.org/escalade/-/escalade-3.1.1.tgz", - "integrity": "sha512-k0er2gUkLf8O0zKJiAhmkTnJlTvINGv7ygDNPbeIsX/TJjGJZHuh9B2UxbsaEkmlEo9MfhrSzmhIlhRlI2GXnw==", - "optional": true + "integrity": "sha512-k0er2gUkLf8O0zKJiAhmkTnJlTvINGv7ygDNPbeIsX/TJjGJZHuh9B2UxbsaEkmlEo9MfhrSzmhIlhRlI2GXnw==" }, "escape-html": { "version": "1.0.3", @@ -10015,8 +10068,7 @@ "get-caller-file": { "version": "2.0.5", "resolved": "https://registry.npmjs.org/get-caller-file/-/get-caller-file-2.0.5.tgz", - "integrity": "sha512-DyFP3BM/3YHTQOCUL/w0OZHR0lpKeGrxotcHWcqNEdnltqFwXVfhEBQ94eIo34AfQpo0rGki4cyIiftY06h2Fg==", - "optional": true + "integrity": "sha512-DyFP3BM/3YHTQOCUL/w0OZHR0lpKeGrxotcHWcqNEdnltqFwXVfhEBQ94eIo34AfQpo0rGki4cyIiftY06h2Fg==" }, "get-intrinsic": { "version": "1.1.3", @@ -10345,8 +10397,7 @@ "is-fullwidth-code-point": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/is-fullwidth-code-point/-/is-fullwidth-code-point-3.0.0.tgz", - "integrity": "sha512-zymm5+u+sCsSWyD9qNaejV3DFvhCKclKdizYaJUuHA83RLjb7nSuGnddCHGv0hk+KY7BMAlsWeK4Ueg6EV6XQg==", - "optional": true + "integrity": "sha512-zymm5+u+sCsSWyD9qNaejV3DFvhCKclKdizYaJUuHA83RLjb7nSuGnddCHGv0hk+KY7BMAlsWeK4Ueg6EV6XQg==" }, "is-generator-function": { "version": "1.0.10", @@ -11117,8 +11168,7 @@ "require-directory": { "version": "2.1.1", "resolved": "https://registry.npmjs.org/require-directory/-/require-directory-2.1.1.tgz", - "integrity": "sha512-fGxEI7+wsG9xrvdjsrlmL22OMTTiHRwAMroiEeMgq8gzoLC/PQr7RsRDSTLUg/bZAZtF+TVIkHc6/4RIKrui+Q==", - "optional": true + "integrity": "sha512-fGxEI7+wsG9xrvdjsrlmL22OMTTiHRwAMroiEeMgq8gzoLC/PQr7RsRDSTLUg/bZAZtF+TVIkHc6/4RIKrui+Q==" }, "requizzle": { "version": "0.2.4", @@ -11379,7 +11429,6 @@ "version": "4.2.3", "resolved": "https://registry.npmjs.org/string-width/-/string-width-4.2.3.tgz", "integrity": "sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==", - "optional": true, "requires": { "emoji-regex": "^8.0.0", "is-fullwidth-code-point": "^3.0.0", @@ -11390,7 +11439,6 @@ "version": "6.0.1", "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-6.0.1.tgz", "integrity": "sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A==", - "optional": true, "requires": { "ansi-regex": "^5.0.1" } @@ -11635,7 +11683,6 @@ "version": "7.0.0", "resolved": "https://registry.npmjs.org/wrap-ansi/-/wrap-ansi-7.0.0.tgz", "integrity": "sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q==", - "optional": true, "requires": { "ansi-styles": "^4.0.0", "string-width": "^4.1.0", @@ -11676,8 +11723,7 @@ "y18n": { "version": "5.0.8", "resolved": "https://registry.npmjs.org/y18n/-/y18n-5.0.8.tgz", - "integrity": "sha512-0pfFzegeDWJHJIAmTLRP2DwHjdF5s7jo9tuztdQxAhINCdvS+3nGINqPd00AphqJR/0LhANUS6/+7SCb98YOfA==", - "optional": true + "integrity": "sha512-0pfFzegeDWJHJIAmTLRP2DwHjdF5s7jo9tuztdQxAhINCdvS+3nGINqPd00AphqJR/0LhANUS6/+7SCb98YOfA==" }, "yallist": { "version": "4.0.0", @@ -11685,25 +11731,23 @@ "integrity": "sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A==" }, "yargs": { - "version": "16.2.0", - "resolved": "https://registry.npmjs.org/yargs/-/yargs-16.2.0.tgz", - "integrity": "sha512-D1mvvtDG0L5ft/jGWkLpG1+m0eQxOfaBvTNELraWj22wSVUMWxZUvYgJYcKh6jGGIkJFhH4IZPQhR4TKpc8mBw==", - "optional": true, + "version": "17.7.2", + "resolved": "https://registry.npmjs.org/yargs/-/yargs-17.7.2.tgz", + "integrity": "sha512-7dSzzRQ++CKnNI/krKnYRV7JKKPUXMEh61soaHKg9mrWEhzFWhFnxPxGl+69cD1Ou63C13NUPCnmIcrvqCuM6w==", "requires": { - "cliui": "^7.0.2", + "cliui": "^8.0.1", "escalade": "^3.1.1", "get-caller-file": "^2.0.5", "require-directory": "^2.1.1", - "string-width": "^4.2.0", + "string-width": "^4.2.3", "y18n": "^5.0.5", - "yargs-parser": "^20.2.2" + "yargs-parser": "^21.1.1" } }, "yargs-parser": { - "version": "20.2.9", - "resolved": "https://registry.npmjs.org/yargs-parser/-/yargs-parser-20.2.9.tgz", - "integrity": "sha512-y11nGElTIV+CT3Zv9t7VKl+Q3hTQoT9a1Qzezhhl6Rp21gJ/IVTW7Z3y9EWXhuUBC2Shnf+DX0antecpAwSP8w==", - "optional": true + "version": "21.1.1", + "resolved": "https://registry.npmjs.org/yargs-parser/-/yargs-parser-21.1.1.tgz", + "integrity": "sha512-tVpsJW7DdjecAiFpbIB1e3qxIQsE6NoPc5/eTdrbbIC4h0LVsWhnoa3g+m2HclBIujHzsxZ4VJVA+GUuc2/LBw==" }, "yocto-queue": { "version": "0.1.0", diff --git a/backend/package.json b/backend/package.json index ce33fe7..b16f6f9 100644 --- a/backend/package.json +++ b/backend/package.json @@ -30,6 +30,7 @@ "node-cron": "^3.0.2", "node-fetch": "^2.6.7", "node-mailjet": "^5.1.1", - "unique-names-generator": "^4.7.1" + "unique-names-generator": "^4.7.1", + "yargs": "^17.7.2" } } diff --git a/backend/server.js b/backend/server.js index d033482..824511d 100644 --- a/backend/server.js +++ b/backend/server.js @@ -33,7 +33,6 @@ const groupRouter = require('./routes/googlegroup.routes') const filesRouter = require('./routes/files.routes') const commentRouter = require('./routes/comments.routes') -app.use(express.json()) app.use('/fundingitems', fundingItemsRouter) app.use('/personalpurchases', personalPurchaseRouter) app.use('/sponsorshipfunds', sponsorshipFundsRouter) diff --git a/backend/utils/seedData.js b/backend/utils/seedData.js index 7de2966..751c879 100644 --- a/backend/utils/seedData.js +++ b/backend/utils/seedData.js @@ -6,6 +6,33 @@ const { animals, } = require('unique-names-generator') const fetch = require('node-fetch') +const admin = require('firebase-admin') +const fs = require('fs') +// ------------------- Seed Data ------------------- +const yargs = require('yargs') +const argv = yargs.option('prod', { + alias: 'p', + describe: 'Set to true to only create necessary data for production', + type: 'boolean', +}).argv +// ------------------- Seed Data ------------------- + +// ------------------- Auth Setup ------------------- +if (!fs.existsSync('./serviceAccountKey.json')) { + console.log('❌ No service account key provided. Exiting...') + process.exit(1) +} +const serviceAccount = require('../serviceAccountKey.json') +require('dotenv').config() +if (!process.env.WATO_FINANCE_FIREBASE_API_KEY) { + console.log('❌ No firebase api key provided. Exiting...') + process.exit(1) +} +admin.initializeApp({ + credential: admin.credential.cert(serviceAccount), +}) +let headers +// ------------------- Auth Setup ------------------- const numSponsorhipFundsToCreate = 2 const minFundingAllocationForSponsorshipFunds = 1000 @@ -41,6 +68,11 @@ const generateDummyData = async () => { // as the cash fund even though it takes the shape of a funding item await createCashFund() console.log('✅ cash fund created') + if (argv.prod) { + // don't create dummy data for production + console.log('Exiting early due to prod flag.') + return + } const sponsorshipFundIds = await createSponsorshipFunds() console.log('✅ sponsorship funds created') const fundingItemIds = await createFundingItems(sponsorshipFundIds) @@ -66,8 +98,11 @@ const createCashFund = async () => { } await fetch(`${backend_url}/fundingitems`, { method: 'POST', - headers: { 'Content-Type': 'application/json' }, + headers: headers, body: JSON.stringify(cashFund), + }).catch((err) => { + console.log('❌ cash fund failed to create: ') + console.log(err) }) } @@ -168,7 +203,7 @@ const createSponsorshipFunds = async () => { sponsorshipFundsData.map(async (sponsorshipFund) => { const res = await fetch(`${backend_url}/sponsorshipfunds`, { method: 'POST', - headers: { 'Content-Type': 'application/json' }, + headers: headers, body: JSON.stringify(sponsorshipFund), }) const data = await res.json() @@ -191,7 +226,7 @@ const createFundingItems = async (sf_ids) => { fundingItemsData.map(async (fundingItem) => { const res = await fetch(`${backend_url}/fundingitems`, { method: 'POST', - headers: { 'Content-Type': 'application/json' }, + headers: headers, body: JSON.stringify(fundingItem), }) const data = await res.json() @@ -214,7 +249,7 @@ const createPersonalPurchases = async (fi_ids) => { personalPurchases.map(async (personalPurchase) => { const res = await fetch(`${backend_url}/personalpurchases`, { method: 'POST', - headers: { 'Content-Type': 'application/json' }, + headers: headers, body: JSON.stringify(personalPurchase), }) const data = await res.json() @@ -237,7 +272,7 @@ const createUWFinancePurchases = async (fi_ids) => { uwFinancePurchases.map(async (uwFinancePurchase) => { const res = await fetch(`${backend_url}/uwfinancepurchases`, { method: 'POST', - headers: { 'Content-Type': 'application/json' }, + headers: headers, body: JSON.stringify(uwFinancePurchase), }) const data = await res.json() @@ -246,4 +281,27 @@ const createUWFinancePurchases = async (fi_ids) => { ) } -generateDummyData() +const getIdToken = async () => { + const token = await admin.auth().createCustomToken('seed-token') + // fetch firebase api key + const res = await fetch( + `https://identitytoolkit.googleapis.com/v1/accounts:signInWithCustomToken?key=${process.env.WATO_FINANCE_FIREBASE_API_KEY}`, + { + method: 'POST', + body: JSON.stringify({ + token: token, + returnSecureToken: true, + }), + } + ) + const data = await res.json() + return data.idToken +} + +getIdToken().then((token) => { + headers = { + 'Content-Type': 'application/json', + Authorization: 'Bearer ' + token, + } + generateDummyData() +})