@jskit-ai/rewarded-core 0.2.3 → 0.2.5
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/README.md +2 -3
- package/docs/protecting-server-actions.md +55 -34
- package/locales/en.json +1 -2
- package/migrations/constraints/rewarded_foreign_keys.cjs +32 -0
- package/migrations/rewarded_provider_configs_initial.cjs +1 -1
- package/migrations/rewarded_rules_initial.cjs +1 -1
- package/migrations/rewarded_unlock_receipts_initial.cjs +1 -3
- package/migrations/rewarded_watch_sessions_initial.cjs +1 -2
- package/package.json +19 -13
- package/src/server/{RewardedCoreProvider.js → RewardedCoreFeature.js} +14 -8
- package/src/server/RewardedResources.js +5 -17
- package/src/server/inputSchemas.js +7 -11
- package/src/server/rewardedActions.js +114 -0
- package/src/server/{registerRoutes.js → rewardedRoutes.js} +24 -33
- package/src/server/service.js +101 -155
- package/src/server/support/requireRewardedUnlock.js +29 -36
- package/src/server/types.js +17 -0
- package/test/featureRuntime.test.js +29 -16
- package/test/foreignKeysMigration.integration.test.js +104 -0
- package/test/requireRewardedUnlock.test.js +7 -10
- package/test/routes.test.js +2 -2
- package/test/service.integration.test.js +11 -8
- package/test/service.test.js +6 -7
- package/src/server/actions.js +0 -136
package/README.md
CHANGED
|
@@ -80,13 +80,12 @@ The dedicated manual page is:
|
|
|
80
80
|
|
|
81
81
|
- [docs/protecting-server-actions.md](docs/protecting-server-actions.md)
|
|
82
82
|
|
|
83
|
-
That page shows the
|
|
83
|
+
That page shows the service and feature wiring pattern.
|
|
84
84
|
|
|
85
85
|
## Grant authorization
|
|
86
86
|
|
|
87
87
|
The application must supply capability `rewarded.grant-policy` with an
|
|
88
|
-
`authorizeGrant({ session, context, trx })` function.
|
|
89
|
-
supplies the same function as `authorizeGrant`. Missing policy fails at setup;
|
|
88
|
+
`authorizeGrant({ session, context, trx })` function. Missing policy fails at setup;
|
|
90
89
|
only the literal result `true` permits a grant. False, absent or other results
|
|
91
90
|
reject it before any watch-session update or receipt creation.
|
|
92
91
|
|
|
@@ -20,7 +20,8 @@ The correct split is:
|
|
|
20
20
|
|
|
21
21
|
## What The Helper Does
|
|
22
22
|
|
|
23
|
-
`requireRewardedUnlock()`
|
|
23
|
+
`requireRewardedUnlock()` asks the `rewarded.core` capability for the gate's
|
|
24
|
+
current state (`getCurrentState(input, { context })`) and then enforces one simple rule:
|
|
24
25
|
|
|
25
26
|
- if the gate is already unlocked, it returns the gate state
|
|
26
27
|
- if the gate is not configured and `requireConfigured` is not enabled, it returns the gate state
|
|
@@ -38,60 +39,80 @@ import { requireRewardedUnlock } from "@jskit-ai/rewarded-core/server/requireRew
|
|
|
38
39
|
|
|
39
40
|
## Basic Service Pattern
|
|
40
41
|
|
|
41
|
-
|
|
42
|
+
Your service receives the `rewarded.core` capability, typed `RewardedCore`, and
|
|
43
|
+
calls the helper before the protected mutation.
|
|
42
44
|
|
|
43
45
|
Example:
|
|
44
46
|
|
|
45
47
|
```js
|
|
48
|
+
// notesService.js
|
|
46
49
|
import { requireRewardedUnlock } from "@jskit-ai/rewarded-core/server/requireRewardedUnlock";
|
|
47
50
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
51
|
+
/**
|
|
52
|
+
* The notes export service: an export needs the "notes-export" rewarded unlock.
|
|
53
|
+
* @param {object} dependencies
|
|
54
|
+
* @param {import("@jskit-ai/rewarded-core/server/types").RewardedCore} dependencies.rewarded
|
|
55
|
+
* @param {import("@jskit-ai/crud-core/server/types").CrudService} dependencies.notesService
|
|
56
|
+
*/
|
|
57
|
+
function createNotesExportService({ rewarded, notesService }) {
|
|
58
|
+
async function exportNotes(input, { context }) {
|
|
53
59
|
await requireRewardedUnlock(
|
|
54
|
-
|
|
55
|
-
{
|
|
56
|
-
|
|
57
|
-
workspaceSlug: input.workspaceSlug
|
|
58
|
-
},
|
|
59
|
-
{
|
|
60
|
-
context: options.context,
|
|
61
|
-
errorMessage: "Watch a rewarded ad before logging progress."
|
|
62
|
-
}
|
|
60
|
+
rewarded,
|
|
61
|
+
{ gateKey: "notes-export", workspaceSlug: input.workspaceSlug },
|
|
62
|
+
{ context, errorMessage: "Watch a rewarded ad before exporting notes." }
|
|
63
63
|
);
|
|
64
64
|
|
|
65
|
-
return
|
|
65
|
+
return notesService.queryDocuments({}, { context });
|
|
66
66
|
}
|
|
67
67
|
|
|
68
|
-
return Object.freeze({
|
|
69
|
-
logProgress
|
|
70
|
-
});
|
|
68
|
+
return Object.freeze({ exportNotes });
|
|
71
69
|
}
|
|
72
70
|
```
|
|
73
71
|
|
|
74
|
-
##
|
|
72
|
+
## Feature Wiring Pattern
|
|
75
73
|
|
|
76
|
-
|
|
74
|
+
The feature requires `rewarded.core` like any other capability and hands it to
|
|
75
|
+
the service it builds in `setup`.
|
|
77
76
|
|
|
78
77
|
Example:
|
|
79
78
|
|
|
80
79
|
```js
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
80
|
+
// NotesExportFeature.js
|
|
81
|
+
import { defineFeature } from "@jskit-ai/kernel/server/features";
|
|
82
|
+
import { createNotesExportService } from "./notesService.js";
|
|
83
|
+
import { createNotesExportActions } from "./notesExportActions.js";
|
|
84
|
+
|
|
85
|
+
const NotesExportFeature = defineFeature({
|
|
86
|
+
id: "notes.export",
|
|
87
|
+
domain: "notes",
|
|
88
|
+
requires: {
|
|
89
|
+
rewarded: "rewarded.core",
|
|
90
|
+
notes: "notes"
|
|
91
|
+
},
|
|
92
|
+
provides: {
|
|
93
|
+
notesExport: "notes.export"
|
|
94
|
+
},
|
|
95
|
+
/**
|
|
96
|
+
* @param {object} capabilities
|
|
97
|
+
* @param {import("@jskit-ai/rewarded-core/server/types").RewardedCore} capabilities.rewarded
|
|
98
|
+
* @param {import("@jskit-ai/crud-core/server/types").CrudResourceApi} capabilities.notes
|
|
99
|
+
*/
|
|
100
|
+
setup({ rewarded, notes }) {
|
|
101
|
+
return { notesExport: createNotesExportService({ rewarded, notesService: notes.service }) };
|
|
102
|
+
},
|
|
103
|
+
actions({ notesExport }) {
|
|
104
|
+
return createNotesExportActions({ notesExport });
|
|
105
|
+
}
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
export { NotesExportFeature };
|
|
88
109
|
```
|
|
89
110
|
|
|
90
111
|
## Exact Helper Signature
|
|
91
112
|
|
|
92
113
|
```js
|
|
93
114
|
await requireRewardedUnlock(
|
|
94
|
-
|
|
115
|
+
rewarded,
|
|
95
116
|
{
|
|
96
117
|
gateKey,
|
|
97
118
|
workspaceSlug
|
|
@@ -107,13 +128,13 @@ await requireRewardedUnlock(
|
|
|
107
128
|
|
|
108
129
|
Arguments:
|
|
109
130
|
|
|
110
|
-
- `
|
|
111
|
-
-
|
|
131
|
+
- `rewarded`
|
|
132
|
+
- the `rewarded.core` capability (`RewardedCore`), whose `getCurrentState(input, { context })` it calls
|
|
112
133
|
- first object
|
|
113
134
|
- `gateKey`: required gate identifier
|
|
114
135
|
- `workspaceSlug`: required for the current workspace gate
|
|
115
136
|
- second object
|
|
116
|
-
- `context`: the
|
|
137
|
+
- `context`: the action context of the protected operation
|
|
117
138
|
- `requireConfigured`: fail closed when no rule/provider config exists
|
|
118
139
|
- `errorCode`: optional override for the thrown `AppError.code`
|
|
119
140
|
- `errorMessage`: optional override for the thrown `AppError.message`
|
|
@@ -159,7 +180,7 @@ Important built-in failure cases:
|
|
|
159
180
|
|
|
160
181
|
## What The Helper Returns
|
|
161
182
|
|
|
162
|
-
On success, the helper returns the gate state from `
|
|
183
|
+
On success, the helper returns the gate state from `rewarded.getCurrentState(...)`.
|
|
163
184
|
|
|
164
185
|
That lets your service inspect the unlock window if it wants to log or branch on it, for example:
|
|
165
186
|
|
package/locales/en.json
CHANGED
|
@@ -5,8 +5,7 @@
|
|
|
5
5
|
"rewarded_cooldown_active": "Rewarded unlock is cooling down.",
|
|
6
6
|
"rewarded_daily_limit_reached": "Rewarded unlock limit reached.",
|
|
7
7
|
"rewarded_not_configured": "Rewarded gate is not configured.",
|
|
8
|
-
"rewarded_unlock_blocked": "Rewarded unlock is required before this action can continue."
|
|
9
|
-
"rewarded_gate_state_invalid": "Rewarded gate returned an invalid state."
|
|
8
|
+
"rewarded_unlock_blocked": "Rewarded unlock is required before this action can continue."
|
|
10
9
|
},
|
|
11
10
|
"rewarded": {
|
|
12
11
|
"auth": {
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
// users-core and workspaces-core migrate after rewarded-core, and rewarded_watch_sessions is created
|
|
2
|
+
// after rewarded_unlock_receipts, so these keys are added in this later phase, once every parent exists.
|
|
3
|
+
const KEYS = [
|
|
4
|
+
{ tableName: "rewarded_provider_configs", columnName: "workspace_id", parentTable: "workspaces",
|
|
5
|
+
name: "fk_rewarded_provider_configs_workspace", onUpdate: "RESTRICT", onDelete: "CASCADE" },
|
|
6
|
+
{ tableName: "rewarded_rules", columnName: "workspace_id", parentTable: "workspaces",
|
|
7
|
+
name: "fk_rewarded_rules_workspace", onUpdate: "RESTRICT", onDelete: "CASCADE" },
|
|
8
|
+
{ tableName: "rewarded_unlock_receipts", columnName: "user_id", parentTable: "users",
|
|
9
|
+
name: "fk_rewarded_unlock_receipts_user", onUpdate: "RESTRICT", onDelete: "CASCADE" },
|
|
10
|
+
{ tableName: "rewarded_unlock_receipts", columnName: "watch_session_id", parentTable: "rewarded_watch_sessions",
|
|
11
|
+
name: "fk_rewarded_unlock_receipts_watch_session", onUpdate: "RESTRICT", onDelete: "SET NULL" },
|
|
12
|
+
{ tableName: "rewarded_unlock_receipts", columnName: "workspace_id", parentTable: "workspaces",
|
|
13
|
+
name: "fk_rewarded_unlock_receipts_workspace", onUpdate: "RESTRICT", onDelete: "CASCADE" },
|
|
14
|
+
{ tableName: "rewarded_watch_sessions", columnName: "user_id", parentTable: "users",
|
|
15
|
+
name: "fk_rewarded_watch_sessions_user", onUpdate: "RESTRICT", onDelete: "CASCADE" },
|
|
16
|
+
{ tableName: "rewarded_watch_sessions", columnName: "workspace_id", parentTable: "workspaces",
|
|
17
|
+
name: "fk_rewarded_watch_sessions_workspace", onUpdate: "RESTRICT", onDelete: "CASCADE" }
|
|
18
|
+
];
|
|
19
|
+
|
|
20
|
+
exports.up = async function up(knex) {
|
|
21
|
+
const { addMissingForeignKey } = await import("@jskit-ai/database-runtime/server/foreignKeyMigrations");
|
|
22
|
+
for (const key of KEYS) {
|
|
23
|
+
await addMissingForeignKey(knex, key);
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
exports.down = async function down(knex) {
|
|
28
|
+
const { dropForeignKeyIfPresent } = await import("@jskit-ai/database-runtime/server/foreignKeyMigrations");
|
|
29
|
+
for (const key of KEYS) {
|
|
30
|
+
await dropForeignKeyIfPresent(knex, key);
|
|
31
|
+
}
|
|
32
|
+
};
|
|
@@ -6,6 +6,7 @@ exports.up = async function up(knex) {
|
|
|
6
6
|
return;
|
|
7
7
|
}
|
|
8
8
|
|
|
9
|
+
// constraints/rewarded_foreign_keys.cjs adds the workspaces key.
|
|
9
10
|
await knex.schema.createTable(TABLE_NAME, (table) => {
|
|
10
11
|
table.bigIncrements("id").unsigned().primary();
|
|
11
12
|
table.bigInteger("workspace_id").unsigned().notNullable();
|
|
@@ -17,7 +18,6 @@ exports.up = async function up(knex) {
|
|
|
17
18
|
table.timestamp("updated_at").notNullable().defaultTo(knex.raw("CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP"));
|
|
18
19
|
table.index(["workspace_id"], "idx_rewarded_provider_configs_workspace");
|
|
19
20
|
table.unique(["workspace_id","surface"], "uq_rewarded_provider_configs_workspace_surface");
|
|
20
|
-
table.foreign(["workspace_id"], "fk_rewarded_provider_configs_workspace").references(["id"]).inTable("workspaces").onUpdate("RESTRICT").onDelete("CASCADE");
|
|
21
21
|
});
|
|
22
22
|
|
|
23
23
|
};
|
|
@@ -6,6 +6,7 @@ exports.up = async function up(knex) {
|
|
|
6
6
|
return;
|
|
7
7
|
}
|
|
8
8
|
|
|
9
|
+
// constraints/rewarded_foreign_keys.cjs adds the workspaces key.
|
|
9
10
|
await knex.schema.createTable(TABLE_NAME, (table) => {
|
|
10
11
|
table.bigIncrements("id").unsigned().primary();
|
|
11
12
|
table.bigInteger("workspace_id").unsigned().notNullable();
|
|
@@ -21,7 +22,6 @@ exports.up = async function up(knex) {
|
|
|
21
22
|
table.timestamp("updated_at").notNullable().defaultTo(knex.raw("CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP"));
|
|
22
23
|
table.index(["workspace_id"], "idx_rewarded_rules_workspace");
|
|
23
24
|
table.unique(["workspace_id","gate_key"], "uq_rewarded_rules_workspace_gate");
|
|
24
|
-
table.foreign(["workspace_id"], "fk_rewarded_rules_workspace").references(["id"]).inTable("workspaces").onUpdate("RESTRICT").onDelete("CASCADE");
|
|
25
25
|
});
|
|
26
26
|
|
|
27
27
|
};
|
|
@@ -6,6 +6,7 @@ exports.up = async function up(knex) {
|
|
|
6
6
|
return;
|
|
7
7
|
}
|
|
8
8
|
|
|
9
|
+
// constraints/rewarded_foreign_keys.cjs adds the users, workspaces and rewarded_watch_sessions keys.
|
|
9
10
|
await knex.schema.createTable(TABLE_NAME, (table) => {
|
|
10
11
|
table.bigIncrements("id").unsigned().primary();
|
|
11
12
|
table.bigInteger("workspace_id").unsigned().notNullable();
|
|
@@ -23,9 +24,6 @@ exports.up = async function up(knex) {
|
|
|
23
24
|
table.index(["watch_session_id"], "idx_rewarded_unlock_receipts_watch_session");
|
|
24
25
|
table.index(["workspace_id","user_id"], "idx_rewarded_unlock_receipts_workspace_user");
|
|
25
26
|
table.foreign(["provider_config_id"], "fk_rewarded_unlock_receipts_provider_config").references(["id"]).inTable("rewarded_provider_configs").onUpdate("RESTRICT").onDelete("SET NULL");
|
|
26
|
-
table.foreign(["user_id"], "fk_rewarded_unlock_receipts_user").references(["id"]).inTable("users").onUpdate("RESTRICT").onDelete("CASCADE");
|
|
27
|
-
table.foreign(["watch_session_id"], "fk_rewarded_unlock_receipts_watch_session").references(["id"]).inTable("rewarded_watch_sessions").onUpdate("RESTRICT").onDelete("SET NULL");
|
|
28
|
-
table.foreign(["workspace_id"], "fk_rewarded_unlock_receipts_workspace").references(["id"]).inTable("workspaces").onUpdate("RESTRICT").onDelete("CASCADE");
|
|
29
27
|
});
|
|
30
28
|
|
|
31
29
|
};
|
|
@@ -6,6 +6,7 @@ exports.up = async function up(knex) {
|
|
|
6
6
|
return;
|
|
7
7
|
}
|
|
8
8
|
|
|
9
|
+
// constraints/rewarded_foreign_keys.cjs adds the users and workspaces keys.
|
|
9
10
|
await knex.schema.createTable(TABLE_NAME, (table) => {
|
|
10
11
|
table.bigIncrements("id").unsigned().primary();
|
|
11
12
|
table.bigInteger("workspace_id").unsigned().notNullable();
|
|
@@ -25,8 +26,6 @@ exports.up = async function up(knex) {
|
|
|
25
26
|
table.index(["status"], "idx_rewarded_watch_sessions_status");
|
|
26
27
|
table.index(["workspace_id","user_id"], "idx_rewarded_watch_sessions_workspace_user");
|
|
27
28
|
table.foreign(["provider_config_id"], "fk_rewarded_watch_sessions_provider_config").references(["id"]).inTable("rewarded_provider_configs").onUpdate("RESTRICT").onDelete("SET NULL");
|
|
28
|
-
table.foreign(["user_id"], "fk_rewarded_watch_sessions_user").references(["id"]).inTable("users").onUpdate("RESTRICT").onDelete("CASCADE");
|
|
29
|
-
table.foreign(["workspace_id"], "fk_rewarded_watch_sessions_workspace").references(["id"]).inTable("workspaces").onUpdate("RESTRICT").onDelete("CASCADE");
|
|
30
29
|
});
|
|
31
30
|
|
|
32
31
|
};
|
package/package.json
CHANGED
|
@@ -1,14 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jskit-ai/rewarded-core",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.5",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"test": "node --test --test-concurrency=1"
|
|
7
7
|
},
|
|
8
8
|
"exports": {
|
|
9
9
|
"./shared": "./src/shared/index.js",
|
|
10
|
-
"./server/
|
|
10
|
+
"./server/rewardedActions": "./src/server/rewardedActions.js",
|
|
11
11
|
"./server/requireRewardedUnlock": "./src/server/support/requireRewardedUnlock.js",
|
|
12
|
+
"./server/types": "./src/server/types.js",
|
|
12
13
|
"./locales/en.json": "./locales/en.json"
|
|
13
14
|
},
|
|
14
15
|
"description": "Rewarded workflow runtime plus internal CRUD providers for rules, provider configs, watch sessions, and unlock receipts.",
|
|
@@ -53,7 +54,7 @@
|
|
|
53
54
|
"export": "RewardedUnlockReceiptsFeature"
|
|
54
55
|
},
|
|
55
56
|
{
|
|
56
|
-
"entrypoint": "src/server/
|
|
57
|
+
"entrypoint": "src/server/RewardedCoreFeature.js",
|
|
57
58
|
"export": "RewardedCoreFeature"
|
|
58
59
|
}
|
|
59
60
|
]
|
|
@@ -96,12 +97,16 @@
|
|
|
96
97
|
"summary": "Exports Rewarded shared CRUD resources."
|
|
97
98
|
},
|
|
98
99
|
{
|
|
99
|
-
"subpath": "./server/
|
|
100
|
-
"summary": "Exports Rewarded workflow action identifiers and
|
|
100
|
+
"subpath": "./server/rewardedActions",
|
|
101
|
+
"summary": "Exports the Rewarded workflow action identifiers and createRewardedActions()."
|
|
101
102
|
},
|
|
102
103
|
{
|
|
103
104
|
"subpath": "./server/requireRewardedUnlock",
|
|
104
105
|
"summary": "Exports the server helper that enforces a rewarded unlock before a protected feature action continues."
|
|
106
|
+
},
|
|
107
|
+
{
|
|
108
|
+
"subpath": "./server/types",
|
|
109
|
+
"summary": "Types of the rewarded.core, rewarded.rules, rewarded.provider-configs, rewarded.watch-sessions and rewarded.unlock-receipts capabilities, for features that require them."
|
|
105
110
|
}
|
|
106
111
|
],
|
|
107
112
|
"capabilities": [
|
|
@@ -120,16 +125,17 @@
|
|
|
120
125
|
}
|
|
121
126
|
},
|
|
122
127
|
"dependencies": {
|
|
123
|
-
"@jskit-ai/crud-core": "0.2.
|
|
124
|
-
"@jskit-ai/database-runtime": "0.1.
|
|
125
|
-
"@jskit-ai/json-rest-api-core": "0.2.
|
|
126
|
-
"@jskit-ai/resource-crud-core": "0.1.
|
|
127
|
-
"@jskit-ai/
|
|
128
|
+
"@jskit-ai/crud-core": "0.2.4",
|
|
129
|
+
"@jskit-ai/database-runtime": "0.1.253",
|
|
130
|
+
"@jskit-ai/json-rest-api-core": "0.2.4",
|
|
131
|
+
"@jskit-ai/resource-crud-core": "0.1.195",
|
|
132
|
+
"@jskit-ai/users-core": "0.2.5",
|
|
133
|
+
"@jskit-ai/workspaces-core": "0.2.5",
|
|
128
134
|
"json-rest-schema": "^1.0.17"
|
|
129
135
|
},
|
|
130
136
|
"peerDependencies": {
|
|
131
|
-
"@jskit-ai/auth-core": "0.2.
|
|
132
|
-
"@jskit-ai/http-runtime": "0.2.
|
|
133
|
-
"@jskit-ai/kernel": "0.2.
|
|
137
|
+
"@jskit-ai/auth-core": "0.2.4",
|
|
138
|
+
"@jskit-ai/http-runtime": "0.2.4",
|
|
139
|
+
"@jskit-ai/kernel": "0.2.4"
|
|
134
140
|
}
|
|
135
141
|
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { defineFeature } from "@jskit-ai/kernel/server/features";
|
|
2
2
|
|
|
3
3
|
import { createService } from "./service.js";
|
|
4
|
-
import { createRewardedActions } from "./
|
|
5
|
-
import {
|
|
4
|
+
import { createRewardedActions } from "./rewardedActions.js";
|
|
5
|
+
import { registerRewardedRoutes } from "./rewardedRoutes.js";
|
|
6
6
|
|
|
7
7
|
const RewardedCoreFeature = defineFeature({
|
|
8
8
|
id: "rewarded.core",
|
|
@@ -18,22 +18,28 @@ const RewardedCoreFeature = defineFeature({
|
|
|
18
18
|
provides: {
|
|
19
19
|
rewarded: "rewarded.core"
|
|
20
20
|
},
|
|
21
|
+
/**
|
|
22
|
+
* @param {object} capabilities
|
|
23
|
+
* @param {import("@jskit-ai/kernel/server/types").RuntimeHttp} capabilities.http
|
|
24
|
+
* @param {import("./types.js").RewardedGrantPolicy} capabilities.grantPolicy
|
|
25
|
+
* @param {import("./types.js").RewardedProviderConfigs} capabilities.providerConfigs
|
|
26
|
+
* @param {import("./types.js").RewardedRules} capabilities.rules
|
|
27
|
+
* @param {import("./types.js").RewardedUnlockReceipts} capabilities.unlockReceipts
|
|
28
|
+
* @param {import("./types.js").RewardedWatchSessions} capabilities.watchSessions
|
|
29
|
+
*/
|
|
21
30
|
setup({ http, grantPolicy, providerConfigs, rules, unlockReceipts, watchSessions }) {
|
|
31
|
+
// readable-check: allow method-probe - input validation: the application provides rewarded.grant-policy
|
|
22
32
|
if (typeof grantPolicy.authorizeGrant !== "function") {
|
|
23
33
|
throw new TypeError("rewarded.grant-policy requires authorizeGrant.");
|
|
24
34
|
}
|
|
25
35
|
const rewarded = createService({
|
|
26
|
-
|
|
36
|
+
grantPolicy,
|
|
27
37
|
rewardedRulesRepository: rules.repository,
|
|
28
38
|
rewardedProviderConfigsRepository: providerConfigs.repository,
|
|
29
39
|
rewardedWatchSessionsRepository: watchSessions.repository,
|
|
30
40
|
rewardedUnlockReceiptsRepository: unlockReceipts.repository
|
|
31
41
|
});
|
|
32
|
-
|
|
33
|
-
routeOwnershipFilter: "workspace_user",
|
|
34
|
-
routeSurface: "app",
|
|
35
|
-
routeRelativePath: "rewarded"
|
|
36
|
-
});
|
|
42
|
+
registerRewardedRoutes(http.router);
|
|
37
43
|
return { rewarded };
|
|
38
44
|
},
|
|
39
45
|
actions({ rewarded }) {
|
|
@@ -1,22 +1,10 @@
|
|
|
1
1
|
import { defineCrudJsonApiFeature } from "@jskit-ai/crud-core/server/defineCrudJsonApiFeature";
|
|
2
|
-
import {
|
|
3
|
-
import { workspaceSlugParamsValidator } from "@jskit-ai/workspaces-core/server/validators/routeParamsValidator";
|
|
4
|
-
import { buildWorkspaceInputFromRouteParams } from "@jskit-ai/workspaces-core/server/support/workspaceRouteInput";
|
|
2
|
+
import { WORKSPACE_CRUD_SCOPE } from "@jskit-ai/workspaces-core/server/support/workspaceCrud";
|
|
5
3
|
import { resource as providerConfigResource } from "../shared/rewardedProviderConfigResource.js";
|
|
6
4
|
import { resource as ruleResource } from "../shared/rewardedRuleResource.js";
|
|
7
5
|
import { resource as unlockReceiptResource } from "../shared/rewardedUnlockReceiptResource.js";
|
|
8
6
|
import { resource as watchSessionResource } from "../shared/rewardedWatchSessionResource.js";
|
|
9
7
|
|
|
10
|
-
function workspaceScope() {
|
|
11
|
-
return {
|
|
12
|
-
routeBase: "/w/:workspaceSlug",
|
|
13
|
-
actionInputValidator: workspaceSlugParamsValidator,
|
|
14
|
-
routeParamsValidator,
|
|
15
|
-
inputKeys: ["workspaceSlug"],
|
|
16
|
-
input: (request) => buildWorkspaceInputFromRouteParams(request.input.params)
|
|
17
|
-
};
|
|
18
|
-
}
|
|
19
|
-
|
|
20
8
|
const RewardedRulesFeature = defineCrudJsonApiFeature({
|
|
21
9
|
id: "rewarded.rules",
|
|
22
10
|
capability: "rewarded.rules",
|
|
@@ -25,7 +13,7 @@ const RewardedRulesFeature = defineCrudJsonApiFeature({
|
|
|
25
13
|
ownershipFilter: "workspace",
|
|
26
14
|
relativePath: "/rewarded-rules",
|
|
27
15
|
internal: true,
|
|
28
|
-
scope:
|
|
16
|
+
scope: WORKSPACE_CRUD_SCOPE
|
|
29
17
|
});
|
|
30
18
|
|
|
31
19
|
const RewardedProviderConfigsFeature = defineCrudJsonApiFeature({
|
|
@@ -36,7 +24,7 @@ const RewardedProviderConfigsFeature = defineCrudJsonApiFeature({
|
|
|
36
24
|
ownershipFilter: "workspace",
|
|
37
25
|
relativePath: "/rewarded-provider-configs",
|
|
38
26
|
internal: true,
|
|
39
|
-
scope:
|
|
27
|
+
scope: WORKSPACE_CRUD_SCOPE
|
|
40
28
|
});
|
|
41
29
|
|
|
42
30
|
const RewardedWatchSessionsFeature = defineCrudJsonApiFeature({
|
|
@@ -47,7 +35,7 @@ const RewardedWatchSessionsFeature = defineCrudJsonApiFeature({
|
|
|
47
35
|
ownershipFilter: "workspace_user",
|
|
48
36
|
relativePath: "/rewarded-watch-sessions",
|
|
49
37
|
internal: true,
|
|
50
|
-
scope:
|
|
38
|
+
scope: WORKSPACE_CRUD_SCOPE
|
|
51
39
|
});
|
|
52
40
|
|
|
53
41
|
const RewardedUnlockReceiptsFeature = defineCrudJsonApiFeature({
|
|
@@ -58,7 +46,7 @@ const RewardedUnlockReceiptsFeature = defineCrudJsonApiFeature({
|
|
|
58
46
|
ownershipFilter: "workspace_user",
|
|
59
47
|
relativePath: "/rewarded-unlock-receipts",
|
|
60
48
|
internal: true,
|
|
61
|
-
scope:
|
|
49
|
+
scope: WORKSPACE_CRUD_SCOPE
|
|
62
50
|
});
|
|
63
51
|
|
|
64
52
|
export {
|
|
@@ -17,13 +17,11 @@ const gateQuerySchema = createSchema({
|
|
|
17
17
|
});
|
|
18
18
|
|
|
19
19
|
const currentQueryInputValidator = deepFreeze({
|
|
20
|
-
schema: gateQuerySchema
|
|
21
|
-
mode: "patch"
|
|
20
|
+
schema: gateQuerySchema
|
|
22
21
|
});
|
|
23
22
|
|
|
24
23
|
const startCommandInputValidator = deepFreeze({
|
|
25
|
-
schema: gateQuerySchema
|
|
26
|
-
mode: "patch"
|
|
24
|
+
schema: gateQuerySchema
|
|
27
25
|
});
|
|
28
26
|
|
|
29
27
|
const grantCommandInputValidator = deepFreeze({
|
|
@@ -38,8 +36,7 @@ const grantCommandInputValidator = deepFreeze({
|
|
|
38
36
|
minLength: 1,
|
|
39
37
|
maxLength: 120
|
|
40
38
|
}
|
|
41
|
-
})
|
|
42
|
-
mode: "patch"
|
|
39
|
+
})
|
|
43
40
|
});
|
|
44
41
|
|
|
45
42
|
const closeCommandInputValidator = deepFreeze({
|
|
@@ -54,8 +51,7 @@ const closeCommandInputValidator = deepFreeze({
|
|
|
54
51
|
minLength: 1,
|
|
55
52
|
maxLength: 120
|
|
56
53
|
}
|
|
57
|
-
})
|
|
58
|
-
mode: "patch"
|
|
54
|
+
})
|
|
59
55
|
});
|
|
60
56
|
|
|
61
57
|
const gateRuleOutputSchema = createSchema({
|
|
@@ -223,7 +219,7 @@ const unlockReceiptOutputSchema = createSchema({
|
|
|
223
219
|
}
|
|
224
220
|
});
|
|
225
221
|
|
|
226
|
-
function
|
|
222
|
+
function buildGateStateOutputValidator({
|
|
227
223
|
includeSession = false
|
|
228
224
|
} = {}) {
|
|
229
225
|
const fields = {
|
|
@@ -309,8 +305,8 @@ function createGateStateOutputValidator({
|
|
|
309
305
|
});
|
|
310
306
|
}
|
|
311
307
|
|
|
312
|
-
const currentStateOutputValidator =
|
|
313
|
-
const startGateOutputValidator =
|
|
308
|
+
const currentStateOutputValidator = buildGateStateOutputValidator();
|
|
309
|
+
const startGateOutputValidator = buildGateStateOutputValidator({
|
|
314
310
|
includeSession: true
|
|
315
311
|
});
|
|
316
312
|
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import {
|
|
2
|
+
currentQueryInputValidator,
|
|
3
|
+
startCommandInputValidator,
|
|
4
|
+
grantCommandInputValidator,
|
|
5
|
+
closeCommandInputValidator,
|
|
6
|
+
currentStateOutputValidator,
|
|
7
|
+
startGateOutputValidator,
|
|
8
|
+
grantRewardOutputValidator,
|
|
9
|
+
closeSessionOutputValidator
|
|
10
|
+
} from "./inputSchemas.js";
|
|
11
|
+
import { createEntityChangedActionEvent } from "@jskit-ai/kernel/server/actions";
|
|
12
|
+
import { resolveCrudRecordChangedEvent } from "@jskit-ai/resource-crud-core/shared/crudNamespaceSupport";
|
|
13
|
+
|
|
14
|
+
const ACTION_CURRENT = "rewarded.current.read";
|
|
15
|
+
const ACTION_START = "rewarded.start";
|
|
16
|
+
const ACTION_GRANT = "rewarded.grant";
|
|
17
|
+
const ACTION_CLOSE = "rewarded.close";
|
|
18
|
+
|
|
19
|
+
const watchSessionChanged = resolveCrudRecordChangedEvent("rewarded_watch_sessions");
|
|
20
|
+
const unlockReceiptChanged = resolveCrudRecordChangedEvent("rewarded_unlock_receipts");
|
|
21
|
+
|
|
22
|
+
function changedEvent({ entity, operation, entityId, event }) {
|
|
23
|
+
return createEntityChangedActionEvent({
|
|
24
|
+
source: "rewarded",
|
|
25
|
+
entity,
|
|
26
|
+
operation,
|
|
27
|
+
entityId,
|
|
28
|
+
realtime: { event, audience: "event_scope" }
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** @param {{ rewarded: import("./types.js").RewardedCore }} dependencies */
|
|
33
|
+
function createRewardedActions({ rewarded }) {
|
|
34
|
+
return Object.freeze([
|
|
35
|
+
Object.freeze({
|
|
36
|
+
id: ACTION_CURRENT,
|
|
37
|
+
version: 1,
|
|
38
|
+
kind: "query",
|
|
39
|
+
channels: ["api", "automation", "internal"],
|
|
40
|
+
surfaces: ["app"],
|
|
41
|
+
permission: { require: "authenticated" },
|
|
42
|
+
input: currentQueryInputValidator,
|
|
43
|
+
output: currentStateOutputValidator,
|
|
44
|
+
execute: (input, context) => rewarded.getCurrentState(input, { context })
|
|
45
|
+
}),
|
|
46
|
+
Object.freeze({
|
|
47
|
+
id: ACTION_START,
|
|
48
|
+
version: 1,
|
|
49
|
+
kind: "command",
|
|
50
|
+
channels: ["api", "automation", "internal"],
|
|
51
|
+
surfaces: ["app"],
|
|
52
|
+
permission: { require: "authenticated" },
|
|
53
|
+
input: startCommandInputValidator,
|
|
54
|
+
output: startGateOutputValidator,
|
|
55
|
+
events: [changedEvent({
|
|
56
|
+
entity: "watch-session",
|
|
57
|
+
operation: "created",
|
|
58
|
+
entityId: ({ result }) => result?.session?.id,
|
|
59
|
+
event: watchSessionChanged
|
|
60
|
+
})],
|
|
61
|
+
execute: (input, context) => rewarded.startGate(input, { context })
|
|
62
|
+
}),
|
|
63
|
+
Object.freeze({
|
|
64
|
+
id: ACTION_GRANT,
|
|
65
|
+
version: 1,
|
|
66
|
+
kind: "command",
|
|
67
|
+
channels: ["api", "automation", "internal"],
|
|
68
|
+
surfaces: ["app"],
|
|
69
|
+
permission: { require: "authenticated" },
|
|
70
|
+
input: grantCommandInputValidator,
|
|
71
|
+
output: grantRewardOutputValidator,
|
|
72
|
+
events: [
|
|
73
|
+
changedEvent({
|
|
74
|
+
entity: "watch-session",
|
|
75
|
+
operation: "updated",
|
|
76
|
+
entityId: ({ result }) => result?.session?.id,
|
|
77
|
+
event: watchSessionChanged
|
|
78
|
+
}),
|
|
79
|
+
changedEvent({
|
|
80
|
+
entity: "unlock-receipt",
|
|
81
|
+
operation: "created",
|
|
82
|
+
entityId: ({ result }) => result?.unlock?.id,
|
|
83
|
+
event: unlockReceiptChanged
|
|
84
|
+
})
|
|
85
|
+
],
|
|
86
|
+
execute: (input, context) => rewarded.grantReward(input, { context })
|
|
87
|
+
}),
|
|
88
|
+
Object.freeze({
|
|
89
|
+
id: ACTION_CLOSE,
|
|
90
|
+
version: 1,
|
|
91
|
+
kind: "command",
|
|
92
|
+
channels: ["api", "automation", "internal"],
|
|
93
|
+
surfaces: ["app"],
|
|
94
|
+
permission: { require: "authenticated" },
|
|
95
|
+
input: closeCommandInputValidator,
|
|
96
|
+
output: closeSessionOutputValidator,
|
|
97
|
+
events: [changedEvent({
|
|
98
|
+
entity: "watch-session",
|
|
99
|
+
operation: "updated",
|
|
100
|
+
entityId: ({ result }) => result?.session?.id,
|
|
101
|
+
event: watchSessionChanged
|
|
102
|
+
})],
|
|
103
|
+
execute: (input, context) => rewarded.closeSession(input, { context })
|
|
104
|
+
})
|
|
105
|
+
]);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export {
|
|
109
|
+
ACTION_CURRENT,
|
|
110
|
+
ACTION_START,
|
|
111
|
+
ACTION_GRANT,
|
|
112
|
+
ACTION_CLOSE,
|
|
113
|
+
createRewardedActions
|
|
114
|
+
};
|