@7365admin1/layer-common 4.189.1 → 4.190.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.
@@ -58,6 +58,9 @@ declare type TInventoryItem = {
58
58
  available?: number;
59
59
  /** Issued but not yet confirmed (Received) by the person. */
60
60
  awaitingReceipt?: number;
61
+ /** DV-1787: serial numbers - in all, and of units on the shelf (> 0: choose serials when issuing). */
62
+ serialsTotal?: number;
63
+ serialsInStore?: number;
61
64
  /** Only while the item is low. */
62
65
  lowStockAlert?: {
63
66
  since?: string;
@@ -147,6 +150,8 @@ declare type TInventoryTransaction = {
147
150
  workOrder?: string;
148
151
  workOrderNumber?: string;
149
152
  workOrderExists?: boolean;
153
+ /** Issue (DV-1787): when the person is expected to give it back ("YYYY-MM-DD" at midnight UTC). */
154
+ returnDate?: string | null;
150
155
  /** The feedback the work order is tied to (2026-11): its link goes there. */
151
156
  workOrderFeedback?: string | null;
152
157
  supplier?: string;
@@ -174,6 +179,8 @@ declare type TInventoryTransaction = {
174
179
  photosAfterAt?: string | null;
175
180
  submittedAt?: string;
176
181
  writeOffReason?: string;
182
+ /** DV-1787: the serial numbers this movement covers (issue, return, write-off, use). */
183
+ serials?: string[];
177
184
  /** "Also attended" on a usage record. */
178
185
  attendedWith?: string;
179
186
  /** A draft taken over from a colleague. */
@@ -246,3 +253,62 @@ declare type TInventoryPoLine = {
246
253
  receivedBy: string;
247
254
  }[];
248
255
  };
256
+
257
+ /** DV-1787: the work order picked in Issue Stock - its id, number as shown ("#CLN0012"), subject and status. */
258
+ declare type TPickedWorkOrder = { _id: string; number: string; subject: string; status: string };
259
+
260
+ /** DV-1787: stock issued for a work order and/or to come back by a date, tracked until it does. */
261
+ declare type TInventoryCheckout = {
262
+ _id: string;
263
+ store: string;
264
+ site?: string;
265
+ item: string;
266
+ itemName: string;
267
+ unit: string;
268
+ holder: string;
269
+ holderName: string;
270
+ qty: number;
271
+ outQty: number;
272
+ workOrder?: string;
273
+ workOrderNumber?: string;
274
+ returnDate: string | null;
275
+ status: "out" | "returned" | "written_off" | "used" | "handed_over" | "closed";
276
+ createdByName: string;
277
+ createdAt: string;
278
+ closedAt?: string;
279
+ overdue: boolean;
280
+ daysOverdue: number;
281
+ /** A return the holder asked for, waiting for a store keeper (its id), or null. */
282
+ returnRequest?: string | null;
283
+ /** The issue (request) it came from: Received / Not received act on it. */
284
+ request?: string;
285
+ /** DV-1787: false while the person has not tapped Received for its issue yet (no Ask to return until then). */
286
+ received?: boolean;
287
+ /** DV-1787: its issue's Received step - awaiting, not_received (the person said they did not get it), or received. */
288
+ ackStatus?: "awaiting" | "not_received" | "received";
289
+ /** DV-1787: what is asked to return on it so far (requests naming it): how many, and which serial ids. */
290
+ returnAsked?: { qty: number; serials: string[] } | null;
291
+ /** DV-1787 (work order / feedback list): how much of it ended so far, by how (returned, written_off, used, handed_over). */
292
+ ended?: Partial<Record<"returned" | "written_off" | "used" | "handed_over" | "closed", number>>;
293
+ /** DV-1787 (work order / feedback list): serial numbers that left it - how and when. */
294
+ endedSerials?: { serial: string; type: "returned" | "written_off" | "used" | "handed_over" | "closed"; at: string; toName?: string }[];
295
+ /** DV-1787 (work order / feedback list): units without a serial that left it, per step. */
296
+ endedPlain?: { type: "returned" | "written_off" | "used" | "handed_over" | "closed"; qty: number; at: string; toName?: string }[];
297
+ /** Serial numbers still out on it. */
298
+ serials?: { _id: string; serial: string }[];
299
+ /** Return date set, changed or cleared after the issue (newest last). */
300
+ dateChanges?: { from: string | null; to: string | null; at: string; byName: string; reason?: string }[];
301
+ };
302
+
303
+ /** DV-1787: one unit with its own serial number, and its history. */
304
+ declare type TInventorySerial = {
305
+ _id: string;
306
+ item: string;
307
+ itemName: string;
308
+ serial: string;
309
+ status: "in_store" | "out" | "written_off" | "used";
310
+ holderName?: string;
311
+ workOrderNumber?: string;
312
+ history: { type: string; at: string; byName: string; holderName?: string; workOrderNumber?: string; reason?: string; fromName?: string; note?: string }[];
313
+ createdAt: string;
314
+ };
@@ -0,0 +1,58 @@
1
+ /**
2
+ * THE ACCOUNT SITE'S "BACK" LINK (DV-1874).
3
+ *
4
+ * Manage Account sends the user to the Account application on its own
5
+ * hostname, and that application had no way back: no button, no link, only the
6
+ * browser's Back. The services apps now pass the page the user was on as
7
+ * `?return=`, and the Account header offers a Back button to it.
8
+ *
9
+ * `?return=` comes from the address bar, so it is untrusted: a link that sends
10
+ * someone to Account with `?return=https://evil.example` must not turn our
11
+ * Back button into an open redirect. Only an http(s) address on the SAME parent
12
+ * domain as the Account application itself is accepted:
13
+ *
14
+ * account.app.iservice365.org -> *.app.iservice365.org
15
+ * iservice365-staging-account.apptesting.work -> *.apptesting.work
16
+ * localhost (development) -> localhost
17
+ *
18
+ * The Account application itself is refused - "Back" to the page you are on is
19
+ * no way back at all.
20
+ */
21
+ export function safeAccountReturn(candidate: unknown, accountUrl: string): string {
22
+ if (typeof candidate !== "string" || !candidate) return "";
23
+
24
+ let target: URL;
25
+ let account: URL;
26
+ try {
27
+ target = new URL(candidate);
28
+ account = new URL(accountUrl);
29
+ } catch {
30
+ return "";
31
+ }
32
+
33
+ if (target.protocol !== "https:" && target.protocol !== "http:") return "";
34
+ if (target.origin === account.origin) return "";
35
+
36
+ const host = target.hostname.toLowerCase();
37
+ const accountHost = account.hostname.toLowerCase();
38
+
39
+ if (accountHost === "localhost" || accountHost === "127.0.0.1") {
40
+ return host === accountHost ? target.href : "";
41
+ }
42
+
43
+ // The Account host minus its first label is the shared parent domain.
44
+ const parent = accountHost.split(".").slice(1).join(".");
45
+ if (!parent || !parent.includes(".")) return "";
46
+
47
+ return host.endsWith(`.${parent}`) ? target.href : "";
48
+ }
49
+
50
+ /** Where the Account header keeps the Back target while the user moves around it. */
51
+ export const ACCOUNT_RETURN_KEY = "account-return-to";
52
+
53
+ /**
54
+ * The `useState` key the header fills with the checked Back target, for the
55
+ * Account app's Personal Info page to draw its Back button from. Empty when
56
+ * there is nowhere safe to go back to.
57
+ */
58
+ export const ACCOUNT_RETURN_STATE = "account-return";
@@ -0,0 +1,114 @@
1
+ import { qtyUnit } from "./inventory-ack";
2
+ /**
3
+ * DV-1787: shared wording for checkouts (stock issued for a work order / to come back by a date),
4
+ * used by the Checked out list, My Stock and a work order's Issued items.
5
+ */
6
+
7
+ /** A return date is a calendar day saved as midnight UTC: the same day as a local Date, wherever the browser is. */
8
+ export function returnDayKey(d: string): Date {
9
+ const [y, m, day] = String(d).slice(0, 10).split("-").map(Number);
10
+ return new Date(y, m - 1, day);
11
+ }
12
+
13
+ type TChip = { text: string; color: string; icon: string; info: string };
14
+
15
+ const ENDED: Record<string, TChip> = {
16
+ returned: { text: "Returned", color: "success", icon: "mdi-check", info: "Back in the store. Nothing more to do." },
17
+ written_off: {
18
+ text: "Written off",
19
+ color: "grey",
20
+ icon: "mdi-close-circle-outline",
21
+ info: "Written off (lost, damaged, spoilt or used up). It is not coming back to the store.",
22
+ },
23
+ used: { text: "Used", color: "grey", icon: "mdi-check", info: "Used up on the job, so it is not coming back to the store." },
24
+ handed_over: {
25
+ text: "Handed over",
26
+ color: "grey",
27
+ icon: "mdi-account-switch-outline",
28
+ info: "Passed to a colleague. They have it now: see their row for its status.",
29
+ },
30
+ closed: { text: "Closed", color: "grey", icon: "mdi-check", info: "Closed: it is no longer out with anyone." },
31
+ };
32
+
33
+ /** Out / Due today / Overdue N days, or how it ended - with `info`, what the status means (hover). */
34
+ export function checkoutChip(c: { status: string; overdue?: boolean; daysOverdue?: number; returnDate?: string | null; received?: boolean; ackStatus?: string }): TChip {
35
+ if (c.status !== "out") return ENDED[c.status] ?? ENDED.closed;
36
+ // DV-1787: not received yet - it is not with the person until they tap Received.
37
+ if (c.received === false && c.ackStatus === "not_received") {
38
+ return { text: "Not Received", color: "error", icon: "mdi-alert-circle-outline", info: "The person tapped Not Received: they say they did not get it. The store keeper checks and takes it back; if it turns up, they tap Received." };
39
+ }
40
+ if (c.received === false) {
41
+ return { text: "Pending Receipt", color: "grey", icon: "mdi-timer-sand", info: "Issued, but the person has not tapped Received or Not Received yet. They can ask to return it only after Received." };
42
+ }
43
+ if (c.overdue) {
44
+ const n = c.daysOverdue ?? 0;
45
+ return {
46
+ text: `Overdue ${n} ${n === 1 ? "day" : "days"}`,
47
+ color: "error",
48
+ icon: "mdi-alarm",
49
+ info: "Past its return date and still not returned. The person and the store keepers get a reminder every day until it comes back.",
50
+ };
51
+ }
52
+ const today = new Date().toLocaleDateString("en-CA", { timeZone: "Asia/Singapore" });
53
+ if (c.returnDate && String(c.returnDate).slice(0, 10) === today) {
54
+ return { text: "Due today", color: "warning", icon: "mdi-clock-outline", info: "Must be back in the store today. From tomorrow it shows as overdue." };
55
+ }
56
+ return c.returnDate
57
+ ? {
58
+ text: "Not returned yet",
59
+ color: "info",
60
+ icon: "mdi-arrow-top-right",
61
+ info: "Still with the person. The return date has not come yet, so it is not overdue.",
62
+ }
63
+ : {
64
+ text: "Out",
65
+ color: "info",
66
+ icon: "mdi-arrow-top-right",
67
+ info: "With the person, for the work order. No return date was set, so it is never overdue. It closes when returned, used up, written off or handed over.",
68
+ };
69
+ }
70
+
71
+ /** "Return asked": the holder asked to return it; a store keeper accepts it when they have it. */
72
+ export const RETURN_ASKED_INFO = "The person asked to return it. It stays with them until a store keeper accepts the return.";
73
+
74
+ /**
75
+ * DV-1787: how much of a checked-out row can still be asked to return. A row asked for in parts
76
+ * (returnAsked) has the rest left; a row marked by a Return to store from My Stock counts as asked.
77
+ */
78
+ export function askLeft(c: { status: string; outQty: number; received?: boolean; returnRequest?: string | null; returnAsked?: { qty: number } | null }): number {
79
+ // Not received yet: it is not with them, so nothing to ask to return.
80
+ if (c.status !== "out" || c.received === false) return 0;
81
+ if (c.returnAsked) return Math.max(0, c.outQty - c.returnAsked.qty);
82
+ return c.returnRequest ? 0 : c.outQty;
83
+ }
84
+
85
+ /** DV-1787: what is asked to return on a row, e.g. "2 pcs · S/N AG-0001 + 1 without serial number". */
86
+ export function askedText(c: { unit: string; serials?: { _id: string; serial: string }[]; returnAsked?: { qty: number; serials: string[] } | null }): string {
87
+ const a = c.returnAsked;
88
+ if (!a?.qty) return "";
89
+ const names = (c.serials ?? [])
90
+ .filter((s) => a.serials.includes(String(s._id)))
91
+ .map((s) => s.serial)
92
+ .sort((x, y) => x.localeCompare(y, undefined, { numeric: true }));
93
+ const plain = Math.max(0, a.qty - a.serials.length);
94
+ const what = [names.length ? `S/N ${names.join(", ")}` : "", plain ? `${plain} without serial number` : ""].filter(Boolean).join(" + ");
95
+ return `${qtyUnit(a.qty, c.unit)}${what ? ` · ${what}` : ""}`;
96
+ }
97
+
98
+ /**
99
+ * DV-1787: totals of checked-out rows by state (Not Returned, Return Asked, Returned, Written Off,
100
+ * Used) - Not Returned and Returned always, the others when there are any.
101
+ */
102
+ export function checkoutStats(rows: { status: string; outQty: number; returnAsked?: { qty: number } | null; ended?: Partial<Record<string, number>> }[]) {
103
+ const n = (v: unknown) => Number(v) || 0;
104
+ const sum = (f: (c: (typeof rows)[number]) => number) => rows.reduce((t, c) => t + f(c), 0);
105
+ const list = [
106
+ // Not Returned leaves out what is already asked to return, so the totals add up to what was issued.
107
+ { label: "Not Returned", n: sum((c) => (c.status === "out" ? Math.max(0, c.outQty - (c.returnAsked?.qty ?? 0)) : 0)), cls: "text-warning" },
108
+ { label: "Return Asked", n: sum((c) => (c.status === "out" ? (c.returnAsked?.qty ?? 0) : 0)), cls: "text-primary" },
109
+ { label: "Returned", n: sum((c) => n(c.ended?.returned)), cls: "text-success" },
110
+ { label: "Written Off", n: sum((c) => n(c.ended?.written_off)), cls: "text-error" },
111
+ { label: "Used", n: sum((c) => n(c.ended?.used)), cls: "" },
112
+ ];
113
+ return list.filter((x, i) => i === 0 || i === 2 || x.n > 0);
114
+ }