@jskit-ai/console-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/migrations/console_core_generic_initial.cjs +2 -1
- package/migrations/constraints/console_core_users_foreign_key.cjs +15 -0
- package/package.json +13 -12
- package/src/server/ConsoleFeature.js +19 -35
- package/src/server/consoleAuthServiceDecorator.js +42 -43
- package/src/server/consoleBootstrapContributor.js +18 -24
- package/src/server/consoleRuntime.js +27 -0
- package/src/server/consoleSettings/consoleService.js +12 -11
- package/src/server/consoleSettings/consoleSettingsActions.js +53 -73
- package/src/server/consoleSettings/consoleSettingsRepository.js +13 -13
- package/src/server/consoleSettings/{bootConsoleSettingsRoutes.js → consoleSettingsRoutes.js} +1 -4
- package/src/server/consoleSettings/consoleSettingsService.js +19 -22
- package/src/server/types.js +18 -0
- package/src/shared/operationMessages.js +6 -15
- package/src/shared/resources/consoleSettingsResource.js +2 -2
- package/test/consoleBootstrapContributor.test.js +0 -1
- package/test/consoleRouteRequestInputValidator.test.js +1 -1
- package/test/consoleSettingsService.test.js +5 -8
- package/test/exportsContract.test.js +2 -0
- package/test/featureRuntime.test.js +5 -3
- package/test/repositoryContracts.test.js +1 -1
- package/test/usersForeignKeyMigration.test.js +34 -0
|
@@ -6,7 +6,8 @@ exports.up = async function up(knex) {
|
|
|
6
6
|
if (!hasConsoleSettingsTable) {
|
|
7
7
|
await knex.schema.createTable("console_settings", (table) => {
|
|
8
8
|
table.bigInteger("id").primary();
|
|
9
|
-
|
|
9
|
+
// constraints/console_core_users_foreign_key.cjs adds the users key once the users table exists.
|
|
10
|
+
table.bigInteger("owner_user_id").unsigned().nullable();
|
|
10
11
|
table.timestamp("created_at", { useTz: false }).notNullable().defaultTo(knex.fn.now());
|
|
11
12
|
table.timestamp("updated_at", { useTz: false }).notNullable().defaultTo(knex.fn.now());
|
|
12
13
|
});
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
// The users table comes from users-core, whose migrations run after console-core's, or from the
|
|
2
|
+
// application's own migrations, which run after every package's table migrations, so
|
|
3
|
+
// console_settings gets its users key in this later phase.
|
|
4
|
+
// A database migrated before this file already has it from the table migration.
|
|
5
|
+
const USERS_KEY = { tableName: "console_settings", columnName: "owner_user_id", parentTable: "users", onDelete: "SET NULL" };
|
|
6
|
+
|
|
7
|
+
exports.up = async function up(knex) {
|
|
8
|
+
const { addMissingForeignKey } = await import("@jskit-ai/database-runtime/server/foreignKeyMigrations");
|
|
9
|
+
await addMissingForeignKey(knex, USERS_KEY);
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
exports.down = async function down(knex) {
|
|
13
|
+
const { dropForeignKeyIfPresent } = await import("@jskit-ai/database-runtime/server/foreignKeyMigrations");
|
|
14
|
+
await dropForeignKeyIfPresent(knex, USERS_KEY);
|
|
15
|
+
};
|
package/package.json
CHANGED
|
@@ -1,19 +1,20 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jskit-ai/console-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
|
"./server/ConsoleFeature": "./src/server/ConsoleFeature.js",
|
|
10
|
+
"./server/types": "./src/server/types.js",
|
|
10
11
|
"./shared/resources/consoleSettingsResource": "./src/shared/resources/consoleSettingsResource.js",
|
|
11
12
|
"./locales/en.json": "./locales/en.json"
|
|
12
13
|
},
|
|
13
14
|
"dependencies": {
|
|
14
|
-
"@jskit-ai/database-runtime": "0.1.
|
|
15
|
-
"@jskit-ai/resource-crud-core": "0.1.
|
|
16
|
-
"@jskit-ai/users-core": "0.2.
|
|
15
|
+
"@jskit-ai/database-runtime": "0.1.253",
|
|
16
|
+
"@jskit-ai/resource-crud-core": "0.1.195",
|
|
17
|
+
"@jskit-ai/users-core": "0.2.5",
|
|
17
18
|
"json-rest-schema": "^1.0.17"
|
|
18
19
|
},
|
|
19
20
|
"description": "Console runtime: console settings schema, bootstrap flags, actions, and HTTP routes.",
|
|
@@ -57,16 +58,16 @@
|
|
|
57
58
|
"apiSummary": {
|
|
58
59
|
"surfaces": [
|
|
59
60
|
{
|
|
60
|
-
"subpath": "./server",
|
|
61
|
-
"summary": "Exports
|
|
61
|
+
"subpath": "./server/ConsoleFeature",
|
|
62
|
+
"summary": "Exports ConsoleFeature, which provides console.core: owner access, the console settings actions and routes, and the console bootstrap."
|
|
62
63
|
},
|
|
63
64
|
{
|
|
64
|
-
"subpath": "./shared",
|
|
65
|
+
"subpath": "./shared/resources/consoleSettingsResource",
|
|
65
66
|
"summary": "Exports the shared console settings resource contract."
|
|
66
67
|
},
|
|
67
68
|
{
|
|
68
|
-
"subpath": "./
|
|
69
|
-
"summary": "
|
|
69
|
+
"subpath": "./server/types",
|
|
70
|
+
"summary": "Type of the console.core capability, for features that require it."
|
|
70
71
|
}
|
|
71
72
|
],
|
|
72
73
|
"capabilities": [
|
|
@@ -95,8 +96,8 @@
|
|
|
95
96
|
}
|
|
96
97
|
},
|
|
97
98
|
"peerDependencies": {
|
|
98
|
-
"@jskit-ai/auth-core": "0.2.
|
|
99
|
-
"@jskit-ai/http-runtime": "0.2.
|
|
100
|
-
"@jskit-ai/kernel": "0.2.
|
|
99
|
+
"@jskit-ai/auth-core": "0.2.4",
|
|
100
|
+
"@jskit-ai/http-runtime": "0.2.4",
|
|
101
|
+
"@jskit-ai/kernel": "0.2.4"
|
|
101
102
|
}
|
|
102
103
|
}
|
|
@@ -1,32 +1,9 @@
|
|
|
1
1
|
import { defineFeature } from "@jskit-ai/kernel/server/features";
|
|
2
2
|
import { createConsoleAuthServiceDecorator } from "./consoleAuthServiceDecorator.js";
|
|
3
3
|
import { createConsoleBootstrapContributor } from "./consoleBootstrapContributor.js";
|
|
4
|
-
import { registerConsoleSettingsRoutes } from "./consoleSettings/
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import { createRepository as createConsoleSettingsRepository } from "./consoleSettings/consoleSettingsRepository.js";
|
|
8
|
-
import { createService as createConsoleSettingsService } from "./consoleSettings/consoleSettingsService.js";
|
|
9
|
-
|
|
10
|
-
function createConsoleRuntime({ database } = {}) {
|
|
11
|
-
if (!database || typeof database.knex !== "function") {
|
|
12
|
-
throw new TypeError("ConsoleFeature requires runtime.database.knex.");
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
const consoleSettingsRepository = createConsoleSettingsRepository(database.knex);
|
|
16
|
-
const consoleService = createConsoleService({ consoleSettingsRepository });
|
|
17
|
-
const consoleSettingsService = createConsoleSettingsService({
|
|
18
|
-
consoleService,
|
|
19
|
-
consoleSettingsRepository
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
return Object.freeze({
|
|
23
|
-
repositories: Object.freeze({ settings: consoleSettingsRepository }),
|
|
24
|
-
services: Object.freeze({
|
|
25
|
-
access: consoleService,
|
|
26
|
-
settings: consoleSettingsService
|
|
27
|
-
})
|
|
28
|
-
});
|
|
29
|
-
}
|
|
4
|
+
import { registerConsoleSettingsRoutes } from "./consoleSettings/consoleSettingsRoutes.js";
|
|
5
|
+
import { createConsoleSettingsActions } from "./consoleSettings/consoleSettingsActions.js";
|
|
6
|
+
import { createConsoleRuntime } from "./consoleRuntime.js";
|
|
30
7
|
|
|
31
8
|
const ConsoleFeature = defineFeature({
|
|
32
9
|
id: "console.core",
|
|
@@ -38,30 +15,37 @@ const ConsoleFeature = defineFeature({
|
|
|
38
15
|
http: "runtime.http"
|
|
39
16
|
},
|
|
40
17
|
provides: {
|
|
41
|
-
|
|
18
|
+
consoleCore: "console.core"
|
|
42
19
|
},
|
|
20
|
+
/**
|
|
21
|
+
* @param {object} capabilities
|
|
22
|
+
* @param {import("@jskit-ai/auth-core/server/types").AuthExtensions} capabilities.authExtensions
|
|
23
|
+
* @param {import("@jskit-ai/kernel/server/types").RuntimeBootstrap} capabilities.bootstrap
|
|
24
|
+
* @param {import("@jskit-ai/database-runtime/server/types").RuntimeDatabase} capabilities.database
|
|
25
|
+
* @param {import("@jskit-ai/kernel/server/types").RuntimeHttp} capabilities.http
|
|
26
|
+
*/
|
|
43
27
|
setup({ authExtensions, bootstrap, database, http }) {
|
|
44
|
-
const
|
|
28
|
+
const consoleCore = createConsoleRuntime({ database });
|
|
45
29
|
|
|
46
30
|
authExtensions.registerServiceDecorator(
|
|
47
|
-
createConsoleAuthServiceDecorator({ consoleService:
|
|
31
|
+
createConsoleAuthServiceDecorator({ consoleService: consoleCore.services.access })
|
|
48
32
|
);
|
|
49
33
|
bootstrap.register({
|
|
50
34
|
id: "console.bootstrap",
|
|
51
35
|
order: 300,
|
|
52
36
|
contribute: createConsoleBootstrapContributor({
|
|
53
|
-
consoleService:
|
|
37
|
+
consoleService: consoleCore.services.access
|
|
54
38
|
}).contribute
|
|
55
39
|
});
|
|
56
40
|
registerConsoleSettingsRoutes(http.router);
|
|
57
41
|
|
|
58
|
-
return {
|
|
42
|
+
return { consoleCore };
|
|
59
43
|
},
|
|
60
|
-
actions({
|
|
61
|
-
return
|
|
62
|
-
consoleSettingsService:
|
|
44
|
+
actions({ consoleCore }) {
|
|
45
|
+
return createConsoleSettingsActions({
|
|
46
|
+
consoleSettingsService: consoleCore.services.settings
|
|
63
47
|
});
|
|
64
48
|
}
|
|
65
49
|
});
|
|
66
50
|
|
|
67
|
-
export { ConsoleFeature
|
|
51
|
+
export { ConsoleFeature };
|
|
@@ -24,57 +24,56 @@ function normalizeRequestPathname(request = null) {
|
|
|
24
24
|
return "";
|
|
25
25
|
}
|
|
26
26
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
27
|
+
/**
|
|
28
|
+
* Makes the first user who reads their session the console's owner.
|
|
29
|
+
* @param {{ consoleService: import("./types.js").ConsoleService }} dependencies
|
|
30
|
+
*/
|
|
31
|
+
function createConsoleAuthServiceDecorator({ consoleService }) {
|
|
32
32
|
let consoleOwnerInitialized = false;
|
|
33
33
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
34
|
+
/** @param {import("@jskit-ai/auth-core/server/types").AuthService} authService */
|
|
35
|
+
function decorateAuthService(authService) {
|
|
36
|
+
return Object.freeze(
|
|
37
|
+
Object.defineProperty(Object.create(authService), "authenticateRequest", {
|
|
38
|
+
enumerable: true,
|
|
39
|
+
value: async function authenticateRequest(request, ...args) {
|
|
40
|
+
const authResult = await authService.authenticateRequest(request, ...args);
|
|
41
|
+
if (consoleOwnerInitialized) {
|
|
42
|
+
return authResult;
|
|
43
|
+
}
|
|
41
44
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
const authResult = await authService.authenticateRequest(request, ...args);
|
|
47
|
-
if (consoleOwnerInitialized) {
|
|
48
|
-
return authResult;
|
|
49
|
-
}
|
|
45
|
+
const requestPathname = normalizeRequestPathname(request);
|
|
46
|
+
if (requestPathname !== AUTH_PATHS.SESSION) {
|
|
47
|
+
return authResult;
|
|
48
|
+
}
|
|
50
49
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
50
|
+
const authenticatedUserId =
|
|
51
|
+
authResult?.authenticated === true
|
|
52
|
+
? normalizeRecordId(authResult?.actor?.id, { fallback: null })
|
|
53
|
+
: null;
|
|
55
54
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
: null;
|
|
55
|
+
if (!authenticatedUserId) {
|
|
56
|
+
return authResult;
|
|
57
|
+
}
|
|
60
58
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
}
|
|
59
|
+
const ownerUserId = normalizeRecordId(
|
|
60
|
+
await consoleService.ensureInitialConsoleMember(authenticatedUserId),
|
|
61
|
+
{ fallback: null }
|
|
62
|
+
);
|
|
63
|
+
if (ownerUserId) {
|
|
64
|
+
consoleOwnerInitialized = true;
|
|
65
|
+
}
|
|
64
66
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
consoleOwnerInitialized = true;
|
|
71
|
-
}
|
|
67
|
+
return authResult;
|
|
68
|
+
}
|
|
69
|
+
})
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
72
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
}
|
|
73
|
+
return Object.freeze({
|
|
74
|
+
decoratorId: "console.core.authServiceDecorator",
|
|
75
|
+
order: 0,
|
|
76
|
+
decorateAuthService
|
|
78
77
|
});
|
|
79
78
|
}
|
|
80
79
|
|
|
@@ -1,33 +1,27 @@
|
|
|
1
1
|
import { normalizeObject, normalizeRecordId } from "@jskit-ai/kernel/shared/support/normalize";
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
3
|
+
/** @param {{ consoleService: import("./types.js").ConsoleService }} dependencies */
|
|
4
|
+
function createConsoleBootstrapContributor({ consoleService }) {
|
|
5
|
+
async function contribute({ payload }) {
|
|
6
|
+
const session = normalizeObject(payload.session);
|
|
7
|
+
const surfaceAccess = normalizeObject(payload.surfaceAccess);
|
|
8
|
+
const authenticatedUserId =
|
|
9
|
+
session.authenticated === true ? normalizeRecordId(session.userId, { fallback: null }) : null;
|
|
7
10
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
const normalizedPayload = normalizeObject(payload);
|
|
13
|
-
const session = normalizeObject(normalizedPayload.session);
|
|
14
|
-
const surfaceAccess = normalizeObject(normalizedPayload.surfaceAccess);
|
|
15
|
-
const authenticatedUserId =
|
|
16
|
-
session.authenticated === true ? normalizeRecordId(session.userId, { fallback: null }) : null;
|
|
11
|
+
let consoleOwner = false;
|
|
12
|
+
if (authenticatedUserId) {
|
|
13
|
+
consoleOwner = await consoleService.isConsoleOwnerUserId(authenticatedUserId);
|
|
14
|
+
}
|
|
17
15
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
16
|
+
return {
|
|
17
|
+
surfaceAccess: {
|
|
18
|
+
...surfaceAccess,
|
|
19
|
+
consoleowner: consoleOwner
|
|
21
20
|
}
|
|
21
|
+
};
|
|
22
|
+
}
|
|
22
23
|
|
|
23
|
-
|
|
24
|
-
surfaceAccess: {
|
|
25
|
-
...surfaceAccess,
|
|
26
|
-
consoleowner: consoleOwner
|
|
27
|
-
}
|
|
28
|
-
};
|
|
29
|
-
}
|
|
30
|
-
});
|
|
24
|
+
return Object.freeze({ contribute });
|
|
31
25
|
}
|
|
32
26
|
|
|
33
27
|
export { createConsoleBootstrapContributor };
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { createService as createConsoleService } from "./consoleSettings/consoleService.js";
|
|
2
|
+
import { createRepository as createConsoleSettingsRepository } from "./consoleSettings/consoleSettingsRepository.js";
|
|
3
|
+
import { createService as createConsoleSettingsService } from "./consoleSettings/consoleSettingsService.js";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* The `console.core` capability: the console settings repository, the
|
|
7
|
+
* console access service and the console settings service.
|
|
8
|
+
* @param {{ database: import("@jskit-ai/database-runtime/server/types").RuntimeDatabase }} dependencies
|
|
9
|
+
*/
|
|
10
|
+
function createConsoleRuntime({ database }) {
|
|
11
|
+
const consoleSettingsRepository = createConsoleSettingsRepository({ knex: database.knex });
|
|
12
|
+
const consoleService = createConsoleService({ consoleSettingsRepository });
|
|
13
|
+
const consoleSettingsService = createConsoleSettingsService({
|
|
14
|
+
consoleService,
|
|
15
|
+
consoleSettingsRepository
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
return Object.freeze({
|
|
19
|
+
repositories: Object.freeze({ settings: consoleSettingsRepository }),
|
|
20
|
+
services: Object.freeze({
|
|
21
|
+
access: consoleService,
|
|
22
|
+
settings: consoleSettingsService
|
|
23
|
+
})
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export { createConsoleRuntime };
|
|
@@ -1,14 +1,9 @@
|
|
|
1
|
+
import { resolveActorId } from "@jskit-ai/kernel/server/actions/actionContributorHelpers";
|
|
1
2
|
import { AppError } from "@jskit-ai/kernel/server/runtime/errors";
|
|
2
3
|
import { normalizeRecordId } from "@jskit-ai/kernel/shared/support/normalize";
|
|
3
4
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
throw new Error("consoleService requires consoleSettingsRepository.ensureOwnerUserId().");
|
|
7
|
-
}
|
|
8
|
-
if (!consoleSettingsRepository || typeof consoleSettingsRepository.getSingleton !== "function") {
|
|
9
|
-
throw new Error("consoleService requires consoleSettingsRepository.getSingleton().");
|
|
10
|
-
}
|
|
11
|
-
|
|
5
|
+
/** @param {{ consoleSettingsRepository: import("../types.js").ConsoleSettingsRepository }} dependencies */
|
|
6
|
+
function createService({ consoleSettingsRepository }) {
|
|
12
7
|
async function ensureInitialConsoleMember(userId, options = {}) {
|
|
13
8
|
const normalizedUserId = normalizeRecordId(userId, { fallback: null });
|
|
14
9
|
if (!normalizedUserId) {
|
|
@@ -25,12 +20,18 @@ function createService({ consoleSettingsRepository } = {}) {
|
|
|
25
20
|
}
|
|
26
21
|
|
|
27
22
|
const settings = await consoleSettingsRepository.getSingleton(options);
|
|
28
|
-
const ownerUserId = normalizeRecordId(settings
|
|
23
|
+
const ownerUserId = normalizeRecordId(settings.ownerUserId, { fallback: null });
|
|
29
24
|
return Boolean(ownerUserId) && ownerUserId === normalizedUserId;
|
|
30
25
|
}
|
|
31
26
|
|
|
32
|
-
|
|
33
|
-
|
|
27
|
+
/**
|
|
28
|
+
* Refuses anyone but the console's owner; the first signed-in user to ask
|
|
29
|
+
* becomes the owner.
|
|
30
|
+
* @param {object | null | undefined} context The call's context: its actor, else its request's user.
|
|
31
|
+
* @param {{ trx?: import("@jskit-ai/database-runtime/server/types").RuntimeKnexTransaction | null }} [options]
|
|
32
|
+
*/
|
|
33
|
+
async function requireConsoleOwner(context, options = {}) {
|
|
34
|
+
const actorUserId = normalizeRecordId(resolveActorId(context), { fallback: null });
|
|
34
35
|
if (!actorUserId) {
|
|
35
36
|
throw new AppError(401, "Authentication required.", { code: "console.owner.authenticationRequired" });
|
|
36
37
|
}
|
|
@@ -1,81 +1,61 @@
|
|
|
1
1
|
import {
|
|
2
2
|
emptyInputValidator
|
|
3
3
|
} from "@jskit-ai/kernel/server/actions/actionContributorHelpers";
|
|
4
|
-
import { returnJsonApiData } from "@jskit-ai/http-runtime/shared";
|
|
5
4
|
import { createEntityChangedActionEvent } from "@jskit-ai/kernel/server/actions";
|
|
6
5
|
import { consoleSettingsResource } from "../../shared/resources/consoleSettingsResource.js";
|
|
7
6
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
})],
|
|
61
|
-
async run(consoleSettingsService, input, context) {
|
|
62
|
-
return returnJsonApiData(await consoleSettingsService.updateSettings(input, {
|
|
63
|
-
context
|
|
64
|
-
}));
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
]);
|
|
68
|
-
|
|
69
|
-
function buildConsoleSettingsActions({ consoleSettingsService } = {}) {
|
|
70
|
-
if (!consoleSettingsService) {
|
|
71
|
-
throw new TypeError("buildConsoleSettingsActions requires consoleSettingsService.");
|
|
72
|
-
}
|
|
73
|
-
return consoleSettingsActionSpecifications.map(({ run, ...definition }) => Object.freeze({
|
|
74
|
-
...definition,
|
|
75
|
-
execute(input, context) {
|
|
76
|
-
return run(consoleSettingsService, input, context);
|
|
77
|
-
}
|
|
78
|
-
}));
|
|
7
|
+
/** @param {{ consoleSettingsService: import("../types.js").ConsoleSettingsService }} dependencies */
|
|
8
|
+
function createConsoleSettingsActions({ consoleSettingsService }) {
|
|
9
|
+
return Object.freeze([
|
|
10
|
+
Object.freeze({
|
|
11
|
+
id: "console.settings.read",
|
|
12
|
+
version: 1,
|
|
13
|
+
kind: "query",
|
|
14
|
+
channels: ["api", "automation", "internal"],
|
|
15
|
+
surfaces: ["console"],
|
|
16
|
+
permission: {
|
|
17
|
+
require: "authenticated"
|
|
18
|
+
},
|
|
19
|
+
input: emptyInputValidator,
|
|
20
|
+
output: null,
|
|
21
|
+
extensions: {
|
|
22
|
+
assistant: {
|
|
23
|
+
exclude: "Application-owned console settings require an explicit assistant field allowlist; arbitrary configuration is not model-safe."
|
|
24
|
+
}
|
|
25
|
+
},
|
|
26
|
+
execute: (_input, context) => consoleSettingsService.getSettings({ context })
|
|
27
|
+
}),
|
|
28
|
+
Object.freeze({
|
|
29
|
+
id: "console.settings.update",
|
|
30
|
+
version: 1,
|
|
31
|
+
kind: "command",
|
|
32
|
+
channels: ["api", "automation", "internal"],
|
|
33
|
+
surfaces: ["console"],
|
|
34
|
+
permission: {
|
|
35
|
+
require: "authenticated"
|
|
36
|
+
},
|
|
37
|
+
input: consoleSettingsResource.operations.replace.body,
|
|
38
|
+
output: null,
|
|
39
|
+
extensions: {
|
|
40
|
+
assistant: { exclude: "Application-owned console settings require an explicit assistant field allowlist; arbitrary configuration is not model-safe." },
|
|
41
|
+
realtime: {
|
|
42
|
+
event: "console.settings.changed",
|
|
43
|
+
audience: "all_users"
|
|
44
|
+
}
|
|
45
|
+
},
|
|
46
|
+
events: [createEntityChangedActionEvent({
|
|
47
|
+
source: "console",
|
|
48
|
+
entity: "settings",
|
|
49
|
+
operation: "updated",
|
|
50
|
+
entityId: 1,
|
|
51
|
+
realtime: {
|
|
52
|
+
event: "console.settings.changed",
|
|
53
|
+
audience: "all_users"
|
|
54
|
+
}
|
|
55
|
+
})],
|
|
56
|
+
execute: (_input, context) => consoleSettingsService.updateSettings({ context })
|
|
57
|
+
})
|
|
58
|
+
]);
|
|
79
59
|
}
|
|
80
60
|
|
|
81
|
-
export {
|
|
61
|
+
export { createConsoleSettingsActions };
|
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import {
|
|
2
|
+
createWithTransaction,
|
|
2
3
|
normalizeDbRecordId,
|
|
3
|
-
|
|
4
|
-
|
|
4
|
+
resolveRepoClient,
|
|
5
|
+
toInsertDateTime,
|
|
6
|
+
toIsoString
|
|
5
7
|
} from "@jskit-ai/database-runtime/shared";
|
|
6
|
-
import {
|
|
7
|
-
import { normalizeRecordId as normalizeKernelRecordId } from "@jskit-ai/kernel/shared/support/normalize";
|
|
8
|
+
import { normalizeRecordId } from "@jskit-ai/kernel/shared/support/normalize";
|
|
8
9
|
|
|
9
10
|
function nowDb() {
|
|
10
11
|
return toInsertDateTime();
|
|
@@ -25,10 +26,8 @@ function mapSingletonRow(row) {
|
|
|
25
26
|
};
|
|
26
27
|
}
|
|
27
28
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
throw new TypeError("consoleSettingsRepository requires knex.");
|
|
31
|
-
}
|
|
29
|
+
/** @param {{ knex: import("@jskit-ai/database-runtime/server/types").RuntimeKnex }} dependencies */
|
|
30
|
+
function createRepository({ knex }) {
|
|
32
31
|
const withTransaction = createWithTransaction(knex);
|
|
33
32
|
|
|
34
33
|
async function readSingleton(client) {
|
|
@@ -36,13 +35,13 @@ function createRepository(knex) {
|
|
|
36
35
|
}
|
|
37
36
|
|
|
38
37
|
async function getSingleton(options = {}) {
|
|
39
|
-
const client = options
|
|
38
|
+
const client = resolveRepoClient(knex, options);
|
|
40
39
|
return mapSingletonRow(await readSingleton(client));
|
|
41
40
|
}
|
|
42
41
|
|
|
43
42
|
async function ensureOwnerUserId(userId, options = {}) {
|
|
44
|
-
const client = options
|
|
45
|
-
const candidateOwnerUserId =
|
|
43
|
+
const client = resolveRepoClient(knex, options);
|
|
44
|
+
const candidateOwnerUserId = normalizeRecordId(userId, { fallback: null });
|
|
46
45
|
if (!candidateOwnerUserId) {
|
|
47
46
|
throw new TypeError("consoleSettingsRepository.ensureOwnerUserId requires a positive user id.");
|
|
48
47
|
}
|
|
@@ -68,8 +67,9 @@ function createRepository(knex) {
|
|
|
68
67
|
return reloaded.ownerUserId;
|
|
69
68
|
}
|
|
70
69
|
|
|
71
|
-
|
|
72
|
-
|
|
70
|
+
// Touches the singleton row's updated_at; the settings have no columns.
|
|
71
|
+
async function updateSingleton(options = {}) {
|
|
72
|
+
const client = resolveRepoClient(knex, options);
|
|
73
73
|
const dbPatch = {
|
|
74
74
|
updated_at: nowDb()
|
|
75
75
|
};
|
package/src/server/consoleSettings/{bootConsoleSettingsRoutes.js → consoleSettingsRoutes.js}
RENAMED
|
@@ -5,11 +5,8 @@ function resolveConsoleSettingsRecordId() {
|
|
|
5
5
|
return "console-settings";
|
|
6
6
|
}
|
|
7
7
|
|
|
8
|
+
/** @param {import("@jskit-ai/kernel/server/types").RuntimeRouter} router */
|
|
8
9
|
function registerConsoleSettingsRoutes(router) {
|
|
9
|
-
if (!router || typeof router.register !== "function") {
|
|
10
|
-
throw new TypeError("registerConsoleSettingsRoutes requires router.register().");
|
|
11
|
-
}
|
|
12
|
-
|
|
13
10
|
router.register(
|
|
14
11
|
"GET",
|
|
15
12
|
"/api/console/settings",
|
|
@@ -1,34 +1,31 @@
|
|
|
1
|
-
|
|
1
|
+
import { returnJsonApiData } from "@jskit-ai/http-runtime/shared";
|
|
2
|
+
|
|
3
|
+
// The settings record as the console settings resource answers it.
|
|
4
|
+
function buildSettingsResponse(record) {
|
|
2
5
|
return {
|
|
3
|
-
settings: {
|
|
4
|
-
...(record?.settings && typeof record.settings === "object" ? record.settings : {})
|
|
5
|
-
}
|
|
6
|
+
settings: { ...record.settings }
|
|
6
7
|
};
|
|
7
8
|
}
|
|
8
9
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
throw new Error("consoleSettingsService requires consoleService.requireConsoleOwner().");
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
async function getSettings(options = {}) {
|
|
21
|
-
await consoleService.requireConsoleOwner(options?.context, options);
|
|
10
|
+
/**
|
|
11
|
+
* @param {object} dependencies
|
|
12
|
+
* @param {import("../types.js").ConsoleSettingsRepository} dependencies.consoleSettingsRepository
|
|
13
|
+
* @param {import("../types.js").ConsoleService} dependencies.consoleService
|
|
14
|
+
*/
|
|
15
|
+
function createService({ consoleSettingsRepository, consoleService }) {
|
|
16
|
+
async function getSettings({ context }) {
|
|
17
|
+
await consoleService.requireConsoleOwner(context);
|
|
22
18
|
const settings = await consoleSettingsRepository.getSingleton();
|
|
23
19
|
|
|
24
|
-
return buildSettingsResponse(settings);
|
|
20
|
+
return returnJsonApiData(buildSettingsResponse(settings));
|
|
25
21
|
}
|
|
26
22
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
23
|
+
// The console settings have no fields, so an update only touches the row.
|
|
24
|
+
async function updateSettings({ context }) {
|
|
25
|
+
await consoleService.requireConsoleOwner(context);
|
|
26
|
+
const settings = await consoleSettingsRepository.updateSingleton();
|
|
30
27
|
|
|
31
|
-
return buildSettingsResponse(settings);
|
|
28
|
+
return returnJsonApiData(buildSettingsResponse(settings));
|
|
32
29
|
}
|
|
33
30
|
|
|
34
31
|
return Object.freeze({
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
// The type of the `console.core` capability, for features that require it.
|
|
2
|
+
|
|
3
|
+
/** @typedef {ReturnType<typeof import("./consoleRuntime.js").createConsoleRuntime>} ConsoleCore */
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* The console access service: who owns the console.
|
|
7
|
+
* @typedef {ReturnType<typeof import("./consoleSettings/consoleService.js").createService>} ConsoleService
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* The console settings repository: the one console_settings row.
|
|
11
|
+
* @typedef {ReturnType<typeof import("./consoleSettings/consoleSettingsRepository.js").createRepository>} ConsoleSettingsRepository
|
|
12
|
+
*/
|
|
13
|
+
/**
|
|
14
|
+
* The console settings service: `services.settings` of `console.core`.
|
|
15
|
+
* @typedef {ReturnType<typeof import("./consoleSettings/consoleSettingsService.js").createService>} ConsoleSettingsService
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
export {};
|
|
@@ -1,16 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
const apiValidation = String(apiValidationMessage || validation || "Validation failed.");
|
|
1
|
+
/** The messages a resource's operations show when their input fails validation. */
|
|
2
|
+
const OPERATION_MESSAGES = Object.freeze({
|
|
3
|
+
validation: "Validation failed.",
|
|
4
|
+
apiValidation: "Validation failed."
|
|
5
|
+
});
|
|
7
6
|
|
|
8
|
-
|
|
9
|
-
validation,
|
|
10
|
-
apiValidation
|
|
11
|
-
});
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
export {
|
|
15
|
-
createOperationMessages
|
|
16
|
-
};
|
|
7
|
+
export { OPERATION_MESSAGES };
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { createSchema } from "json-rest-schema";
|
|
2
2
|
import { defineCrudResource } from "@jskit-ai/resource-crud-core/shared/crudResource";
|
|
3
|
-
import {
|
|
3
|
+
import { OPERATION_MESSAGES } from "../operationMessages.js";
|
|
4
4
|
|
|
5
5
|
const consoleSettingsBodySchema = createSchema({});
|
|
6
6
|
const consoleSettingsOutputSchema = createSchema({
|
|
@@ -13,7 +13,7 @@ const consoleSettingsOutputSchema = createSchema({
|
|
|
13
13
|
|
|
14
14
|
const consoleSettingsResource = defineCrudResource({
|
|
15
15
|
namespace: "consoleSettings",
|
|
16
|
-
messages:
|
|
16
|
+
messages: OPERATION_MESSAGES,
|
|
17
17
|
crudOperations: ["view", "list", "create", "replace", "patch"],
|
|
18
18
|
crud: {
|
|
19
19
|
output: consoleSettingsOutputSchema,
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import assert from "node:assert/strict";
|
|
2
2
|
import test from "node:test";
|
|
3
|
-
import { registerConsoleSettingsRoutes } from "../src/server/consoleSettings/
|
|
3
|
+
import { registerConsoleSettingsRoutes } from "../src/server/consoleSettings/consoleSettingsRoutes.js";
|
|
4
4
|
|
|
5
5
|
function createReplyDouble() {
|
|
6
6
|
return {
|
|
@@ -23,8 +23,8 @@ function createFixture({ deny = false } = {}) {
|
|
|
23
23
|
async getSingleton() {
|
|
24
24
|
return {};
|
|
25
25
|
},
|
|
26
|
-
async updateSingleton(
|
|
27
|
-
calls.updateSingleton.push(
|
|
26
|
+
async updateSingleton() {
|
|
27
|
+
calls.updateSingleton.push("touched");
|
|
28
28
|
return {};
|
|
29
29
|
}
|
|
30
30
|
}
|
|
@@ -41,7 +41,7 @@ test("consoleSettingsService.getSettings requires owner access and returns norma
|
|
|
41
41
|
}
|
|
42
42
|
};
|
|
43
43
|
|
|
44
|
-
const response = await service.getSettings({ context });
|
|
44
|
+
const response = (await service.getSettings({ context })).value;
|
|
45
45
|
|
|
46
46
|
assert.deepEqual(calls.requireConsoleOwner, [context]);
|
|
47
47
|
assert.deepEqual(response, {
|
|
@@ -57,13 +57,10 @@ test("consoleSettingsService.updateSettings requires owner access before writing
|
|
|
57
57
|
}
|
|
58
58
|
};
|
|
59
59
|
|
|
60
|
-
const response = await service.updateSettings(
|
|
61
|
-
{},
|
|
62
|
-
{ context }
|
|
63
|
-
);
|
|
60
|
+
const response = (await service.updateSettings({ context })).value;
|
|
64
61
|
|
|
65
62
|
assert.deepEqual(calls.requireConsoleOwner, [context]);
|
|
66
|
-
assert.deepEqual(calls.updateSingleton, [
|
|
63
|
+
assert.deepEqual(calls.updateSingleton, ["touched"]);
|
|
67
64
|
assert.deepEqual(response, {
|
|
68
65
|
settings: {}
|
|
69
66
|
});
|
|
@@ -16,6 +16,8 @@ test("console-core exports are explicit and aligned with production/template usa
|
|
|
16
16
|
requiredExports: [
|
|
17
17
|
"./server/ConsoleFeature",
|
|
18
18
|
"./shared/resources/consoleSettingsResource",
|
|
19
|
+
// Features that require this package's capabilities take their types from it.
|
|
20
|
+
"./server/types",
|
|
19
21
|
// The generated client bootstrap registers it, so clients render console's codes.
|
|
20
22
|
"./locales/en.json"
|
|
21
23
|
]
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import assert from "node:assert/strict";
|
|
2
2
|
import test from "node:test";
|
|
3
3
|
import { createAuthExtensions } from "@jskit-ai/auth-core/server/authExtensions";
|
|
4
|
-
import {
|
|
4
|
+
import { ActionProvider } from "@jskit-ai/kernel/server/actions";
|
|
5
|
+
import { silentLogger } from "@jskit-ai/kernel/server/runtime/logger";
|
|
5
6
|
import { createCapabilityRuntime, defineProvider } from "@jskit-ai/kernel/shared/capabilities";
|
|
6
|
-
import { createBootstrapRuntime } from "@jskit-ai/kernel/server/runtime";
|
|
7
|
+
import { createBootstrapRuntime, EventProvider } from "@jskit-ai/kernel/server/runtime";
|
|
7
8
|
import { ConsoleFeature } from "../src/server/ConsoleFeature.js";
|
|
8
9
|
|
|
9
10
|
test("ConsoleFeature assembles owner access, settings, routes, actions, and bootstrap explicitly", async () => {
|
|
@@ -26,6 +27,7 @@ test("ConsoleFeature assembles owner access, settings, routes, actions, and boot
|
|
|
26
27
|
});
|
|
27
28
|
const runtime = createCapabilityRuntime({
|
|
28
29
|
inputs: {
|
|
30
|
+
"runtime.logger": silentLogger,
|
|
29
31
|
"auth.extensions": authExtensions,
|
|
30
32
|
"runtime.bootstrap": bootstrap,
|
|
31
33
|
"runtime.database": { knex() {} },
|
|
@@ -37,7 +39,7 @@ test("ConsoleFeature assembles owner access, settings, routes, actions, and boot
|
|
|
37
39
|
}
|
|
38
40
|
}
|
|
39
41
|
},
|
|
40
|
-
providers: [
|
|
42
|
+
providers: [EventProvider, ActionProvider, ConsoleFeature, probe]
|
|
41
43
|
});
|
|
42
44
|
|
|
43
45
|
await runtime.start();
|
|
@@ -17,7 +17,7 @@ function createKnexStub() {
|
|
|
17
17
|
test("console-core repositories expose withTransaction", async () => {
|
|
18
18
|
const knex = createKnexStub();
|
|
19
19
|
const repositories = [
|
|
20
|
-
createConsoleSettingsRepository(knex)
|
|
20
|
+
createConsoleSettingsRepository({ knex })
|
|
21
21
|
];
|
|
22
22
|
|
|
23
23
|
for (const repository of repositories) {
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import test from "node:test";
|
|
3
|
+
import knex from "knex";
|
|
4
|
+
import initialMigration from "../migrations/console_core_generic_initial.cjs";
|
|
5
|
+
import usersForeignKeyMigration from "../migrations/constraints/console_core_users_foreign_key.cjs";
|
|
6
|
+
|
|
7
|
+
async function usersKeys(db) {
|
|
8
|
+
return (await db.raw("PRAGMA foreign_key_list(console_settings)"))
|
|
9
|
+
.filter((key) => key.table === "users")
|
|
10
|
+
.map(({ from, to, on_delete: onDelete }) => ({ from, to, onDelete }));
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
test("console_settings is created before users exists and gets its users key in the constraints phase", async (t) => {
|
|
14
|
+
const db = knex({ client: "better-sqlite3", connection: { filename: ":memory:" }, useNullAsDefault: true });
|
|
15
|
+
t.after(() => db.destroy());
|
|
16
|
+
|
|
17
|
+
// console-core's table migrations run before users-core's and before the application's.
|
|
18
|
+
await initialMigration.up(db);
|
|
19
|
+
assert.deepEqual(await usersKeys(db), []);
|
|
20
|
+
|
|
21
|
+
await assert.rejects(usersForeignKeyMigration.up(db), /the users table does not exist/u);
|
|
22
|
+
await db.schema.createTable("users", (table) => table.bigInteger("id").primary());
|
|
23
|
+
await usersForeignKeyMigration.up(db);
|
|
24
|
+
const key = [{ from: "owner_user_id", to: "id", onDelete: "SET NULL" }];
|
|
25
|
+
assert.deepEqual(await usersKeys(db), key);
|
|
26
|
+
assert.equal((await db("console_settings")).length, 1);
|
|
27
|
+
|
|
28
|
+
// A table that already has its users key, as databases migrated before this phase do, gains no duplicate.
|
|
29
|
+
await usersForeignKeyMigration.up(db);
|
|
30
|
+
assert.deepEqual(await usersKeys(db), key);
|
|
31
|
+
|
|
32
|
+
await usersForeignKeyMigration.down(db);
|
|
33
|
+
assert.deepEqual(await usersKeys(db), []);
|
|
34
|
+
});
|