@7365admin1/layer-common 4.15.1 → 4.15.3-staging.326

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.
Files changed (38) hide show
  1. package/CHANGELOG.md +10 -4
  2. package/components/ClientDetailForm.vue +3 -0
  3. package/components/DocumentForm.vue +407 -112
  4. package/components/DocumentManagement.vue +213 -12
  5. package/components/EntryPass/QrTemplatePreview.vue +21 -25
  6. package/components/EntryPassInformation.vue +119 -24
  7. package/components/Facility/BookingSetup.vue +63 -0
  8. package/components/HidIntercomManagement.vue +193 -43
  9. package/components/HidProfileQrCode.vue +323 -0
  10. package/components/HidReaderUserRoster.vue +32 -2
  11. package/components/HidUserEnrollment.vue +771 -212
  12. package/components/InvitationClientForm.vue +19 -1
  13. package/components/Layout/Header.vue +26 -0
  14. package/components/NotificationBell.vue +321 -0
  15. package/components/VehicleAddSelection.vue +7 -2
  16. package/components/VehicleBulkAddForm.vue +1392 -0
  17. package/components/VehicleManagement.vue +31 -3
  18. package/components/VisitorManagement.vue +267 -5
  19. package/composables/useAccessManagement.ts +5 -0
  20. package/composables/useAppLinks.ts +4 -2
  21. package/composables/useDocument.ts +7 -0
  22. package/composables/useFacility.ts +11 -0
  23. package/composables/useHidAmico.ts +97 -0
  24. package/composables/useHidNavigation.ts +25 -1
  25. package/composables/useMember.ts +12 -2
  26. package/composables/useNotification.ts +78 -0
  27. package/composables/usePeople.ts +19 -0
  28. package/composables/useVehicle.ts +35 -0
  29. package/composables/useWebUsb.ts +14 -11
  30. package/package.json +1 -1
  31. package/pages/[org]/[site]/access-mgmt/hid-qr-code/index.vue +23 -0
  32. package/plugins/secure-member.client.ts +68 -14
  33. package/types/document.d.ts +14 -0
  34. package/types/facility.d.ts +4 -0
  35. package/types/people.d.ts +4 -1
  36. package/utils/materialSymbols.ts +3 -0
  37. package/utils/vehicle-bulk-rows.ts +534 -0
  38. package/utils/vehicle-bulk-sheet.ts +361 -0
@@ -0,0 +1,534 @@
1
+ /**
2
+ * The Whitelist (Bulk) sheet: what a row is, when a cell is wrong, and how a
3
+ * block of cells pasted from Excel is read.
4
+ *
5
+ * All of it is plain data, apart from the component that draws the grid, so
6
+ * the rules a person meets as red cells are testable on their own - see
7
+ * `vehicle-bulk-rows.test.ts`.
8
+ */
9
+
10
+ export type BulkRowStatus = "pending" | "adding" | "added" | "failed";
11
+
12
+ /** One line of the sheet. `block`, `level` and `unit` hold ids, not names. */
13
+ export type BulkVehicleRow = {
14
+ /** This row's own id. Sent to the server and handed back with the outcome. */
15
+ id: string;
16
+ block: string;
17
+ level: string;
18
+ unit: string;
19
+ nric: string;
20
+ name: string;
21
+ /** The country's dial code for `phone`, e.g. "+65". Never blank. */
22
+ dialCode: string;
23
+ phone: string;
24
+ plateNumber: string;
25
+ status: BulkRowStatus;
26
+ /** What the server said about this row - shown behind the info icon. */
27
+ message: string;
28
+ /** The cells to paint red. */
29
+ invalid: string[];
30
+ };
31
+
32
+ /**
33
+ * The cells a person TYPES IN, left to right. The Code column is not one of
34
+ * them: every row carries a dial code from the moment it exists, so counting
35
+ * it would make an untouched row look filled in.
36
+ */
37
+ export const BULK_ROW_FIELDS = [
38
+ "block",
39
+ "level",
40
+ "unit",
41
+ "nric",
42
+ "name",
43
+ "phone",
44
+ "plateNumber",
45
+ ] as const;
46
+
47
+ /** Without these a row cannot be added. */
48
+ export const BULK_ROW_REQUIRED = [
49
+ "block",
50
+ "level",
51
+ "unit",
52
+ "name",
53
+ "plateNumber",
54
+ ] as const;
55
+
56
+ let rowSeq = 0;
57
+
58
+ export function emptyBulkRow(id?: string): BulkVehicleRow {
59
+ rowSeq += 1;
60
+ return {
61
+ id: id ?? `row-${Date.now().toString(36)}-${rowSeq}`,
62
+ block: "",
63
+ level: "",
64
+ unit: "",
65
+ nric: "",
66
+ name: "",
67
+ dialCode: DEFAULT_DIAL_CODE,
68
+ phone: "",
69
+ plateNumber: "",
70
+ status: "pending",
71
+ message: "",
72
+ invalid: [],
73
+ };
74
+ }
75
+
76
+ /** A row nobody has typed anything into. Left alone by Submit entirely. */
77
+ export function isBulkRowEmpty(row: BulkVehicleRow): boolean {
78
+ return BULK_ROW_FIELDS.every((field) => !String(row[field] ?? "").trim());
79
+ }
80
+
81
+ /**
82
+ * A phone number, as loosely as the single-vehicle form takes one: blank is
83
+ * fine, and anything else has to be 7 to 15 digits, with an optional country
84
+ * code and the spacing, brackets or dashes people type.
85
+ */
86
+ export function isValidBulkPhone(value: unknown): boolean {
87
+ const text = String(value ?? "").trim();
88
+ if (!text) return true;
89
+ if (!/^\+?[\d\s()\-]+$/.test(text)) return false;
90
+ const digits = text.replace(/\D/g, "");
91
+ return digits.length >= 7 && digits.length <= 15;
92
+ }
93
+
94
+ /** What the one-vehicle form puts in front of a number with no country code. */
95
+ export const DEFAULT_DIAL_CODE = "+65";
96
+
97
+ /**
98
+ * The phone number as it is STORED, so a vehicle added from a sheet looks like
99
+ * one added through the form.
100
+ *
101
+ * The form's phone field keeps the picked country's dial code in front of the
102
+ * number, so "91234567" with Singapore picked is saved as "+6591234567". The
103
+ * sheet has the same picker in its Code column, and this puts the two
104
+ * together. A number already written with a country code keeps its own.
105
+ */
106
+ export function bulkPhoneToStore(value: unknown, dialCode?: string): string {
107
+ const text = String(value ?? "").trim();
108
+ if (!text) return "";
109
+
110
+ const code = String(dialCode ?? "").trim() || DEFAULT_DIAL_CODE;
111
+ const compact = text.replace(/[\s()\-]/g, "");
112
+
113
+ // A number that already carries a country code keeps the one it carries.
114
+ if (compact.startsWith("+")) return compact;
115
+ if (compact.startsWith("00")) return `+${compact.slice(2)}`;
116
+
117
+ const digits = code.replace(/\D/g, "");
118
+ if (digits && compact.startsWith(digits) && compact.length > digits.length) {
119
+ return `+${compact}`;
120
+ }
121
+
122
+ return `${code}${compact}`;
123
+ }
124
+
125
+ /** Each dial code's number patterns, e.g. { "+65": ["^\\+65\\d{8}$"] }. */
126
+ export type BulkDialRules = Record<string, string[]>;
127
+
128
+ const compiled = new Map<string, RegExp>();
129
+
130
+ function matches(pattern: string, value: string): boolean {
131
+ let regex = compiled.get(pattern);
132
+ if (!regex) {
133
+ regex = new RegExp(pattern);
134
+ compiled.set(pattern, regex);
135
+ }
136
+ return regex.test(value);
137
+ }
138
+
139
+ /** The dial code a finished number belongs to - the longest one it starts with. */
140
+ export function bulkDialCodeOf(stored: string, codes: string[]): string {
141
+ return (
142
+ [...codes]
143
+ .sort((a, b) => b.length - a.length)
144
+ .find((code) => stored.startsWith(code)) ?? ""
145
+ );
146
+ }
147
+
148
+ /**
149
+ * Is this a number that country really has?
150
+ *
151
+ * A phone number is not required, so a blank one is fine. One that IS filled
152
+ * in has to be a number the country of its Code column would issue - the same
153
+ * check the one-vehicle form makes, and made here instead of at the camera or
154
+ * the database, where a wrong number is found out far too late.
155
+ */
156
+ export function isValidBulkNumber(
157
+ phone: unknown,
158
+ dialCode?: string,
159
+ rules?: BulkDialRules,
160
+ ): boolean {
161
+ const text = String(phone ?? "").trim();
162
+ if (!text) return true;
163
+ if (!isValidBulkPhone(text)) return false;
164
+ if (!rules) return true;
165
+
166
+ const stored = bulkPhoneToStore(text, dialCode);
167
+ const code = bulkDialCodeOf(stored, Object.keys(rules));
168
+ // A number under no country anybody knows is not a number we can store.
169
+ if (!code) return false;
170
+
171
+ const patterns = rules[code] ?? [];
172
+ // A known code with nothing to check it against passes on its shape alone.
173
+ if (!patterns.length) return true;
174
+
175
+ return patterns.some((pattern) => matches(pattern, stored));
176
+ }
177
+
178
+ /** A plate number: letters and digits, and nothing exotic. */
179
+ export function isValidBulkPlate(value: unknown): boolean {
180
+ const text = String(value ?? "").trim();
181
+ return /^[A-Za-z0-9][A-Za-z0-9 -]{0,14}$/.test(text);
182
+ }
183
+
184
+ /** Plates written the same way once spacing and case are ignored. */
185
+ export function normaliseBulkPlate(value: unknown): string {
186
+ return String(value ?? "").replace(/\s+/g, "").toUpperCase();
187
+ }
188
+
189
+ export type BulkRowOptions = {
190
+ /** The ids the block / level / unit pickers offer for THIS row. */
191
+ blockIds: string[];
192
+ levelIds: string[];
193
+ unitIds: string[];
194
+ /** Every dial code the Code column offers, e.g. ["+65", "+60", …]. */
195
+ dialCodes?: string[];
196
+ /** What a number must look like under each of those codes. */
197
+ dialRules?: BulkDialRules;
198
+ /** Plates typed on other rows of the same sheet, normalised. */
199
+ otherPlates?: string[];
200
+ };
201
+
202
+ /**
203
+ * The cells of one row that are wrong, in the order they are shown.
204
+ *
205
+ * A cell is wrong when it is empty and required, when the block, level or unit
206
+ * is not one this site offers (a pasted name that matched nothing), when a
207
+ * phone number is not one its country would issue (blank is fine - it is not
208
+ * required), when the Code is not one the picker offers, or when the plate is
209
+ * written twice in the same sheet.
210
+ */
211
+ export function invalidBulkCells(
212
+ row: BulkVehicleRow,
213
+ options: BulkRowOptions,
214
+ ): string[] {
215
+ const invalid: string[] = [];
216
+ const value = (field: (typeof BULK_ROW_FIELDS)[number]) =>
217
+ String(row[field] ?? "").trim();
218
+
219
+ for (const field of BULK_ROW_REQUIRED) {
220
+ if (!value(field)) invalid.push(field);
221
+ }
222
+
223
+ if (value("block") && !options.blockIds.includes(value("block"))) {
224
+ invalid.push("block");
225
+ }
226
+ if (value("level") && !options.levelIds.includes(value("level"))) {
227
+ invalid.push("level");
228
+ }
229
+ if (value("unit") && !options.unitIds.includes(value("unit"))) {
230
+ invalid.push("unit");
231
+ }
232
+ if (!isValidBulkNumber(row.phone, row.dialCode, options.dialRules)) {
233
+ invalid.push("phone");
234
+ }
235
+ // The code only has to be right when there is a number for it to belong to.
236
+ if (
237
+ row.phone.trim() &&
238
+ options.dialCodes &&
239
+ !options.dialCodes.includes(String(row.dialCode ?? "").trim())
240
+ ) {
241
+ invalid.push("dialCode");
242
+ }
243
+ if (value("plateNumber") && !isValidBulkPlate(row.plateNumber)) {
244
+ invalid.push("plateNumber");
245
+ }
246
+ if (
247
+ value("plateNumber") &&
248
+ (options.otherPlates ?? []).includes(normaliseBulkPlate(row.plateNumber))
249
+ ) {
250
+ invalid.push("plateNumber");
251
+ }
252
+
253
+ return Array.from(new Set(invalid));
254
+ }
255
+
256
+ /**
257
+ * Check every row that is going to be sent, and write the red cells onto the
258
+ * rows themselves. Returns the rows that have something wrong.
259
+ *
260
+ * Rows that are already ADDED, and rows nobody has typed in, are left out of
261
+ * both the check and the answer.
262
+ */
263
+ export function validateBulkRows(
264
+ rows: BulkVehicleRow[],
265
+ optionsFor: (row: BulkVehicleRow) => BulkRowOptions,
266
+ ): BulkVehicleRow[] {
267
+ const live = rows.filter(
268
+ (row) => row.status !== "added" && !isBulkRowEmpty(row),
269
+ );
270
+
271
+ const seen = new Map<string, number>();
272
+ for (const row of rows) {
273
+ if (row.status === "added") continue;
274
+ const plate = normaliseBulkPlate(row.plateNumber);
275
+ if (plate) seen.set(plate, (seen.get(plate) ?? 0) + 1);
276
+ }
277
+
278
+ const bad: BulkVehicleRow[] = [];
279
+
280
+ for (const row of rows) {
281
+ if (!live.includes(row)) {
282
+ row.invalid = [];
283
+ continue;
284
+ }
285
+ const plate = normaliseBulkPlate(row.plateNumber);
286
+ const options = optionsFor(row);
287
+ row.invalid = invalidBulkCells(row, {
288
+ ...options,
289
+ // Its own plate counts as a repeat only when another row has it too.
290
+ otherPlates: (seen.get(plate) ?? 0) > 1 ? [plate] : [],
291
+ });
292
+ if (row.invalid.length) bad.push(row);
293
+ }
294
+
295
+ return bad;
296
+ }
297
+
298
+ /** The rows Submit sends: filled in, and not already added. */
299
+ export function bulkRowsToSubmit(rows: BulkVehicleRow[]): BulkVehicleRow[] {
300
+ return rows.filter(
301
+ (row) => row.status !== "added" && !isBulkRowEmpty(row),
302
+ );
303
+ }
304
+
305
+ export function bulkRowCounts(rows: BulkVehicleRow[]) {
306
+ let added = 0;
307
+ let failed = 0;
308
+ let pending = 0;
309
+ for (const row of rows) {
310
+ if (row.status === "added") added++;
311
+ else if (row.status === "failed") failed++;
312
+ else if (!isBulkRowEmpty(row)) pending++;
313
+ }
314
+ return { added, failed, pending, total: added + failed + pending };
315
+ }
316
+
317
+ /**
318
+ * A block of cells copied from Excel, Sheets or a CSV, as a grid of text.
319
+ *
320
+ * Tabs separate columns (what a spreadsheet puts on the clipboard) unless
321
+ * there is not a single tab, in which case commas do - someone pasting a CSV
322
+ * line means the same thing. Quoted cells keep their commas. Blank lines go.
323
+ */
324
+ export function parsePastedGrid(text: unknown): string[][] {
325
+ const body = String(text ?? "").replace(/\r\n?/g, "\n").trim();
326
+ if (!body) return [];
327
+
328
+ const byTab = body.includes("\t");
329
+
330
+ return body
331
+ .split("\n")
332
+ .map((line) => (byTab ? line.split("\t") : splitCsvLine(line)))
333
+ .map((cells) => cells.map((cell) => cell.trim()))
334
+ .filter((cells) => cells.some((cell) => cell !== ""));
335
+ }
336
+
337
+ function splitCsvLine(line: string): string[] {
338
+ const cells: string[] = [];
339
+ let cell = "";
340
+ let quoted = false;
341
+
342
+ for (let index = 0; index < line.length; index++) {
343
+ const character = line[index];
344
+ if (character === '"') {
345
+ if (quoted && line[index + 1] === '"') {
346
+ cell += '"';
347
+ index++;
348
+ } else {
349
+ quoted = !quoted;
350
+ }
351
+ // eslint-disable-next-line no-continue
352
+ continue;
353
+ }
354
+ if (character === "," && !quoted) {
355
+ cells.push(cell);
356
+ cell = "";
357
+ continue;
358
+ }
359
+ cell += character;
360
+ }
361
+
362
+ cells.push(cell);
363
+ return cells;
364
+ }
365
+
366
+ /** Find a block / level / unit by the NAME a pasted sheet carries. */
367
+ export function matchOptionByTitle(
368
+ options: Array<{ title?: string; value?: string }>,
369
+ title: unknown,
370
+ ): string {
371
+ const wanted = String(title ?? "").trim().toLowerCase();
372
+ if (!wanted) return "";
373
+ const hit = options.find(
374
+ (option) => String(option.title ?? "").trim().toLowerCase() === wanted,
375
+ );
376
+ return hit?.value ? String(hit.value) : "";
377
+ }
378
+
379
+ /* --------------------------------------------------------------------------
380
+ * The sheet behaviour: a selected range of cells, copy and paste between
381
+ * cells, and the fill handle that drags a value down a column.
382
+ *
383
+ * All of it is arithmetic over row and column numbers, so the grid component
384
+ * only has to draw what these work out.
385
+ * ----------------------------------------------------------------------- */
386
+
387
+ export type CellRef = { row: number; col: number };
388
+ export type CellRange = { top: number; left: number; bottom: number; right: number };
389
+
390
+ /** The rectangle two corners make, whichever way round they were given. */
391
+ export function normaliseRange(a: CellRef, b: CellRef): CellRange {
392
+ return {
393
+ top: Math.min(a.row, b.row),
394
+ bottom: Math.max(a.row, b.row),
395
+ left: Math.min(a.col, b.col),
396
+ right: Math.max(a.col, b.col),
397
+ };
398
+ }
399
+
400
+ export function isInRange(range: CellRange, row: number, col: number): boolean {
401
+ return (
402
+ row >= range.top && row <= range.bottom && col >= range.left && col <= range.right
403
+ );
404
+ }
405
+
406
+ /** The selected cells as text, tab-separated - what a spreadsheet expects. */
407
+ export function rangeToTsv(
408
+ range: CellRange,
409
+ valueAt: (row: number, col: number) => string,
410
+ ): string {
411
+ const lines: string[] = [];
412
+ for (let row = range.top; row <= range.bottom; row++) {
413
+ const cells: string[] = [];
414
+ for (let col = range.left; col <= range.right; col++) {
415
+ cells.push(String(valueAt(row, col) ?? ""));
416
+ }
417
+ lines.push(cells.join("\t"));
418
+ }
419
+ return lines.join("\n");
420
+ }
421
+
422
+ /**
423
+ * Where a pasted block of cells lands when it is dropped at `start`.
424
+ *
425
+ * Cells past the last column are dropped - a sheet does not grow sideways -
426
+ * and `extraRows` says how many rows have to be added at the bottom to hold
427
+ * the rest, which is what a spreadsheet does when you paste past the end.
428
+ */
429
+ export function planPaste(
430
+ grid: string[][],
431
+ start: CellRef,
432
+ size: { rows: number; cols: number },
433
+ ): { writes: Array<{ row: number; col: number; value: string }>; extraRows: number } {
434
+ const writes: Array<{ row: number; col: number; value: string }> = [];
435
+ let lastRow = start.row;
436
+
437
+ grid.forEach((cells, rowOffset) => {
438
+ const row = start.row + rowOffset;
439
+ lastRow = Math.max(lastRow, row);
440
+ cells.forEach((value, colOffset) => {
441
+ const col = start.col + colOffset;
442
+ if (col >= size.cols) return;
443
+ writes.push({ row, col, value });
444
+ });
445
+ });
446
+
447
+ return { writes, extraRows: Math.max(0, lastRow + 1 - size.rows) };
448
+ }
449
+
450
+ /**
451
+ * Dragging the fill handle from the selection down (or up) to `toRow`.
452
+ *
453
+ * The selection's own values repeat over the rows it is dragged across, which
454
+ * is what a spreadsheet does with a block of cells; one cell dragged down is
455
+ * that cell copied down. Rows inside the selection are left alone.
456
+ */
457
+ export function planFill(
458
+ selection: CellRange,
459
+ toRow: number,
460
+ valueAt: (row: number, col: number) => string,
461
+ ): Array<{ row: number; col: number; value: string }> {
462
+ const writes: Array<{ row: number; col: number; value: string }> = [];
463
+ const height = selection.bottom - selection.top + 1;
464
+
465
+ const target = (row: number, step: number) => {
466
+ for (let col = selection.left; col <= selection.right; col++) {
467
+ const source = selection.top + ((step % height) + height) % height;
468
+ writes.push({ row, col, value: String(valueAt(source, col) ?? "") });
469
+ }
470
+ };
471
+
472
+ if (toRow > selection.bottom) {
473
+ for (let row = selection.bottom + 1, step = 0; row <= toRow; row++, step++) {
474
+ target(row, step);
475
+ }
476
+ } else if (toRow < selection.top) {
477
+ for (let row = selection.top - 1, step = -1; row >= toRow; row--, step--) {
478
+ target(row, step);
479
+ }
480
+ }
481
+
482
+ return writes;
483
+ }
484
+
485
+ /** How many rows the sheet can be set to, and the one it opens with. */
486
+ export const BULK_SHEET_SIZES = [100, 200, 300, 400, 500, 600, 700, 800, 900, 1000];
487
+ export const BULK_SHEET_DEFAULT_SIZE = 100;
488
+
489
+ /** Most rows one request may carry - the endpoint's own limit. */
490
+ export const BULK_SUBMIT_BATCH = 100;
491
+
492
+ /**
493
+ * Grow or shrink the sheet to `target` rows.
494
+ *
495
+ * Growing adds empty rows at the bottom. Shrinking only ever drops rows
496
+ * NOBODY HAS TYPED IN: a row with anything in it, or one already sent, stays
497
+ * wherever it is, so choosing a smaller size can never throw work away.
498
+ */
499
+ export function resizeBulkRows(
500
+ rows: BulkVehicleRow[],
501
+ target: number,
502
+ ): BulkVehicleRow[] {
503
+ const wanted = Math.max(1, Math.floor(target) || 0);
504
+
505
+ if (rows.length < wanted) {
506
+ const grown = rows.slice();
507
+ while (grown.length < wanted) grown.push(emptyBulkRow());
508
+ return grown;
509
+ }
510
+
511
+ const kept = rows.slice();
512
+ for (let index = kept.length - 1; index >= 0 && kept.length > wanted; index--) {
513
+ const row = kept[index];
514
+ if (row.status === "added" || row.status === "failed" || !isBulkRowEmpty(row)) {
515
+ continue;
516
+ }
517
+ kept.splice(index, 1);
518
+ }
519
+
520
+ return kept.length ? kept : [emptyBulkRow()];
521
+ }
522
+
523
+ /** The rows to send, split into requests the endpoint will accept. */
524
+ export function bulkSubmitBatches(
525
+ rows: BulkVehicleRow[],
526
+ size = BULK_SUBMIT_BATCH,
527
+ ): BulkVehicleRow[][] {
528
+ const sending = bulkRowsToSubmit(rows);
529
+ const batches: BulkVehicleRow[][] = [];
530
+ for (let index = 0; index < sending.length; index += size) {
531
+ batches.push(sending.slice(index, index + size));
532
+ }
533
+ return batches;
534
+ }