-
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.
- Loading branch information
1 parent
ddac8e6
commit 8dcd93c
Showing
3 changed files
with
106 additions
and
18 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
103 changes: 103 additions & 0 deletions
103
services/app-api/src/resolvers/contract/unlockedContractResolver.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,103 @@ | ||
import statePrograms from '../../../../app-web/src/common-code/data/statePrograms.json' | ||
import type { Resolvers, SubmissionReason } from '../../gen/gqlServer' | ||
import { GraphQLError } from 'graphql' | ||
import type { | ||
ContractPackageSubmissionWithCauseType, | ||
RateRevisionType, | ||
} from '../../domain-models' | ||
|
||
export function unlockedContractResolver(): Resolvers['UnlockedContract'] { | ||
return { | ||
initiallySubmittedAt(parent) { | ||
if (parent.packageSubmissions.length > 0) { | ||
const firstSubmission = | ||
parent.packageSubmissions[ | ||
parent.packageSubmissions.length - 1 | ||
] | ||
return firstSubmission.submitInfo.updatedAt | ||
} | ||
|
||
return null | ||
}, | ||
state(parent) { | ||
const packageState = parent.stateCode | ||
const state = statePrograms.states.find( | ||
(st) => st.code === packageState | ||
) | ||
|
||
if (state === undefined) { | ||
const errMessage = | ||
'State not found in database: ' + packageState | ||
throw new GraphQLError(errMessage, { | ||
extensions: { | ||
code: 'INTERNAL_SERVER_ERROR', | ||
cause: 'DB_ERROR', | ||
}, | ||
}) | ||
} | ||
return state | ||
}, | ||
packageSubmissions(parent) { | ||
const gqlSubs: ContractPackageSubmissionWithCauseType[] = [] | ||
for (let i = 0; i < parent.packageSubmissions.length; i++) { | ||
const thisSub = parent.packageSubmissions[i] | ||
let prevSub = undefined | ||
if (i < parent.packageSubmissions.length - 1) { | ||
prevSub = parent.packageSubmissions[i + 1] | ||
} | ||
|
||
// determine the cause for this submission | ||
let cause: SubmissionReason = 'CONTRACT_SUBMISSION' | ||
|
||
if ( | ||
!thisSub.submittedRevisions.find( | ||
(r) => r.id === thisSub.contractRevision.id | ||
) | ||
) { | ||
// not a contract submission, this contract wasn't in the submitted bits | ||
const connectedRateRevisionIDs = thisSub.rateRevisions.map( | ||
(r) => r.id | ||
) | ||
const submittedRate = thisSub.submittedRevisions.find((r) => | ||
connectedRateRevisionIDs.includes(r.id) | ||
) | ||
|
||
if (!submittedRate) { | ||
cause = 'RATE_UNLINK' | ||
} else { | ||
const thisSubmittedRate = | ||
submittedRate as RateRevisionType | ||
if (!prevSub) { | ||
throw new Error( | ||
'Programming Error: a non-contract submission must have a previous contract submission' | ||
) | ||
} | ||
const previousRateRevisionIDs = | ||
prevSub.rateRevisions.map((r) => r.rateID) | ||
if ( | ||
previousRateRevisionIDs.includes( | ||
thisSubmittedRate.rateID | ||
) | ||
) { | ||
cause = 'RATE_SUBMISSION' | ||
} else { | ||
cause = 'RATE_LINK' | ||
} | ||
} | ||
} | ||
|
||
const gqlSub: ContractPackageSubmissionWithCauseType = { | ||
cause, | ||
submitInfo: thisSub.submitInfo, | ||
submittedRevisions: thisSub.submittedRevisions, | ||
contractRevision: thisSub.contractRevision, | ||
rateRevisions: thisSub.rateRevisions, | ||
} | ||
|
||
gqlSubs.push(gqlSub) | ||
} | ||
|
||
return gqlSubs | ||
}, | ||
} | ||
} |
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