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

Feature | Create Widget to look created Pets and its composition | Back end #27

Merged
merged 2 commits into from
Dec 11, 2022
Merged
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
20 changes: 10 additions & 10 deletions coverage/coverage-final.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "my-pets-api",
"version": "0.1.34",
"version": "0.2.0",
"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",
5 changes: 5 additions & 0 deletions src/functions/parsers.js
Original file line number Diff line number Diff line change
@@ -71,3 +71,8 @@ export const findIds = async (model, ids, findOne = false) => {

return (await model.find().where('_id').in(ids)).map(data => parsedAuxiliaryData(data))
}

export const parseUniqueArray = (list, callback) =>
Array.isArray(list)
? Array.from(new Set(list)).map((item, i) => (callback ? callback(item, i) : item))
: []
24 changes: 24 additions & 0 deletions src/graphql/resolvers/Queries.js
Original file line number Diff line number Diff line change
@@ -5,6 +5,7 @@ import Pet from '../../db/models/pet.model'
import User from '../../db/models/user.model'
import { ApolloError } from 'apollo-server-errors'
import { ERROR_MSGS, HTTP_CODES } from '../../constants/errors.json'
import { findIds, parseUniqueArray } from '../../functions/parsers'

const Queries = {
getUser: async (_, __, { loggedUser, token }) => ({
@@ -35,6 +36,29 @@ const Queries = {
}

return foundedPet
},
getMyPetsPopulation: async (_, __, { loggedUser }) => {
if (!loggedUser) {
throw new ApolloError(ERROR_MSGS.MISSING_USER_DATA, HTTP_CODES.UNAUTHORIZED)
}

const { _id } = await User.findOne({ userName: loggedUser.userName })
const petPopulation = await Pet.find({ user: _id })

if (petPopulation.length === 0) {
return [{ name: 'All', quantity: 0 }]
}

const petTypeInfo = Promise.allSettled(
petPopulation.map(pet => new Promise(resolve => resolve(findIds(PetType, pet.petType))))
)
const petTypeList = (await petTypeInfo).map(data => data.value[0].name)
const parsedPetTypeList = parseUniqueArray(petTypeList, info => ({
name: info,
quantity: petTypeList.filter(_info => _info === info).length
}))

return [{ name: 'All', quantity: petPopulation.length }, ...parsedPetTypeList]
}
}

28 changes: 27 additions & 1 deletion src/graphql/resolvers/tests/Queries.test.js
Original file line number Diff line number Diff line change
@@ -86,7 +86,7 @@ describe('[Queries]', () => {
})
})

describe('[GetPet]', () => {
describe('[getPet]', () => {
describe('[HAPPY PATH]', () => {
test('Should return one of my pets', async () => {
const getPetInfo = await Query.getPet(null, { id: petId }, { loggedUser: testEnv.user })
@@ -115,4 +115,30 @@ describe('[Queries]', () => {
})
})
})

describe('[getMyPetsPopulation]', () => {
describe('[HAPPY PATH]', () => {
test('Should return my pets population', async () => {
const getPopulationInfo = await Query.getMyPetsPopulation(null, null, {
loggedUser: testEnv.user
})
const [petType] = await Query.getPetTypes()

expect(getPopulationInfo).toStrictEqual([
{ name: 'All', quantity: 1 },
{ name: petType.name, quantity: 1 }
])
})
})

describe('[SAD PATH]', () => {
test('Should return a USER_MISSING_DATA error by not passing the loggedUser', async () => {
try {
await Query.getMyPetsPopulation(null, null, {})
} catch (error) {
expect(error.message).toBe(ERROR_MSGS.MISSING_USER_DATA)
}
})
})
})
})
5 changes: 5 additions & 0 deletions src/graphql/schemas/Entities.gql
Original file line number Diff line number Diff line change
@@ -30,4 +30,9 @@ type Pet {
type AuxiliaryData {
id: ID!
name: String!
}

type AmountData {
name: String!
quantity: Int!
}
1 change: 1 addition & 0 deletions src/graphql/schemas/Operations.gql
Original file line number Diff line number Diff line change
@@ -4,6 +4,7 @@ type Query {
getColors: [AuxiliaryData]!
getMyPets: [Pet]!
getPet(id: ID!): Pet
getMyPetsPopulation: [AmountData]
}

type Mutation {