how should I design status field for an enitity

Issue

I’m currently working on a hosting project so this is how I design an entity for storing hosting information.

// entity design for storing hosting instance
data class HostingInstance(
    val hostingId: String,
    val customerId: String,
    val active: Boolean,
    val status: HostingStatus,
    ...
)
// enum class for status
enum class HostingStatus {
    CREATION_PENDING,
    CREATION_SUCCESS,
    CREATION_FAILED,
    RENEW_PENDING,
    RENEW_SUCCESS,
    RENEW_FAILED,
    SUSPEND_PENDING,
    SUSPEND_SUCCESS,
    SUSPEND_FAILED,
    UPGRADE_PENDING,
    UPGRADE_SUCCESS,
    UPGRADE_FAILED,
    ...
}

As you can see there are a lot of status that I have to store. On the front-end side they also have a fixed status icon such as active, suspend, fail (upgrade_failed and renew_failed are also active) and I feel bad for them because they have to map all my enum status to their icon which is a really big and hard work. So my question is how should I design my status field so the frontend team can easily use?

Solution

You could either include a new attribute in your response to the frontend with this “active, suspend, fail” information. Another option would be to replace the HostingStatus with a HostingSimpleStatus in your response to the frontend. Something like the following:

enum class HostingSimpleStatus {
    ACTIVE,
    SUSPEND,
    FAIL
}

Then your HostingStatus may include the mapping as follows:

enum class HostingStatus(val simpleStatus: HostingSimpleStatus) {

    CREATION_PENDING (SUSPEND),
    CREATION_SUCCESS (ACTIVE),
    CREATION_FAILED (FAIL),
    (...)
}

With this, you would include all the logic in one single place.

Answered By – João Dias

Leave a Comment