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

setup_values_get_put_delete Issue #11 Resolved #16

Merged
merged 4 commits into from
Jan 9, 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: 2 additions & 6 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {}
}
27 changes: 13 additions & 14 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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
6 changes: 3 additions & 3 deletions .github/workflows/scripts/reviewReviewed.js
Original file line number Diff line number Diff line change
Expand Up @@ -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:`,
Expand All @@ -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:`,
Expand Down
5 changes: 3 additions & 2 deletions .prettierrc
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
"printWidth": 100,
"proseWrap": "always",
"trailingComma": "all",
"semi": true
}
"semi": true,
"endOfLine": "lf"
}
40 changes: 27 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,35 @@
# 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

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.

Expand All @@ -33,38 +40,45 @@ 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

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).
To learn about express, check out the this express tutorial
[here](https://www.tutorialspoint.com/nodejs/nodejs_express_framework.htm).
4 changes: 2 additions & 2 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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}`);
Expand Down
2 changes: 1 addition & 1 deletion routes/README.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Blank file
Blank file
102 changes: 98 additions & 4 deletions routes/valueRouter.js
Original file line number Diff line number Diff line change
@@ -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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const { business_id: businessId, message, timestamp, been_dismissed: beenDismissed } = req.body;

do it like so, we redefine the snake case into camel case so that we have consistent variables across our different code packages

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;