@mongoosejs/studio 0.0.89 → 0.0.91

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.
Files changed (36) hide show
  1. package/astra.js +2 -2
  2. package/backend/actions/ChatMessage/executeScript.js +7 -1
  3. package/backend/actions/ChatThread/createChatMessage.js +7 -1
  4. package/backend/actions/ChatThread/createChatThread.js +6 -0
  5. package/backend/actions/ChatThread/getChatThread.js +7 -1
  6. package/backend/actions/ChatThread/listChatThreads.js +8 -2
  7. package/backend/actions/Dashboard/createDashboard.js +9 -2
  8. package/backend/actions/Dashboard/deleteDashboard.js +8 -3
  9. package/backend/actions/Dashboard/getDashboard.js +9 -3
  10. package/backend/actions/Dashboard/getDashboards.js +5 -2
  11. package/backend/actions/Dashboard/updateDashboard.js +10 -4
  12. package/backend/actions/Model/createDocument.js +5 -6
  13. package/backend/actions/Model/deleteDocument.js +5 -5
  14. package/backend/actions/Model/deleteDocuments.js +6 -6
  15. package/backend/actions/Model/dropIndex.js +36 -0
  16. package/backend/actions/Model/exportQueryResults.js +7 -1
  17. package/backend/actions/Model/getDocument.js +9 -3
  18. package/backend/actions/Model/getDocuments.js +7 -0
  19. package/backend/actions/Model/getIndexes.js +6 -2
  20. package/backend/actions/Model/index.js +1 -0
  21. package/backend/actions/Model/listModels.js +14 -2
  22. package/backend/actions/Model/updateDocument.js +5 -5
  23. package/backend/actions/Model/updateDocuments.js +5 -6
  24. package/backend/authorize.js +36 -0
  25. package/frontend/public/app.js +45 -9
  26. package/frontend/src/api.js +9 -0
  27. package/frontend/src/index.js +6 -4
  28. package/frontend/src/models/models.html +8 -7
  29. package/frontend/src/models/models.js +8 -1
  30. package/frontend/src/mothership.js +8 -0
  31. package/frontend/src/splash/splash.html +19 -7
  32. package/frontend/src/splash/splash.js +4 -0
  33. package/frontend/src/team/new-invitation/new-invitation.html +5 -1
  34. package/frontend/src/team/new-invitation/new-invitation.js +6 -0
  35. package/frontend/src/team/team.html +2 -1
  36. package/package.json +1 -1
package/astra.js CHANGED
@@ -154,6 +154,6 @@ void async function main() {
154
154
  # Astra Notes
155
155
 
156
156
  1. Must use collections. Tables don't support `countDocuments()` or `estimatedDocumentCount()`.
157
- 2. Annoying issue: collections don't let you store keys that start with '$'. Ended up creating separate connection to store ChatMessages in MongoDB.
158
- 3. `countDocuments()` with filter erroring out with more than 1000 documents is problematic, e.g. https://studio-astra-demo.netlify.app/imdb/#/?search={genres:'Drama'} fails
157
+ 2. Collections don't let you store keys that start with '$', which is problematic for `$chart`. Ended up creating separate connection to store ChatMessages in MongoDB.
158
+ 3. `countDocuments()` with filter erroring out with more than 1000 documents caused trouble. Worked around it by converting `countDocuments()` to `find()` using Mongoose middleware.
159
159
  */
@@ -1,6 +1,7 @@
1
1
  'use strict';
2
2
 
3
3
  const Archetype = require('archetype');
4
+ const authorize = require('../../authorize');
4
5
  const mongoose = require('mongoose');
5
6
  const vm = require('vm');
6
7
 
@@ -12,7 +13,10 @@ const ExecuteScriptParams = new Archetype({
12
13
  $type: mongoose.Types.ObjectId
13
14
  },
14
15
  script: {
15
- $type: String
16
+ $type: 'string'
17
+ },
18
+ roles: {
19
+ $type: ['string']
16
20
  }
17
21
  }).compile('ExecuteScriptParams');
18
22
 
@@ -20,6 +24,8 @@ module.exports = ({ db, studioConnection }) => async function executeScript(para
20
24
  const { userId, chatMessageId, script } = new ExecuteScriptParams(params);
21
25
  const ChatMessage = studioConnection.model('__Studio_ChatMessage');
22
26
 
27
+ await authorize('ChatMessage.executeScript', roles);
28
+
23
29
  const chatMessage = await ChatMessage.findById(chatMessageId);
24
30
  if (!chatMessage) {
25
31
  throw new Error('Chat message not found');
@@ -1,6 +1,7 @@
1
1
  'use strict';
2
2
 
3
3
  const Archetype = require('archetype');
4
+ const authorize = require('../../authorize');
4
5
  const getModelDescriptions = require('../../helpers/getModelDescriptions');
5
6
  const mongoose = require('mongoose');
6
7
 
@@ -17,6 +18,9 @@ const CreateChatMessageParams = new Archetype({
17
18
  authorization: {
18
19
  $type: 'string',
19
20
  $required: true
21
+ },
22
+ roles: {
23
+ $type: ['string'],
20
24
  }
21
25
  }).compile('CreateChatMessageParams');
22
26
 
@@ -56,10 +60,12 @@ Here is a description of the user's models. Assume these are the only models ava
56
60
  `.trim();
57
61
 
58
62
  module.exports = ({ db, studioConnection, options }) => async function createChatMessage(params) {
59
- const { chatThreadId, userId, content, script, authorization } = new CreateChatMessageParams(params);
63
+ const { chatThreadId, userId, content, script, authorization, roles } = new CreateChatMessageParams(params);
60
64
  const ChatThread = studioConnection.model('__Studio_ChatThread');
61
65
  const ChatMessage = studioConnection.model('__Studio_ChatMessage');
62
66
 
67
+ await authorize('ChatThread.createChatMessage', roles);
68
+
63
69
  // Check that the user owns the thread
64
70
  const chatThread = await ChatThread.findOne({ _id: chatThreadId });
65
71
  if (!chatThread) {
@@ -1,11 +1,15 @@
1
1
  'use strict';
2
2
 
3
3
  const Archetype = require('archetype');
4
+ const authorize = require('../../authorize');
4
5
  const mongoose = require('mongoose');
5
6
 
6
7
  const CreateChatThreadParams = new Archetype({
7
8
  userId: {
8
9
  $type: mongoose.Types.ObjectId
10
+ },
11
+ roles: {
12
+ $type: ['string'],
9
13
  }
10
14
  }).compile('CreateChatThreadParams');
11
15
 
@@ -13,6 +17,8 @@ module.exports = ({ studioConnection }) => async function createChatThread(param
13
17
  const { userId } = new CreateChatThreadParams(params);
14
18
  const ChatThread = studioConnection.model('__Studio_ChatThread');
15
19
 
20
+ await authorize('ChatThread.createChatThread', roles);
21
+
16
22
  const chatThread = await ChatThread.create({ userId });
17
23
 
18
24
  return { chatThread };
@@ -1,6 +1,7 @@
1
1
  'use strict';
2
2
 
3
3
  const Archetype = require('archetype');
4
+ const authorize = require('../../authorize');
4
5
  const mongoose = require('mongoose');
5
6
 
6
7
  const GetChatThreadParams = new Archetype({
@@ -9,14 +10,19 @@ const GetChatThreadParams = new Archetype({
9
10
  },
10
11
  userId: {
11
12
  $type: mongoose.Types.ObjectId
13
+ },
14
+ roles: {
15
+ $type: ['string']
12
16
  }
13
17
  }).compile('GetChatThreadParams');
14
18
 
15
19
  module.exports = ({ db, studioConnection }) => async function getChatThread(params) {
16
- const { chatThreadId, userId } = new GetChatThreadParams(params);
20
+ const { chatThreadId, userId, roles } = new GetChatThreadParams(params);
17
21
  const ChatThread = studioConnection.model('__Studio_ChatThread');
18
22
  const ChatMessage = studioConnection.model('__Studio_ChatMessage');
19
23
 
24
+ await authorize('ChatThread.getChatThread', roles);
25
+
20
26
  const chatThread = await ChatThread.findById(chatThreadId);
21
27
 
22
28
  if (!chatThread) {
@@ -1,19 +1,25 @@
1
1
  'use strict';
2
2
 
3
3
  const Archetype = require('archetype');
4
+ const authorize = require('../../authorize');
4
5
  const mongoose = require('mongoose');
5
6
 
6
7
  const ListChatThreadsParams = new Archetype({
7
8
  userId: {
8
9
  $type: mongoose.Types.ObjectId
10
+ },
11
+ roles: {
12
+ $type: ['string']
9
13
  }
10
14
  }).compile('ListChatThreadsParams');
11
15
 
12
16
  module.exports = ({ db, studioConnection }) => async function listChatThreads(params) {
13
- // Just validate the params object, but no actual parameters needed
14
- const { userId } = new ListChatThreadsParams(params);
17
+ // Validate the params object
18
+ const { userId, roles } = new ListChatThreadsParams(params);
15
19
  const ChatThread = studioConnection.model('__Studio_ChatThread');
16
20
 
21
+ await authorize('ChatThread.listChatThreads', roles);
22
+
17
23
  // Get all chat threads
18
24
  const chatThreads = await ChatThread.find(userId ? { userId } : {})
19
25
  .sort({ updatedAt: -1 }); // Sort by most recently updated
@@ -1,5 +1,7 @@
1
1
  'use strict';
2
+
2
3
  const Archetype = require('archetype');
4
+ const authorize = require('../../authorize');
3
5
 
4
6
  const CreateDashboardParams = new Archetype({
5
7
  title: {
@@ -9,14 +11,19 @@ const CreateDashboardParams = new Archetype({
9
11
  code: {
10
12
  $type: 'string',
11
13
  $required: true
14
+ },
15
+ roles: {
16
+ $type: ['string']
12
17
  }
13
18
  }).compile('CreateDashboardParams');
14
19
 
15
20
  module.exports = ({ db }) => async function createDashboard(params) {
16
- const { title, code } = new CreateDashboardParams(params);
21
+ const { title, code, roles } = new CreateDashboardParams(params);
17
22
  const Dashboard = db.model('__Studio_Dashboard');
18
23
 
24
+ await authorize('Dashboard.createDashboard', roles);
25
+
19
26
  const dashboard = await Dashboard.create({ title, code });
20
27
 
21
28
  return { dashboard };
22
- };
29
+ };
@@ -1,19 +1,24 @@
1
1
  'use strict';
2
2
 
3
3
  const Archetype = require('archetype');
4
- const vm = require('vm');
4
+ const authorize = require('../../authorize');
5
5
 
6
6
  const DeleteDashboardParams = new Archetype({
7
7
  dashboardId: {
8
8
  $type: 'string',
9
9
  $required: true
10
10
  },
11
+ roles: {
12
+ $type: ['string']
13
+ }
11
14
  }).compile('DeleteDashboardParams');
12
15
 
13
16
  module.exports = ({ db }) => async function deleteDashboard(params) {
14
- const { dashboardId } = new DeleteDashboardParams(params);
17
+ const { dashboardId, roles } = new DeleteDashboardParams(params);
15
18
  const Dashboard = db.model('__Studio_Dashboard');
16
19
 
20
+ await authorize('Dashboard.deleteDashboard', roles);
21
+
17
22
  const result = await Dashboard.deleteOne({ _id: dashboardId }).orFail();
18
23
  return { result };
19
- };
24
+ };
@@ -2,6 +2,7 @@
2
2
 
3
3
  const Archetype = require('archetype');
4
4
  const vm = require('vm');
5
+ const authorize = require('../../authorize');
5
6
 
6
7
  const GetDashboardParams = new Archetype({
7
8
  dashboardId: {
@@ -10,13 +11,18 @@ const GetDashboardParams = new Archetype({
10
11
  },
11
12
  evaluate: {
12
13
  $type: 'boolean'
14
+ },
15
+ roles: {
16
+ $type: ['string']
13
17
  }
14
18
  }).compile('GetDashboardParams');
15
19
 
16
20
  module.exports = ({ db }) => async function getDashboard(params) {
17
- const { dashboardId, evaluate } = new GetDashboardParams(params);
21
+ const { dashboardId, evaluate, roles } = new GetDashboardParams(params);
18
22
  const Dashboard = db.model('__Studio_Dashboard');
19
23
 
24
+ await authorize('Dashboard.getDashboard', roles);
25
+
20
26
  const dashboard = await Dashboard.findOne({ _id: dashboardId });
21
27
  if (evaluate) {
22
28
  let result = null;
@@ -25,9 +31,9 @@ module.exports = ({ db }) => async function getDashboard(params) {
25
31
  } catch (error) {
26
32
  return { dashboard, error: { message: error.message } };
27
33
  }
28
-
34
+
29
35
  return { dashboard, result };
30
36
  }
31
37
 
32
38
  return { dashboard };
33
- };
39
+ };
@@ -1,10 +1,13 @@
1
1
  'use strict';
2
2
 
3
+ const authorize = require('../../authorize');
3
4
 
4
- module.exports = ({ db }) => async function getDashboards() {
5
+ module.exports = ({ db }) => async function getDashboards(roles) {
5
6
  const Dashboard = db.model('__Studio_Dashboard');
6
7
 
8
+ await authorize('Dashboard.getDashboards', roles);
9
+
7
10
  const dashboards = await Dashboard.find();
8
11
 
9
12
  return { dashboards }
10
- };
13
+ };
@@ -1,6 +1,7 @@
1
1
  'use strict';
2
2
 
3
3
  const Archetype = require('archetype');
4
+ const authorize = require('../../authorize');
4
5
 
5
6
  const UpdateDashboardParams = new Archetype({
6
7
  dashboardId: {
@@ -16,16 +17,21 @@ const UpdateDashboardParams = new Archetype({
16
17
  },
17
18
  description: {
18
19
  $type: 'string'
20
+ },
21
+ roles: {
22
+ $type: ['string']
19
23
  }
20
24
  }).compile('UpdateDashboardParams');
21
25
 
22
26
  module.exports = ({ db }) => async function updateDashboard(params) {
23
- const { dashboardId, code, title, description } = new UpdateDashboardParams(params);
27
+ const { dashboardId, code, title, description, roles } = new UpdateDashboardParams(params);
24
28
 
25
29
  const Dashboard = db.models[`__Studio_Dashboard`];
26
30
 
31
+ await authorize('Dashboard.updateDashboard', roles);
32
+
27
33
  const updateObj = { code };
28
-
34
+
29
35
  if (title) {
30
36
  updateObj.title = title;
31
37
  }
@@ -36,7 +42,7 @@ module.exports = ({ db }) => async function updateDashboard(params) {
36
42
 
37
43
  const doc = await Dashboard.
38
44
  findByIdAndUpdate(dashboardId, updateObj, { sanitizeFilter: true, returnDocument: 'after', overwriteImmutable: true });
39
-
45
+
40
46
  let result = null;
41
47
  try {
42
48
  result = await doc.evaluate();
@@ -45,4 +51,4 @@ module.exports = ({ db }) => async function updateDashboard(params) {
45
51
  }
46
52
 
47
53
  return { doc, result };
48
- };
54
+ };
@@ -2,6 +2,7 @@
2
2
 
3
3
  const Archetype = require('archetype');
4
4
  const { EJSON } = require('bson');
5
+ const authorize = require('../../authorize');
5
6
 
6
7
  const CreateDocumentParams = new Archetype({
7
8
  model: {
@@ -20,16 +21,14 @@ const CreateDocumentParams = new Archetype({
20
21
  module.exports = ({ db }) => async function CreateDocument(params) {
21
22
  const { model, data, roles } = new CreateDocumentParams(params);
22
23
 
23
- if (roles && roles.includes('readonly')) {
24
- throw new Error('Not authorized');
25
- }
24
+ await authorize('Model.createDocument', roles);
26
25
 
27
26
  const Model = db.models[model];
28
27
  if (Model == null) {
29
28
  throw new Error(`Model ${model} not found`);
30
29
  }
31
-
30
+
32
31
  const doc = await Model.create(EJSON.deserialize(data));
33
-
32
+
34
33
  return { doc };
35
- };
34
+ };
@@ -1,6 +1,7 @@
1
1
  'use strict';
2
2
 
3
3
  const Archetype = require('archetype');
4
+ const authorize = require('../../authorize');
4
5
 
5
6
  const DeleteDocumentParams = new Archetype({
6
7
  model: {
@@ -21,9 +22,8 @@ module.exports = ({ db }) => async function DeleteDocument(params) {
21
22
 
22
23
  const Model = db.models[model];
23
24
 
24
- if (roles && roles.includes('readonly')) {
25
- throw new Error('Not authorized');
26
- }
25
+ await authorize('Model.deleteDocument', roles);
26
+
27
27
  if (Model == null) {
28
28
  throw new Error(`Model ${model} not found`);
29
29
  }
@@ -33,6 +33,6 @@ module.exports = ({ db }) => async function DeleteDocument(params) {
33
33
  setOptions({ sanitizeFilter: true }).
34
34
  orFail();
35
35
  console.log('what is doc', doc);
36
-
36
+
37
37
  return { doc };
38
- };
38
+ };
@@ -1,6 +1,7 @@
1
1
  'use strict';
2
2
 
3
3
  const Archetype = require('archetype');
4
+ const authorize = require('../../authorize');
4
5
 
5
6
  const DeleteDocumentsParams = new Archetype({
6
7
  model: {
@@ -21,9 +22,8 @@ module.exports = ({ db }) => async function DeleteDocuments(params) {
21
22
 
22
23
  const Model = db.models[model];
23
24
 
24
- if (roles && roles.includes('readonly')) {
25
- throw new Error('Not authorized');
26
- }
25
+ await authorize('Model.deleteDocuments', roles);
26
+
27
27
  if (Model == null) {
28
28
  throw new Error(`Model ${model} not found`);
29
29
  }
@@ -32,7 +32,7 @@ module.exports = ({ db }) => async function DeleteDocuments(params) {
32
32
  deleteMany({_id: { $in: documentIds }}).
33
33
  setOptions({ sanitizeFilter: true }).
34
34
  orFail();
35
-
36
-
35
+
36
+
37
37
  return { };
38
- };
38
+ };
@@ -0,0 +1,36 @@
1
+ 'use strict';
2
+
3
+ const Archetype = require('archetype');
4
+ const authorize = require('../../authorize');
5
+
6
+ const DropIndexParams = new Archetype({
7
+ model: {
8
+ $type: 'string',
9
+ $required: true
10
+ },
11
+ name: {
12
+ $type: 'string',
13
+ $required: true
14
+ },
15
+ roles: {
16
+ $type: ['string']
17
+ }
18
+ }).compile('DropIndexParams');
19
+
20
+ module.exports = ({ db }) => async function getIndexes(params) {
21
+ const { model, name, roles } = new DropIndexParams(params);
22
+
23
+ await authorize('Model.dropIndex', roles);
24
+
25
+ const Model = db.models[model];
26
+ if (Model == null) {
27
+ throw new Error(`Model ${model} not found`);
28
+ }
29
+
30
+ await Model.collection.dropIndex(name);
31
+
32
+ const mongoDBIndexes = await Model.listIndexes();
33
+ return {
34
+ mongoDBIndexes
35
+ };
36
+ };
@@ -3,6 +3,7 @@
3
3
  const Archetype = require('archetype');
4
4
  const mongoose = require('mongoose');
5
5
  const { stringify } = require('csv-stringify/sync');
6
+ const authorize = require('../../authorize');
6
7
 
7
8
  const GetDocumentsParams = new Archetype({
8
9
  model: {
@@ -21,13 +22,18 @@ const GetDocumentsParams = new Archetype({
21
22
  }
22
23
  return v;
23
24
  }
25
+ },
26
+ roles: {
27
+ $type: ['string']
24
28
  }
25
29
  }).compile('GetDocumentsParams');
26
30
 
27
31
  module.exports = ({ db }) => async function exportQueryResults(params, req, res) {
28
32
  params = new GetDocumentsParams(params);
29
33
  let { filter } = params;
30
- const { model, propertiesToInclude } = params;
34
+ const { model, propertiesToInclude, roles } = params;
35
+
36
+ await authorize('Model.exportQueryResults', roles);
31
37
 
32
38
  const Model = db.models[model];
33
39
  if (Model == null) {
@@ -2,6 +2,7 @@
2
2
 
3
3
  const Archetype = require('archetype');
4
4
  const removeSpecifiedPaths = require('../../helpers/removeSpecifiedPaths');
5
+ const authorize = require('../../authorize');
5
6
 
6
7
  const GetDocumentParams = new Archetype({
7
8
  model: {
@@ -11,11 +12,16 @@ const GetDocumentParams = new Archetype({
11
12
  documentId: {
12
13
  $type: 'string',
13
14
  $required: true
15
+ },
16
+ roles: {
17
+ $type: ['string']
14
18
  }
15
19
  }).compile('GetDocumentParams');
16
20
 
17
21
  module.exports = ({ db }) => async function getDocument(params) {
18
- const { model, documentId } = new GetDocumentParams(params);
22
+ const { model, documentId, roles } = new GetDocumentParams(params);
23
+
24
+ await authorize('Model.getDocument', roles);
19
25
 
20
26
  const Model = db.models[model];
21
27
  if (Model == null) {
@@ -35,6 +41,6 @@ module.exports = ({ db }) => async function getDocument(params) {
35
41
  };
36
42
  }
37
43
  removeSpecifiedPaths(schemaPaths, '.$*');
38
-
44
+
39
45
  return { doc: doc.toJSON({ virtuals: true, getters: false, transform: false }), schemaPaths };
40
- };
46
+ };
@@ -3,6 +3,7 @@
3
3
  const Archetype = require('archetype');
4
4
  const removeSpecifiedPaths = require('../../helpers/removeSpecifiedPaths');
5
5
  const { EJSON } = require('bson')
6
+ const authorize = require('../../authorize');
6
7
 
7
8
  const GetDocumentsParams = new Archetype({
8
9
  model: {
@@ -24,11 +25,17 @@ const GetDocumentsParams = new Archetype({
24
25
  },
25
26
  sort: {
26
27
  $type: Archetype.Any
28
+ },
29
+ roles: {
30
+ $type: ['string']
27
31
  }
28
32
  }).compile('GetDocumentsParams');
29
33
 
30
34
  module.exports = ({ db }) => async function getDocuments(params) {
31
35
  params = new GetDocumentsParams(params);
36
+ const { roles } = params;
37
+ await authorize('Model.getDocuments', roles);
38
+
32
39
  let { filter } = params;
33
40
  if (filter != null && Object.keys(filter).length > 0) {
34
41
  filter = EJSON.parse(filter);
@@ -1,18 +1,22 @@
1
1
  'use strict';
2
2
 
3
3
  const Archetype = require('archetype');
4
+ const authorize = require('../../authorize');
4
5
 
5
6
  const GetDocumentsParams = new Archetype({
6
7
  model: {
7
8
  $type: 'string',
8
9
  $required: true
9
10
  },
11
+ roles: {
12
+ $type: ['string']
13
+ }
10
14
  }).compile('GetDocumentsParams');
11
15
 
12
16
  module.exports = ({ db }) => async function getIndexes(params) {
13
- params = new GetDocumentsParams(params);
17
+ const { model, roles } = new GetDocumentsParams(params);
14
18
 
15
- const { model } = params;
19
+ await authorize('Model.getIndexes', roles);
16
20
 
17
21
  const Model = db.models[model];
18
22
  if (Model == null) {
@@ -3,6 +3,7 @@
3
3
  exports.createDocument = require('./createDocument')
4
4
  exports.deleteDocument = require('./deleteDocument');
5
5
  exports.deleteDocuments = require('./deleteDocuments');
6
+ exports.dropIndex = require('./dropIndex');
6
7
  exports.exportQueryResults = require('./exportQueryResults');
7
8
  exports.getDocument = require('./getDocument');
8
9
  exports.getDocuments = require('./getDocuments');
@@ -1,7 +1,19 @@
1
1
  'use strict';
2
2
 
3
- module.exports = ({ db }) => async function listModels() {
3
+ const Archetype = require('archetype');
4
+ const authorize = require('../../authorize');
5
+
6
+ const ListModelsParams = new Archetype({
7
+ roles: {
8
+ $type: ['string']
9
+ }
10
+ }).compile('ListModelsParams');
11
+
12
+ module.exports = ({ db }) => async function listModels(params) {
13
+ const { roles } = new ListModelsParams(params);
14
+ await authorize('Model.listModels', roles);
15
+
4
16
  return {
5
17
  models: Object.keys(db.models).filter(key => !key.startsWith('__Studio_')).sort()
6
18
  };
7
- };
19
+ };
@@ -1,6 +1,7 @@
1
1
  'use strict';
2
2
 
3
3
  const Archetype = require('archetype');
4
+ const authorize = require('../../authorize');
4
5
 
5
6
  const UpdateDocumentsParams = new Archetype({
6
7
  model: {
@@ -23,9 +24,8 @@ const UpdateDocumentsParams = new Archetype({
23
24
  module.exports = ({ db }) => async function updateDocument(params) {
24
25
  const { model, _id, update, roles } = new UpdateDocumentsParams(params);
25
26
 
26
- if (roles && roles.includes('readonly')) {
27
- throw new Error('Not authorized');
28
- }
27
+ await authorize('Document.updateDocument', roles);
28
+
29
29
  const Model = db.models[model];
30
30
  if (Model == null) {
31
31
  throw new Error(`Model ${model} not found`);
@@ -40,6 +40,6 @@ module.exports = ({ db }) => async function updateDocument(params) {
40
40
 
41
41
  const doc = await Model.
42
42
  findByIdAndUpdate(_id, processedUpdate, { sanitizeFilter: true, returnDocument: 'after', overwriteImmutable: true, runValidators: false });
43
-
43
+
44
44
  return { doc };
45
- };
45
+ };
@@ -1,7 +1,7 @@
1
1
  'use strict';
2
2
 
3
3
  const Archetype = require('archetype');
4
- const mongoose = require('mongoose');
4
+ const authorize = require('../../authorize');
5
5
 
6
6
  const UpdateDocumentsParams = new Archetype({
7
7
  model: {
@@ -24,9 +24,8 @@ const UpdateDocumentsParams = new Archetype({
24
24
  module.exports = ({ db }) => async function updateDocuments(params) {
25
25
  const { model, _id, update, roles } = new UpdateDocumentsParams(params);
26
26
 
27
- if (roles && roles.includes('readonly')) {
28
- throw new Error('Not authorized');
29
- }
27
+ await authorize('Document.updateDocuments', roles);
28
+
30
29
  const Model = db.models[model];
31
30
  if (Model == null) {
32
31
  throw new Error(`Model ${model} not found`);
@@ -41,6 +40,6 @@ module.exports = ({ db }) => async function updateDocuments(params) {
41
40
 
42
41
  const result = await Model.
43
42
  updateMany({ _id: { $in: _id } }, processedUpdate, { overwriteImmutable: true, runValidators: false });
44
-
43
+
45
44
  return { result };
46
- };
45
+ };