Skip to content

Commit 31f62b7

Browse files
authored
Merge pull request #770 from DEFRA/feat/df-907-form-id
feat/df-907: Allows searching on form id
2 parents 0bf4729 + 9901be6 commit 31f62b7

2 files changed

Lines changed: 17 additions & 2 deletions

File tree

src/api/forms/repositories/aggregation/form-metadata-aggregation.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { FormStatus } from '@defra/forms-model'
2+
import { ObjectId } from 'mongodb'
23

34
import { escapeRegExp } from '~/src/helpers/string-utils.js'
45

@@ -12,8 +13,12 @@ export function buildFilterConditions(options) {
1213
const conditions = {}
1314

1415
if (title) {
15-
const regex = new RegExp(escapeRegExp(title), 'i')
16-
conditions.title = { $regex: regex }
16+
if (ObjectId.isValid(title)) {
17+
conditions._id = { $eq: new ObjectId(title) }
18+
} else {
19+
const regex = new RegExp(escapeRegExp(title), 'i')
20+
conditions.title = { $regex: regex }
21+
}
1722
}
1823

1924
if (author) {

src/api/forms/repositories/aggregation/form-metadata-aggregation.test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { FormStatus } from '@defra/forms-model'
2+
import { ObjectId } from 'mongodb'
23

34
import {
45
addDateFieldStage,
@@ -36,6 +37,15 @@ describe('Form metadata aggregation', () => {
3637
title: { $regex: /Test Form/i }
3738
})
3839
})
40+
41+
it('should create object id filter if a form id is supplied', () => {
42+
const result = buildFilterConditions({
43+
title: '691db72966b1bdc98fa3e72a'
44+
})
45+
expect(result).toEqual({
46+
_id: { $eq: new ObjectId('691db72966b1bdc98fa3e72a') }
47+
})
48+
})
3949
})
4050

4151
describe('with author filter', () => {

0 commit comments

Comments
 (0)