Skip to content

Commit

Permalink
fix api test and add unit test for creatQuestion resolver
Browse files Browse the repository at this point in the history
  • Loading branch information
pearl-truss committed Nov 21, 2023
1 parent dc923bc commit 5f6eab4
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,20 @@ import {
unlockTestHealthPlanPackage,
createTestQuestion,
indexTestQuestions,
defaultFloridaProgram,
} from '../../testHelpers/gqlHelpers'
import { packageName } from '../../../../app-web/src/common-code/healthPlanFormDataType'
import { assertAnError, assertAnErrorCode } from '../../testHelpers'
import {
createDBUsersWithFullData,
testCMSUser,
} from '../../testHelpers/userHelpers'
import { base64ToDomain } from '../../../../app-web/src/common-code/proto/healthPlanFormDataProto'
import { testLDService } from '../../testHelpers/launchDarklyHelpers'
import { testEmailConfig, testEmailer } from '../../testHelpers/emailerHelpers'
// import {
// mockEmailParameterStoreError,
// } from '../../testHelpers/parameterStoreHelpers'

describe('createQuestion', () => {
const mockLDService = testLDService({ ['rates-db-refactor']: true })
Expand Down Expand Up @@ -151,7 +158,7 @@ describe('createQuestion', () => {
})
)
})
it('returns an error package status is DRAFT', async () => {
it('returns an error if package status is DRAFT', async () => {
const stateServer = await constructTestPostgresServer({
ldService: mockLDService,
})
Expand Down Expand Up @@ -286,4 +293,61 @@ describe('createQuestion', () => {
`users without an assigned division are not authorized to create a question`
)
})
it('send state email to state contacts and all submitters when unlocking submission succeeds', async () => {
const config = testEmailConfig()
const mockEmailer = testEmailer(config)
//mock invoke email submit lambda
const stateServer = await constructTestPostgresServer({
ldService: mockLDService,
})
const cmsServer = await constructTestPostgresServer({
context: {
user: cmsUser,
},
ldService: mockLDService,
emailer: mockEmailer,
})

const stateSubmission = await createAndSubmitTestHealthPlanPackage(
stateServer
)

await createTestQuestion(cmsServer, stateSubmission.id)

const currentRevision = stateSubmission.revisions[0].node.formDataProto

const sub = base64ToDomain(currentRevision)
if (sub instanceof Error) {
throw sub
}

const programs = [defaultFloridaProgram()]
const name = packageName(
sub.stateCode,
sub.stateNumber,
sub.programIDs,
programs
)

const stateReceiverEmails = [
'james@example.com',
...sub.stateContacts.map((contact) => contact.email),
]

// email subject line is correct for state email
// Mock emailer is called 1 time
expect(mockEmailer.sendEmail).toHaveBeenNthCalledWith(
1,
expect.objectContaining({
subject: expect.stringContaining(
`[LOCAL] New questions about ${name}`
),
sourceEmail: config.emailSource,
toAddresses: expect.arrayContaining(
Array.from(stateReceiverEmails)
),
bodyHTML: expect.stringContaining(name),
})
)
})
})
27 changes: 9 additions & 18 deletions services/app-api/src/resolvers/questionResponse/createQuestion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,16 @@ export function createQuestionResolver(
})
}

if (!contractResult.draftRevision) {
throw new Error(
'PROGRAMMING ERROR: Status should not be submittable without a draft revision'
)
// Return error if package status is DRAFT, contract will have no submitted revisions
if (contractResult.revisions.length === 0) {
const errMessage = `Issue creating question for health plan package. Message: Cannot create question for health plan package in DRAFT status`
logError('createQuestion', errMessage)
setErrorAttributesOnActiveSpan(errMessage, span)
throw new UserInputError(errMessage)
}

const conversionResult = convertContractWithRatesToFormData(
contractResult.draftRevision,
contractResult.revisions[0],
contractResult.id,
contractResult.stateCode,
contractResult.stateNumber
Expand All @@ -94,9 +97,7 @@ export function createQuestionResolver(
throw new Error(errMessage)
}

// Now we do the conversions
const pkg = convertContractWithRatesToUnlockedHPP(contractResult)

if (pkg instanceof Error) {
const errMessage = `Error converting draft contract. Message: ${pkg.message}`
logError('createHealthPlanPackage', errMessage)
Expand All @@ -109,9 +110,7 @@ export function createQuestionResolver(
})
}

const statePrograms = store.findStatePrograms(
conversionResult.stateCode
)
const statePrograms = store.findStatePrograms(contractResult.stateCode)
const submitterEmails = packageSubmitters(pkg)

if (statePrograms instanceof Error) {
Expand All @@ -125,14 +124,6 @@ export function createQuestionResolver(
})
}

// Return error if package status is DRAFT, contract will have no submitted revisions
if (contractResult.revisions.length === 0) {
const errMessage = `Issue creating question for health plan package. Message: Cannot create question for health plan package in DRAFT status`
logError('createQuestion', errMessage)
setErrorAttributesOnActiveSpan(errMessage, span)
throw new UserInputError(errMessage)
}

const questionResult = await store.insertQuestion(input, user)

if (isStoreError(questionResult)) {
Expand Down

0 comments on commit 5f6eab4

Please sign in to comment.