diff --git a/.eslintrc.json b/.eslintrc.json index 995f32f..8ea0b80 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -3,14 +3,10 @@ "browser": true, "es2021": true }, - "extends": [ - "airbnb-base", - "plugin:prettier/recommended" - ], + "extends": ["airbnb-base", "plugin:prettier/recommended"], "parserOptions": { "ecmaVersion": 13, "sourceType": "module" }, - "rules": { - } + "rules": {} } diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml index 318a41d..796f4eb 100644 --- a/.github/workflows/CI.yml +++ b/.github/workflows/CI.yml @@ -1,14 +1,13 @@ name: CI -on: - [pull_request] - +on: [pull_request] + permissions: read-all jobs: - eslint: + eslint: runs-on: ubuntu-latest - steps: + steps: - uses: actions/checkout@v2 - uses: actions/setup-node@v2 with: @@ -21,16 +20,16 @@ jobs: reporter: github-pr-review fail_on_error: false eslint_flags: '**/*.{js,jsx}' - + detect-secrets: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 - - name: Run detect-secrets with reviewdog - uses: reviewdog/action-detect-secrets@master - with: - github_token: ${{ secrets.CTC_DEVOPS_ORG_PAT }} - reporter: github-pr-review + - uses: actions/checkout@v2 + - name: Run detect-secrets with reviewdog + uses: reviewdog/action-detect-secrets@master + with: + github_token: ${{ secrets.CTC_DEVOPS_ORG_PAT }} + reporter: github-pr-review - # Prevents false positives from failing job - fail_on_error: false + # Prevents false positives from failing job + fail_on_error: false diff --git a/.github/workflows/scripts/reviewReviewed.js b/.github/workflows/scripts/reviewReviewed.js index 9d83536..9dd2f44 100644 --- a/.github/workflows/scripts/reviewReviewed.js +++ b/.github/workflows/scripts/reviewReviewed.js @@ -40,11 +40,11 @@ const messageAssignee = async ({ context }) => { try { const UserModel = getUserModel(); const slackAssignees = await Promise.allSettled( - githubAssignees.map(assignee => UserModel.findOne({ github: assignee.login })), + githubAssignees.map((assignee) => UserModel.findOne({ github: assignee.login })), ); if (context.payload.review.state === 'approved') { await Promise.all( - slackAssignees.map(assignee => + slackAssignees.map((assignee) => Bot.client.chat.postMessage({ channel: assignee.value?.slackId, text: `One of your pull requests has been APPROVED by ${reviewer}! <${url}|View Review> :shrek::thumbsup:`, @@ -53,7 +53,7 @@ const messageAssignee = async ({ context }) => { ); } else { await Promise.all( - slackAssignees.map(assignee => + slackAssignees.map((assignee) => Bot.client.chat.postMessage({ channel: assignee.value?.slackId, text: `One of your pull requests has been REVIEWED by ${reviewer}! <${url}|View Review> :shrek:`, diff --git a/.prettierrc b/.prettierrc index a0048ce..ff98ca7 100644 --- a/.prettierrc +++ b/.prettierrc @@ -5,5 +5,6 @@ "printWidth": 100, "proseWrap": "always", "trailingComma": "all", - "semi": true -} \ No newline at end of file + "semi": true, + "endOfLine": "lf" +} diff --git a/README.md b/README.md index e5254c1..1e6c458 100644 --- a/README.md +++ b/README.md @@ -1,20 +1,26 @@ # npo-backend-template + This template will be used to create all NPO backend repos ## Setting up development environment To start working on with this project, follow these steps: + 1. Install the [EditorConfig plugin](https://editorconfig.org/#download) for your IDE. 1. Add the `.env` file stored in your projects Google Drive folder to the root of the project. -1. Install NodeJS and yarn following the [instructions here](https://classic.yarnpkg.com/lang/en/docs/install). +1. Install NodeJS and yarn following the + [instructions here](https://classic.yarnpkg.com/lang/en/docs/install). 1. Navigate to the project folder in your terminal and run `yarn` to install required packages. ## Project branching structure -Due to complications with some of the GitHub Actions this project uses, the git branch structure is non-standard. +Due to complications with some of the GitHub Actions this project uses, the git branch structure is +non-standard. -1. `dev`: This is the main branch of the project. All PRs should be merged into this branch, as if it was "main". -1. `main`: This is the "production-ready" branch of the project; `dev` should only be merged into `main` when it is at a presentable state. +1. `dev`: This is the main branch of the project. All PRs should be merged into this branch, as if + it was "main". +1. `main`: This is the "production-ready" branch of the project; `dev` should only be merged into + `main` when it is at a presentable state. ## Available Scripts @@ -22,7 +28,8 @@ In the project directory, you can run: ### `yarn start` -Runs the app in the development mode, with hot-reload support from [nodemon](https://github.com/remy/nodemon).\ +Runs the app in the development mode, with hot-reload support from +[nodemon](https://github.com/remy/nodemon).\ The server will be accessible from http://localhost:3001, and will reload if you make edits.\ You will also see any lint errors in the console. @@ -33,20 +40,24 @@ See the [Prettier docs](https://prettier.io/docs/en/index.html) for more informa ## ESLint and Prettier -This project uses ESLint and Prettier to enforce the [Airbnb JavaScript Style Guide](https://github.com/airbnb/javascript). +This project uses ESLint and Prettier to enforce the +[Airbnb JavaScript Style Guide](https://github.com/airbnb/javascript). ### ESLint Plugins Currently, the following ESLint plugins are installed: + 1. [eslint-plugin-prettier](https://github.com/prettier/eslint-plugin-prettier) Visit the links to learn more about each plugin. ### Configuration -The configuration for ESLint is inside the `.eslintrc.json` file, located in the root of the project. Learn more about ESLint [here](https://eslint.org/). +The configuration for ESLint is inside the `.eslintrc.json` file, located in the root of the +project. Learn more about ESLint [here](https://eslint.org/). -The configuration for Prettier is inside the `.prettierrc` file, located in the root of the project. Learn more about Prettier [here](https://prettier.io/docs/en/index.html). +The configuration for Prettier is inside the `.prettierrc` file, located in the root of the project. +Learn more about Prettier [here](https://prettier.io/docs/en/index.html). ## Husky and lint-staged @@ -54,17 +65,20 @@ This project uses lint-staged and husky to run ESLint checks before all commits. ### Skipping pre-commit checks -Use the `--no-verify` option to skip pre-commit checks, but please note that this is **strongly discouraged**. +Use the `--no-verify` option to skip pre-commit checks, but please note that this is **strongly +discouraged**. ### Configuration -The configuration for lint-staged is inside the `lint-staged` object inside of `package.json`. Learn more about lint-staged [here](https://github.com/okonet/lint-staged). - -The configuration for husky is in the `.husky` directory, located in the root of the project. Learn more about husky [here](https://typicode.github.io/husky/). +The configuration for lint-staged is inside the `lint-staged` object inside of `package.json`. Learn +more about lint-staged [here](https://github.com/okonet/lint-staged). +The configuration for husky is in the `.husky` directory, located in the root of the project. Learn +more about husky [here](https://typicode.github.io/husky/). ## Learn more about Node and ExpressJS You can learn about Node [here](https://nodejs.org/en/). -To learn about express, check out the this express tutorial [here](https://www.tutorialspoint.com/nodejs/nodejs_express_framework.htm). \ No newline at end of file +To learn about express, check out the this express tutorial +[here](https://www.tutorialspoint.com/nodejs/nodejs_express_framework.htm). diff --git a/app.js b/app.js index 5a336d0..d1f244b 100644 --- a/app.js +++ b/app.js @@ -7,7 +7,7 @@ require('dotenv').config(); const businessRouter = require('./routes/businessRouter'); // const donationRouter = require('./routes/donationRouter'); // const notificationRouter = require('./routes/notificationRouter'); -// const valueRouter = require('./routes/valueRouter'); +const valueRouter = require('./routes/valueRouter'); const app = express(); app.use(express.json()); @@ -23,7 +23,7 @@ app.use( app.use('/business', businessRouter); // app.use('/donation', donationRouter); // app.use('/notification', notificationRouter); -// app.use('/value', valueRouter); +app.use('/value', valueRouter); app.listen(PORT, () => { console.log(`Server listening on ${PORT}`); diff --git a/routes/README.md b/routes/README.md index c64c2df..7eab67b 100644 --- a/routes/README.md +++ b/routes/README.md @@ -1 +1 @@ -Blank file \ No newline at end of file +Blank file diff --git a/routes/valueRouter.js b/routes/valueRouter.js index e969ae0..452d184 100644 --- a/routes/valueRouter.js +++ b/routes/valueRouter.js @@ -1,6 +1,100 @@ -// const express = require('express'); -// const { db } = require('../server/db'); +const express = require('express'); +const { db } = require('../server/db'); -// Your code below (delete this line when committing): +const valueRouter = express.Router(); -// const valueRouter = express.Router(); +valueRouter.get('/', async (req, res) => { + try { + const allValues = await db.query(` + SELECT * + FROM donation_tracking; + `); + res.status(200).send(allValues); + } catch (err) { + res.status(500).send(err.message); + } +}); + +valueRouter.get('/:id', async (req, res) => { + try { + const { id } = req.params; + const value = await db.query( + ` + SELECT * + FROM donation_tracking + WHERE donation_id = $1; + `, + [id], + ); + res.status(200).send(value); + } catch (err) { + res.status(500).send(err.message); + } +}); + +valueRouter.delete('/:id', async (req, res) => { + try { + const { id } = req.params; + await db.query('DELETE from donation_tracking WHERE donation_id = $1;', [id]); + res.status(200).send('Deleted donation'); + } catch (err) { + res.status(500).send(err.message); + } +}); + +valueRouter.put('/:id', async (req, res) => { + try { + const { id } = req.params; + const { + businessId, + foodBankDonation, + reporter, + email, + date, + cannedDogFoodQuantity, + dryDogFoodQuantity, + cannedCatFoodQuantity, + dryCatFoodQuantity, + miscItems, + volunteerHours, + } = req.body; + + const updatedValue = await db.query( + `UPDATE donation_tracking + SET donation_id = $(id) + ${businessId ? `, business_id = $(businessId) ` : ''} + ${foodBankDonation ? `, food_bank_donation = $(foodBankDonation) ` : ''} + ${reporter ? `, reporter = $(reporter) ` : ''} + ${email ? `, email = $(email) ` : ''} + ${date ? `, date = $(date) ` : ''} + ${cannedDogFoodQuantity ? `, canned_dog_food_quantity = $(cannedDogFoodQuantity) ` : ''} + ${dryDogFoodQuantity ? `, dry_dog_food_quantity = $(dryDogFoodQuantity) ` : ''} + ${cannedCatFoodQuantity ? `, canned_cat_food_quanitty = $(cannedCatFoodQuanitty) ` : ''} + ${dryCatFoodQuantity ? `, dry_cat_food_quantity = $(dryCatFoodQuantity) ` : ''} + ${miscItems ? `, misc_items = $(miscItems) ` : ''} + ${volunteerHours ? `, volunteer_hours = $(volunteerHours) ` : ''} + + WHERE id = $(id) + RETURNING *;`, + { + id, + businessId, + foodBankDonation, + reporter, + email, + date, + cannedDogFoodQuantity, + dryDogFoodQuantity, + cannedCatFoodQuantity, + dryCatFoodQuantity, + miscItems, + volunteerHours, + }, + ); + return res.status(200).send(updatedValue); + } catch (err) { + return res.status(500).send(err.message); + } +}); + +module.exports = valueRouter;