@terreno/admin-backend 0.20.1 → 0.21.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,28 @@
1
+ import {describe, expect, it} from "bun:test";
2
+
3
+ import {buildAdminModelQueryFields} from "./adminUiV2";
4
+
5
+ describe("buildAdminModelQueryFields", () => {
6
+ it("includes _id, list fields, listDisplay, searchFields, and filter fields", () => {
7
+ const fields = buildAdminModelQueryFields({
8
+ filters: [
9
+ {field: "completed", kind: "boolean"},
10
+ {choices: [{label: "L", value: "low"}], field: "priority", kind: "choice"},
11
+ {field: "created", kind: "dateRange"},
12
+ ],
13
+ listDisplay: ["title", "created"],
14
+ listFields: ["title", "ownerId"],
15
+ searchFields: ["tags"],
16
+ });
17
+ expect(fields).toContain("_id");
18
+ expect(fields).toContain("title");
19
+ expect(fields).toContain("ownerId");
20
+ expect(fields).toContain("created");
21
+ expect(fields).toContain("tags");
22
+ expect(fields).toContain("completed");
23
+ expect(fields).toContain("priority");
24
+ expect(fields).toContain("created_gte");
25
+ expect(fields).toContain("created_lte");
26
+ expect(new Set(fields).size).toBe(fields.length);
27
+ });
28
+ });
@@ -0,0 +1,188 @@
1
+ /**
2
+ * Admin UI v2 helpers and constants (Django-parity config contract).
3
+ * @see docs/implementationPlans/admin-ui-v2-django-parity.md
4
+ */
5
+
6
+ export const ADMIN_SCHEMA_VERSION = 2;
7
+
8
+ export const MAX_BULK_PATCH_IDS = 1000;
9
+
10
+ export const SYSTEM_ADMIN_FIELDS = new Set(["_id", "id", "__v", "created", "updated", "deleted"]);
11
+
12
+ export interface AdminHomeSlotsInput {
13
+ contentTop?: string[];
14
+ main?: string[];
15
+ navGlobal?: string[];
16
+ sidebar?: string[];
17
+ }
18
+
19
+ export interface AdminHomeInput {
20
+ slots?: AdminHomeSlotsInput;
21
+ title?: string;
22
+ /** Legacy: widget ids treated as main column when slots omitted */
23
+ widgets?: string[];
24
+ }
25
+
26
+ export interface AdminHomeNormalized {
27
+ slots: {
28
+ contentTop?: string[];
29
+ main?: string[];
30
+ navGlobal?: string[];
31
+ sidebar?: string[];
32
+ };
33
+ title: string;
34
+ }
35
+
36
+ export const normalizeSidebarRecentLast = (sidebar?: string[]): string[] | undefined => {
37
+ if (!sidebar || sidebar.length === 0) {
38
+ return sidebar;
39
+ }
40
+ if (!sidebar.includes("recentActivity")) {
41
+ return sidebar;
42
+ }
43
+ const without = sidebar.filter((id) => id !== "recentActivity");
44
+ return [...without, "recentActivity"];
45
+ };
46
+
47
+ export const normalizeAdminHome = (home?: AdminHomeInput): AdminHomeNormalized => {
48
+ const title = home?.title ?? "Administration";
49
+ if (home?.slots) {
50
+ return {
51
+ slots: {
52
+ ...home.slots,
53
+ sidebar: normalizeSidebarRecentLast(home.slots.sidebar),
54
+ },
55
+ title,
56
+ };
57
+ }
58
+ if (home?.widgets && home.widgets.length > 0) {
59
+ const main = home.widgets.includes("modelsGrid")
60
+ ? [...home.widgets]
61
+ : ["modelsGrid", ...home.widgets];
62
+ return {
63
+ slots: {main},
64
+ title,
65
+ };
66
+ }
67
+ return {
68
+ slots: {main: ["modelsGrid"]},
69
+ title,
70
+ };
71
+ };
72
+
73
+ export interface AdminListFilterBoolean {
74
+ field: string;
75
+ kind: "boolean";
76
+ label?: string;
77
+ }
78
+
79
+ export interface AdminListFilterChoice {
80
+ choices: {label: string; value: string}[];
81
+ field: string;
82
+ kind: "choice";
83
+ label?: string;
84
+ }
85
+
86
+ export interface AdminListFilterDateRange {
87
+ field: string;
88
+ kind: "dateRange";
89
+ label?: string;
90
+ }
91
+
92
+ export interface AdminListFilterRef {
93
+ field: string;
94
+ kind: "ref";
95
+ label?: string;
96
+ refModel?: string;
97
+ }
98
+
99
+ export interface AdminListFilterText {
100
+ field: string;
101
+ kind: "text";
102
+ label?: string;
103
+ }
104
+
105
+ export type AdminListFilter =
106
+ | AdminListFilterBoolean
107
+ | AdminListFilterChoice
108
+ | AdminListFilterDateRange
109
+ | AdminListFilterRef
110
+ | AdminListFilterText;
111
+
112
+ /** Inputs shared by admin config and {@link buildAdminModelQueryFields}. */
113
+ export interface AdminModelQueryFieldSource {
114
+ filters?: AdminListFilter[];
115
+ listDisplay?: string[];
116
+ listFields: string[];
117
+ searchFields?: string[];
118
+ }
119
+
120
+ /**
121
+ * Derives `modelRouter` `queryFields` from admin UI v2 list metadata so changelist
122
+ * filters and sort keys match allowed query parameters on `GET` list routes.
123
+ */
124
+ export const buildAdminModelQueryFields = (config: AdminModelQueryFieldSource): string[] => {
125
+ const fields = new Set<string>(["_id"]);
126
+ for (const name of config.listFields) {
127
+ fields.add(name);
128
+ }
129
+ for (const name of config.listDisplay ?? []) {
130
+ fields.add(name);
131
+ }
132
+ for (const name of config.searchFields ?? []) {
133
+ fields.add(name);
134
+ }
135
+ for (const filter of config.filters ?? []) {
136
+ fields.add(filter.field);
137
+ if (filter.kind === "dateRange") {
138
+ fields.add(`${filter.field}_gte`);
139
+ fields.add(`${filter.field}_lte`);
140
+ }
141
+ }
142
+ return [...fields];
143
+ };
144
+
145
+ export interface AdminFieldsetInput {
146
+ fields: string[];
147
+ title: string;
148
+ }
149
+
150
+ export interface AdminActionInput {
151
+ background?: boolean;
152
+ confirm?: string;
153
+ id: string;
154
+ label: string;
155
+ /** Keys allowed for synchronous bulk-patch actions */
156
+ patchKeys?: string[];
157
+ }
158
+
159
+ export interface AdminModelPermissionsInput {
160
+ create?: boolean;
161
+ delete?: boolean;
162
+ update?: boolean;
163
+ }
164
+
165
+ export const defaultBulkPatchAllowlistFrom = (params: {
166
+ hiddenFieldSet: Set<string>;
167
+ listFields: string[];
168
+ readonlyFields: string[];
169
+ schemaPaths: Set<string>;
170
+ }): string[] => {
171
+ const out: string[] = [];
172
+ for (const field of params.listFields) {
173
+ if (SYSTEM_ADMIN_FIELDS.has(field)) {
174
+ continue;
175
+ }
176
+ if (params.hiddenFieldSet.has(field)) {
177
+ continue;
178
+ }
179
+ if (params.readonlyFields.includes(field)) {
180
+ continue;
181
+ }
182
+ if (!params.schemaPaths.has(field)) {
183
+ continue;
184
+ }
185
+ out.push(field);
186
+ }
187
+ return out;
188
+ };
package/src/index.ts CHANGED
@@ -1,5 +1,13 @@
1
- export type {AdminModelConfig, AdminOptions, AdminScriptConfig} from "./adminApp";
1
+ export type {
2
+ AdminAuditEvent,
3
+ AdminCustomScreenConfig,
4
+ AdminFieldOverride,
5
+ AdminModelConfig,
6
+ AdminOptions,
7
+ AdminScriptConfig,
8
+ } from "./adminApp";
2
9
  export {AdminApp} from "./adminApp";
10
+ export * from "./adminUiV2";
3
11
  export type {
4
12
  DocumentFile,
5
13
  DocumentListResponse,
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Preload before `api/src/tests/bunSetup.ts` so admin-backend tests use
3
+ * mongodb-memory-server (see TERRENO_TEST_USE_MEMORY_MONGO in that file).
4
+ */
5
+ process.env.TERRENO_TEST_USE_MEMORY_MONGO = "true";