-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MCR-4826: add round number to Question object (#3092)
* add round number to Question object * move add round number logic to resolver chain * fix mocks * Add resolver test, clean up resolver * remove change to domain model
- Loading branch information
1 parent
eb30891
commit 7d265a2
Showing
8 changed files
with
145 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
services/app-api/src/resolvers/questionResponse/questionResolver.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) |
46 changes: 46 additions & 0 deletions
46
services/app-api/src/resolvers/questionResponse/questionResolver.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
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 return questions for contract message: ${questions.message}` | ||
|
||
throw new GraphQLError(errMessage, { | ||
extensions: { | ||
code: 'INTERNAL_SERVER_ERROR', | ||
cause: 'DB_ERROR', | ||
}, | ||
}) | ||
} | ||
|
||
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) { | ||
return 0 | ||
} else { | ||
return divisionQuestions.indexOf(matchingQuestion) !== undefined | ||
? divisionQuestions.indexOf(matchingQuestion) + 1 | ||
: 0 | ||
} | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters