Skip to content

Commit fb6ec80

Browse files
committed
refactor(audit-log): walk field path patterns with one shared walker
1 parent dffc55e commit fb6ec80

6 files changed

Lines changed: 90 additions & 149 deletions

File tree

‎packages/payload-plugin-audit-log/src/core/changeDiff.ts‎

Lines changed: 15 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { TRUNCATED_MARKER } from "../constants";
22
import type { FieldPathPattern, StoredValuesOf } from "../utils/fields";
3+
import { isRecord } from "../utils/isRecord";
34
import { localeOfSave } from "../utils/locales";
45
import type { LocaleOfRequest } from "../utils/locales";
56
import { createdPaths, diffPaths, isLexicalRichText } from "./diffPaths";
6-
import { isRowOf, matchingRowOf } from "./rows";
7+
import { mapAtPaths, matchingRowOf, valueAt } from "./fieldPaths";
78
import { withStoredLocalizedValues } from "./storedLocalizedValues";
89

910
type Doc = Record<string, unknown>;
@@ -29,95 +30,22 @@ const withinLimit = (paths: readonly string[], maxChangedPaths: number): string[
2930
return truncated ? [...unique.slice(0, maxChangedPaths), TRUNCATED_MARKER] : unique;
3031
};
3132

32-
const isRecord = (value: unknown): value is Doc =>
33-
typeof value === "object" && value !== null && !Array.isArray(value) && !(value instanceof Date);
34-
35-
function withoutFieldMissingFromAfter(
36-
before: unknown,
37-
after: unknown,
38-
pattern: FieldPathPattern
39-
): unknown {
40-
const [segment, ...rest] = pattern;
41-
if (segment === undefined) return before;
42-
if (typeof segment !== "string") {
43-
if (!Array.isArray(before)) return before;
44-
const afterRows = Array.isArray(after) ? after : [];
45-
const rows = before.map((row, index) =>
46-
isRowOf(segment, row)
47-
? withoutFieldMissingFromAfter(row, matchingRowOf(row, index, afterRows), rest)
48-
: row
49-
);
50-
return rows.every((row, index) => row === before[index]) ? before : rows;
51-
}
52-
if (!(isRecord(before) && segment in before)) return before;
53-
const afterValue = isRecord(after) ? after[segment] : undefined;
54-
if (rest.length === 0) {
55-
if (afterValue !== undefined) return before;
56-
return Object.fromEntries(Object.entries(before).filter(([key]) => key !== segment));
57-
}
58-
const stripped = withoutFieldMissingFromAfter(before[segment], afterValue, rest);
59-
return stripped === before[segment] ? before : { ...before, [segment]: stripped };
60-
}
61-
6233
const withoutFieldsMissingFromAfter = (
6334
before: Doc | undefined,
6435
after: Doc,
6536
patterns: readonly FieldPathPattern[]
6637
): Doc | undefined =>
67-
patterns.reduce<Doc | undefined>(
68-
(readable, pattern) =>
69-
withoutFieldMissingFromAfter(readable, after, pattern) as Doc | undefined,
70-
before
38+
mapAtPaths(before, [after], patterns, (parent, key, [afterParent]) =>
39+
key in parent && valueAt(afterParent, key) === undefined
40+
? Object.fromEntries(Object.entries(parent).filter(([field]) => field !== key))
41+
: parent
7142
);
7243

7344
type ComparedValueOf = (before: Doc, after: Doc, saved: Doc, key: string) => unknown;
7445

7546
const restrictedValueToCompare: ComparedValueOf = (before, after, saved, key) =>
7647
after[key] === undefined && key in before && saved[key] !== undefined ? saved[key] : after[key];
7748

78-
function withSavedValueAt(
79-
before: unknown,
80-
after: unknown,
81-
saved: unknown,
82-
pattern: FieldPathPattern,
83-
comparedValueOf: ComparedValueOf
84-
): unknown {
85-
const [segment, ...rest] = pattern;
86-
if (segment === undefined) return after;
87-
if (typeof segment !== "string") {
88-
if (!Array.isArray(after)) return after;
89-
const beforeRows = Array.isArray(before) ? before : [];
90-
const savedRows = Array.isArray(saved) ? saved : [];
91-
const rows = after.map((row, index) =>
92-
isRowOf(segment, row)
93-
? withSavedValueAt(
94-
matchingRowOf(row, index, beforeRows),
95-
row,
96-
matchingRowOf(row, index, savedRows),
97-
rest,
98-
comparedValueOf
99-
)
100-
: row
101-
);
102-
return rows.every((row, index) => row === after[index]) ? after : rows;
103-
}
104-
if (!isRecord(after)) return after;
105-
const comparedBefore = isRecord(before) ? before : {};
106-
const comparedSaved = isRecord(saved) ? saved : {};
107-
if (rest.length === 0) {
108-
const compared = comparedValueOf(comparedBefore, after, comparedSaved, segment);
109-
return Object.is(compared, after[segment]) ? after : { ...after, [segment]: compared };
110-
}
111-
const withSaved = withSavedValueAt(
112-
comparedBefore[segment],
113-
after[segment],
114-
comparedSaved[segment],
115-
rest,
116-
comparedValueOf
117-
);
118-
return withSaved === after[segment] ? after : { ...after, [segment]: withSaved };
119-
}
120-
12149
const storedValueMissingFromDoc: ComparedValueOf = (_before, doc, stored, key) =>
12250
doc[key] === undefined && stored[key] !== undefined ? stored[key] : doc[key];
12351

@@ -128,11 +56,15 @@ const withSavedValuesAt = (
12856
patterns: readonly FieldPathPattern[],
12957
comparedValueOf: ComparedValueOf
13058
): Doc =>
131-
patterns.reduce<Doc>(
132-
(comparable, pattern) =>
133-
withSavedValueAt(before, comparable, saved, pattern, comparedValueOf) as Doc,
134-
after
135-
);
59+
mapAtPaths(after, [before, saved], patterns, (parent, key, [beforeParent, savedParent]) => {
60+
const compared = comparedValueOf(
61+
isRecord(beforeParent) ? beforeParent : {},
62+
parent,
63+
isRecord(savedParent) ? savedParent : {},
64+
key
65+
);
66+
return Object.is(compared, parent[key]) ? parent : { ...parent, [key]: compared };
67+
});
13668

13769
export const withStoredValuesOfFieldsMissingFrom = (
13870
doc: Doc,
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import type { FieldPathPattern, RowsSegment } from "../utils/fields";
2+
import { isRecord } from "../utils/isRecord";
3+
4+
type Doc = Record<string, unknown>;
5+
6+
export type AtPathEnd = (parent: Doc, key: string, referenceParents: readonly unknown[]) => Doc;
7+
8+
export const matchingRowOf = (row: unknown, index: number, rows: readonly unknown[]): unknown => {
9+
const rowId = isRecord(row) ? row.id : undefined;
10+
if (rowId === undefined || rowId === null) return rows[index];
11+
return rows.find((candidate) => isRecord(candidate) && candidate.id === rowId);
12+
};
13+
14+
const isRowOf = (segment: RowsSegment, row: unknown): boolean =>
15+
segment.blockType === undefined || (isRecord(row) && row.blockType === segment.blockType);
16+
17+
export const valueAt = (parent: unknown, key: string): unknown =>
18+
isRecord(parent) ? parent[key] : undefined;
19+
20+
function mapAtPath(
21+
value: unknown,
22+
references: readonly unknown[],
23+
pattern: FieldPathPattern,
24+
atPathEnd: AtPathEnd
25+
): unknown {
26+
const [segment, ...rest] = pattern;
27+
if (segment === undefined) return value;
28+
if (typeof segment !== "string") {
29+
if (!Array.isArray(value)) return value;
30+
const referenceRows = references.map((reference) =>
31+
Array.isArray(reference) ? reference : []
32+
);
33+
const rows = value.map((row, index) =>
34+
isRowOf(segment, row)
35+
? mapAtPath(
36+
row,
37+
referenceRows.map((candidates) => matchingRowOf(row, index, candidates)),
38+
rest,
39+
atPathEnd
40+
)
41+
: row
42+
);
43+
return rows.every((row, index) => row === value[index]) ? value : rows;
44+
}
45+
if (!isRecord(value)) return value;
46+
if (rest.length === 0) return atPathEnd(value, segment, references);
47+
const mapped = mapAtPath(
48+
value[segment],
49+
references.map((reference) => valueAt(reference, segment)),
50+
rest,
51+
atPathEnd
52+
);
53+
return mapped === value[segment] ? value : { ...value, [segment]: mapped };
54+
}
55+
56+
export const mapAtPaths = <V extends Doc | undefined>(
57+
value: V,
58+
references: readonly unknown[],
59+
patterns: readonly FieldPathPattern[],
60+
atPathEnd: AtPathEnd
61+
): V =>
62+
patterns.reduce<unknown>(
63+
(mapped, pattern) => mapAtPath(mapped, references, pattern, atPathEnd),
64+
value
65+
) as V;

‎packages/payload-plugin-audit-log/src/core/rows.ts‎

Lines changed: 0 additions & 15 deletions
This file was deleted.
Lines changed: 6 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,7 @@
11
import type { FieldPathPattern } from "../utils/fields";
2-
import { isRowOf, matchingRowOf } from "./rows";
2+
import { mapAtPaths, valueAt } from "./fieldPaths";
33

44
type Doc = Record<string, unknown>;
5-
type LeafValue = (value: unknown, reference: unknown) => unknown;
6-
7-
const isRecord = (value: unknown): value is Doc =>
8-
typeof value === "object" && value !== null && !Array.isArray(value) && !(value instanceof Date);
9-
10-
function withLeafValueAt(
11-
value: unknown,
12-
reference: unknown,
13-
path: FieldPathPattern,
14-
leafValue: LeafValue
15-
): unknown {
16-
const [segment, ...rest] = path;
17-
if (segment === undefined) return value;
18-
if (typeof segment !== "string") {
19-
if (!Array.isArray(value)) return value;
20-
const referenceRows = Array.isArray(reference) ? reference : [];
21-
const rows = value.map((row, index) =>
22-
isRowOf(segment, row)
23-
? withLeafValueAt(row, matchingRowOf(row, index, referenceRows), rest, leafValue)
24-
: row
25-
);
26-
return rows.every((row, index) => row === value[index]) ? value : rows;
27-
}
28-
if (!(isRecord(value) && segment in value)) return value;
29-
const referenceValue = isRecord(reference) ? reference[segment] : undefined;
30-
const replaced =
31-
rest.length === 0
32-
? leafValue(value[segment], referenceValue)
33-
: withLeafValueAt(value[segment], referenceValue, rest, leafValue);
34-
return replaced === value[segment] ? value : { ...value, [segment]: replaced };
35-
}
36-
37-
const withLeafValuesAt = (
38-
doc: Doc,
39-
reference: unknown,
40-
paths: readonly FieldPathPattern[],
41-
leafValue: LeafValue
42-
): Doc =>
43-
paths.reduce<Doc>(
44-
(replaced, path) => withLeafValueAt(replaced, reference, path, leafValue) as Doc,
45-
doc
46-
);
47-
48-
const storedInLocale = (stored: unknown, locale: string): unknown =>
49-
isRecord(stored) ? stored[locale] : undefined;
505

516
export function withStoredLocalizedValues(
527
doc: Doc,
@@ -55,7 +10,9 @@ export function withStoredLocalizedValues(
5510
locale: string
5611
): Doc {
5712
if (!stored) return doc;
58-
return withLeafValuesAt(doc, stored, localizedFields, (_shown, storedValue) =>
59-
storedInLocale(storedValue, locale)
60-
);
13+
return mapAtPaths(doc, [stored], localizedFields, (parent, key, [storedParent]) => {
14+
if (!(key in parent)) return parent;
15+
const storedInLocale = valueAt(valueAt(storedParent, key), locale);
16+
return storedInLocale === parent[key] ? parent : { ...parent, [key]: storedInLocale };
17+
});
6118
}

‎packages/payload-plugin-audit-log/src/utils/fields.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ type Doc = Record<string, unknown>;
7575

7676
export type StoredValuesOf = (doc: Doc) => Doc;
7777

78-
const isRecord = (value: unknown): value is Doc =>
78+
const isNonArrayObject = (value: unknown): value is Doc =>
7979
typeof value === "object" && value !== null && !Array.isArray(value);
8080

8181
const hasNamedSubfields = (field: FlattenedFieldShape): boolean =>
@@ -95,7 +95,7 @@ function withStoredValues(
9595
.filter(([key]) => isStored(key))
9696
.map(([key, value]) => {
9797
const field = fieldsByName.get(key);
98-
if (!(field?.flattenedFields && hasNamedSubfields(field) && isRecord(value))) {
98+
if (!(field?.flattenedFields && hasNamedSubfields(field) && isNonArrayObject(value))) {
9999
return [key, value];
100100
}
101101
const subfields = fieldsByNameOf(field.flattenedFields);
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export const isRecord = (value: unknown): value is Record<string, unknown> =>
2+
typeof value === "object" && value !== null && !Array.isArray(value) && !(value instanceof Date);

0 commit comments

Comments
 (0)