Skip to main content

Xavier-Alice Agreement

name: Xavier-Alice Agreement

agreed:
description: Whether Alice has agreed to the terms
type: Boolean

eventMonitor:
description: MyOS Event Monitor
type: MyOS Agent

approvedAccounts:
description: List of accounts approved by Alice
type: List
itemType: MyOS Account

monitoringTimeline:
description: Timeline where monitored events are published
type: MyOS Timeline

payment:
description: Payment details for the monitoring service
type: Recurring Payment

contracts:
aliceChannel:
type: MyOS Timeline Channel
account: alice@example.com

xavierChannel:
type: MyOS Timeline Channel
account: xavier@example.com

monitorAgentChannel:
type: MyOS Agent Channel

# Enable processing of embedded documents
embedded:
type: Process Embedded
paths:
- /payment

agreementSetter:
type: Field Setter
fieldPath: /agreed
operationName: setAgreement
channel: aliceChannel
fieldType: Boolean
validationExpression: event === true
validationErrorMessage: "You must agree to continue"

eventMonitorSetter:
type: Field Setter
fieldPath: /eventMonitor
operationName: setEventMonitor
channel: aliceChannel
fieldType: MyOS Agent

approvedAccountsSetter:
type: Field Setter
fieldPath: /approvedAccounts
operationName: setApprovedAccounts
channel: aliceChannel
fieldType: List

paymentSetter:
type: Field Setter
fieldPath: /payment
operationName: setPayment
channel: aliceChannel
fieldType: Recurring Payment

monitorChannelUpdater:
type: Sequential Workflow
channel: Document Update
event:
type: Document Update
path: /eventMonitor
steps:
- name: UpdateMonitorChannel
type: Update Document
changeset:
- op: replace
path: /contracts/monitorAgentChannel
val: ${event.after}

monitorActivationTracker:
type: Sequential Workflow
channel: monitorAgentChannel
event:
type: MyOS Monitor Activated
steps:
- name: SaveMonitoringTimeline
type: Update Document
changeset:
- op: replace
path: /monitoringTimeline
val: ${event.timeline}

monitorTerminationTracker:
type: Sequential Workflow
channel: monitorAgentChannel
event:
type: MyOS Monitor Terminated
steps:
- name: TriggerTerminationNotification
type: Trigger Event
event:
type: Monitor Termination Notification
message: "The event monitor has been terminated."

eventsAnnouncer:
type: Event Action Trigger
events:
- on:
type: Document Processing Initiated
event:
type: Inform User About Pending Action
operation: setAgreement
title: Agreement Required
message: Alice, please agree to the terms to continue with the setup process.
participant: Alice
expectedRequest: true

- on:
type: Document Update
path: /agreed
event:
type: Inform User About Pending Action
operation: setEventMonitor
title: Event Monitor Setup
message: Alice, please provide a MyOS Event Monitor to complete the setup.
participant: Alice
expectedRequest:
type: MyOS Agent
initiationDocument:
type: MyOS Event Monitor
agent:
agentId: 1234 # Online Lessons Agent
eventPatterns:
- pattern:
description: "Booking events from approved accounts"
rules:
- path: "/type"
matchAny: ["Booking Created", "Booking Cancelled"] # type blueIds in real life
- path: "/contracts/student"
matchAny: ${document('/allowedAccounts')}
authorizedAccounts:
- account:
accountId: 456 # Xavier
- account:
email: myos@stripe.com # Stripe
allowedAccounts:
description: Accounts from which we want to be getting data about the booking changes
type: List
itemType: MyOS Account
contracts:
accountsSourceChannel:
type: MyOS Agent Channel
agentId: ${agent.currentAgentId}
accountsUpdater:
type: Sequential Workflow
channel: accountsSourceChannel
event:
type: Allowed Accounts Changed
steps:
- name: UpdateAllowedAccounts
type: Update Document
changeset:
- op: replace
path: /allowedAccounts
val: ${event.accounts}

- on:
type: Document Update
path: /approvedAccounts
event:
type: Allowed Accounts Changed
accounts: ${event.after}

# New event to prompt for payment setup once timeline is set
- on:
type: Document Update
path: /monitoringTimeline
event:
type: Inform User About Pending Action
operation: setPayment
title: Payment Setup Required
message: Alice, now that the monitor is activated, please set up a recurring payment.
participant: Alice
expectedRequest:
name: Recurring Payment for Adwords promo to Xavier
type: Recurring Stripe Payment

balance:
description: Current balance to be paid
type: Double
value: 0.00

xavierStripeAccount: 90852934852

contracts:

aliceChannel:
type: MyOS Timeline Channel
account: alice@example.com

xavierChannel:
type: MyOS Timeline Channel
account: xavier@example.com

eventsTimelineChannel:
type: MyOS Timeline Channel
account: ${document('/monitoringTimeline')}

bookingProcessor:
type: Sequential Workflow
channel: eventsTimelineChannel
event:
type: Booking Created
steps:
- name: ExtractAndAddAmount
type: JavaScript Code
code: |
// Extract price amount from booking event
const bookingAmount = event.price?.amount || 0;

// Return the amount to be added to balance
return {
amount: bookingAmount,
bookingId: event.id || "unknown"
};

- name: UpdateBalance
type: Update Document
changeset:
- op: replace
path: /balance
val: ${document('/balance') + steps.ExtractAndAddAmount.amount}

cancellationProcessor:
type: Sequential Workflow
channel: eventsTimelineChannel
event:
type: Booking Cancelled
steps:
- name: ExtractAndSubtractAmount
type: JavaScript Code
code: |
// Extract price amount from cancellation event
const bookingAmount = event.price?.amount || 0;

// Return the amount to be subtracted from balance
return {
amount: bookingAmount,
bookingId: event.id || "unknown"
};

- name: UpdateBalance
type: Update Document
changeset:
- op: replace
path: /balance
val: ${document('/balance') - steps.ExtractAndSubtractAmount.amount}

requestPayment:
type: Operation

requestPaymentImpl:
type: Sequential Workflow Operation
operation: requestPayment
channel: xavierChannel
steps:
- name: ValidateBalance
type: JavaScript Code
code: |
// Check if there's a balance to pay
const currentBalance = document('/balance');
if (currentBalance <= 0) {
throw new Error("No balance to pay. Current balance: $" + currentBalance);
}

return {
paymentAmount: currentBalance,
notes: event || ""
};

- name: TriggerPayment
type: Trigger Event
event:
type: Make Internal Stripe Payment
amount: ${steps.ValidateBalance.paymentAmount}
currency: USD
notes: ${steps.ValidateBalance.notes}
recipientStripeAccount: ${document('/xavierStripeAccount')}

- name: UpdatePaymentRecord
type: Update Document
changeset:
- op: replace
path: /balance
val: 0