Skip to content

Commit

Permalink
Add resolver test, clean up resolver
Browse files Browse the repository at this point in the history
  • Loading branch information
pearl-truss committed Jan 22, 2025
1 parent f2a7ad5 commit 689112c
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 41 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import {
constructTestPostgresServer,
createTestQuestion,
} from '../../testHelpers/gqlHelpers'
import {
testCMSUser,
createDBUsersWithFullData,
} from '../../testHelpers/userHelpers'
import { testS3Client } from '../../testHelpers/s3Helpers'
import {
createAndSubmitTestContract,
fetchTestContractWithQuestions,
} from '../../testHelpers'

describe(`questionResolver`, () => {
const mockS3 = testS3Client()
const dmcpCMSUser = testCMSUser({
divisionAssignment: 'DMCP',
})

beforeAll(async () => {
//Inserting a new CMS user, with division assigned, in postgres in order to create the question to user relationship.
await createDBUsersWithFullData([dmcpCMSUser])
})

it('populates a round number on fetch', async () => {
const stateServer = await constructTestPostgresServer()

const dmcpCMSServer = await constructTestPostgresServer({
context: {
user: dmcpCMSUser,
},
s3Client: mockS3,
})

const contract = await createAndSubmitTestContract(stateServer)

const createdDMCPQuestion = await createTestQuestion(
dmcpCMSServer,
contract.id,
{
documents: [
{
name: 'Test Question 2',
s3URL: 's3://bucketname/key/test12',
},
],
}
)

const createdDMCPQuestion2 = await createTestQuestion(
dmcpCMSServer,
contract.id,
{
documents: [
{
name: 'Test Question 2',
s3URL: 's3://bucketname/key/test12',
},
],
}
)

const contractWithQuestions = await fetchTestContractWithQuestions(
stateServer,
contract.id
)
const indexQuestionsResult = contractWithQuestions.questions
const firstDMCPQuestion =
indexQuestionsResult?.DMCPQuestions.edges.find(
(q) => q.node.id === createdDMCPQuestion.question.id
)
const secondDMCPQuestion =
indexQuestionsResult?.DMCPQuestions.edges.find(
(q) => q.node.id === createdDMCPQuestion2.question.id
)
expect(firstDMCPQuestion?.node.round).toBe(1)
expect(secondDMCPQuestion?.node.round).toBe(2)
})
})
52 changes: 11 additions & 41 deletions services/app-api/src/resolvers/questionResponse/questionResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,21 @@ import type { Resolvers } from '../../gen/gqlServer'

import type { ContractQuestionType } from '../../domain-models'
import type { Store } from '../../postgres'

import { GraphQLError } from 'graphql'

export function questionResolver(store: Store): Resolvers['QuestionResolver'] {
return {
round: async (parent: ContractQuestionType) => {
const questions = await store.findAllQuestionsByContract(
parent.contractID
)
if (!questions) {
throw new Error(
`Questions not found for contract: ${parent.contractID}`
)
}
if (questions instanceof Error) {
const errMessage = `Issue finding contract message: ${questions.message}`
const errMessage = `Issue return questions for contract message: ${questions.message}`

throw new GraphQLError(errMessage, {
extensions: {
Expand All @@ -22,55 +27,20 @@ export function questionResolver(store: Store): Resolvers['QuestionResolver'] {
}

const divisionQuestions = questions
?.filter((q) => q.division === parent.division)
.filter((q) => q.division === parent.division)
.sort((a, b) => a.createdAt.getTime() - b.createdAt.getTime())

const matchingQuestion = divisionQuestions?.find(
const matchingQuestion = divisionQuestions.find(
(question) => question.id == parent.id
)

if (!matchingQuestion) {
return 0
} else {
return divisionQuestions?.indexOf(matchingQuestion) !==
undefined
? divisionQuestions?.indexOf(matchingQuestion) + 1
return divisionQuestions.indexOf(matchingQuestion) !== undefined
? divisionQuestions.indexOf(matchingQuestion) + 1
: 0
}
},
// responses: async (parent: any) => {
// const questions = await store.findAllQuestionsByContract(parent.contractID)
// if (questions instanceof Error) {
// const errMessage = `Issue finding contract message: ${questions.message}`

// throw new GraphQLError(errMessage, {
// extensions: {
// code: 'INTERNAL_SERVER_ERROR',
// cause: 'DB_ERROR',
// },
// })
// }

// let round = 0
// const divisionQuestions = questions?.
// filter((q) => q.division === parent.division).
// sort((a, b) => a.createdAt.getTime() - b.createdAt.getTime())

// const matchingQuestion = divisionQuestions?.find(
// (question) => question.id == parent.id)

// if (!matchingQuestion) {
// round = 0
// } else {
// divisionQuestions?.indexOf(matchingQuestion) !== undefined
// ? round = divisionQuestions?.indexOf(matchingQuestion) + 1
// : round = 0
// }
// const responses = parent.responses.map((res: any) => {
// res.round = round
// return round
// })
// return responses
// }
}
}

0 comments on commit 689112c

Please sign in to comment.