Your first process
This page builds a process that pays a claim on an insurance policy. It appraises the claim, asks an adjuster to approve it when it is over the appraisal, and pays it.
The whole process
import { act, choice, party, process, wait } from '@trestlerun/process';
// A claim on a policy, for an amount in cents.
export type Claim = { policy: string; amount: number };
// The bank's payments, by the key each was made under.
export const payments = new Map<string, number>();
const surveyor = {
appraise: (policy: string) => policy.length * 1_000,
};
// The bank makes one payment per key, however often it is asked.
const bank = {
pay(amount: number, key: string) {
payments.set(key, amount);
},
};
const adjuster = party({ name: 'adjuster' });
// Appraise a claim, have a person approve it if it is over the appraisal, then pay
// it.
export const claim = process({
name: 'claim',
body: async ({ policy, amount }: Claim) => {
const value = await act({
name: 'Appraise the claim',
body: () => surveyor.appraise(policy),
});
const isOver = await choice({
name: 'Over the appraisal?',
condition: amount > value,
});
if (isOver) {
const review = await wait({
name: 'Adjuster approves',
party: adjuster,
window: '3 days',
});
const isApproved = await choice({
name: 'Approved?',
condition: review.isAnswered && review.answer === true,
});
if (!isApproved) {
return 'refused';
}
}
await act({
name: 'Pay the claim',
body: ({ actKey }) => bank.pay(amount, actKey),
});
return 'paid';
},
});import { act, choice, party, process, wait } from '@trestlerun/process';
// A claim on a policy, for an amount in cents.
export type Claim = { policy: string; amount: number };
// The bank's payments, by the key each was made under.
export const payments = new Map<string, number>();
const surveyor = {
appraise: (policy: string) => policy.length * 1_000,
};
// The bank makes one payment per key, however often it is asked.
const bank = {
pay(amount: number, key: string) {
payments.set(key, amount);
},
};
const adjuster = party({ name: 'adjuster' });
// Appraise a claim, have a person approve it if it is over the
// appraisal, then pay it.
export const claim = process({
name: 'claim',
body: async ({ policy, amount }: Claim) => {
const value = await act({
name: 'Appraise the claim',
body: () => surveyor.appraise(policy),
});
const isOver = await choice({
name: 'Over the appraisal?',
condition: amount > value,
});
if (isOver) {
const review = await wait({
name: 'Adjuster approves',
party: adjuster,
window: '3 days',
});
const isApproved = await choice({
name: 'Approved?',
condition: review.isAnswered && review.answer === true,
});
if (!isApproved) {
return 'refused';
}
}
await act({
name: 'Pay the claim',
body: ({ actKey }) => bank.pay(amount, actKey),
});
return 'paid';
},
});import {
act,
choice,
party,
process,
wait,
} from '@trestlerun/process';
// A claim on a policy, for an amount in cents.
export type Claim = { policy: string; amount: number };
// The bank's payments, by the key each was made under.
export const payments = new Map<string, number>();
const surveyor = {
appraise: (policy: string) => policy.length * 1_000,
};
// The bank makes one payment per key, however often it is
// asked.
const bank = {
pay(amount: number, key: string) {
payments.set(key, amount);
},
};
const adjuster = party({ name: 'adjuster' });
// Appraise a claim, have a person approve it if it is over
// the appraisal, then pay it.
export const claim = process({
name: 'claim',
body: async ({ policy, amount }: Claim) => {
const value = await act({
name: 'Appraise the claim',
body: () => surveyor.appraise(policy),
});
const isOver = await choice({
name: 'Over the appraisal?',
condition: amount > value,
});
if (isOver) {
const review = await wait({
name: 'Adjuster approves',
party: adjuster,
window: '3 days',
});
const isApproved = await choice({
name: 'Approved?',
condition:
review.isAnswered && review.answer === true,
});
if (!isApproved) {
return 'refused';
}
}
await act({
name: 'Pay the claim',
body: ({ actKey }) => bank.pay(amount, actKey),
});
return 'paid';
},
});import {
act,
choice,
party,
process,
wait,
} from '@trestlerun/process';
// A claim on a policy, for an amount in cents.
export type Claim = {
policy: string;
amount: number;
};
// The bank's payments, by the key each was made
// under.
export const payments = new Map<string, number>();
const surveyor = {
appraise: (policy: string) =>
policy.length * 1_000,
};
// The bank makes one payment per key, however
// often it is asked.
const bank = {
pay(amount: number, key: string) {
payments.set(key, amount);
},
};
const adjuster = party({ name: 'adjuster' });
// Appraise a claim, have a person approve it if
// it is over the appraisal, then pay it.
export const claim = process({
name: 'claim',
body: async ({ policy, amount }: Claim) => {
const value = await act({
name: 'Appraise the claim',
body: () => surveyor.appraise(policy),
});
const isOver = await choice({
name: 'Over the appraisal?',
condition: amount > value,
});
if (isOver) {
const review = await wait({
name: 'Adjuster approves',
party: adjuster,
window: '3 days',
});
const isApproved = await choice({
name: 'Approved?',
condition:
review.isAnswered &&
review.answer === true,
});
if (!isApproved) {
return 'refused';
}
}
await act({
name: 'Pay the claim',
body: ({ actKey }) =>
bank.pay(amount, actKey),
});
return 'paid';
},
});import {
act,
choice,
party,
process,
wait,
} from '@trestlerun/process';
// A claim on a policy, for an amount in
// cents.
export type Claim = {
policy: string;
amount: number;
};
// The bank's payments, by the key each
// was made under.
export const payments = new Map<
string,
number
>();
const surveyor = {
appraise: (policy: string) =>
policy.length * 1_000,
};
// The bank makes one payment per key,
// however often it is asked.
const bank = {
pay(amount: number, key: string) {
payments.set(key, amount);
},
};
const adjuster = party({
name: 'adjuster',
});
// Appraise a claim, have a person approve
// it if it is over the appraisal, then
// pay it.
export const claim = process({
name: 'claim',
body: async ({
policy,
amount,
}: Claim) => {
const value = await act({
name: 'Appraise the claim',
body: () =>
surveyor.appraise(policy),
});
const isOver = await choice({
name: 'Over the appraisal?',
condition: amount > value,
});
if (isOver) {
const review = await wait({
name: 'Adjuster approves',
party: adjuster,
window: '3 days',
});
const isApproved = await choice({
name: 'Approved?',
condition:
review.isAnswered &&
review.answer === true,
});
if (!isApproved) {
return 'refused';
}
}
await act({
name: 'Pay the claim',
body: ({ actKey }) =>
bank.pay(amount, actKey),
});
return 'paid';
},
});import {
act,
choice,
party,
process,
wait,
} from '@trestlerun/process';
// A claim on a policy, for an
// amount in cents.
export type Claim = {
policy: string;
amount: number;
};
// The bank's payments, by the key
// each was made under.
export const payments = new Map<
string,
number
>();
const surveyor = {
appraise: (policy: string) =>
policy.length * 1_000,
};
// The bank makes one payment per
// key, however often it is asked.
const bank = {
pay(amount: number, key: string) {
payments.set(key, amount);
},
};
const adjuster = party({
name: 'adjuster',
});
// Appraise a claim, have a person
// approve it if it is over the
// appraisal, then pay it.
export const claim = process({
name: 'claim',
body: async ({
policy,
amount,
}: Claim) => {
const value = await act({
name: 'Appraise the claim',
body: () =>
surveyor.appraise(policy),
});
const isOver = await choice({
name: 'Over the appraisal?',
condition: amount > value,
});
if (isOver) {
const review = await wait({
name: 'Adjuster approves',
party: adjuster,
window: '3 days',
});
const isApproved =
await choice({
name: 'Approved?',
condition:
review.isAnswered &&
review.answer === true,
});
if (!isApproved) {
return 'refused';
}
}
await act({
name: 'Pay the claim',
body: ({ actKey }) =>
bank.pay(amount, actKey),
});
return 'paid';
},
});import {
act,
choice,
party,
process,
wait,
} from '@trestlerun/process';
// A claim on a policy, for an
// amount in cents.
export type Claim = {
policy: string;
amount: number;
};
// The bank's payments, by the
// key each was made under.
export const payments =
new Map<string, number>();
const surveyor = {
appraise: (
policy: string,
) => policy.length * 1_000,
};
// The bank makes one payment
// per key, however often it
// is asked.
const bank = {
pay(
amount: number,
key: string,
) {
payments.set(key, amount);
},
};
const adjuster = party({
name: 'adjuster',
});
// Appraise a claim, have a
// person approve it if it is
// over the appraisal, then
// pay it.
export const claim = process({
name: 'claim',
body: async ({
policy,
amount,
}: Claim) => {
const value = await act({
name: 'Appraise the claim',
body: () =>
surveyor.appraise(
policy,
),
});
const isOver =
await choice({
name: 'Over the appraisal?',
condition:
amount > value,
});
if (isOver) {
const review =
await wait({
name: 'Adjuster approves',
party: adjuster,
window: '3 days',
});
const isApproved =
await choice({
name: 'Approved?',
condition:
review.isAnswered &&
review.answer ===
true,
});
if (!isApproved) {
return 'refused';
}
}
await act({
name: 'Pay the claim',
body: ({ actKey }) =>
bank.pay(
amount,
actKey,
),
});
return 'paid';
},
});Statements
Each act, choice and wait is a statement. Trestle saves each one before it starts and keeps its result, so after a crash the run reads back what it already did instead of doing it again.
- Appraise the claim is an act. Its body calls the surveyor, and its result is kept.
- Over the appraisal? is a choice. Its condition decides which way the run goes.
- Adjuster approves is a wait. The run stops until the adjuster answers or three days pass.
- Pay the claim is an act whose body passes its act key to the bank, so the bank makes the payment once however many times the act is tried.
Ends
The body returns 'paid' or 'refused', the names of the process’s two ends. A run that fails ends as failed, which the graph draws without your writing it.