adapt-authoring-courseassets 0.0.1

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/.eslintignore ADDED
@@ -0,0 +1 @@
1
+ node_modules
package/.eslintrc ADDED
@@ -0,0 +1,14 @@
1
+ {
2
+ "env": {
3
+ "browser": false,
4
+ "node": true,
5
+ "commonjs": false,
6
+ "es2020": true
7
+ },
8
+ "extends": [
9
+ "standard"
10
+ ],
11
+ "parserOptions": {
12
+ "ecmaVersion": 2020
13
+ }
14
+ }
@@ -0,0 +1,55 @@
1
+ name: Bug Report
2
+ description: File a bug report
3
+ labels: ["bug"]
4
+ body:
5
+ - type: markdown
6
+ attributes:
7
+ value: |
8
+ Thanks for taking the time to fill out this bug report!
9
+ - type: textarea
10
+ id: description
11
+ attributes:
12
+ label: What happened?
13
+ description: Please describe the issue
14
+ validations:
15
+ required: true
16
+ - type: textarea
17
+ id: expected
18
+ attributes:
19
+ label: Expected behaviour
20
+ description: Tell us what should have happened
21
+ - type: textarea
22
+ id: repro-steps
23
+ attributes:
24
+ label: Steps to reproduce
25
+ description: Tell us how to reproduce the issue
26
+ validations:
27
+ required: true
28
+ - type: input
29
+ id: aat-version
30
+ attributes:
31
+ label: Authoring tool version
32
+ description: What version of the Adapt authoring tool are you running?
33
+ validations:
34
+ required: true
35
+ - type: input
36
+ id: fw-version
37
+ attributes:
38
+ label: Framework version
39
+ description: What version of the Adapt framework are you running?
40
+ - type: dropdown
41
+ id: browsers
42
+ attributes:
43
+ label: What browsers are you seeing the problem on?
44
+ multiple: true
45
+ options:
46
+ - Firefox
47
+ - Chrome
48
+ - Safari
49
+ - Microsoft Edge
50
+ - type: textarea
51
+ id: logs
52
+ attributes:
53
+ label: Relevant log output
54
+ description: Please copy and paste any relevant log output. This will be automatically formatted into code, so no need for backticks.
55
+ render: sh
@@ -0,0 +1 @@
1
+ blank_issues_enabled: false
@@ -0,0 +1,22 @@
1
+ name: Feature request
2
+ description: Request a new feature
3
+ labels: ["enhancement"]
4
+ body:
5
+ - type: markdown
6
+ attributes:
7
+ value: |
8
+ Thanks for taking the time to request a new feature in the Adapt authoring tool! The Adapt team will consider all new feature requests, but unfortunately cannot commit to every one.
9
+ - type: textarea
10
+ id: description
11
+ attributes:
12
+ label: Feature description
13
+ description: Please describe your feature request
14
+ validations:
15
+ required: true
16
+ - type: checkboxes
17
+ id: contribute
18
+ attributes:
19
+ label: Can you work on this feature?
20
+ description: If you are able to commit your own time to work on this feature, it will greatly increase the liklihood of it being implemented by the core dev team. Otherwise, it will be triaged and prioritised alongside the core team's current priorities.
21
+ options:
22
+ - label: I can contribute
@@ -0,0 +1,11 @@
1
+ # To get started with Dependabot version updates, you'll need to specify which
2
+ # package ecosystems to update and where the package manifests are located.
3
+ # Please see the documentation for all configuration options:
4
+ # https://docs.github.com/code-security/dependabot/dependabot-version-updates/configuration-options-for-the-dependabot.yml-file
5
+
6
+ version: 2
7
+ updates:
8
+ - package-ecosystem: "npm" # See documentation for possible values
9
+ directory: "/" # Location of package manifests
10
+ schedule:
11
+ interval: "weekly"
@@ -0,0 +1,25 @@
1
+ [//]: # (Please title your PR according to eslint commit conventions)
2
+ [//]: # (See https://github.com/conventional-changelog/conventional-changelog/tree/master/packages/conventional-changelog-eslint#eslint-convention for details)
3
+
4
+ [//]: # (Add a link to the original issue)
5
+
6
+ [//]: # (Delete as appropriate)
7
+ ### Fix
8
+ * A sentence describing each fix
9
+
10
+ ### Update
11
+ * A sentence describing each udpate
12
+
13
+ ### New
14
+ * A sentence describing each new feature
15
+
16
+ ### Breaking
17
+ * A sentence describing each breaking change
18
+
19
+ [//]: # (List appropriate steps for testing if needed)
20
+ ### Testing
21
+ 1. Steps for testing
22
+
23
+ [//]: # (Mention any other dependencies)
24
+
25
+
@@ -0,0 +1,16 @@
1
+ name: Add labelled PRs to project
2
+
3
+ on:
4
+ pull_request:
5
+ types: [ labeled ]
6
+
7
+ jobs:
8
+ add-to-project:
9
+ if: ${{ github.event.label.name == 'dependencies' }}
10
+ name: Add to main project
11
+ runs-on: ubuntu-latest
12
+ steps:
13
+ - uses: actions/add-to-project@v0.1.0
14
+ with:
15
+ project-url: https://github.com/orgs/adapt-security/projects/5
16
+ github-token: ${{ secrets.PROJECTS_SECRET }}
@@ -0,0 +1,19 @@
1
+ name: Add to main project
2
+
3
+ on:
4
+ issues:
5
+ types:
6
+ - opened
7
+ pull_request:
8
+ types:
9
+ - opened
10
+
11
+ jobs:
12
+ add-to-project:
13
+ name: Add to main project
14
+ runs-on: ubuntu-latest
15
+ steps:
16
+ - uses: actions/add-to-project@v0.1.0
17
+ with:
18
+ project-url: https://github.com/orgs/adapt-security/projects/5
19
+ github-token: ${{ secrets.PROJECTS_SECRET }}
@@ -0,0 +1,5 @@
1
+ {
2
+ "documentation": {
3
+ "enable": true
4
+ }
5
+ }
@@ -0,0 +1,10 @@
1
+ {
2
+ "RESOURCE_IN_USE": {
3
+ "data": {
4
+ "type": "Type of resource",
5
+ "courses": "Courses using the resource"
6
+ },
7
+ "description": "Resource is currently being using in courses",
8
+ "statusCode": 400
9
+ }
10
+ }
package/index.js ADDED
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Tracking of asset usage within courses
3
+ * @namespace courseassets
4
+ */
5
+ export { default } from './lib/courseassetsModule.js'
@@ -0,0 +1,123 @@
1
+ import AbstractApiModule from 'adapt-authoring-api'
2
+ /**
3
+ * Module which handles courseassets automatically using on content events
4
+ * @memberof courseassets
5
+ * @extends {AbstractApiModule}
6
+ */
7
+ class CourseAssetsModule extends AbstractApiModule {
8
+ /** @override */
9
+ async setValues () {
10
+ /** @ignore */ this.root = 'courseassets'
11
+ /** @ignore */ this.schemaName = 'courseasset'
12
+ /** @ignore */ this.collectionName = 'courseassets'
13
+ /** @ignore */ this.routes = this.DEFAULT_ROUTES.filter(r => r.route === '/query')
14
+ }
15
+
16
+ /**
17
+ * Initialise the module
18
+ * @return {Promise}
19
+ */
20
+ async init () {
21
+ await super.init()
22
+
23
+ const [assets, content] = await this.app.waitForModule('assets', 'content')
24
+ /**
25
+ * Cached module instance for easy access
26
+ * @type {AssetsModule}
27
+ */
28
+ this.assets = assets
29
+ /**
30
+ * Cached module instance for easy access
31
+ * @type {ContentModule}
32
+ */
33
+ this.content = content
34
+
35
+ this.assets.preDeleteHook.tap(this.handleDeletedAsset.bind(this));
36
+
37
+ ['insert', 'update', 'delete'].forEach(action => { // note we just log any errors
38
+ const hookName = `post${action[0].toUpperCase()}${action.slice(1)}Hook`
39
+ this.content[hookName].tap(async (...args) => {
40
+ try {
41
+ await this.handleContentEvent(action, ...args).catch(e => this.log('error', e))
42
+ } catch (e) {
43
+ this.log('error', 'COURSEASSETS_UPDATE', e)
44
+ }
45
+ })
46
+ })
47
+ }
48
+
49
+ /**
50
+ * Search data object for asset types and retrieve _ids
51
+ * @param {Object} data
52
+ */
53
+ extractAssetIds (schema, data, assets = []) {
54
+ Object.entries(schema).forEach(([key, val]) => {
55
+ if (!Object.prototype.hasOwnProperty.call(data, key)) {
56
+ return
57
+ }
58
+ if (val.properties) {
59
+ this.extractAssetIds(val.properties, data[key], assets)
60
+ } else if (val?.items?.properties) {
61
+ data[key].forEach(d => this.extractAssetIds(val.items.properties, d, assets))
62
+ } else if (val?._backboneForms?.type === 'Asset' || val?._backboneForms === 'Asset') {
63
+ assets.push(data[key].toString())
64
+ }
65
+ })
66
+ return Array.from(new Set(assets))
67
+ };
68
+
69
+ /**
70
+ * Handler for content event
71
+ * @param {String} action The action performed
72
+ * @param {object} arg1 First argument passed by the event
73
+ * @param {object} arg2 Second argument passed by the event
74
+ */
75
+ async handleContentEvent (action, arg1, arg2) {
76
+ if (action === 'delete') {
77
+ return Promise.all((Array.isArray(arg1) ? arg1 : [arg1]).map(async c => {
78
+ const key = c._type === 'course' ? 'courseId' : 'contentId'
79
+ await this.deleteMany({ [key]: c._id })
80
+ this.log('debug', 'DELETE', c._courseId.toString(), c._id.toString())
81
+ }))
82
+ }
83
+ const type = arg1._type
84
+ const contentId = arg1._id.toString()
85
+ const courseId = arg1._courseId?.toString() ?? (type === 'course' ? contentId : undefined)
86
+ const isModify = action === 'update'
87
+
88
+ if (!contentId || !courseId) {
89
+ return
90
+ }
91
+ // delete any existing course assets for content
92
+ await this.deleteMany({ courseId, contentId })
93
+
94
+ const data = isModify ? arg2 : arg1
95
+ const schema = await this.content.getSchema(this.content.schemaName, data)
96
+ const ids = this.extractAssetIds(schema.built.properties, data)
97
+
98
+ if (!ids.length) {
99
+ return
100
+ }
101
+ await Promise.all(ids.map(async _id => {
102
+ const [asset] = await this.assets.find({ _id })
103
+ if (!asset) {
104
+ throw this.app.errors.NOT_FOUND.setData({ type: 'asset', id: _id })
105
+ }
106
+ await this.insert({ courseId, contentId, assetId: _id })
107
+ }))
108
+ this.log('debug', 'UPDATE', courseId, contentId)
109
+ }
110
+
111
+ async handleDeletedAsset (asset) {
112
+ const results = await this.find({ assetId: asset._id })
113
+ if (!results.length) {
114
+ return
115
+ }
116
+ const courses = (await this.content.find({ _id: { $in: results.map(r => r.courseId) } }))
117
+ .map(c => c.displayTitle || c.title)
118
+
119
+ throw this.app.errors.RESOURCE_IN_USE.setData({ type: 'asset', courses })
120
+ }
121
+ }
122
+
123
+ export default CourseAssetsModule
package/package.json ADDED
@@ -0,0 +1,20 @@
1
+ {
2
+ "name": "adapt-authoring-courseassets",
3
+ "version": "0.0.1",
4
+ "description": "Module for managing courseassets",
5
+ "homepage": "https://github.com/deltanetdan/adapt-authoring-courseassets",
6
+ "license": "GPL-3.0",
7
+ "type": "module",
8
+ "main": "index.js",
9
+ "repository": "github:adapt-security/adapt-authoring-courseassets",
10
+ "peerDependencies": {
11
+ "adapt-authoring-api": "github:adapt-security/adapt-authoring-api",
12
+ "adapt-authoring-content": "github:adapt-security/adapt-authoring-content",
13
+ "adapt-authoring-core": "github:adapt-security/adapt-authoring-core",
14
+ "adapt-authoring-mongodb": "github:adapt-security/adapt-authoring-mongodb"
15
+ },
16
+ "devDependencies": {
17
+ "eslint": "^9.12.0",
18
+ "standard": "^17.1.0"
19
+ }
20
+ }
@@ -0,0 +1,24 @@
1
+ {
2
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
3
+ "$anchor": "courseasset",
4
+ "description": "Metadata related to where an asset is used in a course",
5
+ "type": "object",
6
+ "properties": {
7
+ "courseId": {
8
+ "description": "Course record unique ID",
9
+ "type": "string",
10
+ "isObjectId": true
11
+ },
12
+ "contentId": {
13
+ "description": "The ID for the content record that uses this asset",
14
+ "type": "string",
15
+ "isObjectId": true
16
+ },
17
+ "assetId": {
18
+ "description": "Asset record unique ID",
19
+ "type": "string",
20
+ "isObjectId": true
21
+ }
22
+ },
23
+ "required": ["courseId", "contentId", "assetId"]
24
+ }