@7365admin1/layer-common 4.5.1-staging.293 → 4.5.1-staging.294

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.
@@ -1,3 +1,8 @@
1
+ // Explicit import rather than Nuxt auto-import. Identical inside the app, and it
2
+ // makes this catalogue readable OUTSIDE a Nuxt context -- which is what lets
3
+ // `utils/module-applications.ts` derive the module-to-application map from the
4
+ // apps themselves instead of a hand-written list that silently goes stale.
5
+ import { useCommonPermissions } from "./useCommonPermission";
1
6
  export default function useCleaningPermission() {
2
7
  const {
3
8
  rolePermissions,
@@ -1,3 +1,8 @@
1
+ // Explicit import rather than Nuxt auto-import. Identical inside the app, and it
2
+ // makes this catalogue readable OUTSIDE a Nuxt context -- which is what lets
3
+ // `utils/module-applications.ts` derive the module-to-application map from the
4
+ // apps themselves instead of a hand-written list that silently goes stale.
5
+ import { useCommonPermissions } from "./useCommonPermission";
1
6
  /**
2
7
  * THE LANDSCAPE ROLE CATALOGUE — the shared copy.
3
8
  *
@@ -1,3 +1,9 @@
1
+ // Explicit import rather than Nuxt auto-import. Identical inside the app, and it
2
+ // makes this catalogue readable OUTSIDE a Nuxt context -- which is what lets
3
+ // `utils/module-applications.ts` derive the module-to-application map from the
4
+ // apps themselves instead of a hand-written list that silently goes stale.
5
+ import { useCommonPermissions } from "./useCommonPermission";
6
+ import useCleaningPermission from "./useCleaningPermission";
1
7
  /**
2
8
  * THE MECHANICAL & ELECTRICAL ROLE CATALOGUE — the shared copy.
3
9
  *
@@ -1,3 +1,8 @@
1
+ // Explicit import rather than Nuxt auto-import. Identical inside the app, and it
2
+ // makes this catalogue readable OUTSIDE a Nuxt context -- which is what lets
3
+ // `utils/module-applications.ts` derive the module-to-application map from the
4
+ // apps themselves instead of a hand-written list that silently goes stale.
5
+ import { useCommonPermissions } from "./useCommonPermission";
1
6
  /**
2
7
  * THE PEST CONTROL ROLE CATALOGUE — the shared copy.
3
8
  *
@@ -1,3 +1,8 @@
1
+ // Explicit import rather than Nuxt auto-import. Identical inside the app, and it
2
+ // makes this catalogue readable OUTSIDE a Nuxt context -- which is what lets
3
+ // `utils/module-applications.ts` derive the module-to-application map from the
4
+ // apps themselves instead of a hand-written list that silently goes stale.
5
+ import { useCommonPermissions } from "./useCommonPermission";
1
6
  /**
2
7
  * THE POOL MAINTENANCE ROLE CATALOGUE — the shared copy.
3
8
  *
@@ -1,3 +1,8 @@
1
+ // Explicit import rather than Nuxt auto-import. Identical inside the app, and it
2
+ // makes this catalogue readable OUTSIDE a Nuxt context -- which is what lets
3
+ // `utils/module-applications.ts` derive the module-to-application map from the
4
+ // apps themselves instead of a hand-written list that silently goes stale.
5
+ import { useCommonPermissions } from "./useCommonPermission";
1
6
  /**
2
7
  * THE PROPERTY-MANAGEMENT ROLE CATALOGUE — the shared copy.
3
8
  *
@@ -1,3 +1,8 @@
1
+ // Explicit import rather than Nuxt auto-import. Identical inside the app, and it
2
+ // makes this catalogue readable OUTSIDE a Nuxt context -- which is what lets
3
+ // `utils/module-applications.ts` derive the module-to-application map from the
4
+ // apps themselves instead of a hand-written list that silently goes stale.
5
+ import { useCommonPermissions } from "./useCommonPermission";
1
6
  /**
2
7
  * THE SECURITY ROLE CATALOGUE — the shared copy.
3
8
  *
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.5.1-staging.293",
5
+ "version": "4.5.1-staging.294",
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,146 @@
1
+ import useSecurityPermission from "../composables/useSecurityPermission";
2
+ import useCleaningPermission from "../composables/useCleaningPermission";
3
+ import useLandscapingPermission from "../composables/useLandscapingPermission";
4
+ import useMechanicalElectricalPermission from "../composables/useMechanicalElectricalPermission";
5
+ import usePestControlPermission from "../composables/usePestControlPermission";
6
+ import usePoolMaintenancePermission from "../composables/usePoolMaintenancePermission";
7
+ import usePropertyPermission from "../composables/usePropertyPermission";
8
+
9
+ /**
10
+ * WHICH APPLICATION EACH MODULE BELONGS TO.
11
+ *
12
+ * Owner report, 2026-09-10: *"I check security company and nature of business is
13
+ * security but have cleaning schedule management."* The Seven365 console's
14
+ * "Modules offered to this client" card listed the whole 47-entry catalogue as
15
+ * one flat alphabetical list, with nothing to say which service each module is
16
+ * part of — so a security client was offered Cleaning schedule management, and a
17
+ * cleaning client was offered Virtual patrol.
18
+ *
19
+ * R1 is written in terms of APPLICATIONS: *"assigns roles & permissions to an
20
+ * ORGANISATION for the applications it needs (Security, Cleaning, Pest, M&E, MA,
21
+ * Landscape, Pool)."* This is the map that makes that possible.
22
+ *
23
+ * ## DERIVED, never hand-written
24
+ *
25
+ * Each application's role editor already declares exactly what it offers — its
26
+ * `use*Permission` composable IS that declaration, and it is the same object the
27
+ * app passes to `RolePermissionMain` as `:permissions`. So the grouping is read
28
+ * from those composables at load time rather than copied out of them.
29
+ *
30
+ * A hand-written list would be correct on the day it was written and wrong the
31
+ * first time somebody adds a module to an app — silently, because nothing would
32
+ * fail. Deriving it means the console cannot disagree with the apps.
33
+ *
34
+ * The composables are pure (no Nuxt APIs, no lifecycle hooks), so calling them
35
+ * here is safe and costs one object construction each.
36
+ *
37
+ * ## A module can belong to SEVERAL applications, and that is not a mistake
38
+ *
39
+ * `cleaning-schedule-mgmt` is offered by BOTH Cleaning and M&E — M&E reuses the
40
+ * cleaning schedule module deliberately (`useMechanicalElectricalPermission`
41
+ * takes it from `useCleaningPermission`). `attendance-mgmt`, `site-settings`,
42
+ * `members`, `roles` and `invitations` are shared by nearly all of them.
43
+ *
44
+ * So this is a MANY-TO-MANY map, and the console has to show a shared module
45
+ * under each application that offers it. Presenting it as one-to-one would be
46
+ * the same class of wrongness as the flat list it replaces.
47
+ *
48
+ * ## It still stores a flat list of module keys
49
+ *
50
+ * `organizations.modules` is unchanged — a flat array of resource keys. Ticking
51
+ * an application ticks its keys. Nothing about the cascade, the server contract
52
+ * or the fail-open rule changes; this is how the choice is PRESENTED.
53
+ */
54
+
55
+ /** The seven service applications, in the order the owner names them in R1. */
56
+ export const APPLICATIONS = [
57
+ { key: "security", label: "Security", composable: useSecurityPermission },
58
+ { key: "cleaning", label: "Cleaning", composable: useCleaningPermission },
59
+ { key: "pest", label: "Pest control", composable: usePestControlPermission },
60
+ {
61
+ key: "mechanical-electrical",
62
+ label: "Mechanical & electrical",
63
+ composable: useMechanicalElectricalPermission,
64
+ },
65
+ { key: "landscape", label: "Landscape", composable: useLandscapingPermission },
66
+ { key: "pool", label: "Pool maintenance", composable: usePoolMaintenancePermission },
67
+ {
68
+ key: "property-management",
69
+ label: "Property management",
70
+ composable: usePropertyPermission,
71
+ },
72
+ ] as const;
73
+
74
+ export type ApplicationKey = (typeof APPLICATIONS)[number]["key"];
75
+
76
+ export type ApplicationModules = {
77
+ key: string;
78
+ label: string;
79
+ /** The resource keys this application's role editor offers. */
80
+ modules: string[];
81
+ };
82
+
83
+ /** Read one application's resource keys straight out of its own catalogue. */
84
+ function modulesOf(composable: () => { permissions?: Record<string, unknown> }): string[] {
85
+ try {
86
+ const { permissions } = composable() ?? {};
87
+ if (!permissions || typeof permissions !== "object") return [];
88
+ return Object.keys(permissions).filter(Boolean);
89
+ } catch {
90
+ /*
91
+ * FAILS OPEN as an empty list, never a throw. This runs at module load in
92
+ * the console; a composable that changes shape must not take the whole
93
+ * client-configuration screen down with it — the screen falls back to the
94
+ * ungrouped catalogue, which is exactly what it showed before this existed.
95
+ */
96
+ return [];
97
+ }
98
+ }
99
+
100
+ /** Every application with the modules it offers, derived from the apps themselves. */
101
+ export function applicationModules(): ApplicationModules[] {
102
+ return APPLICATIONS.map((app) => ({
103
+ key: app.key,
104
+ label: app.label,
105
+ modules: modulesOf(app.composable as never),
106
+ }));
107
+ }
108
+
109
+ /** Which applications offer this module. Empty means none of the seven do. */
110
+ export function applicationsForModule(resource: string): string[] {
111
+ if (!resource) return [];
112
+ return applicationModules()
113
+ .filter((app) => app.modules.includes(resource))
114
+ .map((app) => app.label);
115
+ }
116
+
117
+ /**
118
+ * Group a catalogue's keys by application, plus everything left over.
119
+ *
120
+ * The leftovers matter: `constants/permissions.ts` and the seven composables are
121
+ * two different lists and they do NOT agree — `customers`, `sites`,
122
+ * `subscriptions` and `robot-mgmt` appear in the flat catalogue but in no
123
+ * application, and `access-control`, `passes-and-keys-mgmt`, `online-forms`,
124
+ * `blocklist` and `resident-transactions` are the reverse. Dropping either set
125
+ * silently would be worse than showing it: an unticked module a client needs is
126
+ * a module they cannot be given.
127
+ */
128
+ export function groupModulesByApplication(catalogueKeys: readonly string[]): {
129
+ groups: ApplicationModules[];
130
+ ungrouped: string[];
131
+ } {
132
+ const keys = Array.isArray(catalogueKeys) ? catalogueKeys.filter(Boolean) : [];
133
+ const apps = applicationModules();
134
+
135
+ const groups = apps
136
+ .map((app) => ({
137
+ ...app,
138
+ modules: app.modules.filter((module) => keys.includes(module)),
139
+ }))
140
+ .filter((app) => app.modules.length > 0);
141
+
142
+ const claimed = new Set(groups.flatMap((app) => app.modules));
143
+ const ungrouped = keys.filter((key) => !claimed.has(key));
144
+
145
+ return { groups, ungrouped };
146
+ }