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

Add a search bar for "List My Pets" page #146

Merged
merged 4 commits into from
Nov 14, 2023
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
2 changes: 1 addition & 1 deletion .github/workflows/check_quality.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
# Disabling shallow clone is recommended for improving relevancy of reporting
fetch-depth: 0
- name: 2/7 | Setup Node environment
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version: 18.x
- name: 3/7 | Setup MongoDB test instance
Expand Down
2 changes: 0 additions & 2 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,3 @@
. "$(dirname -- "$0")/_/husky.sh"

npm run lint-staged
npm run test:clean:ci
git add .
5 changes: 5 additions & 0 deletions .husky/pre-push
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

npm run test:clean:ci
npm run build
2,174 changes: 1,108 additions & 1,066 deletions package-lock.json

Large diffs are not rendered by default.

38 changes: 21 additions & 17 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "my-pets-api",
"version": "0.2.8",
"version": "0.2.9",
"description": "GraphQL server based on NodeJs to give back-end support a web client based on React",
"author": "Nicolás Omar González Passerino",
"license": "MIT",
Expand All @@ -16,49 +16,53 @@
"scripts": {
"start": "npm run env:local -- nodemon --ext js,gql --watch src --exec npm run server",
"server": "babel-node src/index.js",
"start:ci": "babel ./src/index.js -d ./build ./src --copy-files && node ./build/index.js",
"build": "babel ./src/index.js -d ./build ./src --copy-files",
"start:ci": "npm run build && node ./build/index.js",
"test": "env-cmd -f ./env/test.env jest --runInBand",
"test:ci": "npm test -- --coverage",
"test:clear:cache": "npm test -- --clearCache",
"test:clean": "npm run test:clear:cache && npm test",
"test:clean:dev": "npm run test:clear:cache && npm test",
"test:clean:ci": "npm run test:clear:cache && npm run test:ci",
"env:local": "env-cmd -f ./env/local.env",
"db:create": "npm run env:local -- babel-node scripts/generate-db.js",
"lint": "eslint --fix src",
"prettier": "prettier --write src",
"lint-staged": "lint-staged -v"
"lint": "eslint src --max-warnings 0",
"prettier": "prettier src/**/*.js --write",
"lint-staged": "lint-staged -v",
"update": "npm run update:deps && npm run update:doctor",
"update:deps": "ncu -i --format group",
"update:doctor": "npm run test:clean:dev && npm run build"
},
"dependencies": {
"@babel/cli": "^7.23.0",
"@babel/core": "^7.23.0",
"@babel/core": "^7.23.3",
"@babel/node": "^7.22.19",
"@babel/plugin-transform-runtime": "^7.22.15",
"@babel/preset-env": "^7.22.20",
"@babel/plugin-transform-runtime": "^7.23.3",
"@babel/preset-env": "^7.23.3",
"apollo-server-errors": "^3.3.1",
"apollo-server-express": "^3.12.1",
"apollo-server-express": "^3.13.0",
"babel-plugin-import-graphql": "^2.8.1",
"bcryptjs": "^2.4.3",
"crypto-js": "^4.1.1",
"crypto-js": "^4.2.0",
"express": "^4.18.2",
"graphql": "^16.8.1",
"hbs": "^4.2.0",
"jsonwebtoken": "^9.0.2",
"mongoose": "^7.5.3",
"mongoose": "^8.0.0",
"validator": "^13.11.0"
},
"devDependencies": {
"@babel/eslint-parser": "^7.22.15",
"@babel/eslint-parser": "^7.23.3",
"@babel/register": "^7.22.15",
"env-cmd": "^10.1.0",
"eslint": "^8.50.0",
"eslint-plugin-import": "^2.28.1",
"eslint": "^8.53.0",
"eslint-plugin-import": "^2.29.0",
"eslint-plugin-node": "^11.1.0",
"graphql-depth-limit": "^1.1.0",
"husky": "^8.0.3",
"jest": "^29.7.0",
"lint-staged": "^14.0.1",
"lint-staged": "^15.1.0",
"nodemon": "^3.0.1",
"prettier": "^3.0.3"
"prettier": "^3.1.0"
},
"engines": {
"node": ">=12.9.0"
Expand Down
5 changes: 1 addition & 4 deletions src/db/mongoose.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import mongoose from 'mongoose'

const _mongoose = mongoose.connect(`${process.env.CONNECTION_URL}`, {
useNewUrlParser: true,
useUnifiedTopology: true
})
const _mongoose = mongoose.connect(`${process.env.CONNECTION_URL}`)

export default _mongoose
8 changes: 6 additions & 2 deletions src/graphql/resolvers/Queries.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,17 @@ const Queries = {
}),
getPetTypes: async () => (await PetType.find()).map(({ _id, name }) => ({ id: _id, name })),
getColors: async () => (await Color.find()).map(({ _id, name }) => ({ id: _id, name })),
getMyPets: async (_, __, { loggedUser }) => {
getMyPets: async (_, query, { loggedUser }) => {
if (!loggedUser) {
throw new ApolloError(ERROR_MSGS.MISSING_USER_DATA, HTTP_CODES.UNAUTHORIZED)
}

const { _id } = await User.findOne({ userName: loggedUser.userName })
return await Pet.find({ user: _id })
const petFindQuery = query?.search
? { user: _id, name: new RegExp(query?.search) }
: { user: _id }

return await Pet.find(petFindQuery)
},
getPet: async (_, { id }, { loggedUser }) => {
if (!loggedUser) {
Expand Down
21 changes: 21 additions & 0 deletions src/graphql/resolvers/tests/Queries.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,27 @@ describe('[Queries]', () => {

Object.keys(petInfo).forEach(key => expect(petInfo[key]).toStrictEqual(getPet[key]))
})

test('Should return an array of pets if I search for a part of the name', async () => {
const petsResult = await Query.getMyPets(
null,
{ search: 'te' },
{ loggedUser: testEnv.user }
)

expect(petsResult.length).toBe(1)
expect(petsResult[0].name).toBe(testEnv.pet.name)
})

test('Should return an empty array of pets if I search for wrong names', async () => {
const emptyResult = await Query.getMyPets(
null,
{ search: 'pet' },
{ loggedUser: testEnv.user }
)

expect(emptyResult.length).toBe(0)
})
})

describe('[SAD PATH]', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/graphql/schemas/Operations.gql
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ type Query {
getUser: User!
getPetTypes: [AuxiliaryData]!
getColors: [AuxiliaryData]!
getMyPets: [Pet]!
getMyPets(search: String): [Pet]!
getPet(id: ID!): Pet
getMyPetsPopulation: [AmountData]
}
Expand Down