Skip to content

Commit a1cdde7

Browse files
committed
feat(content-item): allow workflow state facet
1 parent d1c3e74 commit a1cdde7

5 files changed

Lines changed: 61 additions & 0 deletions

File tree

‎docs/CONTENT-ITEM.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ The content item export, copy, move, archive and unarchive commands allow the us
8383
- `schema`: Filter on schema ids. Example: `--facet schema:http://example.com/schema.json`
8484
- `locale`: Filter on content item locale. Example: `--facet locale:en-GB`
8585
- `lastModifiedDate`: Filter on last modified date. Example: `--facet "lastModifiedDate:Last 7 days"`
86+
- `workflowState`: Filter on workflow state (by workflow state ID). Example: `--facet "workflowState:655f5832db7a1637df1984eb"`
8687

8788
Multiple facets can be applied at once when separated by a comma. Example:
8889
`--facet "schema:http://example.com/schema.json, name:/name regex/"`

‎src/common/filter/facet.spec.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,18 @@ describe('facet', () => {
242242
const itemsStatus = [new ContentItem({ status: 'ACTIVE' }), new ContentItem({ status: 'ARCHIVED' })];
243243
expect(applyFacet(itemsStatus, { status: 'ARCHIVED' })).toEqual([itemsStatus[1]]);
244244

245+
const itemsWorkflowState = [
246+
Object.assign(new ContentItem({ label: 'draft', workflow: { state: '655f583334f8833c81af64d1' } }), {
247+
workflowState: '655f583334f8833c81af64d1'
248+
}),
249+
Object.assign(new ContentItem({ label: 'published', workflow: { state: '655f5832db7a1637df1984eb' } }), {
250+
workflowState: '655f5832db7a1637df1984eb'
251+
})
252+
];
253+
expect(applyFacet(itemsWorkflowState, { workflowState: '655f5832db7a1637df1984eb' })).toEqual([
254+
itemsWorkflowState[1]
255+
]);
256+
245257
const itemsDate = [
246258
new ContentItem({ lastModifiedDate: fakeDateM1 }),
247259
new ContentItem({ lastModifiedDate: fakeDateM10 })

‎src/common/filter/facet.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export interface Facet {
1313
schema?: string;
1414
status?: string;
1515
lastModifiedDate?: FacetRange | DatePreset;
16+
workflowState?: string;
1617
}
1718

1819
export type DatePreset = 'Last 7 days' | 'Last 14 days' | 'Last 30 days' | 'Last 60 days' | 'Over 60 days';
@@ -188,6 +189,8 @@ export function applyFacet(items: ContentItem[], facetOrString: Facet | string):
188189
)
189190
return false;
190191
if (facet.status && !equalsOrRegex(item.status, facet.status)) return false;
192+
if (facet.workflowState && (!item.workflow || !equalsOrRegex(item.workflow.state, facet.workflowState)))
193+
return false;
191194

192195
// Date range checks.
193196
if (facet.lastModifiedDate && !dateRangeMatch(item.lastModifiedDate, facet.lastModifiedDate)) return false;

‎src/common/filter/fetch-content.spec.ts‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,39 @@ describe('fetch-content', () => {
357357
expect(facetContentModule.shouldFacetEnriched).not.toHaveBeenCalled();
358358
});
359359

360+
it('should add workflow state to field if an exact match array can be extracted', async () => {
361+
const client = await dynamicContentClientFactory(config);
362+
const hub = new Hub({});
363+
const item1 = new ContentItem({ label: 'item1' });
364+
hub.related.contentItems.facet = jest.fn().mockResolvedValue(new MockPage(ContentItem, [item1]));
365+
jest.spyOn(facetContentModule, 'shouldFacetEnriched').mockResolvedValue(true);
366+
367+
const facet = {
368+
workflowState: '/^655f583334f8833c81af64d1$|^655f5832db7a1637df1984eb$/'
369+
};
370+
371+
expect(await facetContentModule.tryFetchContent(client, hub, facet, params)).toEqual([item1]);
372+
373+
expect(hub.related.contentItems.facet).toHaveBeenCalledWith(
374+
{
375+
fields: [
376+
{
377+
facetAs: 'ENUM',
378+
field: 'workflow.state',
379+
filter: {
380+
type: 'IN',
381+
values: ['655f583334f8833c81af64d1', '655f5832db7a1637df1984eb']
382+
}
383+
}
384+
],
385+
returnEntities: true
386+
},
387+
{ query: 'contentRepositoryId:"repo"folderId:"folder"', size: expect.any(Number) }
388+
);
389+
expect(client.contentItems.get).not.toHaveBeenCalled();
390+
expect(facetContentModule.shouldFacetEnriched).not.toHaveBeenCalled();
391+
});
392+
360393
it('should prefer status from params to the one in the facet object', async () => {
361394
const client = await dynamicContentClientFactory(config);
362395
const hub = new Hub({});

‎src/common/filter/fetch-content.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,18 @@ export const tryFetchContent = async (
128128
});
129129
}
130130

131+
const workflowStateArray = tryGetArray(facet.workflowState, true);
132+
if (workflowStateArray) {
133+
facetQuery.fields.push({
134+
facetAs: 'ENUM',
135+
field: 'workflow.state',
136+
filter: {
137+
type: 'IN',
138+
values: workflowStateArray
139+
}
140+
});
141+
}
142+
131143
if (facet.name) {
132144
const names = tryGetArray(facet.name, false);
133145

0 commit comments

Comments
 (0)