@7365admin1/layer-common 4.10.2 → 4.11.0

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": "4.10.2",
5
+ "version": "4.11.0",
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,70 @@
1
+ /**
2
+ * ANPR camera types ("direction") that carry whitelist and season pass plates.
3
+ *
4
+ * Mirrors `ALLOW_CAMERA_DIRECTIONS` in iservice365-core
5
+ * (`src/utils/anpr-plate.util.ts`): an allowed plate is written to every Entry,
6
+ * Both Entry and Exit, and Residents camera, and never to Visitors or Exit.
7
+ */
8
+ export const ALLOW_CAMERA_DIRECTIONS = ["entry", "both", "residents"];
9
+
10
+ export const isAllowCameraDirection = (direction: unknown): boolean =>
11
+ ALLOW_CAMERA_DIRECTIONS.includes(
12
+ String(direction ?? "").trim().toLowerCase(),
13
+ );
14
+
15
+ /** ANPR camera types that carry blocked plates: every type except Exit. */
16
+ export const BLOCK_CAMERA_DIRECTIONS = ["entry", "both", "residents", "visitors"];
17
+
18
+ export const isBlockCameraDirection = (direction: unknown): boolean =>
19
+ BLOCK_CAMERA_DIRECTIONS.includes(
20
+ String(direction ?? "").trim().toLowerCase(),
21
+ );
22
+
23
+ /**
24
+ * Which of a camera's plate lists are FILLED with the site's vehicles when it
25
+ * is added (`from` empty) or its type changes - the lists the new type holds
26
+ * and the old one did not. Mirrors `listsFilledByDirectionChange` in
27
+ * iservice365-core. Nothing to confirm (nothing is removed), but saving takes
28
+ * longer, so the form says so.
29
+ */
30
+ export const listsFilledByDirectionChange = (
31
+ from: unknown,
32
+ to: unknown,
33
+ ): { allow: boolean; block: boolean } => {
34
+ const before = String(from ?? "").trim().toLowerCase();
35
+ const after = String(to ?? "").trim().toLowerCase();
36
+
37
+ if (!after || after === before) return { allow: false, block: false };
38
+
39
+ return {
40
+ allow: isAllowCameraDirection(after) && !isAllowCameraDirection(before),
41
+ block: isBlockCameraDirection(after) && !isBlockCameraDirection(before),
42
+ };
43
+ };
44
+
45
+ /**
46
+ * Which of a camera's plate lists a type change empties, on the camera itself
47
+ * - every plate on the list, including ones added directly on the camera.
48
+ * Mirrors `listsClearedByDirectionChange` in iservice365-core, which refuses
49
+ * the change unless it is confirmed, so the form asks first.
50
+ *
51
+ * - To Exit: the allowed list and the blocklist.
52
+ * - Entry / Both / Residents to Visitors: the allowed list.
53
+ * - Anything else: nothing.
54
+ */
55
+ export const listsClearedByDirectionChange = (
56
+ from: unknown,
57
+ to: unknown,
58
+ ): { allow: boolean; block: boolean } => {
59
+ const before = String(from ?? "").trim().toLowerCase();
60
+ const after = String(to ?? "").trim().toLowerCase();
61
+
62
+ if (!after || after === before) return { allow: false, block: false };
63
+
64
+ if (after === "exit") return { allow: true, block: true };
65
+
66
+ return {
67
+ allow: isAllowCameraDirection(before) && !isAllowCameraDirection(after),
68
+ block: false,
69
+ };
70
+ };
@@ -0,0 +1,141 @@
1
+ /**
2
+ * Writing a new ANPR camera's plates one at a time from the add-camera screen.
3
+ *
4
+ * The screen shows every plate the camera will receive with its own progress -
5
+ * pending, adding, added, failed - and lets the person retry. The rules for
6
+ * WHEN to stop live here, apart from the component, so they can be tested:
7
+ *
8
+ * - between plates, when the person presses Stop;
9
+ * - at once, when the camera refuses the LOGIN or the permission. Every further
10
+ * attempt would be another failed login, and a Dahua unit locks the account
11
+ * for 30 minutes after 3 failed logins in 30 seconds;
12
+ * - after 3 plates in a row fail for any other reason - the camera has most
13
+ * likely gone away, and each attempt waits for a timeout.
14
+ *
15
+ * Whatever is not attempted stays "pending", and Retry carries on with the
16
+ * failed and pending plates.
17
+ */
18
+
19
+ export type PlateSyncStatus = "pending" | "adding" | "added" | "failed";
20
+
21
+ export type PlateSyncRow = {
22
+ id: string;
23
+ vehicle: string;
24
+ list: "allowed" | "blocked";
25
+ plateNumber: string;
26
+ name: string;
27
+ block: string;
28
+ level: string;
29
+ unit: string;
30
+ kind: string;
31
+ status: PlateSyncStatus;
32
+ error?: string;
33
+ };
34
+
35
+ /** What writing one plate answered. `kind` as the API classifies a refusal. */
36
+ export type PlateWriteAnswer = {
37
+ ok: boolean;
38
+ reason?: string;
39
+ kind?: string;
40
+ };
41
+
42
+ export type PlateQueueStop = "done" | "requested" | "login" | "failures";
43
+
44
+ export const MAX_FAILURES_IN_A_ROW = 3;
45
+
46
+ export function plateSyncCounts(rows: PlateSyncRow[]) {
47
+ let added = 0;
48
+ let failed = 0;
49
+ let pending = 0;
50
+ for (const row of rows) {
51
+ if (row.status === "added") added++;
52
+ else if (row.status === "failed") failed++;
53
+ else pending++;
54
+ }
55
+ return { total: rows.length, added, failed, pending };
56
+ }
57
+
58
+ export type PlateSyncCounts = ReturnType<typeof plateSyncCounts>;
59
+
60
+ /**
61
+ * The same counts, kept apart for the allowed list (whitelist, season pass)
62
+ * and the blocklist - a failed blocked plate is a car that can still get in,
63
+ * which is not the same as a resident who cannot.
64
+ */
65
+ export function plateSyncCountsByList(rows: PlateSyncRow[]): {
66
+ allowed: PlateSyncCounts;
67
+ blocked: PlateSyncCounts;
68
+ } {
69
+ return {
70
+ allowed: plateSyncCounts(rows.filter((row) => row.list !== "blocked")),
71
+ blocked: plateSyncCounts(rows.filter((row) => row.list === "blocked")),
72
+ };
73
+ }
74
+
75
+ /** The plates Retry carries on with: every failed one and every pending one. */
76
+ export function rowsToRetry(rows: PlateSyncRow[]): PlateSyncRow[] {
77
+ return rows.filter((row) => row.status === "failed" || row.status === "pending");
78
+ }
79
+
80
+ /**
81
+ * Write `targets` one at a time, in order, updating each row's `status` (and
82
+ * `error`) in place. `write` may answer `{ ok: false }` or throw - both mark
83
+ * the row failed. Returns why it stopped, and the sentence to show for it.
84
+ */
85
+ export async function runPlateQueue(
86
+ targets: PlateSyncRow[],
87
+ write: (row: PlateSyncRow) => Promise<PlateWriteAnswer>,
88
+ options: { shouldStop?: () => boolean } = {},
89
+ ): Promise<{ stopped: PlateQueueStop; message: string }> {
90
+ let failuresInARow = 0;
91
+
92
+ for (const row of targets) {
93
+ if (options.shouldStop?.()) {
94
+ return {
95
+ stopped: "requested",
96
+ message: "Stopped. Press Retry to carry on with the pending and failed plates.",
97
+ };
98
+ }
99
+
100
+ row.status = "adding";
101
+ row.error = "";
102
+
103
+ let answer: PlateWriteAnswer;
104
+ try {
105
+ answer = await write(row);
106
+ } catch (error: any) {
107
+ answer = { ok: false, reason: String(error?.message ?? error ?? "") };
108
+ }
109
+
110
+ if (answer?.ok) {
111
+ row.status = "added";
112
+ failuresInARow = 0;
113
+ continue;
114
+ }
115
+
116
+ row.status = "failed";
117
+ row.error = answer?.reason || "The plate could not be added.";
118
+ failuresInARow++;
119
+
120
+ if (answer?.kind === "challenge" || answer?.kind === "rejected") {
121
+ return {
122
+ stopped: "login",
123
+ message:
124
+ `Stopped: ${row.error.replace(/\.\s*$/, "")}. Check the camera's ` +
125
+ `username and password, then press Retry.`,
126
+ };
127
+ }
128
+
129
+ if (failuresInARow >= MAX_FAILURES_IN_A_ROW) {
130
+ return {
131
+ stopped: "failures",
132
+ message:
133
+ `Stopped after ${MAX_FAILURES_IN_A_ROW} plates in a row could not ` +
134
+ `be added. Check that the camera is switched on and reachable, then ` +
135
+ `press Retry.`,
136
+ };
137
+ }
138
+ }
139
+
140
+ return { stopped: "done", message: "" };
141
+ }