@7365admin1/layer-common 4.64.3 → 4.65.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/CHANGELOG.md +44 -4
- package/components/AccessCardAddForm.vue +5 -4
- package/components/AccessCardAssignToUnitForm.vue +3 -2
- package/components/AccessCardPreviewDialog.vue +4 -3
- package/components/AreaMain.vue +33 -30
- package/components/BuildingManagement/units.vue +30 -21
- package/components/CameraBulkImportDialog.vue +41 -12
- package/components/EntryPass/QrTemplatePreview.vue +21 -25
- package/components/EntryPassInformation.vue +119 -24
- package/components/ImportExcelDialog.vue +136 -0
- package/components/InventoryImportDialog.vue +33 -104
- package/components/InventoryItemDetail.vue +1 -1
- package/components/InventoryItemDialog.vue +62 -7
- package/components/InventoryReportsTab.vue +2 -1
- package/components/InventoryStockTab.vue +11 -3
- package/components/InvitationForm.vue +5 -3
- package/components/UnitMain.vue +30 -28
- package/components/VehicleBulkAddForm.vue +30 -16
- package/composables/useAccessManagement.ts +5 -0
- package/composables/useImportTemplates.ts +24 -0
- package/composables/useInventory.ts +12 -5
- package/composables/useSiteEntryPassSettings.ts +26 -0
- package/composables/useWebUsb.ts +14 -11
- package/package.json +1 -1
- package/types/inventory.d.ts +2 -0
- package/utils/camera-csv.ts +2 -0
- package/utils/import-result.ts +62 -0
- package/utils/vehicle-bulk-sheet.ts +59 -3
|
@@ -59,7 +59,7 @@
|
|
|
59
59
|
<v-list-item-title class="text-wrap">
|
|
60
60
|
{{ noRolesText }}
|
|
61
61
|
</v-list-item-title>
|
|
62
|
-
<NuxtLink v-if="rolesPageLink" :to="rolesPageLink">
|
|
62
|
+
<NuxtLink v-if="rolesPageLink && !rolesError" :to="rolesPageLink">
|
|
63
63
|
Open Roles & Permissions
|
|
64
64
|
</NuxtLink>
|
|
65
65
|
</v-list-item>
|
|
@@ -474,7 +474,7 @@ const { getInvitableRoles } = useRole();
|
|
|
474
474
|
* templates are gone from any invitation that names an organisation - the
|
|
475
475
|
* server refuses them there (`core roleInvitable`).
|
|
476
476
|
*/
|
|
477
|
-
const { data: RolesData, refresh: refreshRoles } = await useLazyAsyncData(
|
|
477
|
+
const { data: RolesData, refresh: refreshRoles, error: rolesError } = await useLazyAsyncData(
|
|
478
478
|
"get-roles-by-type-" + props.org + "-" + props.app + "-" + fixedSite.value,
|
|
479
479
|
() =>
|
|
480
480
|
getInvitableRoles({
|
|
@@ -502,7 +502,9 @@ watchEffect(() => {
|
|
|
502
502
|
const roleOptions = computed(() => inviteRoleOptions(roles.value));
|
|
503
503
|
|
|
504
504
|
const noRolesText = computed(() =>
|
|
505
|
-
|
|
505
|
+
rolesError?.value
|
|
506
|
+
? "The roles could not be loaded. You may not have access to this organisation's roles; refresh, or sign in again."
|
|
507
|
+
: fixedSite.value
|
|
506
508
|
? "No roles have been created for this site yet. Create one in Roles & Permissions first."
|
|
507
509
|
: "No roles have been created yet. Create one in Roles & Permissions first."
|
|
508
510
|
);
|
package/components/UnitMain.vue
CHANGED
|
@@ -37,9 +37,8 @@
|
|
|
37
37
|
v-bind="t"
|
|
38
38
|
class="ma-1 screen-icon-btn"
|
|
39
39
|
variant="flat"
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
download
|
|
40
|
+
:loading="templateDownloading"
|
|
41
|
+
@click="downloadLiveTemplate"
|
|
43
42
|
>
|
|
44
43
|
<v-icon>mdi-download</v-icon>
|
|
45
44
|
</v-btn>
|
|
@@ -183,16 +182,25 @@
|
|
|
183
182
|
:color="messageColor"
|
|
184
183
|
/>
|
|
185
184
|
|
|
186
|
-
<
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
185
|
+
<ImportExcelDialog
|
|
186
|
+
v-model="showImport"
|
|
187
|
+
title="Import Units from Excel"
|
|
188
|
+
noun="units"
|
|
189
|
+
name-label="Unit"
|
|
190
|
+
fill-hint="One unit per row, e.g. Toilet Bowl, Sofa."
|
|
191
|
+
duplicate-note="Units already on the list are skipped and left unchanged."
|
|
192
|
+
template-file-name="units-import-template.xlsx"
|
|
193
|
+
:download-template="() => downloadTemplate('units', importQuery)"
|
|
194
|
+
:check="(file: File) => checkImport('units', file, importQuery)"
|
|
195
|
+
:commit="(file: File) => commitImport('units', file, importQuery)"
|
|
196
|
+
@done="getUnitsRefresh()"
|
|
197
|
+
@error="(text: string) => showMessage(text, 'error')"
|
|
192
198
|
/>
|
|
193
199
|
</template>
|
|
194
200
|
|
|
195
201
|
<script setup lang="ts">
|
|
202
|
+
import useImportTemplates from "../composables/useImportTemplates";
|
|
203
|
+
import { saveBlob } from "../composables/useInventory";
|
|
196
204
|
import useUnits from "../composables/useUnits";
|
|
197
205
|
import useUtils from "../composables/useUtils";
|
|
198
206
|
|
|
@@ -220,7 +228,6 @@ const {
|
|
|
220
228
|
createUnit,
|
|
221
229
|
updateUnit,
|
|
222
230
|
deleteUnit,
|
|
223
|
-
uploadUnits,
|
|
224
231
|
downloadUnits,
|
|
225
232
|
} = useUnits();
|
|
226
233
|
|
|
@@ -271,7 +278,6 @@ const selectedUnit = ref<Record<string, any> | null>(null);
|
|
|
271
278
|
const message = ref("");
|
|
272
279
|
const messageSnackbar = ref(false);
|
|
273
280
|
const messageColor = ref("");
|
|
274
|
-
const fileInput = ref<HTMLInputElement | null>(null);
|
|
275
281
|
|
|
276
282
|
const dialogShowMoreActions = ref(false);
|
|
277
283
|
const dialogShowForm = ref(false);
|
|
@@ -341,30 +347,26 @@ async function handleDownloadExcel() {
|
|
|
341
347
|
}
|
|
342
348
|
|
|
343
349
|
function onImportData() {
|
|
344
|
-
|
|
350
|
+
showImport.value = true;
|
|
345
351
|
}
|
|
346
352
|
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
+
// The template is made live from this property's own data (dropdowns, instructions, the property named inside).
|
|
354
|
+
const { downloadTemplate, checkImport, commitImport } = useImportTemplates();
|
|
355
|
+
const importQuery = computed(() => ({ site: String(props.site ?? ""), serviceType: props.serviceType || undefined }));
|
|
356
|
+
const showImport = ref(false);
|
|
357
|
+
const templateDownloading = ref(false);
|
|
358
|
+
async function downloadLiveTemplate() {
|
|
359
|
+
templateDownloading.value = true;
|
|
353
360
|
try {
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
showMessage(response?.message, "success");
|
|
358
|
-
|
|
359
|
-
await getUnitsRefresh();
|
|
360
|
-
} catch (err: any) {
|
|
361
|
-
showMessage(err?.data?.message, "error");
|
|
361
|
+
saveBlob(await downloadTemplate("units", importQuery.value), "units-import-template.xlsx");
|
|
362
|
+
} catch (error: any) {
|
|
363
|
+
showMessage(error?.data?.message || "Could not download the template.", "error");
|
|
362
364
|
} finally {
|
|
363
|
-
|
|
364
|
-
target.value = "";
|
|
365
|
+
templateDownloading.value = false;
|
|
365
366
|
}
|
|
366
367
|
}
|
|
367
368
|
|
|
369
|
+
|
|
368
370
|
function onCreateUnit() {
|
|
369
371
|
dialogMode.value = "add";
|
|
370
372
|
editingUnit.value = {};
|
|
@@ -284,6 +284,8 @@
|
|
|
284
284
|
|
|
285
285
|
<script setup lang="ts">
|
|
286
286
|
import useVehicle from "../composables/useVehicle";
|
|
287
|
+
import useImportTemplates from "../composables/useImportTemplates";
|
|
288
|
+
import { saveBlob } from "../composables/useInventory";
|
|
287
289
|
import useBuilding from "../composables/useBuilding";
|
|
288
290
|
import { errorConverter } from "../utils/data";
|
|
289
291
|
// @ts-ignore - the country list the one-vehicle form's phone field uses.
|
|
@@ -320,6 +322,8 @@ import {
|
|
|
320
322
|
mapUploadedGrid,
|
|
321
323
|
vehicleBulkTemplateCsv,
|
|
322
324
|
xlsxToGrid,
|
|
325
|
+
xlsxMeta,
|
|
326
|
+
templateRefusal,
|
|
323
327
|
} from "../utils/vehicle-bulk-sheet";
|
|
324
328
|
|
|
325
329
|
/**
|
|
@@ -348,6 +352,7 @@ const prop = defineProps({
|
|
|
348
352
|
const emit = defineEmits(["back", "close", "added"]);
|
|
349
353
|
|
|
350
354
|
const { addVehiclesBulk } = useVehicle();
|
|
355
|
+
const { downloadTemplate: downloadLiveTemplate } = useImportTemplates();
|
|
351
356
|
const { getBlocksSelection, getLevelsSelection, getUnitSelection } = useBuilding();
|
|
352
357
|
|
|
353
358
|
type Option = { title: string; value: string };
|
|
@@ -1043,18 +1048,19 @@ function statusColour(status: BulkRowStatus) {
|
|
|
1043
1048
|
|
|
1044
1049
|
/* --------------------------- template and file ---------------------------- */
|
|
1045
1050
|
|
|
1046
|
-
/**
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1051
|
+
/**
|
|
1052
|
+
* The sheet to fill in somewhere else: an Excel file made live from this
|
|
1053
|
+
* site's own blocks, levels and units, with them as dropdowns, an example row
|
|
1054
|
+
* and an Instructions sheet. If the server cannot make it, the plain CSV
|
|
1055
|
+
* headings are offered instead so the person is never stuck.
|
|
1056
|
+
*/
|
|
1057
|
+
async function downloadTemplate() {
|
|
1058
|
+
try {
|
|
1059
|
+
saveBlob(await downloadLiveTemplate("vehicles", { site: prop.site }), "vehicle-whitelist-template.xlsx");
|
|
1060
|
+
} catch {
|
|
1061
|
+
// A BOM, so Excel opens it as UTF-8 and a name with an accent survives.
|
|
1062
|
+
saveBlob(new Blob(["", vehicleBulkTemplateCsv()], { type: "text/csv;charset=utf-8;" }), "vehicle-whitelist-template.csv");
|
|
1063
|
+
}
|
|
1058
1064
|
}
|
|
1059
1065
|
|
|
1060
1066
|
/** The first row that is free to be written into. */
|
|
@@ -1083,9 +1089,15 @@ async function onFileChosen(event: Event) {
|
|
|
1083
1089
|
uploading.value = true;
|
|
1084
1090
|
|
|
1085
1091
|
try {
|
|
1086
|
-
const
|
|
1087
|
-
|
|
1088
|
-
|
|
1092
|
+
const workbook = isWorkbookName(chosen.name) ? await chosen.arrayBuffer() : null;
|
|
1093
|
+
if (workbook) {
|
|
1094
|
+
const refusal = templateRefusal(await xlsxMeta(workbook), { kind: "vehicles", site: prop.site });
|
|
1095
|
+
if (refusal) {
|
|
1096
|
+
formError.value = refusal;
|
|
1097
|
+
return;
|
|
1098
|
+
}
|
|
1099
|
+
}
|
|
1100
|
+
const grid = workbook ? await xlsxToGrid(workbook) : parsePastedGrid(await chosen.text());
|
|
1089
1101
|
const sheet = mapUploadedGrid(grid);
|
|
1090
1102
|
|
|
1091
1103
|
if (!sheet.matchesTemplate) {
|
|
@@ -1117,7 +1129,9 @@ async function onFileChosen(event: Event) {
|
|
|
1117
1129
|
moveTo(start, 0);
|
|
1118
1130
|
|
|
1119
1131
|
resultNote.value =
|
|
1120
|
-
`${sheet.rows.length} row(s) read from ${chosen.name}
|
|
1132
|
+
`${sheet.rows.length} row(s) read from ${chosen.name}` +
|
|
1133
|
+
(sheet.exampleRowIgnored ? " (the example row was left out)" : "") +
|
|
1134
|
+
`. Check anything ` +
|
|
1121
1135
|
`marked red - a block, level or unit that is not one of this site's own ` +
|
|
1122
1136
|
`is left blank - then press Submit.`;
|
|
1123
1137
|
} catch {
|
|
@@ -228,7 +228,11 @@ export default function useAccessManagement() {
|
|
|
228
228
|
unitId: string;
|
|
229
229
|
page?: number;
|
|
230
230
|
limit?: number;
|
|
231
|
+
/** Card-number search. Sent only when non-empty; API-core does not read
|
|
232
|
+
* it yet, so until it does the list comes back unfiltered. */
|
|
233
|
+
search?: string;
|
|
231
234
|
}) {
|
|
235
|
+
const search = params.search?.trim();
|
|
232
236
|
return useNuxtApp().$api<Record<string, any>>(
|
|
233
237
|
`/api/access-management/available-card-contractor/${params.type}`,
|
|
234
238
|
{
|
|
@@ -238,6 +242,7 @@ export default function useAccessManagement() {
|
|
|
238
242
|
unitId: params.unitId,
|
|
239
243
|
page: params.page ?? 1,
|
|
240
244
|
limit: params.limit ?? 100,
|
|
245
|
+
...(search ? { search } : {}),
|
|
241
246
|
},
|
|
242
247
|
}
|
|
243
248
|
);
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { TImportResult } from "../utils/import-result";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Excel templates made live from the property's own data (`/api/import-templates`),
|
|
5
|
+
* and the check-then-save import behind building units, areas and units.
|
|
6
|
+
* Vehicles and cameras only download their template here; their files are read
|
|
7
|
+
* on screen.
|
|
8
|
+
*/
|
|
9
|
+
export type TTemplateKind = "building-units" | "areas" | "units" | "vehicles" | "cameras";
|
|
10
|
+
|
|
11
|
+
export default function useImportTemplates() {
|
|
12
|
+
const api = <T = any>(kind: TTemplateKind, path: string, options: Record<string, any> = {}) =>
|
|
13
|
+
useNuxtApp().$api<T>(`/api/import-templates/${kind}/${path}`, options);
|
|
14
|
+
const send = (kind: TTemplateKind, path: "check" | "commit", file: File, query: Record<string, string | undefined>) => {
|
|
15
|
+
const body = new FormData();
|
|
16
|
+
body.append("file", file);
|
|
17
|
+
return api<TImportResult>(kind, path, { method: "POST", body, query });
|
|
18
|
+
};
|
|
19
|
+
return {
|
|
20
|
+
downloadTemplate: (kind: TTemplateKind, query: { site: string; serviceType?: string }) => api<Blob>(kind, "template", { query, responseType: "blob" }),
|
|
21
|
+
checkImport: (kind: TTemplateKind, file: File, query: { site: string; serviceType?: string }) => send(kind, "check", file, query),
|
|
22
|
+
commitImport: (kind: TTemplateKind, file: File, query: { site: string; serviceType?: string }) => send(kind, "commit", file, query),
|
|
23
|
+
};
|
|
24
|
+
}
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import type { TImportResult } from "../utils/import-result";
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
4
|
* Inventory API (`/api/inventory`). What a person may do in a store comes back
|
|
3
5
|
* on the store itself (`store.access`), decided by the server — screens show
|
|
@@ -67,14 +69,19 @@ export default function useInventory() {
|
|
|
67
69
|
`/stores/${store}/legacy-equipment/import`,
|
|
68
70
|
{ method: "POST", body: { serviceType, commit } },
|
|
69
71
|
),
|
|
70
|
-
|
|
72
|
+
/** The store's own item template: live dropdowns (units, packing, categories) and the store named inside. */
|
|
73
|
+
importTemplate: (store: string) => api<Blob>(`/stores/${store}/import-template`, { responseType: "blob" }),
|
|
71
74
|
importItems: (store: string, file: File, commit: boolean) => {
|
|
72
75
|
const body = new FormData();
|
|
73
76
|
body.append("file", file);
|
|
74
|
-
return api<{
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
77
|
+
return api<TImportResult>(`/stores/${store}/import`, { method: "POST", body, query: { commit } });
|
|
78
|
+
},
|
|
79
|
+
/** Bulk deliveries: the store's items as a dropdown. */
|
|
80
|
+
receiveTemplate: (store: string) => api<Blob>(`/stores/${store}/receive-template`, { responseType: "blob" }),
|
|
81
|
+
importReceipts: (store: string, file: File, commit: boolean) => {
|
|
82
|
+
const body = new FormData();
|
|
83
|
+
body.append("file", file);
|
|
84
|
+
return api<TImportResult>(`/stores/${store}/receive-import`, { method: "POST", body, query: { commit } });
|
|
78
85
|
},
|
|
79
86
|
};
|
|
80
87
|
}
|
|
@@ -27,9 +27,35 @@ export default function useSiteEntryPassSettings() {
|
|
|
27
27
|
);
|
|
28
28
|
}
|
|
29
29
|
|
|
30
|
+
/**
|
|
31
|
+
* The encrypted ACM URL the access-card screens hand to API-core, which
|
|
32
|
+
* decrypts it and sends the ACM commands to that host.
|
|
33
|
+
*
|
|
34
|
+
* Resolves the site's own "Enter entrypass URL" setting first, the way
|
|
35
|
+
* `VisitorForm` does for contractor passes. The access-card forms used to
|
|
36
|
+
* call `/api/encrypt-acm-url` with no `url`, so every site's door/lift
|
|
37
|
+
* levels and access groups came from the route's hardcoded default host
|
|
38
|
+
* whatever the site had configured. A site with no URL set, or whose
|
|
39
|
+
* settings cannot be read, still gets that default.
|
|
40
|
+
*/
|
|
41
|
+
async function getEncryptedAcmUrl(siteId: string): Promise<string> {
|
|
42
|
+
let rawUrl = "";
|
|
43
|
+
try {
|
|
44
|
+
const settings: any = await getBySiteId(siteId);
|
|
45
|
+
rawUrl = String(settings?.data?.settings?.url ?? "").trim();
|
|
46
|
+
} catch {
|
|
47
|
+
// No entry pass settings for this site yet - the default host applies.
|
|
48
|
+
}
|
|
49
|
+
const { encrypted } = await $fetch<{ encrypted: string }>(
|
|
50
|
+
`/api/encrypt-acm-url${rawUrl ? `?url=${encodeURIComponent(rawUrl)}` : ""}`,
|
|
51
|
+
);
|
|
52
|
+
return encrypted;
|
|
53
|
+
}
|
|
54
|
+
|
|
30
55
|
return {
|
|
31
56
|
getBySiteId,
|
|
32
57
|
getById,
|
|
33
58
|
save,
|
|
59
|
+
getEncryptedAcmUrl,
|
|
34
60
|
};
|
|
35
61
|
}
|
package/composables/useWebUsb.ts
CHANGED
|
@@ -345,16 +345,19 @@ export default function useWebUsb() {
|
|
|
345
345
|
70,
|
|
346
346
|
);
|
|
347
347
|
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
348
|
+
// The pass used to print five copies of the same QR - four 140px ones at
|
|
349
|
+
// the corners around a 250px centre - so a strip could be cut apart. It now
|
|
350
|
+
// prints a single large QR filling that same band (y 90 to 440, above the
|
|
351
|
+
// company name at y 470), which scans far more reliably on a thermal print.
|
|
352
|
+
// Generated at the size it is drawn at: the old corner copies were
|
|
353
|
+
// downscaled from the 250px canvas, which softened their modules.
|
|
354
|
+
const qrSize = 350;
|
|
355
|
+
const qrCanvas = await QRCode.toCanvas(qrUrlImage, {
|
|
356
|
+
width: qrSize,
|
|
357
|
+
margin: 1,
|
|
358
|
+
});
|
|
352
359
|
|
|
353
|
-
ctx.drawImage(qrCanvas,
|
|
354
|
-
ctx.drawImage(qrCanvas, canvas.width - qrSmall - margin, 90, qrSmall, qrSmall);
|
|
355
|
-
ctx.drawImage(qrCanvas, (canvas.width - qrCenter) / 2, 110, qrCenter, qrCenter);
|
|
356
|
-
ctx.drawImage(qrCanvas, margin, 300, qrSmall, qrSmall);
|
|
357
|
-
ctx.drawImage(qrCanvas, canvas.width - qrSmall - margin, 300, qrSmall, qrSmall);
|
|
360
|
+
ctx.drawImage(qrCanvas, (canvas.width - qrSize) / 2, 90, qrSize, qrSize);
|
|
358
361
|
|
|
359
362
|
ctx.font = "25px Arial";
|
|
360
363
|
ctx.fillText(companyName || "", canvas.width / 2, 470);
|
|
@@ -401,8 +404,8 @@ export default function useWebUsb() {
|
|
|
401
404
|
/**
|
|
402
405
|
* The self-service onboarding QR: one large QR code, a "SCAN QR CODE"-style
|
|
403
406
|
* subtext, and a dark site-name banner along the bottom - a single-purpose
|
|
404
|
-
* gate sign, not the
|
|
405
|
-
*
|
|
407
|
+
* gate sign, not the visitor-pass receipt `createReceiptLayout` produces
|
|
408
|
+
* (that one pairs its QR with company, date, access and instruction lines).
|
|
406
409
|
*/
|
|
407
410
|
async function createOnboardingQrLayout(
|
|
408
411
|
qrUrlImage: string,
|
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
|
+
"version": "4.65.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.",
|
package/types/inventory.d.ts
CHANGED
|
@@ -90,6 +90,8 @@ declare type TInventoryTransaction = {
|
|
|
90
90
|
photos?: { before: string[]; after: string[]; other: string[] };
|
|
91
91
|
remarks?: string;
|
|
92
92
|
reportedBy?: string;
|
|
93
|
+
/** Opening stock: who received it, when not the person who entered it. */
|
|
94
|
+
receivedBy?: string;
|
|
93
95
|
date: string;
|
|
94
96
|
createdByName: string;
|
|
95
97
|
};
|
package/utils/camera-csv.ts
CHANGED
|
@@ -103,6 +103,8 @@ function splitLine(line: string): string[] {
|
|
|
103
103
|
/** A header cell, normalised to the sheet's snake_case spelling. */
|
|
104
104
|
function normaliseColumn(value: string): string {
|
|
105
105
|
return value
|
|
106
|
+
// "Camera Name *" in the Excel template is camera_name: the * only marks it required.
|
|
107
|
+
.replace(/\*/g, "")
|
|
106
108
|
.trim()
|
|
107
109
|
.toLowerCase()
|
|
108
110
|
.replace(/^/, "")
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What an Excel import's check (or save) comes back with, and the plain words
|
|
3
|
+
* and error report built from it. Shared by every "Import from Excel" dialog.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
export type TImportIssue = { row: number; column: string; message: string };
|
|
7
|
+
|
|
8
|
+
export type TImportRow = { row: number; name: string; status: "ready" | "added" | "skipped" | "error"; message: string };
|
|
9
|
+
|
|
10
|
+
export type TImportResult = {
|
|
11
|
+
committed: boolean;
|
|
12
|
+
ready: number;
|
|
13
|
+
skipped: number;
|
|
14
|
+
errors: number;
|
|
15
|
+
exampleRowIgnored?: boolean;
|
|
16
|
+
deliveries?: number;
|
|
17
|
+
issues?: TImportIssue[];
|
|
18
|
+
rows: TImportRow[];
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
/** One sentence for the top of the preview. `noun` is plural, e.g. "items". */
|
|
22
|
+
export function importSummary(result: TImportResult, noun = "rows"): string {
|
|
23
|
+
const parts: string[] = [];
|
|
24
|
+
if (result.committed) parts.push(`${result.ready} ${noun} saved.`);
|
|
25
|
+
else if (result.errors) parts.push(`${result.errors} row(s) need fixing — nothing will be saved until they are fixed.`);
|
|
26
|
+
else parts.push(`${result.ready} ${noun} ready to save.`);
|
|
27
|
+
if (!result.committed && result.errors && result.ready) parts.push(`${result.ready} other row(s) are fine.`);
|
|
28
|
+
if (result.skipped) parts.push(`${result.skipped} already in the system or repeated — they will be left unchanged.`);
|
|
29
|
+
if (result.exampleRowIgnored) parts.push("The example row was ignored.");
|
|
30
|
+
return parts.join(" ");
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export const IMPORT_STATUS_LABEL: Record<TImportRow["status"], string> = {
|
|
34
|
+
ready: "Ready",
|
|
35
|
+
added: "Saved",
|
|
36
|
+
skipped: "Skipped",
|
|
37
|
+
error: "Needs fixing",
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
const csvCell = (v: unknown) => {
|
|
41
|
+
const s = String(v ?? "");
|
|
42
|
+
return /[",\r\n]/.test(s) ? `"${s.replace(/"/g, '""')}"` : s;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* The error report: one line per problem — the row number in the Excel file,
|
|
47
|
+
* the column, and what to fix. Rows with an error but no column-level issue
|
|
48
|
+
* are listed with their message. Opens in Excel.
|
|
49
|
+
*/
|
|
50
|
+
export function importErrorReportCsv(result: TImportResult): string {
|
|
51
|
+
const lines: string[][] = [["Row in your file", "Column", "What to fix", "Item"]];
|
|
52
|
+
const nameOf = new Map(result.rows.map((r) => [r.row, r.name]));
|
|
53
|
+
const covered = new Set<number>();
|
|
54
|
+
for (const i of result.issues ?? []) {
|
|
55
|
+
lines.push([String(i.row), i.column, i.message, nameOf.get(i.row) ?? ""]);
|
|
56
|
+
covered.add(i.row);
|
|
57
|
+
}
|
|
58
|
+
for (const r of result.rows) {
|
|
59
|
+
if (r.status === "error" && !covered.has(r.row)) lines.push([String(r.row), "", r.message, r.name]);
|
|
60
|
+
}
|
|
61
|
+
return "" + lines.map((l) => l.map(csvCell).join(",")).join("\r\n") + "\r\n";
|
|
62
|
+
}
|
|
@@ -97,6 +97,8 @@ export type UploadedSheet = {
|
|
|
97
97
|
matchesTemplate: boolean;
|
|
98
98
|
/** The heading line as the file spells it - for telling a person what it has. */
|
|
99
99
|
header: string[];
|
|
100
|
+
/** A template example row was left in and was not read. */
|
|
101
|
+
exampleRowIgnored?: boolean;
|
|
100
102
|
};
|
|
101
103
|
|
|
102
104
|
/**
|
|
@@ -138,14 +140,16 @@ export function mapUploadedGrid(grid: string[][]): UploadedSheet {
|
|
|
138
140
|
// Every line under the header is somebody's car. Nothing is dropped for
|
|
139
141
|
// looking like something else - two people in one household can have the
|
|
140
142
|
// same details but for the plate, and a repeated row is the person's to fix.
|
|
141
|
-
const
|
|
142
|
-
|
|
143
|
+
const body = lines.slice(1);
|
|
144
|
+
// The example row of a template the server made is marked beside it; it is nobody's car.
|
|
145
|
+
const rows = body
|
|
146
|
+
.filter((line) => !isExampleLine(line))
|
|
143
147
|
.map((line) =>
|
|
144
148
|
BULK_SHEET_HEADINGS.map((_, column) => String(line[column] ?? "").trim()),
|
|
145
149
|
)
|
|
146
150
|
.filter((line) => !isEmptyLine(line));
|
|
147
151
|
|
|
148
|
-
return { rows, matchesTemplate: true, header };
|
|
152
|
+
return { rows, matchesTemplate: true, header, exampleRowIgnored: body.some(isExampleLine) };
|
|
149
153
|
}
|
|
150
154
|
|
|
151
155
|
/**
|
|
@@ -385,3 +389,55 @@ export async function xlsxToGrid(data: ArrayBuffer | Uint8Array): Promise<string
|
|
|
385
389
|
export function isWorkbookName(name: string): boolean {
|
|
386
390
|
return /\.(xlsx|xlsm)$/i.test(String(name ?? ""));
|
|
387
391
|
}
|
|
392
|
+
|
|
393
|
+
/* ------------------------------ live templates ----------------------------- */
|
|
394
|
+
|
|
395
|
+
/** Beside the example row of every template the server makes. */
|
|
396
|
+
export const TEMPLATE_EXAMPLE_MARK = "Example – delete this row";
|
|
397
|
+
|
|
398
|
+
/** Does this line carry the template's example-row mark? */
|
|
399
|
+
export function isExampleLine(cells: string[]): boolean {
|
|
400
|
+
return (cells ?? []).some((cell) => String(cell ?? "").includes(TEMPLATE_EXAMPLE_MARK));
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
/**
|
|
404
|
+
* The hidden "_meta" sheet of a template the server made: which kind of
|
|
405
|
+
* template it is and the property it was made for (`kind`, `site`,
|
|
406
|
+
* `siteName`). Empty for any other workbook.
|
|
407
|
+
*/
|
|
408
|
+
export async function xlsxMeta(data: ArrayBuffer | Uint8Array): Promise<Record<string, string>> {
|
|
409
|
+
const bytes = data instanceof Uint8Array ? data : new Uint8Array(data);
|
|
410
|
+
const entries = readZipEntries(bytes);
|
|
411
|
+
const book = entries.get("xl/workbook.xml");
|
|
412
|
+
const rels = entries.get("xl/_rels/workbook.xml.rels");
|
|
413
|
+
if (!book || !rels) return {};
|
|
414
|
+
const sheet = (await readPart(bytes, book)).match(/<sheet\b[^>]*\bname="_meta"[^>]*>/)?.[0];
|
|
415
|
+
const rid = sheet?.match(/\br:id="([^"]+)"/)?.[1];
|
|
416
|
+
if (!rid) return {};
|
|
417
|
+
const relation = ((await readPart(bytes, rels)).match(/<Relationship\b[^>]*>/g) ?? []).find((tag) => tag.includes(` Id="${rid}"`));
|
|
418
|
+
const target = relation?.match(/\bTarget="([^"]+)"/)?.[1];
|
|
419
|
+
const part = target ? entries.get(target.startsWith("/") ? target.slice(1) : `xl/${target}`) : undefined;
|
|
420
|
+
if (!part) return {};
|
|
421
|
+
const sharedEntry = entries.get("xl/sharedStrings.xml");
|
|
422
|
+
const strings = sharedEntry ? sharedStringsOf(await readPart(bytes, sharedEntry)) : [];
|
|
423
|
+
const meta: Record<string, string> = {};
|
|
424
|
+
for (const line of sheetToGrid(await readPart(bytes, part), strings).slice(1)) {
|
|
425
|
+
if (line[0]) meta[String(line[0])] = String(line[1] ?? "");
|
|
426
|
+
}
|
|
427
|
+
return meta;
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
/**
|
|
431
|
+
* Why a template may not be used here, or "" when it may: a template made for
|
|
432
|
+
* another property, or a different kind of template. A workbook without the
|
|
433
|
+
* hidden sheet (an older template, a person's own file) is allowed.
|
|
434
|
+
*/
|
|
435
|
+
export function templateRefusal(meta: Record<string, string>, expected: { kind: string; site: string }): string {
|
|
436
|
+
if (meta.kind && meta.kind !== expected.kind) {
|
|
437
|
+
return "This is a different kind of template. Download the template on this screen and copy your rows into it.";
|
|
438
|
+
}
|
|
439
|
+
if (meta.site && expected.site && meta.site !== expected.site) {
|
|
440
|
+
return `This file was made for ${meta.siteName ? `"${meta.siteName}"` : "another property"}. Download the template on this screen and copy your rows into it.`;
|
|
441
|
+
}
|
|
442
|
+
return "";
|
|
443
|
+
}
|