@jskit-ai/rewarded-core 0.2.24 → 0.2.25
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/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
## Unreleased
|
|
4
4
|
|
|
5
|
+
- A gate check reads the one receipt that still unlocks the gate (a new server-only `unlockedAfter` receipt filter, `unlockedUntil > now`), and the latest receipt only when the rule has a cooldown, instead of the gate's 25 newest receipts. A gate unlocked before 25 newer receipts that have since expired stayed locked; it now unlocks.
|
|
5
6
|
- Its table resources declare the foreign keys their migrations create (`foreignKeys`), so json-rest-api 3.1.0 can skip the visibility query a system-context read makes for a record such a key guarantees exists. `jskit test --databases` checks each against an app's test database: each table's workspace_id to workspaces, the unlock receipts' and watch sessions' user_id to users and provider_config_id to rewarded_provider_configs, and rewarded_unlock_receipts.watch_session_id to rewarded_watch_sessions. `foreignKeysMigration.integration.test.js` proves the declarations match the migrations on MySQL.
|
|
6
7
|
- `@jskit-ai/database-runtime` and `@jskit-ai/resource-runtime` are peer dependencies, required and at the same versions, where they were dependencies: its rules, provider settings, watch sessions and unlock receipts are table resources, and the app holds one copy of the table runtime that every package shares, as it does the kernel. npm installs a required peer as it installed the dependency, so an app installs the same packages and versions; a release whose versions disagree now fails the install with ERESOLVE instead of nesting a second copy.
|
|
7
8
|
- `@jskit-ai/database-runtime` is a dependency again: the foreign-key migration (`migrations/constraints/rewarded_foreign_keys.cjs`) imports its `server/foreignKeyMigrations` in `up` and `down`, which resolved only through hoisting since the CRUD features moved to the resource runtime.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jskit-ai/rewarded-core",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.25",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"test": "jskit test"
|
|
@@ -125,11 +125,11 @@
|
|
|
125
125
|
}
|
|
126
126
|
},
|
|
127
127
|
"dependencies": {
|
|
128
|
-
"@jskit-ai/crud-core": "0.2.
|
|
128
|
+
"@jskit-ai/crud-core": "0.2.24",
|
|
129
129
|
"@jskit-ai/resource-core": "0.1.209",
|
|
130
|
-
"@jskit-ai/resource-crud-core": "0.1.
|
|
131
|
-
"@jskit-ai/users-core": "0.2.
|
|
132
|
-
"@jskit-ai/workspaces-core": "0.2.
|
|
130
|
+
"@jskit-ai/resource-crud-core": "0.1.210",
|
|
131
|
+
"@jskit-ai/users-core": "0.2.25",
|
|
132
|
+
"@jskit-ai/workspaces-core": "0.2.25",
|
|
133
133
|
"json-rest-schema": "^1.0.20"
|
|
134
134
|
},
|
|
135
135
|
"peerDependencies": {
|
|
@@ -137,6 +137,6 @@
|
|
|
137
137
|
"@jskit-ai/database-runtime": "0.1.267",
|
|
138
138
|
"@jskit-ai/http-runtime": "0.2.18",
|
|
139
139
|
"@jskit-ai/kernel": "0.2.18",
|
|
140
|
-
"@jskit-ai/resource-runtime": "0.2.
|
|
140
|
+
"@jskit-ai/resource-runtime": "0.2.21"
|
|
141
141
|
}
|
|
142
142
|
}
|
package/src/server/service.js
CHANGED
|
@@ -171,14 +171,19 @@ function createService({
|
|
|
171
171
|
return queryFirst(rewardedProviderConfigsRepository, { surface: REWARDED_SURFACE, enabled: true }, "-updatedAt", repositoryOptions);
|
|
172
172
|
}
|
|
173
173
|
|
|
174
|
+
/**
|
|
175
|
+
* The gate's most recently granted receipt that still unlocks it at `now`, however many receipts came after it.
|
|
176
|
+
* @param {RewardedRepositoryOptions} repositoryOptions
|
|
177
|
+
*/
|
|
178
|
+
async function resolveActiveUnlockReceipt(gateKey, now, repositoryOptions) {
|
|
179
|
+
const filters = { gateKey, unlockedAfter: now.toISOString() };
|
|
180
|
+
const receipt = await queryFirst(rewardedUnlockReceiptsRepository, filters, "-grantedAt", repositoryOptions);
|
|
181
|
+
return isFutureDate(receipt?.unlockedUntil, now) ? receipt : null;
|
|
182
|
+
}
|
|
183
|
+
|
|
174
184
|
/** @param {RewardedRepositoryOptions} repositoryOptions */
|
|
175
|
-
async function
|
|
176
|
-
|
|
177
|
-
filters: { gateKey },
|
|
178
|
-
sort: ["-grantedAt"],
|
|
179
|
-
page: { size: 25 }
|
|
180
|
-
}, repositoryOptions);
|
|
181
|
-
return records;
|
|
185
|
+
async function resolveLatestUnlockReceipt(gateKey, repositoryOptions) {
|
|
186
|
+
return queryFirst(rewardedUnlockReceiptsRepository, { gateKey }, "-grantedAt", repositoryOptions);
|
|
182
187
|
}
|
|
183
188
|
|
|
184
189
|
/**
|
|
@@ -240,9 +245,7 @@ function createService({
|
|
|
240
245
|
|
|
241
246
|
const rule = formatRule(ruleRecord);
|
|
242
247
|
const providerConfig = formatProviderConfig(providerConfigRecord);
|
|
243
|
-
const
|
|
244
|
-
const activeReceiptRecord = receipts.find((entry) => isFutureDate(entry?.unlockedUntil, now)) || null;
|
|
245
|
-
const activeUnlock = formatUnlockReceipt(activeReceiptRecord);
|
|
248
|
+
const activeUnlock = formatUnlockReceipt(await resolveActiveUnlockReceipt(gateKey, now, { context }));
|
|
246
249
|
|
|
247
250
|
if (activeUnlock) {
|
|
248
251
|
return {
|
|
@@ -261,9 +264,10 @@ function createService({
|
|
|
261
264
|
};
|
|
262
265
|
}
|
|
263
266
|
|
|
264
|
-
|
|
267
|
+
// Only a cooldown needs the latest receipt.
|
|
268
|
+
const latestReceipt = Number(rule.cooldownMinutes || 0) > 0 ? await resolveLatestUnlockReceipt(gateKey, { context }) : null;
|
|
265
269
|
let cooldownUntil = null;
|
|
266
|
-
if (latestReceipt
|
|
270
|
+
if (latestReceipt) {
|
|
267
271
|
cooldownUntil = addMinutes(parseDate(latestReceipt.grantedAt) || now, rule.cooldownMinutes);
|
|
268
272
|
if (cooldownUntil.getTime() > now.getTime()) {
|
|
269
273
|
return {
|
|
@@ -67,7 +67,9 @@ const rewardedUnlockReceiptTableResource = defineTableResource({
|
|
|
67
67
|
searchSchema: {
|
|
68
68
|
q: { type: "string", oneOf: ["gateKey"], filterOperator: "like", splitBy: " ", matchAll: true },
|
|
69
69
|
// Server-only: the rewarded service counts the receipts granted since a day began.
|
|
70
|
-
grantedSince: { type: "dateTime", actualField: "grantedAt", filterOperator: ">=" }
|
|
70
|
+
grantedSince: { type: "dateTime", actualField: "grantedAt", filterOperator: ">=" },
|
|
71
|
+
// Server-only: the rewarded service finds the gate's receipt that still unlocks it.
|
|
72
|
+
unlockedAfter: { type: "dateTime", actualField: "unlockedUntil", filterOperator: ">" }
|
|
71
73
|
},
|
|
72
74
|
defaultSort: ["-createdAt"]
|
|
73
75
|
});
|
|
@@ -205,6 +205,26 @@ test(`rewarded grants use real resources and atomic transactions (${databaseUrl
|
|
|
205
205
|
assert.equal((await knex("rewarded_unlock_receipts").where({ watch_session_id: session.id }).select()).length, 1);
|
|
206
206
|
});
|
|
207
207
|
|
|
208
|
+
await t.test("an unlock granted before 25 newer, expired receipts still unlocks the gate", async () => {
|
|
209
|
+
await repositories.rewardedRules.create({
|
|
210
|
+
gateKey: "long", surface: "app", enabled: true,
|
|
211
|
+
unlockMinutes: 30, cooldownMinutes: 0, dailyLimit: null,
|
|
212
|
+
title: "Long", description: "Long unlock test"
|
|
213
|
+
}, { context });
|
|
214
|
+
const hourAgo = new Date(Date.now() - 3600000);
|
|
215
|
+
const unlocking = await repositories.rewardedUnlockReceipts.create({
|
|
216
|
+
gateKey: "long", grantedAt: new Date(hourAgo.getTime() - 60000).toISOString(),
|
|
217
|
+
unlockedUntil: new Date(Date.now() + 86400000).toISOString()
|
|
218
|
+
}, { context });
|
|
219
|
+
await repositories.rewardedUnlockReceipts.createMany(Array.from({ length: 25 }, (_, index) => ({
|
|
220
|
+
gateKey: "long", grantedAt: new Date(hourAgo.getTime() + index * 1000).toISOString(), unlockedUntil: hourAgo.toISOString()
|
|
221
|
+
})), { context });
|
|
222
|
+
|
|
223
|
+
const state = await service.getCurrentState({ workspaceSlug: "alpha", gateKey: "long" }, { context });
|
|
224
|
+
assert.equal(state.reason, "already-unlocked");
|
|
225
|
+
assert.equal(String(state.unlock.id), String(unlocking.id));
|
|
226
|
+
});
|
|
227
|
+
|
|
208
228
|
await t.test("a daily limit above one page of receipts is counted exactly and reached", async () => {
|
|
209
229
|
await repositories.rewardedRules.create({
|
|
210
230
|
gateKey: "daily", surface: "app", enabled: true,
|