@htilabs/platform-common-models 0.67.0 → 0.69.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.
@@ -0,0 +1,22 @@
1
+ /**
2
+ * Small value helpers the merge shares with the registry fill script
3
+ * (`scripts/lib/orgRegistryFill.ts` re-exports them).
4
+ */
5
+ const text = (v: unknown): string => (typeof v === "string" ? v.trim() : "");
6
+
7
+ /** A name reduced for comparison: case, punctuation and a leading "the" dropped. */
8
+ export function comparableName(value: unknown): string {
9
+ return text(value)
10
+ .normalize("NFKD")
11
+ .toLowerCase()
12
+ .replace(/&/g, " and ")
13
+ .replace(/[^a-z0-9]+/g, " ")
14
+ .trim()
15
+ .replace(/^the /, "");
16
+ }
17
+
18
+ /** A spreadsheet must not evaluate an org name as a formula. */
19
+ export const sheetText = (v: unknown): string => {
20
+ const s = typeof v === "string" ? v : v == null ? "" : String(v);
21
+ return /^[=+\-@\t\r]/.test(s) ? `'${s}` : s;
22
+ };
@@ -0,0 +1,93 @@
1
+ /**
2
+ * What a merge saves before it writes: everything its undo needs.
3
+ */
4
+ /* eslint-disable @typescript-eslint/no-explicit-any */
5
+
6
+ /** A stored document, as the driver hands it back. */
7
+ export type MergeDocument = Record<string, any>;
8
+
9
+ /**
10
+ * The caller's BSON: the ObjectId class its driver stores and reads, and its
11
+ * EJSON. Passed in so this package imports neither mongoose nor the driver
12
+ * (with `mongodb`: `{ ObjectId, EJSON: BSON.EJSON }`; with mongoose:
13
+ * `mongoose.mongo.ObjectId` and `mongoose.mongo.BSON.EJSON`). An id built with another copy of the class would not match
14
+ * `instanceof` on the values the caller's driver returns.
15
+ */
16
+ export interface MergeDriver {
17
+ ObjectId: { new (hex: string): { toHexString(): string } };
18
+ EJSON: { stringify(value: any, options?: { relaxed?: boolean }): string };
19
+ }
20
+
21
+ export interface MergeCursorLike {
22
+ limit(n: number): MergeCursorLike;
23
+ sort(spec: any): MergeCursorLike;
24
+ toArray(): Promise<any[]>;
25
+ }
26
+
27
+ /** The slice of a driver `Collection` a merge uses. */
28
+ export interface MergeCollectionLike {
29
+ find(filter: any, options?: any): MergeCursorLike;
30
+ findOne(filter: any, options?: any): Promise<any>;
31
+ countDocuments(filter?: any): Promise<number>;
32
+ updateOne(filter: any, update: any): Promise<{ matchedCount: number }>;
33
+ replaceOne(filter: any, replacement: any): Promise<unknown>;
34
+ deleteOne(filter: any): Promise<{ deletedCount: number }>;
35
+ insertOne(doc: any): Promise<unknown>;
36
+ }
37
+
38
+ /** The slice of a driver `Db` a merge uses: a `mongodb` `Db` or a mongoose connection's `.db` fits. */
39
+ export interface MergeDbLike {
40
+ collection(name: string): MergeCollectionLike;
41
+ listCollections(filter?: any, options?: any): { toArray(): Promise<any[]> };
42
+ }
43
+
44
+ type Document = MergeDocument;
45
+
46
+ /** One stored reference a merge moves from the copy to the kept Org (`orgMerge.ts`). */
47
+ export interface MergeRefImage {
48
+ /** The database the document is in (the platform database, or an app's own on the same server). */
49
+ db: string;
50
+ collection: string;
51
+ /**
52
+ * `value`: `path` holds the id. `list`: `path` is a list of ids. `rows`: `path` is a list of sub-records, `field`
53
+ * holds the id. `answers`: `path` is a list of form answers, `field` in each is a list of values.
54
+ */
55
+ kind: "value" | "list" | "rows" | "answers";
56
+ path: string;
57
+ field?: string;
58
+ docId: unknown;
59
+ /** On `value`: the copy's id exactly as stored, and the kept Org's id in the same stored form. Otherwise the two ids as text. */
60
+ from: unknown;
61
+ to: unknown;
62
+ /** On every other kind: the whole list at `path` before the merge, and as the merge leaves it. */
63
+ before?: unknown[];
64
+ after?: unknown[];
65
+ }
66
+
67
+ /** One copy folded into the Org that is kept: everything the undo needs. Written before the merge is. */
68
+ export interface MergeImage {
69
+ duplicateId: unknown;
70
+ duplicateName: string;
71
+ keptId: unknown;
72
+ keptName: string;
73
+ /** The copy's row exactly as stored before the merge. The undo re-inserts it. */
74
+ duplicate: Document;
75
+ refs: MergeRefImage[];
76
+ /** What was planned for the copy once its references had moved. */
77
+ outcome: "delete" | "retire";
78
+ /** Whether this run adds the copy's id to the kept Org's `legacy_ids.merged_from` (false on a second run). The undo removes it only then. */
79
+ addsLegacyEntry: boolean;
80
+ }
81
+
82
+ /**
83
+ * The saved record of a run of merges, as the undo reads it. The registry
84
+ * fill's before-images file is one (`FillBeforeImages`); the Platform's Admin
85
+ * Hub stores one per merge.
86
+ */
87
+ export interface MergeBeforeImages {
88
+ /** The platform database the merges ran in. */
89
+ db: string;
90
+ /** The `updatedTime` the run stamped on every row it wrote. */
91
+ appliedTime: Date;
92
+ merges?: MergeImage[];
93
+ }