Type: "Lifecycle Instance"
Introduction
A lifecycle instance represents an ongoing instance of an OCS Lifecycle, in progress on a wallet or a subscription.
Note that a lifecycle instance cannot be acted on directly. A lifeycle can only be transitioned by triggering a transition event via the OCS engine (see the engine API for available methods).
API Methods
.state
Returns the current state ID (not the name of the state) this
instance of the lifecycle is in. This will never be nil
.
This method has no parameters.
...
local ocs = require "n2.n2ocs"
local wallet = ocs.context().active_wallet();
local lci = wallet.lifecycle_instance()
if (lci == nil) then
ocs.failed (400, "OCS wallet '" .. wallet.id() . "' lifecycle is missing");
return;
end
local state = lci.state()
...
The state returned is a string of the ID of the state. The
state details, along with the type of lifecycle, can be retrieved
using the below lifecycle()
method.
.set_transition_out_time
Set the transition out time for the current lifecycle state, for this particular instance of the lifecycle. This is useful if the transition out state needs to be dynamically updated based on actions taken on the wallet, of when the lifecycle state is entered.
A normal use case for this function is to set and update the expiry date of an wallet due to activation or usage.
Parameters:
Parameter | Type | Description |
---|---|---|
expiry |
Table or Number | If a table is provided, the table is an expiry description of when to trigger the expiry. The target expiry date/time is calculated from the expiry description. If a number is given, the assumption is that the number represents the seconds since the unix epoch time that should be used as the expiry date/time (and no further timezone manipulation is done on this value). |
Pass nil
if the transition out time should be cleared (if it is set)
local ocs = require "n2.n2ocs"
local f = function (context)
local expiry = { days = 100 }
local ok = context.active_lifecycle_instance().set_transition_out_time(expiry)
end
return ocs.create_lifecycle_action_handler(f)
This method returns true if the transition out time was able to be set on the lifecycle instance.
.lifecycle
Returns a reference to a table describing the lifecycle this lifecycle instance is an instance of. This is a description of the state machine that the lifecycle instance is following.
This method has no parameters.
...
local lci = wallet.lifecycle_instance()
local lifecycle = lci.lifecycle()
...
This method will never return nil
, and the lifecycle details
are returned as a table of data.
.delete
Triggers the (engine processed) deletion of the lifecycle instance associated with the lifecycle this is called on. This in turn triggers the removal of the owner of the lifecycle - whether it is a subscription or a wallet.
This method has no parameters.
local ocs = require "n2.n2ocs"
local f = function (context)
local ok = context.active_lifecycle_instance().delete()
end
return ocs.create_lifecycle_action_handler(f)
Note that this method will return an error if the delete()
method
is called on a lifecycle instance for an wallet that is not mutating
(i.e. is considered immutable). This would not be the case ever
if you’re using the active_lifecycle_instance()
method, but if
the delete()
is called on the wallet.lifecycle_instance()
before
enabling mutation, then it will not succeed.