Created
July 25, 2026 22:46
-
-
Save mostlyobvious/c33069af1b509ed744c93a7bf9c8a531 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
| module Jira where | |
| -- data Maybe a = Nothing | Just a | |
| --instance Show Event where | |
| -- show (IssueOpened id) = "IssueOpened " ++ id | |
| -- show (IssueResolved id) = "IssueResolved " ++ id | |
| -- show (IssueClosed id) = "IssueClosed " ++ id | |
| -- show (IssueReopened id) = "IssueReopened " ++ id | |
| -- show (IssueProgressStarted id) = "IssueProgressStarted " ++ id | |
| -- show (IssueProgressStopped id) = "IssueProgressStopped " ++ id | |
| --instance Eq Status where | |
| -- (==) Open Open = True | |
| -- (==) Resolved Resolved = True | |
| -- (==) Closed Closed = True | |
| -- (==) Reopened Reopened = True | |
| -- (==) InProgress InProgress = True | |
| -- (==) _ _ = False | |
| --instance Show Status where | |
| -- show Open = "Open" | |
| -- show Resolved = "Resolved" | |
| -- show Closed = "Closed" | |
| -- show Reopened = "Reopened" | |
| -- show InProgress = "InProgress" | |
| --instance Show Issue where | |
| -- show (Issue id status) = "Issue " ++ id ++ " " ++ (show status) | |
| -- types | |
| type IssueId = String | |
| data Event = IssueOpened IssueId | | |
| IssueResolved IssueId | | |
| IssueClosed IssueId| | |
| IssueReopened IssueId | | |
| IssueProgressStarted IssueId | | |
| IssueProgressStopped IssueId deriving (Show, Eq) | |
| data Command = CreateIssue IssueId | | |
| ResolveIssue IssueId | | |
| CloseIssue IssueId | | |
| ReopenIssue IssueId | | |
| StartIssueProgress IssueId | | |
| StopIssueProgress IssueId deriving (Show, Eq) | |
| type History = [Event] | |
| data Status = Open | | |
| Resolved | | |
| Closed | | |
| Reopened | | |
| InProgress deriving (Show, Eq) | |
| data Issue = Issue { issueId :: IssueId | |
| , status :: Status | |
| } deriving Show | |
| -- command handler | |
| handle :: Command -> History -> History | |
| handle command events = | |
| handle' command issue | |
| where issue = apply events | |
| -- aggregate | |
| -- TODO: better errors over empty array? | |
| handle' :: Command -> Maybe Issue -> History | |
| handle' (CreateIssue issueId) Nothing = | |
| [IssueOpened issueId] | |
| handle' (CloseIssue issueId) (Just issue) | |
| | canClose = [IssueClosed issueId] | |
| | otherwise = [] | |
| where canClose = any (== (status issue)) [Open, Resolved] | |
| handle' (CloseIssue issueId) Nothing = | |
| [] | |
| -- projecting events into Issue record | |
| apply :: History -> Maybe Issue | |
| apply events = foldl apply' Nothing events | |
| apply' :: Maybe Issue -> Event -> Maybe Issue | |
| apply' _ (IssueOpened issueId) = | |
| Just (Issue issueId Open) | |
| apply' _ (IssueResolved issueId) = | |
| Just (Issue issueId Resolved) | |
| apply' _ (IssueClosed issueId) = | |
| Just (Issue issueId Closed) | |
| apply' _ (IssueReopened issueId) = | |
| Just (Issue issueId Reopened) | |
| apply' _ (IssueProgressStarted issueId) = | |
| Just (Issue issueId InProgress) | |
| apply' _ (IssueProgressStopped issueId) = | |
| Just (Issue issueId Open) | |
| -- event handler | |
| react :: Event -> IO () | |
| react event = | |
| print event | |
| -- process | |
| --process :: History -> Event -> Maybe Command | |
| --process events event | |
| -- | complete = Just (CreateIssue "19d7e8c0-ee9d-4aaa-9129-a2d850d5be51") | |
| -- | otherwise = Nothing | |
| -- where complete = events ++ [event] == [] | |
| -- | |
| -- | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment