@hogsend/core 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +93 -0
- package/README.md +9 -0
- package/package.json +40 -0
- package/src/conditions/composite.ts +26 -0
- package/src/conditions/email-engagement.ts +33 -0
- package/src/conditions/evaluate.ts +34 -0
- package/src/conditions/event.ts +69 -0
- package/src/conditions/index.ts +3 -0
- package/src/conditions/property.ts +71 -0
- package/src/duration.ts +25 -0
- package/src/index.ts +16 -0
- package/src/registry/index.ts +43 -0
- package/src/schemas/index.ts +1 -0
- package/src/schemas/journey.schema.ts +88 -0
- package/src/types/conditions.ts +44 -0
- package/src/types/index.ts +3 -0
- package/src/types/journey-context.ts +80 -0
- package/src/types/journey.ts +33 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
Elastic License 2.0 (ELv2)
|
|
2
|
+
|
|
3
|
+
Copyright 2025 Doug Silkstone and Hogsend contributors
|
|
4
|
+
|
|
5
|
+
## Acceptance
|
|
6
|
+
|
|
7
|
+
By using the software, you agree to all of the terms and conditions below.
|
|
8
|
+
|
|
9
|
+
## Copyright License
|
|
10
|
+
|
|
11
|
+
The licensor grants you a non-exclusive, royalty-free, worldwide,
|
|
12
|
+
non-sublicensable, non-transferable license to use, copy, distribute, make
|
|
13
|
+
available, and prepare derivative works of the software, in each case subject to
|
|
14
|
+
the limitations and conditions below.
|
|
15
|
+
|
|
16
|
+
## Limitations
|
|
17
|
+
|
|
18
|
+
You may not provide the software to third parties as a hosted or managed
|
|
19
|
+
service, where the service provides users with access to any substantial set of
|
|
20
|
+
the features or functionality of the software.
|
|
21
|
+
|
|
22
|
+
You may not move, change, disable, or circumvent the license key functionality
|
|
23
|
+
in the software, and you may not remove or obscure any functionality in the
|
|
24
|
+
software that is protected by the license key.
|
|
25
|
+
|
|
26
|
+
You may not alter, remove, or obscure any licensing, copyright, or other notices
|
|
27
|
+
of the licensor in the software. Any use of the licensor's trademarks is subject
|
|
28
|
+
to applicable law.
|
|
29
|
+
|
|
30
|
+
## Patents
|
|
31
|
+
|
|
32
|
+
The licensor grants you a license, under any patent claims the licensor can
|
|
33
|
+
license, or becomes able to license, to make, have made, use, sell, offer for
|
|
34
|
+
sale, import and have imported the software, in each case subject to the
|
|
35
|
+
limitations and conditions in this license. This license does not cover any
|
|
36
|
+
patent claims that you cause to be infringed by modifications or additions to
|
|
37
|
+
the software. If you or your company make any written claim that the software
|
|
38
|
+
infringes or contributes to infringement of any patent, your patent license for
|
|
39
|
+
the software granted under these terms ends immediately. If your company makes
|
|
40
|
+
such a claim, your patent license ends immediately for work on behalf of your
|
|
41
|
+
company.
|
|
42
|
+
|
|
43
|
+
## Notices
|
|
44
|
+
|
|
45
|
+
You must ensure that anyone who gets a copy of any part of the software from you
|
|
46
|
+
also gets a copy of these terms.
|
|
47
|
+
|
|
48
|
+
If you modify the software, you must include in any modified copies of the
|
|
49
|
+
software prominent notices stating that you have modified the software.
|
|
50
|
+
|
|
51
|
+
## No Other Rights
|
|
52
|
+
|
|
53
|
+
These terms do not imply any licenses other than those expressly granted in
|
|
54
|
+
these terms.
|
|
55
|
+
|
|
56
|
+
## Termination
|
|
57
|
+
|
|
58
|
+
If you use the software in violation of these terms, such use is not licensed,
|
|
59
|
+
and your licenses will automatically terminate. If the licensor provides you
|
|
60
|
+
with a notice of your violation, and you cease all violation of this license no
|
|
61
|
+
later than 30 days after you receive that notice, your licenses will be
|
|
62
|
+
reinstated retroactively. However, if you violate these terms after such
|
|
63
|
+
reinstatement, any additional violation of these terms will cause your licenses
|
|
64
|
+
to terminate automatically and permanently.
|
|
65
|
+
|
|
66
|
+
## No Liability
|
|
67
|
+
|
|
68
|
+
*As far as the law allows, the software comes as is, without any warranty or
|
|
69
|
+
condition, and the licensor will not be liable to you for any damages arising out
|
|
70
|
+
of these terms or the use or nature of the software, under any kind of legal
|
|
71
|
+
claim.*
|
|
72
|
+
|
|
73
|
+
## Definitions
|
|
74
|
+
|
|
75
|
+
The **licensor** is the entity offering these terms, and the **software** is the
|
|
76
|
+
software the licensor makes available under these terms, including any portion
|
|
77
|
+
of it.
|
|
78
|
+
|
|
79
|
+
**you** refers to the individual or entity agreeing to these terms.
|
|
80
|
+
|
|
81
|
+
**your company** is any legal entity, sole proprietorship, or other kind of
|
|
82
|
+
organization that you work for, plus all organizations that have control over,
|
|
83
|
+
are under the control of, or are under common control with that organization.
|
|
84
|
+
**control** means ownership of substantially all the assets of an entity, or the
|
|
85
|
+
power to direct its management and policies by vote, contract, or otherwise.
|
|
86
|
+
Control can be direct or indirect.
|
|
87
|
+
|
|
88
|
+
**your licenses** are all the licenses granted to you for the software under
|
|
89
|
+
these terms.
|
|
90
|
+
|
|
91
|
+
**use** means anything you do with the software requiring one of your licenses.
|
|
92
|
+
|
|
93
|
+
**trademark** means trademarks, service marks, and similar rights.
|
package/README.md
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
# @hogsend/core
|
|
2
|
+
|
|
3
|
+
Core types, Zod schemas, the condition-evaluation engine, duration helpers, and
|
|
4
|
+
the `JourneyRegistry` for [Hogsend](https://github.com/dougwithseismic/hogsend) —
|
|
5
|
+
a code-first lifecycle orchestration engine for teams on PostHog + Resend.
|
|
6
|
+
|
|
7
|
+
This package ships raw TypeScript source; consumers bundle it via their own build
|
|
8
|
+
(tsup `noExternal`). See the repo docs for the full architecture and the
|
|
9
|
+
[release model](https://github.com/dougwithseismic/hogsend/blob/main/docs/RELEASING.md).
|
package/package.json
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@hogsend/core",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "https://github.com/dougwithseismic/hogsend.git",
|
|
9
|
+
"directory": "packages/core"
|
|
10
|
+
},
|
|
11
|
+
"sideEffects": false,
|
|
12
|
+
"main": "./src/index.ts",
|
|
13
|
+
"types": "./src/index.ts",
|
|
14
|
+
"exports": {
|
|
15
|
+
".": "./src/index.ts",
|
|
16
|
+
"./types": "./src/types/index.ts",
|
|
17
|
+
"./registry": "./src/registry/index.ts",
|
|
18
|
+
"./conditions": "./src/conditions/index.ts",
|
|
19
|
+
"./schemas": "./src/schemas/index.ts"
|
|
20
|
+
},
|
|
21
|
+
"files": [
|
|
22
|
+
"src",
|
|
23
|
+
"README.md"
|
|
24
|
+
],
|
|
25
|
+
"publishConfig": {
|
|
26
|
+
"access": "public"
|
|
27
|
+
},
|
|
28
|
+
"dependencies": {
|
|
29
|
+
"drizzle-orm": "^0.45.2",
|
|
30
|
+
"zod": "^4.4.3",
|
|
31
|
+
"@hogsend/db": "^0.0.1"
|
|
32
|
+
},
|
|
33
|
+
"devDependencies": {
|
|
34
|
+
"@types/node": "latest",
|
|
35
|
+
"@repo/typescript-config": "0.0.0"
|
|
36
|
+
},
|
|
37
|
+
"scripts": {
|
|
38
|
+
"check-types": "tsc --noEmit"
|
|
39
|
+
}
|
|
40
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { CompositeCondition } from "../types/index.js";
|
|
2
|
+
import type { ConditionContext } from "./evaluate.js";
|
|
3
|
+
import { evaluateCondition } from "./evaluate.js";
|
|
4
|
+
|
|
5
|
+
export async function evaluateCompositeCondition(opts: {
|
|
6
|
+
condition: CompositeCondition;
|
|
7
|
+
ctx: ConditionContext;
|
|
8
|
+
}): Promise<boolean> {
|
|
9
|
+
const { condition, ctx } = opts;
|
|
10
|
+
|
|
11
|
+
if (condition.operator === "and") {
|
|
12
|
+
for (const sub of condition.conditions) {
|
|
13
|
+
if (!(await evaluateCondition({ condition: sub, ctx }))) return false;
|
|
14
|
+
}
|
|
15
|
+
return true;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
if (condition.operator === "or") {
|
|
19
|
+
for (const sub of condition.conditions) {
|
|
20
|
+
if (await evaluateCondition({ condition: sub, ctx })) return true;
|
|
21
|
+
}
|
|
22
|
+
return false;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
return false;
|
|
26
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { emailSends } from "@hogsend/db/schema";
|
|
2
|
+
import { and, eq } from "drizzle-orm";
|
|
3
|
+
import type { EmailEngagementCondition } from "../types/index.js";
|
|
4
|
+
import type { ConditionContext } from "./evaluate.js";
|
|
5
|
+
|
|
6
|
+
export async function evaluateEmailEngagementCondition(opts: {
|
|
7
|
+
condition: EmailEngagementCondition;
|
|
8
|
+
ctx: ConditionContext;
|
|
9
|
+
}): Promise<boolean> {
|
|
10
|
+
const { condition, ctx } = opts;
|
|
11
|
+
const send = await ctx.db.query.emailSends.findFirst({
|
|
12
|
+
where: and(
|
|
13
|
+
eq(emailSends.toEmail, ctx.userId),
|
|
14
|
+
eq(emailSends.templateKey, condition.templateKey),
|
|
15
|
+
),
|
|
16
|
+
orderBy: (sends, { desc }) => [desc(sends.createdAt)],
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
if (!send) return false;
|
|
20
|
+
|
|
21
|
+
switch (condition.check) {
|
|
22
|
+
case "opened":
|
|
23
|
+
return send.openedAt !== null;
|
|
24
|
+
case "clicked":
|
|
25
|
+
return send.clickedAt !== null;
|
|
26
|
+
case "not_opened":
|
|
27
|
+
return send.openedAt === null;
|
|
28
|
+
case "not_clicked":
|
|
29
|
+
return send.clickedAt === null;
|
|
30
|
+
default:
|
|
31
|
+
return false;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { Database } from "@hogsend/db";
|
|
2
|
+
import type { ConditionEval } from "../types/index.js";
|
|
3
|
+
import { evaluateCompositeCondition } from "./composite.js";
|
|
4
|
+
import { evaluateEmailEngagementCondition } from "./email-engagement.js";
|
|
5
|
+
import { evaluateEventCondition } from "./event.js";
|
|
6
|
+
import { evaluatePropertyCondition } from "./property.js";
|
|
7
|
+
|
|
8
|
+
export interface ConditionContext {
|
|
9
|
+
db: Database;
|
|
10
|
+
userId: string;
|
|
11
|
+
journeyContext: Record<string, unknown>;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export async function evaluateCondition(opts: {
|
|
15
|
+
condition: ConditionEval;
|
|
16
|
+
ctx: ConditionContext;
|
|
17
|
+
}): Promise<boolean> {
|
|
18
|
+
const { condition, ctx } = opts;
|
|
19
|
+
|
|
20
|
+
switch (condition.type) {
|
|
21
|
+
case "property":
|
|
22
|
+
return evaluatePropertyCondition({ condition, ctx });
|
|
23
|
+
case "event": {
|
|
24
|
+
const result = await evaluateEventCondition({ condition, ctx });
|
|
25
|
+
return result.matched;
|
|
26
|
+
}
|
|
27
|
+
case "email_engagement":
|
|
28
|
+
return evaluateEmailEngagementCondition({ condition, ctx });
|
|
29
|
+
case "composite":
|
|
30
|
+
return evaluateCompositeCondition({ condition, ctx });
|
|
31
|
+
default:
|
|
32
|
+
return false;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { userEvents } from "@hogsend/db/schema";
|
|
2
|
+
import { and, eq, gte, sql } from "drizzle-orm";
|
|
3
|
+
import { durationToMs } from "../duration.js";
|
|
4
|
+
import type { EventCondition } from "../types/index.js";
|
|
5
|
+
import type { ConditionContext } from "./evaluate.js";
|
|
6
|
+
|
|
7
|
+
export async function evaluateEventCondition(opts: {
|
|
8
|
+
condition: EventCondition;
|
|
9
|
+
ctx: ConditionContext;
|
|
10
|
+
}): Promise<{ matched: boolean; count: number }> {
|
|
11
|
+
const { condition, ctx } = opts;
|
|
12
|
+
const [result] = await ctx.db
|
|
13
|
+
.select({ count: sql<number>`count(*)` })
|
|
14
|
+
.from(userEvents)
|
|
15
|
+
.where(
|
|
16
|
+
and(
|
|
17
|
+
eq(userEvents.userId, ctx.userId),
|
|
18
|
+
eq(userEvents.event, condition.eventName),
|
|
19
|
+
condition.within
|
|
20
|
+
? gte(
|
|
21
|
+
userEvents.occurredAt,
|
|
22
|
+
new Date(Date.now() - durationToMs(condition.within)),
|
|
23
|
+
)
|
|
24
|
+
: undefined,
|
|
25
|
+
),
|
|
26
|
+
);
|
|
27
|
+
|
|
28
|
+
const count = Number(result?.count ?? 0);
|
|
29
|
+
|
|
30
|
+
let matched: boolean;
|
|
31
|
+
switch (condition.check) {
|
|
32
|
+
case "exists":
|
|
33
|
+
matched = count > 0;
|
|
34
|
+
break;
|
|
35
|
+
case "not_exists":
|
|
36
|
+
matched = count === 0;
|
|
37
|
+
break;
|
|
38
|
+
case "count": {
|
|
39
|
+
if (!condition.operator || condition.value === undefined) {
|
|
40
|
+
matched = count > 0;
|
|
41
|
+
} else {
|
|
42
|
+
switch (condition.operator) {
|
|
43
|
+
case "gt":
|
|
44
|
+
matched = count > condition.value;
|
|
45
|
+
break;
|
|
46
|
+
case "gte":
|
|
47
|
+
matched = count >= condition.value;
|
|
48
|
+
break;
|
|
49
|
+
case "lt":
|
|
50
|
+
matched = count < condition.value;
|
|
51
|
+
break;
|
|
52
|
+
case "lte":
|
|
53
|
+
matched = count <= condition.value;
|
|
54
|
+
break;
|
|
55
|
+
case "eq":
|
|
56
|
+
matched = count === condition.value;
|
|
57
|
+
break;
|
|
58
|
+
default:
|
|
59
|
+
matched = false;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
break;
|
|
63
|
+
}
|
|
64
|
+
default:
|
|
65
|
+
matched = false;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
return { matched, count };
|
|
69
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import type { PropertyCondition } from "../types/index.js";
|
|
2
|
+
import type { ConditionContext } from "./evaluate.js";
|
|
3
|
+
|
|
4
|
+
export function evaluatePropertyCondition(opts: {
|
|
5
|
+
condition: PropertyCondition;
|
|
6
|
+
ctx: ConditionContext;
|
|
7
|
+
}): boolean {
|
|
8
|
+
const { condition, ctx } = opts;
|
|
9
|
+
const value = ctx.journeyContext[condition.property];
|
|
10
|
+
return compareValue(value, condition.operator, condition.value);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function evaluatePropertyConditions(opts: {
|
|
14
|
+
conditions: PropertyCondition[];
|
|
15
|
+
properties: Record<string, unknown>;
|
|
16
|
+
}): boolean {
|
|
17
|
+
const { conditions, properties } = opts;
|
|
18
|
+
return conditions.every((condition) => {
|
|
19
|
+
const value = properties[condition.property];
|
|
20
|
+
return compareValue(value, condition.operator, condition.value);
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function compareValue(
|
|
25
|
+
actual: unknown,
|
|
26
|
+
operator: PropertyCondition["operator"],
|
|
27
|
+
expected: PropertyCondition["value"],
|
|
28
|
+
): boolean {
|
|
29
|
+
switch (operator) {
|
|
30
|
+
case "eq":
|
|
31
|
+
return actual === expected;
|
|
32
|
+
case "neq":
|
|
33
|
+
return actual !== expected;
|
|
34
|
+
case "gt":
|
|
35
|
+
return (
|
|
36
|
+
typeof actual === "number" &&
|
|
37
|
+
typeof expected === "number" &&
|
|
38
|
+
actual > expected
|
|
39
|
+
);
|
|
40
|
+
case "gte":
|
|
41
|
+
return (
|
|
42
|
+
typeof actual === "number" &&
|
|
43
|
+
typeof expected === "number" &&
|
|
44
|
+
actual >= expected
|
|
45
|
+
);
|
|
46
|
+
case "lt":
|
|
47
|
+
return (
|
|
48
|
+
typeof actual === "number" &&
|
|
49
|
+
typeof expected === "number" &&
|
|
50
|
+
actual < expected
|
|
51
|
+
);
|
|
52
|
+
case "lte":
|
|
53
|
+
return (
|
|
54
|
+
typeof actual === "number" &&
|
|
55
|
+
typeof expected === "number" &&
|
|
56
|
+
actual <= expected
|
|
57
|
+
);
|
|
58
|
+
case "exists":
|
|
59
|
+
return actual !== undefined && actual !== null;
|
|
60
|
+
case "not_exists":
|
|
61
|
+
return actual === undefined || actual === null;
|
|
62
|
+
case "contains":
|
|
63
|
+
return (
|
|
64
|
+
typeof actual === "string" &&
|
|
65
|
+
typeof expected === "string" &&
|
|
66
|
+
actual.includes(expected)
|
|
67
|
+
);
|
|
68
|
+
default:
|
|
69
|
+
return false;
|
|
70
|
+
}
|
|
71
|
+
}
|
package/src/duration.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export interface DurationObject {
|
|
2
|
+
readonly hours?: number;
|
|
3
|
+
readonly minutes?: number;
|
|
4
|
+
readonly seconds?: number;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export function days(n: number): DurationObject {
|
|
8
|
+
return { hours: n * 24 };
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function hours(n: number): DurationObject {
|
|
12
|
+
return { hours: n };
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function minutes(n: number): DurationObject {
|
|
16
|
+
return { minutes: n };
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function durationToMs(d: DurationObject): number {
|
|
20
|
+
return (
|
|
21
|
+
(d.hours ?? 0) * 3_600_000 +
|
|
22
|
+
(d.minutes ?? 0) * 60_000 +
|
|
23
|
+
(d.seconds ?? 0) * 1_000
|
|
24
|
+
);
|
|
25
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export {
|
|
2
|
+
type ConditionContext,
|
|
3
|
+
evaluateCondition,
|
|
4
|
+
evaluateEventCondition,
|
|
5
|
+
evaluatePropertyConditions,
|
|
6
|
+
} from "./conditions/index.js";
|
|
7
|
+
export {
|
|
8
|
+
type DurationObject,
|
|
9
|
+
days,
|
|
10
|
+
durationToMs,
|
|
11
|
+
hours,
|
|
12
|
+
minutes,
|
|
13
|
+
} from "./duration.js";
|
|
14
|
+
export { JourneyRegistry } from "./registry/index.js";
|
|
15
|
+
export { journeyMetaSchema } from "./schemas/index.js";
|
|
16
|
+
export * from "./types/index.js";
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { journeyMetaSchema } from "../schemas/index.js";
|
|
2
|
+
import type { JourneyMeta } from "../types/index.js";
|
|
3
|
+
|
|
4
|
+
export class JourneyRegistry {
|
|
5
|
+
private journeys: Map<string, JourneyMeta> = new Map();
|
|
6
|
+
private triggerIndex: Map<string, JourneyMeta[]> = new Map();
|
|
7
|
+
|
|
8
|
+
register(journey: JourneyMeta): void {
|
|
9
|
+
const parsed = journeyMetaSchema.parse(journey);
|
|
10
|
+
const validated = parsed as unknown as JourneyMeta;
|
|
11
|
+
|
|
12
|
+
this.journeys.set(validated.id, validated);
|
|
13
|
+
|
|
14
|
+
const event = validated.trigger.event;
|
|
15
|
+
const existing = this.triggerIndex.get(event) ?? [];
|
|
16
|
+
existing.push(validated);
|
|
17
|
+
this.triggerIndex.set(event, existing);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
get(id: string): JourneyMeta | undefined {
|
|
21
|
+
return this.journeys.get(id);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
getByTriggerEvent(eventName: string): JourneyMeta[] {
|
|
25
|
+
return this.triggerIndex.get(eventName) ?? [];
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
getAll(): JourneyMeta[] {
|
|
29
|
+
return Array.from(this.journeys.values());
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
getEnabled(): JourneyMeta[] {
|
|
33
|
+
return this.getAll().filter((j) => j.enabled);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
has(id: string): boolean {
|
|
37
|
+
return this.journeys.has(id);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
count(): number {
|
|
41
|
+
return this.journeys.size;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { journeyMetaSchema } from "./journey.schema.js";
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
|
|
3
|
+
export const propertyConditionSchema = z.object({
|
|
4
|
+
type: z.literal("property"),
|
|
5
|
+
property: z.string(),
|
|
6
|
+
operator: z.enum([
|
|
7
|
+
"eq",
|
|
8
|
+
"neq",
|
|
9
|
+
"gt",
|
|
10
|
+
"gte",
|
|
11
|
+
"lt",
|
|
12
|
+
"lte",
|
|
13
|
+
"exists",
|
|
14
|
+
"not_exists",
|
|
15
|
+
"contains",
|
|
16
|
+
]),
|
|
17
|
+
value: z.union([z.string(), z.number(), z.boolean()]).optional(),
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
export const eventConditionSchema = z.object({
|
|
21
|
+
type: z.literal("event"),
|
|
22
|
+
eventName: z.string(),
|
|
23
|
+
check: z.enum(["exists", "not_exists", "count"]),
|
|
24
|
+
operator: z.enum(["gt", "gte", "lt", "lte", "eq"]).optional(),
|
|
25
|
+
value: z.number().optional(),
|
|
26
|
+
within: z
|
|
27
|
+
.object({
|
|
28
|
+
hours: z.number().optional(),
|
|
29
|
+
minutes: z.number().optional(),
|
|
30
|
+
seconds: z.number().optional(),
|
|
31
|
+
})
|
|
32
|
+
.optional(),
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
export const emailEngagementConditionSchema = z.object({
|
|
36
|
+
type: z.literal("email_engagement"),
|
|
37
|
+
templateKey: z.string(),
|
|
38
|
+
check: z.enum(["opened", "clicked", "not_opened", "not_clicked"]),
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
export const conditionEvalSchema: z.ZodType<unknown> = z.lazy(() =>
|
|
42
|
+
z.discriminatedUnion("type", [
|
|
43
|
+
propertyConditionSchema,
|
|
44
|
+
eventConditionSchema,
|
|
45
|
+
emailEngagementConditionSchema,
|
|
46
|
+
z.object({
|
|
47
|
+
type: z.literal("composite"),
|
|
48
|
+
operator: z.enum(["and", "or"]),
|
|
49
|
+
conditions: z.array(conditionEvalSchema),
|
|
50
|
+
}),
|
|
51
|
+
]),
|
|
52
|
+
);
|
|
53
|
+
|
|
54
|
+
export const journeyMetaSchema = z.object({
|
|
55
|
+
id: z.string().min(1),
|
|
56
|
+
name: z.string().min(1),
|
|
57
|
+
description: z.string().optional(),
|
|
58
|
+
enabled: z.boolean(),
|
|
59
|
+
|
|
60
|
+
trigger: z.object({
|
|
61
|
+
event: z.string().min(1),
|
|
62
|
+
where: z.array(propertyConditionSchema).optional(),
|
|
63
|
+
}),
|
|
64
|
+
|
|
65
|
+
entryLimit: z.enum(["once", "once_per_period", "unlimited"]),
|
|
66
|
+
entryPeriod: z
|
|
67
|
+
.object({
|
|
68
|
+
hours: z.number().optional(),
|
|
69
|
+
minutes: z.number().optional(),
|
|
70
|
+
seconds: z.number().optional(),
|
|
71
|
+
})
|
|
72
|
+
.optional(),
|
|
73
|
+
|
|
74
|
+
exitOn: z
|
|
75
|
+
.array(
|
|
76
|
+
z.object({
|
|
77
|
+
event: z.string().min(1),
|
|
78
|
+
where: z.array(propertyConditionSchema).optional(),
|
|
79
|
+
}),
|
|
80
|
+
)
|
|
81
|
+
.optional(),
|
|
82
|
+
|
|
83
|
+
suppress: z.object({
|
|
84
|
+
hours: z.number().optional(),
|
|
85
|
+
minutes: z.number().optional(),
|
|
86
|
+
seconds: z.number().optional(),
|
|
87
|
+
}),
|
|
88
|
+
});
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
export interface PropertyCondition {
|
|
2
|
+
type: "property";
|
|
3
|
+
property: string;
|
|
4
|
+
operator:
|
|
5
|
+
| "eq"
|
|
6
|
+
| "neq"
|
|
7
|
+
| "gt"
|
|
8
|
+
| "gte"
|
|
9
|
+
| "lt"
|
|
10
|
+
| "lte"
|
|
11
|
+
| "exists"
|
|
12
|
+
| "not_exists"
|
|
13
|
+
| "contains";
|
|
14
|
+
value?: string | number | boolean;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
import type { DurationObject } from "../duration.js";
|
|
18
|
+
|
|
19
|
+
export interface EventCondition {
|
|
20
|
+
type: "event";
|
|
21
|
+
eventName: string;
|
|
22
|
+
check: "exists" | "not_exists" | "count";
|
|
23
|
+
operator?: "gt" | "gte" | "lt" | "lte" | "eq";
|
|
24
|
+
value?: number;
|
|
25
|
+
within?: DurationObject;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export interface EmailEngagementCondition {
|
|
29
|
+
type: "email_engagement";
|
|
30
|
+
templateKey: string;
|
|
31
|
+
check: "opened" | "clicked" | "not_opened" | "not_clicked";
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export interface CompositeCondition {
|
|
35
|
+
type: "composite";
|
|
36
|
+
operator: "and" | "or";
|
|
37
|
+
conditions: ConditionEval[];
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export type ConditionEval =
|
|
41
|
+
| PropertyCondition
|
|
42
|
+
| EventCondition
|
|
43
|
+
| EmailEngagementCondition
|
|
44
|
+
| CompositeCondition;
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import type { DurationObject } from "../duration.js";
|
|
2
|
+
|
|
3
|
+
export interface SleepOptions {
|
|
4
|
+
duration: DurationObject;
|
|
5
|
+
label?: string;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export interface SleepResult {
|
|
9
|
+
sleptAt: string;
|
|
10
|
+
resumedAt: string;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface TriggerOptions {
|
|
14
|
+
event: string;
|
|
15
|
+
userId: string;
|
|
16
|
+
userEmail?: string;
|
|
17
|
+
properties?: Record<string, unknown>;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface HasEventOptions {
|
|
21
|
+
userId: string;
|
|
22
|
+
event: string;
|
|
23
|
+
within?: DurationObject;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export interface HasEventResult {
|
|
27
|
+
found: boolean;
|
|
28
|
+
count: number;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export interface JourneyHistoryOptions {
|
|
32
|
+
userId: string;
|
|
33
|
+
journeyId: string;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export interface JourneyHistoryResult {
|
|
37
|
+
completed: boolean;
|
|
38
|
+
lastCompletedAt: string | null;
|
|
39
|
+
entryCount: number;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export interface EmailHistoryOptions {
|
|
43
|
+
email: string;
|
|
44
|
+
template: string;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export interface EmailHistoryResult {
|
|
48
|
+
sent: boolean;
|
|
49
|
+
lastSentAt: string | null;
|
|
50
|
+
count: number;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export interface JourneyContext {
|
|
54
|
+
sleep(opts: SleepOptions): Promise<SleepResult>;
|
|
55
|
+
checkpoint(label: string): Promise<void>;
|
|
56
|
+
trigger(opts: TriggerOptions): Promise<void>;
|
|
57
|
+
identify(properties: Record<string, unknown>): void;
|
|
58
|
+
|
|
59
|
+
guard: {
|
|
60
|
+
isSubscribed(): Promise<boolean>;
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
history: {
|
|
64
|
+
hasEvent(opts: HasEventOptions): Promise<HasEventResult>;
|
|
65
|
+
journey(opts: JourneyHistoryOptions): Promise<JourneyHistoryResult>;
|
|
66
|
+
email(opts: EmailHistoryOptions): Promise<EmailHistoryResult>;
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
posthog: {
|
|
70
|
+
capture(opts: {
|
|
71
|
+
event: string;
|
|
72
|
+
properties?: Record<string, unknown>;
|
|
73
|
+
}): void;
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export type JourneyRunFn = (
|
|
78
|
+
user: import("./journey.js").JourneyUser,
|
|
79
|
+
ctx: JourneyContext,
|
|
80
|
+
) => Promise<void>;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { DurationObject } from "../duration.js";
|
|
2
|
+
import type { PropertyCondition } from "./conditions.js";
|
|
3
|
+
|
|
4
|
+
export interface JourneyMeta {
|
|
5
|
+
id: string;
|
|
6
|
+
name: string;
|
|
7
|
+
description?: string;
|
|
8
|
+
enabled: boolean;
|
|
9
|
+
|
|
10
|
+
trigger: {
|
|
11
|
+
event: string;
|
|
12
|
+
where?: PropertyCondition[];
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
entryLimit: "once" | "once_per_period" | "unlimited";
|
|
16
|
+
entryPeriod?: DurationObject;
|
|
17
|
+
|
|
18
|
+
exitOn?: Array<{
|
|
19
|
+
event: string;
|
|
20
|
+
where?: PropertyCondition[];
|
|
21
|
+
}>;
|
|
22
|
+
|
|
23
|
+
suppress: DurationObject;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export interface JourneyUser {
|
|
27
|
+
id: string;
|
|
28
|
+
email: string;
|
|
29
|
+
properties: Record<string, string | number | boolean | null>;
|
|
30
|
+
stateId: string;
|
|
31
|
+
journeyId: string;
|
|
32
|
+
journeyName: string;
|
|
33
|
+
}
|