Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FormStatus } from '@defra/forms-model'
import { FormFilterStatus } from '@defra/forms-model'
import { ObjectId } from 'mongodb'

import { escapeRegExp } from '~/src/helpers/string-utils.js'
Expand Down Expand Up @@ -31,11 +31,15 @@ export function buildFilterConditions(options) {
}

if (status && status.length > 0) {
conditions.$or = status.map((s) =>
s === FormStatus.Live
? { live: { $exists: true } }
: { live: { $exists: false } }
)
conditions.$or = status.map((s) => {
if (s === FormFilterStatus.Live) {
return { live: { $exists: true } }
} else if (s === FormFilterStatus.Offline) {
return { offline: true }
} else {
return { live: { $exists: false } }
}
})
}

return conditions
Expand Down Expand Up @@ -74,7 +78,16 @@ export function buildFiltersFacet() {
_id: null, // Single group for all documents
statuses: {
$addToSet: {
$cond: [{ $ifNull: ['$live', false] }, 'live', 'draft'] // If live field exists, status is 'live', else 'draft'
$switch: {
// If offline === true, status is 'offline'
// If live field exists, status is 'live'
// Otherwise 'draft'
branches: [
{ case: { $eq: ['$offline', true] }, then: 'offline' },
{ case: { $ifNull: ['$live', false] }, then: 'live' }
],
default: 'draft'
}
}
}
}
Expand All @@ -92,7 +105,7 @@ export function buildFiltersFacet() {
* @param {string} title - The title to filter by.
* @param {string} author - The author to filter by.
* @param {string[]} organisations - The organisations to filter by.
* @param {FormStatus[]} status - The status values to filter by.
* @param {FormFilterStatus[]} status - The status values to filter by.
* @returns {{ pipeline: PipelineStage[], aggOptions: AggregateOptions }}
*/
export function buildAggregationPipeline(
Expand Down Expand Up @@ -134,7 +147,7 @@ export function buildAggregationPipeline(
* @param {string} title - The title to filter by.
* @param {string} author - The author to filter by.
* @param {string[]} organisations - The organisations to filter by.
* @param {FormStatus[]} status - The status values to filter by.
* @param {FormFilterStatus[]} status - The status values to filter by.
* @returns {{ pipeline: PipelineStage[], aggOptions: AggregateOptions }}
*/
export function buildAggregationPipelineWithVersions(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FormStatus } from '@defra/forms-model'
import { FormFilterStatus } from '@defra/forms-model'
import { ObjectId } from 'mongodb'

import {
Expand Down Expand Up @@ -70,14 +70,18 @@ describe('Form metadata aggregation', () => {

describe('with status filter', () => {
it('should create status filter for live forms', () => {
const result = buildFilterConditions({ status: [FormStatus.Live] })
const result = buildFilterConditions({
status: [FormFilterStatus.Live]
})
expect(result).toEqual({
$or: [{ live: { $exists: true } }]
})
})

it('should create status filter for draft forms', () => {
const result = buildFilterConditions({ status: [FormStatus.Draft] })
const result = buildFilterConditions({
status: [FormFilterStatus.Draft]
})
expect(result).toEqual({
$or: [{ live: { $exists: false } }]
})
Expand All @@ -87,7 +91,7 @@ describe('Form metadata aggregation', () => {
describe('with multiple status values', () => {
it('should create combined status filter', () => {
const result = buildFilterConditions({
status: [FormStatus.Live, FormStatus.Draft]
status: [FormFilterStatus.Live, FormFilterStatus.Draft]
})
expect(result).toEqual({
$or: [{ live: { $exists: true } }, { live: { $exists: false } }]
Expand All @@ -101,7 +105,7 @@ describe('Form metadata aggregation', () => {
title: 'Wildlife Permit Application',
author: 'Henrique Silva',
organisations: ['Natural England', 'Defra'],
status: [FormStatus.Live]
status: [FormFilterStatus.Live]
})

expect(result).toEqual({
Expand Down Expand Up @@ -141,7 +145,7 @@ describe('Form metadata aggregation', () => {
'Wildlife Permit Application',
'Henrique',
['Defra'],
[FormStatus.Live]
[FormFilterStatus.Live]
)

expect(pipeline[0]).toHaveProperty('$match')
Expand Down Expand Up @@ -208,7 +212,7 @@ describe('Form metadata aggregation', () => {
'Wildlife Permit Application',
'Henrique',
['Defra'],
[FormStatus.Live]
[FormFilterStatus.Live]
)

expect(pipeline[0]).toHaveProperty('$match')
Expand Down Expand Up @@ -455,7 +459,13 @@ describe('Form metadata aggregation', () => {
_id: null,
statuses: {
$addToSet: {
$cond: [{ $ifNull: ['$live', false] }, 'live', 'draft']
$switch: {
branches: [
{ case: { $eq: ['$offline', true] }, then: 'offline' },
{ case: { $ifNull: ['$live', false] }, then: 'live' }
],
default: 'draft'
}
}
}
}
Expand Down Expand Up @@ -498,7 +508,7 @@ describe('Form metadata aggregation', () => {
{ name: 'Sarah Wilson (Natural England)' }
],
organisations: [{ name: 'Defra' }, { name: 'Natural England' }],
status: [{ statuses: [FormStatus.Live, FormStatus.Draft] }]
status: [{ statuses: [FormFilterStatus.Live, FormFilterStatus.Draft] }]
}

const result = processFilterResults(filterResults)
Expand Down
6 changes: 3 additions & 3 deletions src/api/forms/repositories/aggregation/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @property {{ $regex: RegExp }} [title] - Optional MongoDB regex query for title matching
* @property {{ displayName: { $regex: RegExp } }} [createdBy] - Optional MongoDB regex query for author matching
* @property {{ $in: string[] }} [organisation] - Optional MongoDB $in query for organisation matching
* @property {{ live: { $exists: boolean } }[]} [$or] - Optional MongoDB $or query for status matching
* @property {{ live?: { $exists: boolean }, offline?: boolean }[]} [$or] - Optional MongoDB $or query for status matching
* @property {object} [$expr] - Optional MongoDB $expr for expression queries
*/

Expand All @@ -15,7 +15,7 @@
* @typedef {object} FilterAggregationResult
* @property {{ name: string }[]} authors - Array of author names
* @property {{ name: string }[]} organisations - Array of organisation names
* @property {[{ statuses: FormStatus[] }]} status - Array containing status values
* @property {[{ statuses: FormFilterStatus[] }]} status - Array containing status values
*/

/**
Expand Down Expand Up @@ -79,5 +79,5 @@
*/

/**
* @import { SearchOptions, FormStatus } from '@defra/forms-model'
* @import { SearchOptions, FormStatus, FormFilterStatus } from '@defra/forms-model'
*/
6 changes: 3 additions & 3 deletions src/api/forms/repositories/form-metadata-repository.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FormStatus } from '@defra/forms-model'
import { FormFilterStatus } from '@defra/forms-model'
import Boom from '@hapi/boom'
import { MongoServerError, ObjectId } from 'mongodb'

Expand Down Expand Up @@ -249,7 +249,7 @@ describe('form-metadata-repository', () => {
title: 'test',
author: 'John',
organisations: ['Defra'],
status: [FormStatus.Draft]
status: [FormFilterStatus.Draft]
}

const result = await list(options)
Expand Down Expand Up @@ -328,7 +328,7 @@ describe('form-metadata-repository', () => {
title: 'test form',
author: 'Jane Doe',
organisations: ['Defra', 'DWP'],
status: [FormStatus.Draft, FormStatus.Live]
status: [FormFilterStatus.Draft, FormFilterStatus.Live]
}

const result = await listWithVersions(options)
Expand Down
6 changes: 3 additions & 3 deletions src/api/forms/service/definition.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
ComponentType,
Engine,
FormDefinitionRequestType,
FormStatus,
FormFilterStatus,
formDefinitionSchema,
formDefinitionV2Schema
} from '@defra/forms-model'
Expand Down Expand Up @@ -1009,7 +1009,7 @@ describe('Forms service', () => {
perPage: 10,
author: 'Henrique Chase',
organisations: ['Defra'],
status: [FormStatus.Live]
status: [FormFilterStatus.Live]
}

jest.mocked(formMetadata.list).mockResolvedValue({
Expand All @@ -1035,7 +1035,7 @@ describe('Forms service', () => {
perPage: 10,
author: 'Henrique Chase',
organisations: ['Defra', 'Natural England'],
status: [FormStatus.Live, FormStatus.Draft]
status: [FormFilterStatus.Live, FormFilterStatus.Draft]
}

jest.mocked(formMetadata.list).mockResolvedValue({
Expand Down
4 changes: 2 additions & 2 deletions src/routes/forms.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1433,7 +1433,7 @@ describe('Forms route', () => {
expect(response.headers['content-type']).toContain(jsonContentType)
expect(response.result).toMatchObject({
error: 'Bad Request',
message: '"status" must be one of [draft, live]',
message: '"status" must be one of [draft, live, offline]',
validation: {
keys: ['status'],
source: 'query'
Expand All @@ -1453,7 +1453,7 @@ describe('Forms route', () => {
expect(response.result).toMatchObject({
error: 'Bad Request',
message:
'"organisations" must be one of [Animal and Plant Health Agency – APHA, Centre for Environment, Fisheries and Aquaculture Science – Cefas, Defra, Environment Agency, Forestry Commission, Marine Management Organisation – MMO, Natural England, Rural Payments Agency – RPA, Veterinary Medicines Directorate – VMD]. "status" must be one of [draft, live]',
'"organisations" must be one of [Animal and Plant Health Agency – APHA, Centre for Environment, Fisheries and Aquaculture Science – Cefas, Defra, Environment Agency, Forestry Commission, Marine Management Organisation – MMO, Natural England, Rural Payments Agency – RPA, Veterinary Medicines Directorate – VMD]. "status" must be one of [draft, live, offline]',
validation: {
keys: ['organisations', 'status'],
source: 'query'
Expand Down
Loading