Last active
December 14, 2025 17:08
-
-
Save trikitrok/7754271af5a71eac74d80c08c5119dfa to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| class AcmeCompanyApi implements CompanyApi { | |
| constructor( | |
| private readonly forGettingCauses: ForGettingCauses, | |
| private readonly forOpeningClaim: ForOpeningClaim, | |
| private readonly authTokenRetriever: AuthTokenRetriever) { | |
| } | |
| async open(claim: Claim): Promise<OpeningResult> { | |
| try { | |
| const token = await this.authTokenRetriever.retrieveToken(); | |
| const causes = await this.forGettingCauses.getAllFor(claim, token); | |
| const cause = this.findCauseInClaim(causes, claim); | |
| const referenceInCompany = await this.forOpeningClaim.open(claim, cause, token); | |
| return OpeningResult.successful(referenceInCompany, claim); | |
| } catch (e) { | |
| if (e instanceof CannotRetrieveTokenError) { | |
| return OpeningResult.failed(claim, 'Acme API: failure retrieving token'); | |
| } | |
| if (e instanceof CannotGetCausesError) { | |
| return OpeningResult.failed(claim, 'Acme API: failure getting claim causes'); | |
| } | |
| if (e instanceof CannotOpenClaimError) { | |
| return OpeningResult.failed( | |
| claim, `Acme API: cannot open claim ${claim.id()}` | |
| ); | |
| } | |
| if (e instanceof CannotFindMatchingCauseError) { | |
| return OpeningResult.failed( | |
| claim, `Acme API: cannot find cause for claim ${claim.id()}` | |
| ); | |
| } | |
| return OpeningResult.failed( | |
| claim, `Acme API: ${e.message}` | |
| ); | |
| } | |
| } | |
| private findCauseInClaim(causes: Cause[], claim: Claim): Cause { | |
| let foundCause = causes.find((c) => c.causeCode === claim.causeCode()); | |
| if (!foundCause) { | |
| throw new CannotFindMatchingCauseError(); | |
| } | |
| return foundCause; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment