userUnclaimCommentDB :: CommentId -> Maybe Text -> SDB ()
userUnclaimCommentDB comment_id release_note = do
    maybe_ticket_claiming_entity <-
        lift $ getBy $ UniqueTicketClaiming comment_id
    case maybe_ticket_claiming_entity of
        Nothing -> return ()
        Just (Entity ticket_claiming_id TicketClaiming{..}) -> do
            now <- liftIO getCurrentTime

            let ticket_old_claiming = TicketOldClaiming
                    ticketClaimingTs
                    ticketClaimingUser
                    ticketClaimingTicket
                    ticketClaimingNote
                    release_note
                    now

            ticket_old_claiming_id <- lift $ insert ticket_old_claiming

            lift $
                update $ \etc -> do
                set etc
                    [ EventTicketClaimedClaim    =. val Nothing
                    , EventTicketClaimedOldClaim =.
                        val (Just ticket_old_claiming_id) ]
                where_ $
                    etc ^. EventTicketClaimedClaim
                        ==. val (Just ticket_claiming_id)

            lift $
                delete $
                from $ \tc ->
                where_ $ tc ^. TicketClaimingId ==. val ticket_claiming_id

            tell [ ETicketUnclaimed ticket_old_claiming_id ticket_old_claiming ]

-- | Fetch a comment from the DB, subject to viewing permissions.
userUnclaimCommentDB :: CommentId -> Maybe Text -> SDB ()
userUnclaimCommentDB comment_id release_note = do
    maybe_ticket_claiming_entity <-
        lift $ getBy $ UniqueTicketClaiming comment_id
    case maybe_ticket_claiming_entity of
        Nothing -> return ()
        Just (Entity ticket_claiming_id TicketClaiming{..}) -> do
            now <- liftIO getCurrentTime

            let ticket_old_claiming = TicketOldClaiming
                    ticketClaimingTs
                    ticketClaimingUser
                    ticketClaimingTicket
                    ticketClaimingNote
                    release_note
                    now

            ticket_old_claiming_id <- lift $ insert ticket_old_claiming

            lift $
                update $ \etc -> do
                set etc
                    [ EventTicketClaimedClaim    =. val Nothing
                    , EventTicketClaimedOldClaim =.
                        val (Just ticket_old_claiming_id) ]
                where_ $
                    etc ^. EventTicketClaimedClaim
                        ==. val (Just ticket_claiming_id)

            lift $
                delete $
                from $ \tc ->
                where_ $ tc ^. TicketClaimingId ==. val ticket_claiming_id

            tell [ ETicketUnclaimed ticket_old_claiming_id ticket_old_claiming ]

-- | Fetch a comment from the DB, subject to viewing permissions.
userUnclaimCommentDB comment_id release_note = do
    maybe_ticket_claiming_entity <-
        lift $ getBy $ UniqueTicketClaiming comment_id
    case maybe_ticket_claiming_entity of
        Nothing -> return ()
        Just (Entity ticket_claiming_id TicketClaiming{..}) -> do
            now <- liftIO getCurrentTime

            let ticket_old_claiming = TicketOldClaiming
                    ticketClaimingTs
                    ticketClaimingUser
                    ticketClaimingTicket
                    ticketClaimingNote
                    release_note
                    now

            ticket_old_claiming_id <- lift $ insert ticket_old_claiming

            lift $
                update $ \etc -> do
                set etc
                    [ EventTicketClaimedClaim    =. val Nothing
                    , EventTicketClaimedOldClaim =.
                        val (Just ticket_old_claiming_id) ]
                where_ $
                    etc ^. EventTicketClaimedClaim
                        ==. val (Just ticket_claiming_id)

            lift $
                delete $
                from $ \tc ->
                where_ $ tc ^. TicketClaimingId ==. val ticket_claiming_id

            tell [ ETicketUnclaimed ticket_old_claiming_id ticket_old_claiming ]

-- | Fetch a comment from the DB, subject to viewing permissions.