@7365admin1/layer-common 3.2.2-staging.191 → 3.2.2-staging.192

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/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@7365admin1/layer-common",
3
3
  "license": "MIT",
4
4
  "type": "module",
5
- "version": "3.2.2-staging.191",
5
+ "version": "3.2.2-staging.192",
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,72 @@
1
+ import assert from "node:assert/strict";
2
+ import { test } from "node:test";
3
+
4
+ import { findServiceProviderIdBySite } from "./data.ts";
5
+
6
+ const SITE = "68f05fcc0c11062938dadada";
7
+ const OTHER_SITE = "68f05fcc0c11062938d00001";
8
+ const PROVIDER = "68f05fcc0c11062938d00002";
9
+
10
+ test("returns the provider that covers this site", () => {
11
+ assert.equal(
12
+ findServiceProviderIdBySite(
13
+ "service-provider",
14
+ [{ _id: PROVIDER, siteId: SITE }],
15
+ SITE
16
+ ),
17
+ PROVIDER
18
+ );
19
+ });
20
+
21
+ test("a non-service-provider account is not scoped by provider at all", () => {
22
+ assert.equal(findServiceProviderIdBySite("customer", [], SITE), null);
23
+ assert.equal(findServiceProviderIdBySite("", undefined, SITE), null);
24
+ });
25
+
26
+ /**
27
+ * Returning null here would drop the provider filter and show the whole site -
28
+ * another provider's work orders included. Refusing is the correct behaviour.
29
+ */
30
+ test("refuses rather than falling back to an unscoped request", () => {
31
+ assert.throws(() =>
32
+ findServiceProviderIdBySite(
33
+ "service-provider",
34
+ [{ _id: PROVIDER, siteId: OTHER_SITE }],
35
+ SITE
36
+ )
37
+ );
38
+ });
39
+
40
+ /**
41
+ * The thrown message is rendered verbatim in a toast on six applications, so it
42
+ * is part of the user interface. Assert what it SAYS, not that it throws: the
43
+ * old message passed a "throws" assertion while quoting a raw ObjectId at a
44
+ * security officer and naming no action.
45
+ */
46
+ test("the message names an action and never quotes an internal id", () => {
47
+ for (const providers of [[{ _id: PROVIDER, siteId: OTHER_SITE }], [], undefined]) {
48
+ let message = "";
49
+ try {
50
+ findServiceProviderIdBySite("service-provider", providers, SITE);
51
+ assert.fail("expected it to refuse");
52
+ } catch (error: any) {
53
+ message = error.message;
54
+ }
55
+ assert.doesNotMatch(message, /[0-9a-f]{24}/i, `leaks an id: ${message}`);
56
+ assert.doesNotMatch(message, /siteId/i, `internal wording: ${message}`);
57
+ assert.match(message, /not linked to this site/i);
58
+ assert.match(message, /administrator/i);
59
+ }
60
+ });
61
+
62
+ /**
63
+ * An account with no serviceProviders field at all used to die on
64
+ * `undefined.find(...)` - a TypeError, which the toast renders as a raw
65
+ * JavaScript error instead of the sentence above.
66
+ */
67
+ test("an account with no provider list still gets the sentence, not a TypeError", () => {
68
+ assert.throws(
69
+ () => findServiceProviderIdBySite("service-provider", undefined, SITE),
70
+ (error: any) => error.constructor === Error && !/undefined/.test(error.message)
71
+ );
72
+ });
package/utils/data.ts CHANGED
@@ -30,17 +30,40 @@ export const errorConverter = (data: any): string => {
30
30
  return error;
31
31
  };
32
32
 
33
+ /**
34
+ * A service-provider account can only be shown ITS OWN work orders, feedbacks
35
+ * and key logs, so every one of those screens scopes its request by the
36
+ * provider record that links this account to the site currently in the URL.
37
+ *
38
+ * When that link does not exist there is nothing to scope by, and returning
39
+ * null instead would drop the filter and show the WHOLE site - another
40
+ * provider's work orders included. So this still refuses, deliberately.
41
+ *
42
+ * What it refuses WITH is the part that matters. The message is thrown straight
43
+ * into a toast on six applications, so it is written for the person reading it:
44
+ * it says what is missing and what to do about it. It used to read
45
+ * "Service provider with siteId 68f0...dadada not found", which quotes an
46
+ * internal identifier at a security officer and names no action - the screen
47
+ * looked broken when the truth was simply that this site is not one of theirs.
48
+ *
49
+ * The site list in the sidebar offers EVERY site in the organisation, not only
50
+ * the ones this account covers, so an officer can walk into this by picking a
51
+ * neighbouring site. That is why the wording assumes an ordinary mistake and
52
+ * not a fault.
53
+ */
33
54
  export const findServiceProviderIdBySite = (
34
55
  type: string,
35
56
  serviceProviders: any,
36
57
  siteId: string
37
58
  ) => {
38
59
  if (type === "service-provider") {
39
- const result = serviceProviders.find(
60
+ const result = serviceProviders?.find(
40
61
  (item: Record<string, any>) => item.siteId === siteId
41
62
  );
42
63
  if (!result?._id) {
43
- throw new Error(`Service provider with siteId ${siteId} not found`);
64
+ throw new Error(
65
+ "Your service provider account is not linked to this site, so there is nothing to show here. Choose a site your company covers, or ask your administrator to link your company to this one."
66
+ );
44
67
  }
45
68
  return result?._id;
46
69
  }