@jskit-ai/users-core 0.2.30 → 0.2.31

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
+ - Saving a profile picture (`POST /api/settings/profile/avatar`) answers 200 with the account's settings. The route sent the `settings.profile.avatar.upload` action's JSON:API result where its response schema, and the account settings page's upload, read the plain settings: the picture was saved and every save answered 500. The route now sends the settings the result carries (`jsonApiResultValue`); `accountProfileRoutes.test.js` proves it over HTTP.
5
6
  - The user-administration-server pattern's workspace example (`users-workspace`'s `workspaceMembersOnly`) narrows users to the workspace's members in SQL. A list page is one report over the `users.profiles` and `workspaces.memberships` join sources: the users the list's filters match who hold an active membership, in the users list's order (display name, email, id), with a keyset cursor. A view reads that one membership with `findByWorkspaceIdAndUserId`. Before this, both read every active member id with `activeMemberIds` first. The page's cursor is the report's, and a cursor the list did not answer is refused with 400 `users.list.cursorInvalid`. workspaces-core's `userAdministrationExample.mysql.test.js` proves it on MariaDB/MySQL.
6
7
  - `users.profiles` and `users.settings` answer `joinSource({ alias, filters }, options)`, the handle's join source, as `workspaces` and `workspaces.memberships` do: another owner's report reads people and their settings beside its own rows in one statement, under the users table's row policy, where it needed an app-side raw-SQL exception. Its tests run on MariaDB/MySQL (`joinSource.mysql.test.js`) and PostgreSQL (`joinSource.postgres.test.js`).
7
8
  - Two first sign-ins of one identity at once (a double-clicked sign-in, two tabs) make one user, and both answer it: the sync that loses the insert (`auth.profile.projector`'s `syncIdentityProfile`) rolls back in full and runs again, reading the winner's profile as a returning sign-in does, with `created: false` for the lifecycle contributors. It failed with the database's duplicate-entry error (`ER_DUP_ENTRY`). Inside a caller's transaction (`trx`) the loss still fails that transaction, for the caller's own retry.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jskit-ai/users-core",
3
- "version": "0.2.30",
3
+ "version": "0.2.31",
4
4
  "type": "module",
5
5
  "scripts": {
6
6
  "test": "jskit test"
@@ -12,7 +12,7 @@
12
12
  "dependencies": {
13
13
  "@jskit-ai/crud-core": "0.2.28",
14
14
  "@jskit-ai/resource-crud-core": "0.1.213",
15
- "@jskit-ai/users-core": "0.2.30"
15
+ "@jskit-ai/users-core": "0.2.31"
16
16
  },
17
17
  "jskit": {
18
18
  "kind": "runtime",
@@ -12,8 +12,8 @@
12
12
  "dependencies": {
13
13
  "@jskit-ai/crud-core": "0.2.28",
14
14
  "@jskit-ai/resource-crud-core": "0.1.213",
15
- "@jskit-ai/users-core": "0.2.30",
16
- "@jskit-ai/workspaces-core": "0.2.30"
15
+ "@jskit-ai/users-core": "0.2.31",
16
+ "@jskit-ai/workspaces-core": "0.2.31"
17
17
  },
18
18
  "peerDependencies": {
19
19
  "@jskit-ai/kernel": "0.2.20",
@@ -1,3 +1,4 @@
1
+ import { jsonApiResultValue } from "@jskit-ai/http-runtime/shared";
1
2
  import { withStandardErrorResponses } from "@jskit-ai/http-runtime/shared/validators/errorResponses";
2
3
  import { createJsonApiResourceRouteContract } from "@jskit-ai/http-runtime/shared/validators/jsonApiRouteTransport";
3
4
  import { DEFAULT_IMAGE_UPLOAD_MAX_BYTES } from "@jskit-ai/uploads-runtime/shared";
@@ -134,7 +135,9 @@ function registerAccountProfileRoutes(router, { accountProfileService, authServi
134
135
  }
135
136
  });
136
137
 
137
- reply.code(200).send(response);
138
+ // The action answers JSON:API data, as the account's JSON:API routes need. This multipart route has no
139
+ // JSON:API transport to encode it: it answers the plain settings its schema declares and the upload reads.
140
+ reply.code(200).send(jsonApiResultValue(response));
138
141
  }
139
142
  );
140
143
 
@@ -0,0 +1,102 @@
1
+ import assert from "node:assert/strict";
2
+ import test from "node:test";
3
+ import Fastify from "fastify";
4
+ import { createActionCatalogue } from "@jskit-ai/kernel/server/actions";
5
+ import { readSingleMultipartFile } from "@jskit-ai/uploads-runtime/server/multipart/readSingleMultipartFile";
6
+ import { registerMultipartSupport } from "@jskit-ai/uploads-runtime/server/multipart/registerMultipartSupport";
7
+ import { createRouter } from "../../kernel/server/http/lib/router.js";
8
+ import { registerRoutes } from "../../kernel/server/http/lib/routeRegistration.js";
9
+ import { registerApiErrorHandler } from "../../kernel/server/http/errorHandling.js";
10
+ import { createAccountProfileActions } from "../src/server/accountProfile/accountProfileActions.js";
11
+ import { registerAccountProfileRoutes } from "../src/server/accountProfile/accountProfileRoutes.js";
12
+ import { createService as createAccountProfileService } from "../src/server/accountProfile/accountProfileService.js";
13
+ import { createService as createAvatarService } from "../src/server/accountProfile/avatarService.js";
14
+ import { createService as createAvatarStorageService } from "../src/server/accountProfile/avatarStorageService.js";
15
+ import { DEFAULT_USER_SETTINGS } from "../src/shared/settings.js";
16
+
17
+ const PNG_BYTES = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d]);
18
+
19
+ // The account profile as an application serves it: users-core's profile, avatar and avatar storage services, its
20
+ // actions and its routes on Fastify with multipart support, with the profile, the settings and runtime.storage in
21
+ // memory, and the signed-in user 7.
22
+ async function profileApp(t) {
23
+ const storedFiles = new Map();
24
+ let profile = { id: 7, displayName: "Ada", email: "ada@example.test", avatarStorageKey: null, avatarVersion: null };
25
+ const userProfilesRepository = {
26
+ async findById() {
27
+ return { ...profile };
28
+ },
29
+ async updateAvatarById(_userId, { avatarStorageKey, avatarVersion }) {
30
+ profile = { ...profile, avatarStorageKey, avatarVersion };
31
+ return { ...profile };
32
+ }
33
+ };
34
+ const avatarService = createAvatarService({
35
+ userProfilesRepository,
36
+ avatarStorageService: createAvatarStorageService({
37
+ storage: {
38
+ async getItemRaw(key) {
39
+ return storedFiles.get(key) ?? null;
40
+ },
41
+ async setItemRaw(key, value) {
42
+ storedFiles.set(key, value);
43
+ },
44
+ async removeItem(key) {
45
+ storedFiles.delete(key);
46
+ }
47
+ }
48
+ })
49
+ });
50
+ const authService = { getCapabilities: () => ({}) };
51
+ const accountProfileService = createAccountProfileService({
52
+ userProfilesRepository,
53
+ userSettingsRepository: {
54
+ async ensureForUserId() {
55
+ return { ...DEFAULT_USER_SETTINGS };
56
+ }
57
+ },
58
+ authService,
59
+ avatarService
60
+ });
61
+
62
+ const actions = createActionCatalogue();
63
+ actions.register({ contributorId: "users.core", domain: "settings", actions: createAccountProfileActions({ accountProfileService }) });
64
+ actions.registerContextContributor({ id: "test.actor", contribute: () => ({ actor: { id: "7" } }) });
65
+ const router = createRouter();
66
+ registerAccountProfileRoutes(router, { accountProfileService, authService, uploads: { readSingleMultipartFile } });
67
+
68
+ const app = Fastify();
69
+ t.after(() => app.close());
70
+ await registerMultipartSupport(app);
71
+ registerApiErrorHandler(app);
72
+ registerRoutes(app, { routes: router.list(), actions });
73
+ return { app, storedFiles };
74
+ }
75
+
76
+ test("saving a profile picture answers 200 with the account's plain settings", async (t) => {
77
+ const { app, storedFiles } = await profileApp(t);
78
+
79
+ const response = await app.inject({
80
+ method: "POST",
81
+ url: "/api/settings/profile/avatar",
82
+ headers: { "content-type": "multipart/form-data; boundary=jskit-avatar" },
83
+ payload: Buffer.concat([
84
+ Buffer.from(
85
+ '--jskit-avatar\r\nContent-Disposition: form-data; name="avatar"; filename="avatar.png"\r\nContent-Type: image/png\r\n\r\n'
86
+ ),
87
+ PNG_BYTES,
88
+ Buffer.from("\r\n--jskit-avatar--\r\n")
89
+ ])
90
+ });
91
+
92
+ // Before, the route sent the action's JSON:API result where its schema and the upload read the plain settings:
93
+ // the picture was saved, and serializing the answer failed with a 500.
94
+ assert.equal(response.statusCode, 200, response.body);
95
+ assert.deepEqual(storedFiles.get("users/avatars/7/avatar"), PNG_BYTES);
96
+ const settings = response.json();
97
+ assert.deepEqual(Object.keys(settings).sort(), ["notifications", "preferences", "profile", "security"]);
98
+ assert.equal(settings.profile.displayName, "Ada");
99
+ assert.equal(settings.profile.avatar.hasUploadedAvatar, true);
100
+ assert.match(settings.profile.avatar.effectiveUrl, /^\/api\/settings\/profile\/avatar\?v=\d+$/u);
101
+ assert.equal(settings.preferences.avatarSize, DEFAULT_USER_SETTINGS.avatarSize);
102
+ });
@@ -234,6 +234,9 @@ test("account route handlers preserve omitted optional action fields", async ()
234
234
  if (payload.actionId === "settings.security.oauth.link.start") {
235
235
  return { url: "/oauth/link" };
236
236
  }
237
+ if (payload.actionId === "settings.profile.avatar.upload") {
238
+ return { __jskitJsonApiResult: true, kind: "data", value: {} };
239
+ }
237
240
  return {};
238
241
  };
239
242