Skip to content

Commit

Permalink
Fixing relationship data parse method. UT fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
NicolasOmar committed Nov 24, 2023
1 parent c608625 commit b4a91fa
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 12 deletions.
3 changes: 2 additions & 1 deletion src/constants/allowedFields.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
"hairColors",
"hasHeterochromia",
"eyeColors",
"passedAway"
"passedAway",
"events"
]
}
}
1 change: 0 additions & 1 deletion src/db/models/pet.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ const petSchema = new Schema({
},
events: {
type: [Schema.Types.ObjectId],
required: true,
ref: 'Event'
}
})
Expand Down
18 changes: 16 additions & 2 deletions src/functions/parsers.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,24 @@ export const parseErrorMsg = {
noIdeaCode: code => `No idea dude, the code ${code} has not been mapped so far`
}

export const findByIds = async ({ model, ids, findOne = false, parser = '_id name' }) => {
return findOne
export const findByIds = async ({
model,
ids,
findOne = false,
parser = '_id name',
parseId = false

Check warning on line 69 in src/functions/parsers.js

View check run for this annotation

Codecov / codecov/patch

src/functions/parsers.js#L69

Added line #L69 was not covered by tests
}) => {
const findedData = findOne
? await model.findOne({ _id: ids }, parser)
: await model.find().where('_id').in(ids).select(parser)

if (parseId) {
return findOne
? { id: findedData._id, name: findedData.name }
: findedData.map(({ _id, name }) => ({ id: _id, name }))
} else {
return findedData

Check warning on line 80 in src/functions/parsers.js

View check run for this annotation

Codecov / codecov/patch

src/functions/parsers.js#L79-L80

Added lines #L79 - L80 were not covered by tests
}
}

export const parseUniqueArray = (list, callback) =>
Expand Down
3 changes: 2 additions & 1 deletion src/graphql/resolvers/Mutations.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,10 @@ const Mutations = {
try {
const parsedNewEvent = new Event(eventInfo)
await parsedNewEvent.save()
const { events } = await Pet.findById(eventInfo.associatedPets[0])
await Pet.findOneAndUpdate(

Check warning on line 166 in src/graphql/resolvers/Mutations.js

View check run for this annotation

Codecov / codecov/patch

src/graphql/resolvers/Mutations.js#L162-L166

Added lines #L162 - L166 were not covered by tests
{ _id: eventInfo.associatedPets[0] },
{ events: [parsedNewEvent._id] }
{ events: [...events, parsedNewEvent._id] }
)

return parsedNewEvent.toJSON()

Check warning on line 171 in src/graphql/resolvers/Mutations.js

View check run for this annotation

Codecov / codecov/patch

src/graphql/resolvers/Mutations.js#L171

Added line #L171 was not covered by tests
Expand Down
7 changes: 6 additions & 1 deletion src/graphql/resolvers/Queries.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,12 @@ const Queries = {
}

const petTypeInfo = Promise.allSettled(
petPopulation.map(pet => new Promise(resolve => resolve(findByIds(PetType, pet.petType))))
petPopulation.map(
pet =>
new Promise(resolve =>
resolve(findByIds({ model: PetType, ids: pet.petType, parseId: true }))
)
)
)
const petTypeList = (await petTypeInfo).map(data => data.value[0].name)
const parsedPetTypeList = parseUniqueArray(petTypeList, info => ({
Expand Down
9 changes: 6 additions & 3 deletions src/graphql/resolvers/Relationships.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,20 @@ const Relationships = {
await findByIds({
model: PetType,
ids: petType,
findOne: true
findOne: true,
parseId: true
}),
hairColors: async ({ hairColors }) =>
await findByIds({
model: Color,
ids: hairColors
ids: hairColors,
parseId: true
}),
eyeColors: async ({ eyeColors }) =>
await findByIds({
model: Color,
ids: eyeColors
ids: eyeColors,
parseId: true
}),
events: async ({ events }) =>
await findByIds({

Check warning on line 41 in src/graphql/resolvers/Relationships.js

View check run for this annotation

Codecov / codecov/patch

src/graphql/resolvers/Relationships.js#L41

Added line #L41 was not covered by tests
Expand Down
3 changes: 2 additions & 1 deletion src/graphql/resolvers/mocks/Mutations.mocks.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
{ "field": "weight", "value": 150 },
{ "field": "gender", "value": "masculine" },
{ "field": "hasHeterochromia", "value": true },
{ "field": "passedAway", "value": false }
{ "field": "passedAway", "value": false },
{ "field": "events", "value": [] }
]
}
}
6 changes: 4 additions & 2 deletions src/graphql/resolvers/tests/Mutations.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,8 @@ describe('[Mutations]', () => {
const nameToUpdate = 'modifiedTest'
const modifiedPet = {
...petInfo,
name: nameToUpdate
name: nameToUpdate,
events: []
}

await Mutations.updatePet(null, { petInfo: modifiedPet }, { loggedUser })
Expand Down Expand Up @@ -405,7 +406,8 @@ describe('[Mutations]', () => {
...updateFields.pet
.map(({ field, value }) => ({ [field]: value }))
.reduce((finalObj, currentProp) => ({ ...finalObj, ...currentProp }), {}),
id: secondPet._id
id: secondPet._id,
events: []
}

await Mutations.updatePet(null, { petInfo: updatedSecondPet }, { loggedUser })
Expand Down

0 comments on commit b4a91fa

Please sign in to comment.