Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implements suspended state with subtyping #4

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package eu.thoughtway.fsm.fp.domain.model

import java.time.LocalDateTime

final case class Organisation[Status](
final case class Organisation[+Status](
id: String,
created: LocalDateTime,
status: Status,
Expand All @@ -16,15 +16,22 @@ object Organisation {
sealed trait Defined extends Status
case object Defined extends Defined

sealed trait Enabled extends Status
sealed trait EnabledOrSuspended extends Status
sealed trait EnabledOrDisabled extends Status

sealed trait Enabled extends EnabledOrSuspended with EnabledOrDisabled
case object Enabled extends Enabled

sealed trait Disabled extends Status
sealed trait Suspended extends EnabledOrSuspended
case object Suspended extends Suspended

sealed trait Disabled extends EnabledOrDisabled
case object Disabled extends Disabled

type OrganisationDefined = Organisation[Defined]
type OrganisationEnabled = Organisation[Enabled]
type OrganisationDisabled = Organisation[Disabled]
type OrganisationDefined = Organisation[Defined]
type OrganisationEnabled = Organisation[Enabled]
type OrganisationSuspended = Organisation[Suspended]
type OrganisationDisabled = Organisation[Disabled]

def apply(id: String): OrganisationDefined =
new Organisation(id, LocalDateTime.now(), Defined, 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@ trait OrganisationService {
def credit(org: OrganisationEnabled)(
amount: BigDecimal
): OrganisationEnabled
def debit(org: OrganisationEnabled)(
def debit(org: Organisation[EnabledOrSuspended])(
amount: BigDecimal
): OrganisationEnabled
): Organisation[EnabledOrSuspended]
def disable(org: OrganisationEnabled): OrganisationDisabled

def suspend(org: Organisation[EnabledOrDisabled]): OrganisationSuspended
def resume(org: OrganisationSuspended): OrganisationEnabled

def expedite(id: String)(amount: BigDecimal): OrganisationEnabled =
(define _ andThen approve _ andThen credit)(id)(amount)
}
Expand All @@ -31,11 +34,19 @@ trait OrganisationServiceImpl extends OrganisationService {
): OrganisationEnabled =
org.copy(balance = org.balance + amount)

override def debit(org: OrganisationEnabled)(
override def debit(org: Organisation[EnabledOrSuspended])(
amount: BigDecimal
): OrganisationEnabled =
): Organisation[EnabledOrSuspended] =
org.copy(balance = org.balance - amount)

override def suspend(
org: Organisation[EnabledOrDisabled]
): OrganisationSuspended =
org.copy(status = Suspended)

override def resume(org: OrganisationSuspended): OrganisationEnabled =
org.copy(status = Enabled)

override def disable(org: OrganisationEnabled): OrganisationDisabled =
org.copy(status = Disabled)
}