Skip to content

Employee onboarding checklists

When a person joins, the App gives them a checklist of tasks. It counts the tasks as they are finished, and closes the checklist when the last one is. The example is one App of about 100 lines, a small people App that it imports, and one script that runs them on SQLite.

When What the App does
A person joins Makes a checklist for that person, with three tasks
A member of staff lists the checklists Shows every checklist, and the person each one is for
A task is finished Marks it done and adds 1 to the count on its checklist
The last task is finished Moves the checklist to complete
A checklist is still in progress after 9:00 Leaves a reminder on it that says how many tasks are left

You need the project from the Quickstart: Node.js 24, tsx and the packages it installs. Create people.ts, onboarding.ts and run.ts in the project, with the code under The whole App. Run the script:

Terminal window
PYN_ENV=local npx tsx run.ts

It prints:

Onboarding App is running in Workspace 8f5cf90a-2b71-5bef-91a6-d8714cbc7777
Clock: Wed, 01 Jan 2020 00:00:00 GMT
As Hana: Dan joined
As Bob: sees no checklist
As Bob: sees the task "Order a laptop", todo
As Bob: sees the task "Create an email account", todo
As Bob: sees the task "Book a welcome lunch", todo
As Bob: sees Dan's checklist, inProgress, 0 of 3 done
As Bob: "Order a laptop" is done
As Bob: sees Dan's checklist, inProgress, 0 of 3 done
As Bob: sees Dan's checklist, inProgress, 1 of 3 done
As Bob: "Create an email account" is done
As Bob: sees Dan's checklist, inProgress, 2 of 3 done
As Bob: "Book a welcome lunch" is done
As Bob: sees Dan's checklist, complete, 3 of 3 done
As Hana: Eve joined
As Bob: sees Dan's checklist, complete, 3 of 3 done
As Bob: sees Eve's checklist, inProgress, 0 of 3 done
Clock: Thu, 02 Jan 2020 00:00:00 GMT
As Bob: sees Dan's checklist, complete, 3 of 3 done
As Bob: sees Eve's checklist, inProgress, 0 of 3 done
As Bob: and its last reminder, "Tasks left: 3"

The script adds two people and gives each a Role, then makes every call as one of them. Hana is an administrator of the people App and adds the people who join. Bob is staff: he lists the checklists and finishes the tasks. Dan and Eve are person Items of the people App, and make no calls. The clock starts on 1 January 2020 and moves only when the script moves it.

Each part shows the lines of the App behind one row above, and what they print.

people.ts
const join = app.addMutation('join', async (input: JoinInput, current, ctx) => {
const workspace = await ctx.getWorkspaceItem();
const item = await workspace.createChild(current.templates.person, {
properties: { name: input.name },
});
await item.events.joined.publish({});
return item;
});

join belongs to the people App. It saves the person and publishes the joined Event. The people App knows nothing about checklists.

onboarding.ts
const people = app.addDependency('people', peopleApp);
const person = people.templates.person;

addDependency imports the people App and returns a handle on it. person is its Template, and onboarding.ts uses it like a Template of its own.

onboarding.ts
person.events.joined.addListener('createChecklist', async (_event, joiner, _current, ctx) => {
const templates = ctx.apps.onboarding.templates;
const created = await joiner.createChild(templates.checklist, {
properties: { done: 0, total: titles.length },
});
const relation = created.relations.person;
if (relation.type === 'one') await relation.set(joiner);
for (const title of titles) {
await created.createChild(templates.task, { properties: { title } });
}
});

This Listener is on the imported App’s Event. It runs after join returns, when the queued work runs. It creates the checklist as a child of the person and points the checklist’s person Relation at the person. Then it creates the three tasks as children of the checklist.

Hana adds Dan. Bob sees no checklist until the script runs the queued work with harness.tickAll(). Then he sees its three tasks:

Clock: Wed, 01 Jan 2020 00:00:00 GMT
As Hana: Dan joined
As Bob: sees no checklist
As Bob: sees the task "Order a laptop", todo
As Bob: sees the task "Create an email account", todo
As Bob: sees the task "Book a welcome lunch", todo
onboarding.ts
const checklist = app.addTemplate('checklist');
checklist.addProperty('done', 'integer');
checklist.addProperty('total', 'integer');
checklist.addProperty('lastReminder', 'string');
checklist.addRelation('person', 'many-to-one', person, { toKey: 'checklists' });

A checklist holds two counts and its last reminder. Its Relation points at a person, and many-to-one says many checklists point at one person. A checklist calls the Relation person, and a person calls it checklists.

onboarding.ts
const everyStaff = async () => [staff.prn];
const checklistStaff = checklist.addRole('staff', everyStaff);
checklist.addGrant(checklistStaff, Object.values(checklist.properties));
const taskStaff = task.addRole('staff', everyStaff);
task.addGrant(taskStaff, Object.values(task.properties));
const personStaff = person.addRole('staff', everyStaff);
person.addGrant(personStaff, Object.values(person.properties));

A Template Role says who holds it for each Item, and a Grant shows the Item to the people who hold that Role. everyStaff returns the staff Role, so every member of staff sees every checklist and every task.

The last two lines are a Role and a Grant on the imported Template. The people App shows a person to its own administrators, and Bob is not one. With this Grant, staff see the person too.

The list Query is granted to staff, so the Runtime refuses a call from anyone else. In run.ts, Bob calls it and follows the Relation from each checklist:

run.ts
for (const checklist of checklists) {
const person = await checklist.relations.person.get();
const { done, lastReminder, total } = checklist.properties;
const progress = `${checklist.state.value}, ${done} of ${total} done`;
console.log(`As Bob: sees ${person.properties.name}'s checklist, ${progress}`);
if (typeof lastReminder === 'string') {
console.log(`As Bob: and its last reminder, "${lastReminder}"`);
}
}

get() returns the person the checklist points at, and Bob reads the name:

As Bob: sees Dan's checklist, inProgress, 0 of 3 done
onboarding.ts
const finish = app.addMutation('finish', async (input: IdInput, current) => {
const item = await current.templates.task.getItem(input.id);
if (item === undefined) throw new Error('Task not found');
const transition = item.state?.transitions.finish;
if (transition === undefined) throw new Error(`"${item.properties.title}" is already done`);
const updated = await transition.execute({});
await updated.events.finished.publish({});
return updated;
});

A task has two states, todo and done. finish executes the finish transition, which leads from todo to done, then publishes the finished Event. A done task has no finish transition, so the App refuses a second call with its own message, and no task counts twice.

onboarding.ts
const finished = task.addEvent('finished');
checklist.addParentListener('countDone', finished, async (_event, parent) => {
const { done, total } = parent.properties;
if (typeof done !== 'number') return;
const updated = await parent.updateProperties({ done: done + 1 });
const transition = updated.state?.transitions.complete;
if (done + 1 === total) await transition?.execute({});
});

addParentListener registers a Listener on the checklist for an Event published on one of its tasks. countDone reads the count on the checklist and writes back one more. It runs when the queued work runs. After Bob finishes the first task the count is still 0, and it is 1 after harness.tickAll():

As Bob: "Order a laptop" is done
As Bob: sees Dan's checklist, inProgress, 0 of 3 done
As Bob: sees Dan's checklist, inProgress, 1 of 3 done
As Bob: "Create an email account" is done
As Bob: sees Dan's checklist, inProgress, 2 of 3 done
onboarding.ts
const inProgress = checklist.addState('inProgress').setDefault();
const complete = checklist.addState('complete');
inProgress.addTransition('complete', complete);

A checklist is inProgress until its one transition, complete, is executed. No Mutation executes it. The last two lines of countDone do:

onboarding.ts
const transition = updated.state?.transitions.complete;
if (done + 1 === total) await transition?.execute({});

total is the number of tasks the checklist was created with. When the new count reaches it, the Listener that wrote the count also executes the transition.

As Bob: "Book a welcome lunch" is done
As Bob: sees Dan's checklist, complete, 3 of 3 done

A checklist is still in progress after 9:00

Section titled “A checklist is still in progress after 9:00”
onboarding.ts
const reminderDue = checklist.addEvent('reminderDue');
reminderDue.addSchedule('daily', '0 9 * * *').state(['inProgress']);
reminderDue.addListener('remind', async (_event, item) => {
const { done, total } = item.properties;
if (typeof done !== 'number' || typeof total !== 'number') return;
const lastReminder = `Tasks left: ${total - done}`;
await item.updateProperties({ lastReminder });
});

0 9 * * * is a crontab: 9:00 every day. state(['inProgress']) keeps the schedule to checklists in progress. When the queued work runs and 9:00 has passed since the last run, the Runtime publishes reminderDue once for each of those checklists. The Listener writes the number of tasks left.

The schedule starts the first time the queued work runs, which is at midnight in this script. Eve joins the same day, and the script moves the clock one day:

run.ts
// Eve joins, and nobody touches her checklist.
await join('Eve');
await harness.tickAll();
await showChecklists();
// Move the clock one day, past 9:00, then run the queued work.
await harness.tickAll({ days: 1 });

Eve’s checklist is in progress and gets a reminder. Dan’s is complete and gets none:

As Hana: Eve joined
As Bob: sees Dan's checklist, complete, 3 of 3 done
As Bob: sees Eve's checklist, inProgress, 0 of 3 done
Clock: Thu, 02 Jan 2020 00:00:00 GMT
As Bob: sees Dan's checklist, complete, 3 of 3 done
As Bob: sees Eve's checklist, inProgress, 0 of 3 done
As Bob: and its last reminder, "Tasks left: 3"
people.ts
people.ts
import type { AppSpec } from '@alpynehq/alpyne-types/builder';
interface JoinInput {
readonly name: string;
}
export const peopleApp: AppSpec = (app) => {
app.configure('people');
const admin = app.addRole('admin', { label: 'People administrator' });
const person = app.addTemplate('person');
person.addProperty('name', 'string');
person.addEvent('joined');
const admins = person.addRole('admins', async () => [admin.prn]);
person.addGrant(admins, Object.values(person.properties));
const join = app.addMutation('join', async (input: JoinInput, current, ctx) => {
const workspace = await ctx.getWorkspaceItem();
const item = await workspace.createChild(current.templates.person, {
properties: { name: input.name },
});
await item.events.joined.publish({});
return item;
});
join.input.addProperty('name', 'string').mandatory();
join.output.item(person).mandatory();
join.addGrant(admin);
};
onboarding.ts
onboarding.ts
import type { AppSpec } from '@alpynehq/alpyne-types/builder';
import { peopleApp } from './people';
interface IdInput {
readonly id: string;
}
const titles = ['Order a laptop', 'Create an email account', 'Book a welcome lunch'];
export const onboardingApp: AppSpec = (app) => {
app.configure('onboarding');
const people = app.addDependency('people', peopleApp);
const person = people.templates.person;
const staff = app.addRole('staff', { label: 'Staff' });
const checklist = app.addTemplate('checklist');
checklist.addProperty('done', 'integer');
checklist.addProperty('total', 'integer');
checklist.addProperty('lastReminder', 'string');
checklist.addRelation('person', 'many-to-one', person, { toKey: 'checklists' });
const task = app.addTemplate('task');
task.addProperty('title', 'string');
const everyStaff = async () => [staff.prn];
const checklistStaff = checklist.addRole('staff', everyStaff);
checklist.addGrant(checklistStaff, Object.values(checklist.properties));
const taskStaff = task.addRole('staff', everyStaff);
task.addGrant(taskStaff, Object.values(task.properties));
const personStaff = person.addRole('staff', everyStaff);
person.addGrant(personStaff, Object.values(person.properties));
const inProgress = checklist.addState('inProgress').setDefault();
const complete = checklist.addState('complete');
inProgress.addTransition('complete', complete);
const todo = task.addState('todo').setDefault();
const done = task.addState('done');
todo.addTransition('finish', done);
person.events.joined.addListener('createChecklist', async (_event, joiner, _current, ctx) => {
const templates = ctx.apps.onboarding.templates;
const created = await joiner.createChild(templates.checklist, {
properties: { done: 0, total: titles.length },
});
const relation = created.relations.person;
if (relation.type === 'one') await relation.set(joiner);
for (const title of titles) {
await created.createChild(templates.task, { properties: { title } });
}
});
const finished = task.addEvent('finished');
checklist.addParentListener('countDone', finished, async (_event, parent) => {
const { done, total } = parent.properties;
if (typeof done !== 'number') return;
const updated = await parent.updateProperties({ done: done + 1 });
const transition = updated.state?.transitions.complete;
if (done + 1 === total) await transition?.execute({});
});
const reminderDue = checklist.addEvent('reminderDue');
reminderDue.addSchedule('daily', '0 9 * * *').state(['inProgress']);
reminderDue.addListener('remind', async (_event, item) => {
const { done, total } = item.properties;
if (typeof done !== 'number' || typeof total !== 'number') return;
const lastReminder = `Tasks left: ${total - done}`;
await item.updateProperties({ lastReminder });
});
const finish = app.addMutation('finish', async (input: IdInput, current) => {
const item = await current.templates.task.getItem(input.id);
if (item === undefined) throw new Error('Task not found');
const transition = item.state?.transitions.finish;
if (transition === undefined) throw new Error(`"${item.properties.title}" is already done`);
const updated = await transition.execute({});
await updated.events.finished.publish({});
return updated;
});
finish.input.addProperty('id', 'id').mandatory();
finish.output.item(task).mandatory();
const list = app.addQuery('list', async (_input: unknown, current) => {
const checklists = current.templates.checklist.selectItems();
const page = await checklists.sort(['$createdAt asc']).execute();
return page.results;
});
list.output.item(checklist).array();
const tasks = app.addQuery('tasks', async (input: IdInput, current) => {
const item = await current.templates.checklist.getItem(input.id);
if (item === undefined) throw new Error('Checklist not found');
const page = await item.selectChildren().execute();
return page.results;
});
tasks.input.addProperty('id', 'id').mandatory();
tasks.output.item(task).array();
finish.addGrant(staff);
list.addGrant(staff);
tasks.addGrant(staff);
};
run.ts
run.ts
import type { WriteRuntimeInstance } from '@alpynehq/alpyne-types/runtime';
import { createSqliteKnexDatabase } from '@pyn-backend/data-knex-sqlite';
import { createHarnessInstance } from '@pyn-backend/platform-harness-public';
import { createSqliteInstance } from '@pyn-backend/platform-reactor-sqlite';
import { createSqlitePlatformService } from '@pyn-backend/platform-service-sqlite';
import { createTestConfigurationService } from '@pyn-backend/service-configuration-test';
import { createTestCronService } from '@pyn-backend/service-cron-test';
import { createTestSystemService } from '@pyn-backend/service-system-test';
import { createTestDynamicConfigService } from '@pyn-backend/test-services-dynamic-config';
import { createTestLoggerService } from '@pyn-backend/test-services-logger';
import { onboardingApp } from './onboarding';
// A SQLite database, held in memory, with the Alpyne tables.
const knex = await createSqliteKnexDatabase(':memory:');
await knex.current.migrate.latest();
// The services a Runtime depends on.
const system = createTestSystemService();
const services = {
configuration: createTestConfigurationService(),
cron: createTestCronService({ system }),
dynamicConfig: createTestDynamicConfigService(),
knex,
logger: createTestLoggerService(),
system,
};
// The SQLite Runtime with the App and the people App it imports, and a Workspace to hold their data.
const platform = createSqlitePlatformService(services);
const reactor = createSqliteInstance({ ...services, platform }, {});
const runtime = { ...services, platform, reactor };
const harness = await createHarnessInstance(runtime, onboardingApp);
const workspace = await harness.createWorkspace();
console.log(`Onboarding App is running in Workspace ${workspace.workspaceId}`);
// Add the two people who make calls. Hana is an administrator of the people App and Bob is staff.
const callers = await workspace.write(async (ctx) => {
const root = await ctx.getWorkspaceItem();
const user = ctx.apps.identity.templates.user;
const templatePrn = 'prn::identity:template/user';
const hana = await user.createItem(root, { properties: { displayName: 'Hana' } });
const bob = await user.createItem(root, { properties: { displayName: 'Bob' } });
await ctx.apps.people.roles.admin.assign({ id: hana.id, templatePrn });
await ctx.apps.onboarding.roles.staff.assign({ id: bob.id, templatePrn });
return { bob: bob.id, hana: hana.id };
});
// Run a call as one person.
const as = async <T>(id: string, fn: (ctx: WriteRuntimeInstance) => Promise<T>) => {
const instance = await platform.createInstance({
identityId: crypto.randomUUID(),
principalId: id,
rolePrns: [],
sessionId: crypto.randomUUID(),
type: 'principal',
workspaceId: workspace.workspaceId,
});
return await instance.execute('readwrite', { payload: {}, traceId: 'example' }, fn);
};
// Add a person to the people App as Hana.
const join = async (name: string) => {
const person = await as(callers.hana, async (ctx) => {
const people = ctx.apps.people;
return await people.mutations.join.handler({ name }, people, ctx);
});
console.log(`As Hana: ${person.properties.name} joined`);
};
// List the checklists Bob sees. Follow the Relation from each one to its person.
const showChecklists = async () => {
await as(callers.bob, async (ctx) => {
const onboarding = ctx.apps.onboarding;
const checklists = await onboarding.queries.list.handler({}, onboarding, ctx);
if (checklists.length === 0) console.log('As Bob: sees no checklist');
for (const checklist of checklists) {
const person = await checklist.relations.person.get();
const { done, lastReminder, total } = checklist.properties;
const progress = `${checklist.state.value}, ${done} of ${total} done`;
console.log(`As Bob: sees ${person.properties.name}'s checklist, ${progress}`);
if (typeof lastReminder === 'string') {
console.log(`As Bob: and its last reminder, "${lastReminder}"`);
}
}
});
};
// List the tasks of the first checklist as Bob.
const listTasks = async () => {
const tasks = await as(callers.bob, async (ctx) => {
const onboarding = ctx.apps.onboarding;
const [checklist] = await onboarding.queries.list.handler({}, onboarding, ctx);
return await onboarding.queries.tasks.handler({ id: checklist.id }, onboarding, ctx);
});
for (const task of tasks) {
console.log(`As Bob: sees the task "${task.properties.title}", ${task.state.value}`);
}
return tasks;
};
// Finish one task as Bob.
const finish = async (id: string) => {
const task = await as(callers.bob, async (ctx) => {
const onboarding = ctx.apps.onboarding;
return await onboarding.mutations.finish.handler({ id }, onboarding, ctx);
});
console.log(`As Bob: "${task.properties.title}" is ${task.state.value}`);
};
// Dan joins. His checklist is made when the queued work runs.
console.log(`Clock: ${system.current.toUTCString()}`);
await join('Dan');
await showChecklists();
await harness.tickAll();
const [laptop, email, lunch] = await listTasks();
await showChecklists();
// Finish the first task. Show the count before and after the queued work runs.
await finish(laptop.id);
await showChecklists();
await harness.tickAll();
await showChecklists();
// Finish the other two tasks.
await finish(email.id);
await harness.tickAll();
await showChecklists();
await finish(lunch.id);
await harness.tickAll();
await showChecklists();
// Eve joins, and nobody touches her checklist.
await join('Eve');
await harness.tickAll();
await showChecklists();
// Move the clock one day, past 9:00, then run the queued work.
await harness.tickAll({ days: 1 });
console.log(`Clock: ${system.current.toUTCString()}`);
await showChecklists();
await knex.destroy();