@7365admin1/layer-common 4.28.1-staging.356 → 4.28.1-staging.358

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.
@@ -376,6 +376,7 @@ const visitorRecords = ref<Record<string, HidAccessRecord>>({});
376
376
  const logs = ref<HidAccessRecord[]>([]);
377
377
  const liveAccessLogs = ref<HidAccessRecord[]>([]);
378
378
  const administratorUserIds = ref<Set<number>>(new Set());
379
+ const { selectHidReader, resolveForSite } = useHidReaderSelection();
379
380
  const selectedReaderId = ref("");
380
381
  const activeTab = ref<AccessTab>("all");
381
382
  const search = ref("");
@@ -592,9 +593,11 @@ async function loadReaders() {
592
593
  if (!props.site) return;
593
594
  const response = await getReaders({ site: props.site, page: 1, limit: 100 });
594
595
  readers.value = response?.items ?? response?.data?.items ?? response?.data?.readers ?? [];
595
- if (!selectedReaderId.value && readers.value.length) {
596
- selectedReaderId.value = readers.value[0]._id;
597
- }
596
+ /* Shared with every other HID screen — see `useHidReaderSelection`. The
597
+ resolver refuses a remembered reader that is not this site's, so a site
598
+ switch or a deleted reader falls back to the first rather than querying
599
+ an id the site does not own. */
600
+ selectedReaderId.value = resolveForSite(props.site, readers.value);
598
601
  }
599
602
 
600
603
  async function loadDashboard() {
@@ -1572,6 +1575,12 @@ function escapeHtml(value: unknown) {
1572
1575
  .replace(/"/g, "&quot;")
1573
1576
  .replace(/'/g, "&#039;");
1574
1577
  }
1578
+
1579
+ /* Publish every change, including the seed above: a screen that had to fall
1580
+ back to the first reader should leave the others agreeing with it. */
1581
+ watch(selectedReaderId, (value) => {
1582
+ if (value) selectHidReader(props.site, value);
1583
+ });
1575
1584
  </script>
1576
1585
 
1577
1586
  <style scoped>
@@ -126,6 +126,7 @@ const { getReaders, getProfileQr, issueProfileQr, deleteProfileQr } = useHidAmic
126
126
  type HidReaderOption = { _id: string; name?: string; portalName?: string };
127
127
 
128
128
  const readers = ref<HidReaderOption[]>([]);
129
+ const { selectHidReader, resolveForSite } = useHidReaderSelection();
129
130
  const selectedReaderId = ref("");
130
131
  const profile = ref<HidProfileQr | null>(null);
131
132
  const qrImage = ref("");
@@ -203,9 +204,11 @@ async function loadReaders() {
203
204
  const data = toRecord(response.data);
204
205
  const items = response.items ?? data.items ?? data.readers ?? [];
205
206
  readers.value = (Array.isArray(items) ? items : []).filter((reader) => reader?._id);
206
- if (!selectedReaderId.value && readers.value.length) {
207
- selectedReaderId.value = String(readers.value[0]._id);
208
- }
207
+ /* Shared with every other HID screen — see `useHidReaderSelection`. The
208
+ resolver refuses a remembered reader that is not this site's, so a site
209
+ switch or a deleted reader falls back to the first rather than querying
210
+ an id the site does not own. */
211
+ selectedReaderId.value = resolveForSite(props.site, readers.value);
209
212
  }
210
213
 
211
214
  async function loadProfile() {
@@ -263,6 +266,12 @@ watch(
263
266
  },
264
267
  { immediate: true },
265
268
  );
269
+
270
+ /* Publish every change, including the seed above: a screen that had to fall
271
+ back to the first reader should leave the others agreeing with it. */
272
+ watch(selectedReaderId, (value) => {
273
+ if (value) selectHidReader(props.site, value);
274
+ });
266
275
  </script>
267
276
 
268
277
  <style scoped>
@@ -208,6 +208,7 @@ const {
208
208
  } = useHidAmico();
209
209
  const readers = ref<HidReader[]>([]);
210
210
  const users = ref<HidReaderUser[]>([]);
211
+ const { selectHidReader, resolveForSite } = useHidReaderSelection();
211
212
  const selectedReaderId = ref("");
212
213
  const search = ref("");
213
214
  const userType = ref<"all" | "user" | "visitor">("all");
@@ -270,7 +271,11 @@ async function loadReaders() {
270
271
  readers.value = (Array.isArray(items) ? items : [])
271
272
  .map(toReader)
272
273
  .filter((reader): reader is HidReader => reader !== null);
273
- if (!selectedReaderId.value && readers.value.length) selectedReaderId.value = readers.value[0]._id;
274
+ /* Shared with every other HID screen — see `useHidReaderSelection`. The
275
+ resolver refuses a remembered reader that is not this site's, so a site
276
+ switch or a deleted reader falls back to the first rather than querying
277
+ an id the site does not own. */
278
+ selectedReaderId.value = resolveForSite(props.site, readers.value);
274
279
  } catch (error: unknown) {
275
280
  readers.value = [];
276
281
  showToast(getErrorMessage(error), "error");
@@ -541,6 +546,12 @@ function toNumber(value: unknown, fallback: number) {
541
546
  const number = Number(value);
542
547
  return Number.isFinite(number) ? number : fallback;
543
548
  }
549
+
550
+ /* Publish every change, including the seed above: a screen that had to fall
551
+ back to the first reader should leave the others agreeing with it. */
552
+ watch(selectedReaderId, (value) => {
553
+ if (value) selectHidReader(props.site, value);
554
+ });
544
555
  </script>
545
556
 
546
557
  <style scoped>
@@ -750,6 +750,7 @@ type HidIdentityPayload = Record<string, unknown> & {
750
750
  const readers = ref<HidReader[]>([]);
751
751
  const users = ref<HidUser[]>([]);
752
752
  const administratorUserIds = ref<Set<number>>(new Set());
753
+ const { selectHidReader, resolveForSite } = useHidReaderSelection();
753
754
  const selectedReaderId = ref("");
754
755
  const search = ref("");
755
756
  const status = ref("");
@@ -1136,9 +1137,11 @@ async function loadReaders() {
1136
1137
  readers.value = (Array.isArray(items) ? items : [])
1137
1138
  .map(toHidReader)
1138
1139
  .filter((reader): reader is HidReader => reader !== null);
1139
- if (!selectedReaderId.value && readers.value.length) {
1140
- selectedReaderId.value = readers.value[0]._id;
1141
- }
1140
+ /* Shared with every other HID screen — see `useHidReaderSelection`. The
1141
+ resolver refuses a remembered reader that is not this site's, so a site
1142
+ switch or a deleted reader falls back to the first rather than querying
1143
+ an id the site does not own. */
1144
+ selectedReaderId.value = resolveForSite(props.site, readers.value);
1142
1145
  }
1143
1146
 
1144
1147
  /**
@@ -2577,6 +2580,12 @@ function toHidCard(value: unknown): HidCard {
2577
2580
  source: card.source === "reader" || card.source === "manual" || card.source === "device" ? card.source : undefined,
2578
2581
  };
2579
2582
  }
2583
+
2584
+ /* Publish every change, including the seed above: a screen that had to fall
2585
+ back to the first reader should leave the others agreeing with it. */
2586
+ watch(selectedReaderId, (value) => {
2587
+ if (value) selectHidReader(props.site, value);
2588
+ });
2580
2589
  </script>
2581
2590
 
2582
2591
  <style scoped>
@@ -134,6 +134,7 @@ import {
134
134
  timeAgo,
135
135
  type TInboxItem,
136
136
  } from "../utils/notification-inbox";
137
+ import { notificationTarget } from "../utils/notification-target";
137
138
 
138
139
  /**
139
140
  * The top-bar notification feed for the web, in every signed-in app.
@@ -165,6 +166,8 @@ defineProps({
165
166
 
166
167
  const { getAll, getUnreadCount, markAsRead, markAllAsRead } = useNotification();
167
168
  const { currentUser } = useLocalAuth();
169
+ const route = useRoute();
170
+ const router = useRouter();
168
171
 
169
172
  const open = ref(false);
170
173
  const loading = ref(false);
@@ -269,16 +272,49 @@ function announce(fresh: TInboxItem[]) {
269
272
  }
270
273
  }
271
274
 
275
+ /**
276
+ * Clicking a row does two separate things, and the second one is why the row is
277
+ * a button at all: mark it read, and go to what it is about.
278
+ *
279
+ * They are kept apart deliberately. Marking read is best-effort and reverts on
280
+ * failure; navigation must not wait on that round trip, and must still happen
281
+ * for a row that was ALREADY read — a person re-opening the bell to find the
282
+ * order they were told about is the ordinary case, and the old early-return on
283
+ * `hasRead` would have sent them nowhere.
284
+ */
272
285
  async function openNotification(item: Record<string, any>) {
286
+ markRead(item);
287
+ await goToSubject(item);
288
+ }
289
+
290
+ function markRead(item: Record<string, any>) {
273
291
  if (item.hasRead) return;
274
292
  // Marked locally first: waiting on a round trip to move the dot reads as a dead control.
275
293
  item.hasRead = true;
276
294
  unread.value = Math.max(0, unread.value - 1);
277
- try {
278
- await markAsRead(String(item._id), userId.value);
279
- } catch {
295
+ markAsRead(String(item._id), userId.value).catch(() => {
280
296
  item.hasRead = false;
281
297
  unread.value += 1;
298
+ });
299
+ }
300
+
301
+ /**
302
+ * `hasRoute` is the whole reason an org-app route name may live in the shared
303
+ * layer: the ten apps that have no order queue simply do not match it, and the
304
+ * click there stays what it always was.
305
+ */
306
+ async function goToSubject(item: Record<string, any>) {
307
+ const target = notificationTarget(item as any, {
308
+ organization: String(route.params?.organization ?? ""),
309
+ });
310
+ if (!target) return;
311
+ if (typeof router.hasRoute !== "function" || !router.hasRoute(target.name)) return;
312
+
313
+ open.value = false;
314
+ try {
315
+ await router.push(target);
316
+ } catch {
317
+ // A cancelled or duplicate navigation is not worth an error in the top bar.
282
318
  }
283
319
  }
284
320
 
@@ -0,0 +1,50 @@
1
+ import {
2
+ EMPTY_HID_READER_CHOICE,
3
+ resolveHidReaderId,
4
+ type HidReaderChoice,
5
+ } from "../utils/hid-reader-selection";
6
+
7
+ /**
8
+ * ONE HID READER CHOICE, SHARED BY EVERY HID SCREEN.
9
+ *
10
+ * Declared at MODULE scope on purpose. Each screen held its own `ref("")`, and
11
+ * because the HID screens are separate pages the ref was rebuilt on every
12
+ * navigation — HID Reader Users, HID Cards, Access Logs and HID QR Code each
13
+ * reset to the first reader no matter what had just been picked next door.
14
+ *
15
+ * A module-scoped ref is enough because moving between those pages is a ROUTE
16
+ * change, not a page load: the menu's items carry a `route` and
17
+ * `NavigationItem.vue` renders them through `:to`, so vue-router handles them
18
+ * and this module is never re-evaluated.
19
+ *
20
+ * Deliberately NOT persisted. `localStorage` would carry the choice through a
21
+ * hard refresh as well, which is a real but much smaller benefit than the
22
+ * navigation case, and it is worth adding only once somebody has felt the
23
+ * absence. See `utils/hid-reader-selection.ts` for the staleness rule that
24
+ * would have to hold either way.
25
+ *
26
+ * Intercom keeps its own reader and is not wired to this. Its question is which
27
+ * panel you are calling, not which door you are administering, and joining them
28
+ * would mean switching reader on Access Logs changed who you would dial.
29
+ */
30
+ const choice = ref<HidReaderChoice>({ ...EMPTY_HID_READER_CHOICE });
31
+
32
+ export default function useHidReaderSelection() {
33
+ /** Remember a choice. The site travels with it; an id alone means nothing. */
34
+ function selectHidReader(siteId: string, readerId: string) {
35
+ choice.value = { siteId: String(siteId ?? ""), readerId: String(readerId ?? "") };
36
+ }
37
+
38
+ /**
39
+ * The reader this screen should show, given the readers it just loaded.
40
+ *
41
+ * Call it after the reader list arrives, and write the answer back with
42
+ * `selectHidReader` so the screens agree on what "current" means even when
43
+ * one of them had to fall back to the first reader.
44
+ */
45
+ function resolveForSite(siteId: string, readers: readonly { _id?: unknown }[]) {
46
+ return resolveHidReaderId(siteId, readers, choice.value);
47
+ }
48
+
49
+ return { choice, selectHidReader, resolveForSite };
50
+ }
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@7365admin1/layer-common",
3
3
  "license": "MIT",
4
4
  "type": "module",
5
- "version": "4.28.1-staging.356",
5
+ "version": "4.28.1-staging.358",
6
6
  "author": "7365admin1",
7
7
  "main": "./nuxt.config.ts",
8
8
  "//files": "What a consumer extending this layer actually loads. Without this npm ships the whole working tree - the changesets, the CI workflows, the render harness in tools/ and any scratch directory that happened to exist at publish time. Nuxt resolves a layer by directory, so every runtime directory below has to stay listed; adding a new top-level runtime directory means adding it here too.",
@@ -0,0 +1,52 @@
1
+ /**
2
+ * WHICH HID READER THE OPERATOR IS WORKING ON — the one rule behind it.
3
+ *
4
+ * Every HID screen used to hold its own `ref("")` and default it to
5
+ * `readers[0]`, so picking HID 2 on HID Reader Users and clicking through to
6
+ * HID Cards put you back on HID 1. They are separate PAGES, so the component
7
+ * unmounted and the ref was rebuilt from scratch every time — there was no way
8
+ * to make a choice stick. "I am working on HID 2" is a mode, not a per-screen
9
+ * preference.
10
+ *
11
+ * Navigation between those pages is a route change rather than a page load
12
+ * (`NavigationItem.vue` renders the menu's `route` through `:to`), so the
13
+ * module-scoped ref in `useHidReaderSelection` survives it. This file holds the
14
+ * part worth testing: deciding whether a remembered choice may still be used.
15
+ */
16
+
17
+ export type HidReaderChoice = { siteId: string; readerId: string };
18
+
19
+ /** Anything with an id — the four screens all carry a different reader shape. */
20
+ type ReaderLike = { _id?: unknown };
21
+
22
+ export const EMPTY_HID_READER_CHOICE: HidReaderChoice = { siteId: "", readerId: "" };
23
+
24
+ /**
25
+ * The reader a screen should show, given what this site actually has.
26
+ *
27
+ * A remembered id is honoured ONLY when it belongs to this site and is still in
28
+ * the list. That single condition covers every way it goes stale:
29
+ *
30
+ * - the operator switched site, so the id belongs to a site they have left
31
+ * - the reader was deleted or deactivated since they chose it
32
+ * - nothing has been chosen yet
33
+ *
34
+ * In all three the first reader is returned instead. The alternative — using
35
+ * the id anyway — sends requests for a reader the site does not own, which come
36
+ * back empty and read on screen as a reader with no users, no cards and no
37
+ * logs, rather than as the mistake it is.
38
+ */
39
+ export function resolveHidReaderId(
40
+ siteId: string,
41
+ readers: readonly ReaderLike[] | null | undefined,
42
+ choice: HidReaderChoice = EMPTY_HID_READER_CHOICE,
43
+ ): string {
44
+ const ids = (Array.isArray(readers) ? readers : [])
45
+ .map((reader) => String(reader?._id ?? ""))
46
+ .filter(Boolean);
47
+
48
+ if (choice.siteId === String(siteId ?? "") && ids.includes(choice.readerId)) {
49
+ return choice.readerId;
50
+ }
51
+ return ids[0] ?? "";
52
+ }
@@ -0,0 +1,75 @@
1
+ /**
2
+ * Where a notification takes you when you click it.
3
+ *
4
+ * Kept free of Vue and Nuxt so the decision is unit-tested directly
5
+ * (`utils/notification-target.test.ts`); `NotificationBell.vue` does nothing
6
+ * with the answer but hand it to the router.
7
+ *
8
+ * WHY THE BELL NEEDED THIS AT ALL
9
+ * The bell has drawn rows since it shipped and clicking one only ever marked it
10
+ * read. For an expiring document that is survivable — the row says what it says.
11
+ * For a marketplace order it is not: "A new marketplace order has reached your
12
+ * company" is a message whose entire point is the order, and a vendor who taps
13
+ * the push on their phone and then goes looking in the org app had nowhere to
14
+ * land.
15
+ *
16
+ * WHAT THE ROW ACTUALLY CARRIES
17
+ * `iservice365-API-core`'s marketplace senders store `module` as the display
18
+ * tag and the record's id in `params.id` — for the vendor rows that id is the
19
+ * SUB-order, which is the same id the vendor's order queue routes on. `screen`
20
+ * is deliberately empty on those rows (it is a mobile deep-link path, and no
21
+ * mobile screen exists), so the module is what we route on, not `screen`.
22
+ *
23
+ * WHY AN APP-SPECIFIC ROUTE NAME LIVES IN THE SHARED LAYER
24
+ * Eleven web apps render this bell and only `iservice365-web-app-org` has an
25
+ * order queue. Rather than make eleven apps configure a map, the name is
26
+ * written down once here and the bell asks its own router whether the route
27
+ * exists before pushing — so in the ten apps without the page the click stays
28
+ * exactly what it was: mark as read, go nowhere.
29
+ */
30
+
31
+ /** Only the fields a destination is decided from. */
32
+ export type TTargetRow = {
33
+ module?: string;
34
+ params?: { id?: string } | null;
35
+ };
36
+
37
+ /** What the surrounding app knows that the stored row does not. */
38
+ export type TTargetContext = {
39
+ /** The organization in the current URL. Org routes are all nested under it. */
40
+ organization?: string;
41
+ };
42
+
43
+ export type TNotificationTarget = {
44
+ name: string;
45
+ params: Record<string, string>;
46
+ };
47
+
48
+ /** Nuxt's generated names for `pages/org/[organization]/marketplace/orders/`. */
49
+ export const ORDER_QUEUE_ROUTE = "org-organization-marketplace-orders";
50
+ export const ORDER_DETAIL_ROUTE = "org-organization-marketplace-orders-id";
51
+
52
+ /** The stored `module` tag on a row sent to a vendor's staff. */
53
+ export const VENDOR_ORDER_MODULE = "marketplace-vendor-order";
54
+
55
+ export function notificationTarget(
56
+ item: TTargetRow | null | undefined,
57
+ context: TTargetContext = {},
58
+ ): TNotificationTarget | null {
59
+ const organization = String(context.organization ?? "").trim();
60
+
61
+ if (String(item?.module ?? "") !== VENDOR_ORDER_MODULE) return null;
62
+
63
+ // Every order route is nested under the organization. Without one there is no
64
+ // URL to build, and guessing an organization for somebody who belongs to
65
+ // several is worse than leaving the click as it was.
66
+ if (!organization) return null;
67
+
68
+ const id = String(item?.params?.id ?? "").trim();
69
+
70
+ // No id — an older row, or one whose params were dropped — still has an
71
+ // honest destination: the queue the order is sitting in.
72
+ if (!id) return { name: ORDER_QUEUE_ROUTE, params: { organization } };
73
+
74
+ return { name: ORDER_DETAIL_ROUTE, params: { organization, id } };
75
+ }