Bee O`clock
  • ๐Ÿ“„Terms of use
  • ๐Ÿ”Privacy policy
  • ๐Ÿ“‘Documentation
    • Notification
      • Email
  • For programmers
    • Entity
      • State and State History
  • Customer and Order
Powered by GitBook
On this page
  • Overview ๐ŸŽฏ๐Ÿ“˜๐Ÿ’ก
  • State Property ๐Ÿ”„๐Ÿ“‚โš™๏ธ
  • State History Property ๐Ÿ“œ๐Ÿ•’๐Ÿ“Š
  • How It Works โšก๐Ÿ”„๐Ÿ”Ž
  • Changing State ๐Ÿ”งโš™๏ธ๐Ÿ› ๏ธ
  • Developer Guidelines ๐Ÿ“โœ…๐Ÿ› ๏ธ

Was this helpful?

  1. For programmers
  2. Entity

State and State History

Overview ๐ŸŽฏ๐Ÿ“˜๐Ÿ’ก

In the system, documents such as orders or customers are represented as JavaScript objects (e.g., {object: 'OrderDto'}). Each document contains a state property and a stateHistory property to track its lifecycle.

State Property ๐Ÿ”„๐Ÿ“‚โš™๏ธ

The state property represents the current status of a document. It can have one of the following values:

  • active โ€“ The document is currently in use and operational.

  • inactive โ€“ The document exists but is not currently active.

  • archived โ€“ The document is stored for historical reference but is not active.

  • deleted โ€“ The document is marked as removed but still exists in the system.

Important Note: โš ๏ธ๐Ÿ›‘๐Ÿ—‚๏ธ

Our system never deletes any data. Instead, when a document is marked as deleted, it means that the user will no longer see the document, but it remains stored in the database for historical and auditing purposes.

Example: ๐Ÿ“„๐Ÿ”โœ…

{
  "object": "CustomerDto",
  "state": "inactive",
  "stateHistory": [
    {"state": "active", "setAt": "2025-01-01T00:00:00.000Z"},
    {"state": "inactive", "setAt": "2025-01-02T00:00:00.000Z"}
  ]
}

State History Property ๐Ÿ“œ๐Ÿ•’๐Ÿ“Š

The stateHistory property is an array that tracks all state changes of the document. Each entry in the array contains:

  • state: The state the document was in.

  • setAt: The timestamp when the state was set (in ISO format).

Example: ๐Ÿ“‹โณ๐Ÿ“

{
  "object": "CustomerDto",
  "state": "inactive",
  "stateHistory": [
    {"state": "active", "setAt": "2025-01-01T00:00:00.000Z"},
    {"state": "inactive", "setAt": "2025-01-02T00:00:00.000Z"}
  ]
}

How It Works โšก๐Ÿ”„๐Ÿ”Ž

  • The state property always reflects the latest state of the document.

  • The stateHistory property logs all past state changes in chronological order.

  • Developers should ensure that every state change is recorded in the stateHistory array.

Changing State ๐Ÿ”งโš™๏ธ๐Ÿ› ๏ธ

When updating the state of a document, developers must:

  1. Append the previous state to the stateHistory array before updating the state property.

  2. Update the state property to the new state.

Example of Updating State in JavaScript: ๐Ÿ’ป๐Ÿ“Œ๐Ÿ–Š๏ธ

enum StateEnum {
    active = 'active',
    inactive = 'inactive',
    archived = 'archived',
    deleted = 'deleted',
}

namespace ICustomer {

    interface DTO {
    
        object: 'CustomerDto';
        state: StateEnum;
        stateHistory: {
            state: StateEnum;
            setAt: string;
        }[];
        
    }

}

function updateState(document: object, newState: StateEnum) {
    // Ensure the state is actually changing
    if (document.state !== newState) {
        // Add current state to stateHistory before updating
        document.stateHistory.push({
            state: newState,
            setAt: new Date().toISOString()
        });
        
        // Update the state to the new value
        document.state = newState;
    }
}

// Example usage:
const customer = {
    object: 'CustomerDto',
    state: 'active',
    stateHistory: [{state: 'active', setAt: '2025-01-01T00:00:00.000Z'}]
};

updateState(customer, StateEnum.inactive);
console.log(customer);

Expected Output: ๐Ÿ–ฅ๏ธโœ…๐Ÿ“„

{
  "object": "CustomerDto",
  "state": "inactive",
  "stateHistory": [
    {"state": "active", "setAt": "2025-01-01T00:00:00.000Z"},
    {"state": "inactive", "setAt": "2025-02-26T14:00:00.000Z"}
  ]
}

Developer Guidelines ๐Ÿ“โœ…๐Ÿ› ๏ธ

  • Always ensure state changes are meaningful and necessary.

  • Always append the previous state to stateHistory before updating the state property.

  • Use ISO 8601 format for timestamps.

  • Do not remove or alter previous stateHistory entries.

  • Handle state transitions in a controlled and documented manner to maintain system integrity.

  • Remember that deleted documents are not removed from the system; they are simply hidden from users.

By following these guidelines, developers can ensure a reliable and traceable state management system for documents. ๐ŸŽฏ๐Ÿš€๐Ÿ“˜

PreviousEntityNextCustomer and Order

Last updated 3 months ago

Was this helpful?