@labdigital/commercetools-mock 2.20.1 → 2.20.2
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/dist/index.cjs +14 -6
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +14 -6
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/repositories/abstract.ts +10 -3
- package/src/repositories/category/index.test.ts +10 -0
- package/src/repositories/category/index.ts +18 -1
package/package.json
CHANGED
|
@@ -113,7 +113,9 @@ export abstract class AbstractResourceRepository<
|
|
|
113
113
|
id,
|
|
114
114
|
params,
|
|
115
115
|
);
|
|
116
|
-
return resource
|
|
116
|
+
return resource
|
|
117
|
+
? this.postProcessResource(context, resource, params)
|
|
118
|
+
: null;
|
|
117
119
|
}
|
|
118
120
|
|
|
119
121
|
get(
|
|
@@ -127,7 +129,9 @@ export abstract class AbstractResourceRepository<
|
|
|
127
129
|
id,
|
|
128
130
|
params,
|
|
129
131
|
);
|
|
130
|
-
return resource
|
|
132
|
+
return resource
|
|
133
|
+
? this.postProcessResource(context, resource, params)
|
|
134
|
+
: null;
|
|
131
135
|
}
|
|
132
136
|
|
|
133
137
|
getByKey(
|
|
@@ -141,12 +145,15 @@ export abstract class AbstractResourceRepository<
|
|
|
141
145
|
key,
|
|
142
146
|
params,
|
|
143
147
|
);
|
|
144
|
-
return resource
|
|
148
|
+
return resource
|
|
149
|
+
? this.postProcessResource(context, resource, params)
|
|
150
|
+
: null;
|
|
145
151
|
}
|
|
146
152
|
|
|
147
153
|
postProcessResource(
|
|
148
154
|
context: RepositoryContext,
|
|
149
155
|
resource: ResourceMap[T],
|
|
156
|
+
params?: GetParams,
|
|
150
157
|
): ResourceMap[T] {
|
|
151
158
|
return resource;
|
|
152
159
|
}
|
|
@@ -78,5 +78,15 @@ describe("Order repository", () => {
|
|
|
78
78
|
{ id: level1.id, typeId: "category" },
|
|
79
79
|
{ id: root.id, typeId: "category" },
|
|
80
80
|
]);
|
|
81
|
+
|
|
82
|
+
const expandResult = repository.get({ projectKey: "dummy" }, level3.id, {
|
|
83
|
+
expand: ["ancestors[*]"],
|
|
84
|
+
});
|
|
85
|
+
expect(expandResult?.ancestors).toHaveLength(3);
|
|
86
|
+
expect(expandResult?.ancestors).toEqual([
|
|
87
|
+
{ id: level2.id, typeId: "category", obj: level2 },
|
|
88
|
+
{ id: level1.id, typeId: "category", obj: level1 },
|
|
89
|
+
{ id: root.id, typeId: "category", obj: root },
|
|
90
|
+
]);
|
|
81
91
|
});
|
|
82
92
|
});
|
|
@@ -5,10 +5,12 @@ import type {
|
|
|
5
5
|
} from "@commercetools/platform-sdk";
|
|
6
6
|
import { v4 as uuidv4 } from "uuid";
|
|
7
7
|
import { getBaseResourceProperties } from "~src/helpers";
|
|
8
|
+
import { parseExpandClause } from "~src/lib/expandParser";
|
|
8
9
|
import { AbstractStorage } from "~src/storage/abstract";
|
|
9
10
|
import { Writable } from "~src/types";
|
|
10
11
|
import {
|
|
11
12
|
AbstractResourceRepository,
|
|
13
|
+
GetParams,
|
|
12
14
|
type RepositoryContext,
|
|
13
15
|
} from "../abstract";
|
|
14
16
|
import { createCustomFields } from "../helpers";
|
|
@@ -58,16 +60,31 @@ export class CategoryRepository extends AbstractResourceRepository<"category"> {
|
|
|
58
60
|
postProcessResource(
|
|
59
61
|
context: RepositoryContext,
|
|
60
62
|
resource: Writable<Category>,
|
|
63
|
+
params?: GetParams,
|
|
61
64
|
): Category {
|
|
62
65
|
let node: Category = resource;
|
|
63
66
|
const ancestors: CategoryReference[] = [];
|
|
64
67
|
|
|
68
|
+
// TODO: The expand clause here is a hack, the current expand architecture
|
|
69
|
+
// is not able to handle the case for 'dynamic' fields like ancestors which
|
|
70
|
+
// are resolved at runtime. We should do the expand resolution post query
|
|
71
|
+
// execution for all resources
|
|
72
|
+
|
|
73
|
+
const expandClauses = params?.expand?.map(parseExpandClause) ?? [];
|
|
74
|
+
const addExpand = expandClauses?.find(
|
|
75
|
+
(c) => c.element === "ancestors" && c.index === "*",
|
|
76
|
+
);
|
|
77
|
+
|
|
65
78
|
while (node.parent) {
|
|
66
79
|
node = this._storage.getByResourceIdentifier<"category">(
|
|
67
80
|
context.projectKey,
|
|
68
81
|
node.parent,
|
|
69
82
|
);
|
|
70
|
-
ancestors.push({
|
|
83
|
+
ancestors.push({
|
|
84
|
+
typeId: "category",
|
|
85
|
+
id: node.id,
|
|
86
|
+
obj: addExpand ? node : undefined,
|
|
87
|
+
});
|
|
71
88
|
}
|
|
72
89
|
|
|
73
90
|
resource.ancestors = ancestors;
|