adapt-authoring-content 3.2.2 → 3.2.3

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.
@@ -2,7 +2,7 @@ import { AbstractApiModule } from 'adapt-authoring-api'
2
2
  import { Hook, stringifyValues } from 'adapt-authoring-core'
3
3
  import { createObjectId, parseObjectId } from 'adapt-authoring-mongodb'
4
4
  import { ObjectId } from 'mongodb'
5
- import { ContentTree, buildAssetUsagePipeline, computeSortOrderOps, contentTypeToSchemaName, extractAssetIds, formatFriendlyId, parseMaxSeq } from './utils.js'
5
+ import { ContentTree, buildAssetUsagePipeline, computeSortOrderOps, contentTypeToSchemaName, extractAssetIds, fieldsToProjection, formatFriendlyId, parseMaxSeq } from './utils.js'
6
6
  /**
7
7
  * Module which handles course content
8
8
  * @memberof content
@@ -685,10 +685,28 @@ class ContentModule extends AbstractApiModule {
685
685
  if (ifModifiedSince && lastModified <= ifModifiedSince) {
686
686
  return res.status(304).end()
687
687
  }
688
+ const treeFields = [
689
+ '_id',
690
+ '_parentId',
691
+ '_courseId',
692
+ '_type',
693
+ '_sortOrder',
694
+ 'title',
695
+ 'displayTitle',
696
+ '_friendlyId',
697
+ '_component',
698
+ '_layout',
699
+ '_menu',
700
+ '_theme',
701
+ '_enabledPlugins',
702
+ '_colorLabel',
703
+ 'heroImage',
704
+ 'updatedAt'
705
+ ]
688
706
  const items = await this.find(
689
707
  { _courseId },
690
708
  { validate: false },
691
- { projection: { _id: 1, _parentId: 1, _courseId: 1, _type: 1, _sortOrder: 1, title: 1, displayTitle: 1, _friendlyId: 1, _component: 1, _layout: 1, _menu: 1, _theme: 1, _enabledPlugins: 1, _colorLabel: 1, updatedAt: 1 } }
709
+ { projection: fieldsToProjection(treeFields) }
692
710
  )
693
711
  const tree = new ContentTree(items)
694
712
  res.set('Last-Modified', lastModified.toUTCString())
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Builds a MongoDB inclusion projection from a list of field names, mapping
3
+ * each to `1` (e.g. `['_id', 'title']` -> `{ _id: 1, title: 1 }`). Lets a
4
+ * handler declare its projected fields as a readable one-per-line array.
5
+ * @param {Array<string>} fields Field names to include
6
+ * @return {Object} Inclusion projection
7
+ */
8
+ export default function fieldsToProjection (fields) {
9
+ return Object.fromEntries((fields ?? []).map(field => [field, 1]))
10
+ }
package/lib/utils.js CHANGED
@@ -3,5 +3,6 @@ export { default as buildAssetUsagePipeline } from './utils/buildAssetUsagePipel
3
3
  export { default as computeSortOrderOps } from './utils/computeSortOrderOps.js'
4
4
  export { extractAssetIds } from './utils/extractAssetIds.js'
5
5
  export { default as contentTypeToSchemaName } from './utils/contentTypeToSchemaName.js'
6
+ export { default as fieldsToProjection } from './utils/fieldsToProjection.js'
6
7
  export { default as formatFriendlyId } from './utils/formatFriendlyId.js'
7
8
  export { default as parseMaxSeq } from './utils/parseMaxSeq.js'
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "adapt-authoring-content",
3
- "version": "3.2.2",
3
+ "version": "3.2.3",
4
4
  "description": "Module for managing Adapt content",
5
5
  "homepage": "https://github.com/adapt-security/adapt-authoring-content",
6
6
  "license": "GPL-3.0",
@@ -0,0 +1,22 @@
1
+ import { describe, it } from 'node:test'
2
+ import assert from 'node:assert/strict'
3
+ import fieldsToProjection from '../lib/utils/fieldsToProjection.js'
4
+
5
+ describe('fieldsToProjection', () => {
6
+ it('maps each field name to 1', () => {
7
+ assert.deepEqual(fieldsToProjection(['_id', 'title']), { _id: 1, title: 1 })
8
+ })
9
+
10
+ it('returns an empty projection for an empty array', () => {
11
+ assert.deepEqual(fieldsToProjection([]), {})
12
+ })
13
+
14
+ it('treats a missing argument as empty', () => {
15
+ assert.deepEqual(fieldsToProjection(), {})
16
+ assert.deepEqual(fieldsToProjection(undefined), {})
17
+ })
18
+
19
+ it('collapses duplicate field names to a single key', () => {
20
+ assert.deepEqual(fieldsToProjection(['_id', '_id', 'title']), { _id: 1, title: 1 })
21
+ })
22
+ })