dce-expresskit 4.1.0 → 4.2.0

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.
@@ -10,7 +10,6 @@ import {
10
10
  LogFunction,
11
11
  LOG_ROUTE_PATH,
12
12
  LOG_REVIEW_STATUS_ROUTE,
13
- Log,
14
13
  LOG_REVIEW_GET_LOGS_ROUTE,
15
14
  ErrorWithCode,
16
15
  } from 'dce-reactkit';
@@ -21,37 +20,12 @@ import getLogReviewerLogs from './getLogReviewerLogs';
21
20
 
22
21
  // Import shared types
23
22
  import ExpressKitErrorCode from '../types/ExpressKitErrorCode';
24
- import CrossServerCredential from '../types/CrossServerCredential';
25
23
 
26
- // Stored copy of dce-mango log collection
27
- let _logCollection: Collection<Log>;
28
-
29
- // Stored copy of dce-mango cross-server credential collection
30
- let _crossServerCredentialCollection: Collection<CrossServerCredential>;
31
-
32
- /*------------------------------------------------------------------------*/
33
- /* Helpers */
34
- /*------------------------------------------------------------------------*/
35
-
36
- /**
37
- * Get log collection
38
- * @author Gabe Abrams
39
- * @returns log collection if one was included during launch or null if we don't
40
- * have a log collection (yet)
41
- */
42
- export const internalGetLogCollection = () => {
43
- return _logCollection ?? null;
44
- };
45
-
46
- /**
47
- * Get cross-server credential collection
48
- * @author Gabe Abrams
49
- * @return cross-server credential collection if one was included during launch or null
50
- * if we don't have a cross-server credential collection (yet)
51
- */
52
- export const internalGetCrossServerCredentialCollection = () => {
53
- return _crossServerCredentialCollection ?? null;
54
- };
24
+ // Import shared helpers
25
+ import {
26
+ internalGetLogCollection,
27
+ internalGetLogReviewerAdminCollection,
28
+ } from './initExpressKitCollections';
55
29
 
56
30
  /*------------------------------------------------------------------------*/
57
31
  /* Main */
@@ -63,31 +37,12 @@ export const internalGetCrossServerCredentialCollection = () => {
63
37
  * @param opts object containing all arguments
64
38
  * @param opts.app express app from inside of the postprocessor function that
65
39
  * we will add routes to
66
- * @param opts.getLaunchInfo CACCL LTI's get launch info function
67
- * @param [opts.logCollection] mongo collection from dce-mango to use for
68
- * storing logs. If none is included, logs are written to the console
69
- * @param [opts.logReviewAdmins=all] info on which admins can review
70
- * logs from the client. If not included, all Canvas admins are allowed to
71
- * review logs. If null, no Canvas admins are allowed to review logs.
72
- * If an array of Canvas userIds (numbers), only Canvas admins with those
73
- * userIds are allowed to review logs. If a dce-mango collection, only
74
- * Canvas admins with entries in that collection ({ userId, ...}) are allowed
75
- * to review logs
76
- * @param [opts.crossServerCredentialCollection] mongo collection from dce-mango to use for
77
- * storing cross-server credentials. If none is included, cross-server credentials
78
- * are not supported
79
40
  */
80
41
  const initServer = (
81
42
  opts: {
82
43
  app: express.Application,
83
- logReviewAdmins?: (number[] | Collection<any>),
84
- logCollection?: Collection<Log>,
85
- crossServerCredentialCollection?: Collection<CrossServerCredential>,
86
44
  },
87
45
  ) => {
88
- _logCollection = opts.logCollection;
89
- _crossServerCredentialCollection = opts.crossServerCredentialCollection;
90
-
91
46
  /*----------------------------------------*/
92
47
  /* Logging */
93
48
  /*----------------------------------------*/
@@ -190,22 +145,15 @@ const initServer = (
190
145
  return false;
191
146
  }
192
147
 
193
- // If all admins are allowed, we're done
194
- if (!opts.logReviewAdmins) {
195
- return true;
196
- }
148
+ /* ------- Look Up Credential ------- */
197
149
 
198
- // Do a dynamic check
199
- try {
200
- // Array of userIds
201
- if (Array.isArray(opts.logReviewAdmins)) {
202
- return opts.logReviewAdmins.some((allowedId) => {
203
- return (userId === allowedId);
204
- });
205
- }
150
+ // Get the log reviewer admin collection
151
+ const logReviewerAdminCollection = await internalGetLogReviewerAdminCollection();
206
152
 
207
- // Must be a collection
208
- const matches = await opts.logReviewAdmins.find({ userId });
153
+ // Check if the user is in the log reviewer admin collection
154
+ try {
155
+ // Must be in the collection
156
+ const matches = await logReviewerAdminCollection.find({ id: userId });
209
157
 
210
158
  // Make sure at least one entry matches
211
159
  return matches.length > 0;
@@ -224,8 +172,16 @@ const initServer = (
224
172
  LOG_REVIEW_STATUS_ROUTE,
225
173
  genRouteHandler({
226
174
  handler: async ({ params }) => {
227
- const { userId, isAdmin } = params;
175
+ // Destructure params
176
+ const {
177
+ userId,
178
+ isAdmin,
179
+ } = params;
180
+
181
+ // Check if user can review logs
228
182
  const canReview = await canReviewLogs(userId, isAdmin);
183
+
184
+ // Return result
229
185
  return canReview;
230
186
  },
231
187
  }),
@@ -233,7 +189,8 @@ const initServer = (
233
189
 
234
190
  /**
235
191
  * Get filtered logs based on provided filters
236
- * @author Gabe Abrams, Yuen Ler Chow
192
+ * @author Gabe Abrams
193
+ * @author Yuen Ler Chow
237
194
  * @param pageNumber the page number to get
238
195
  * @param filters the filters to apply to the logs
239
196
  * @returns {Log[]} list of logs that match the filters
@@ -265,12 +222,15 @@ const initServer = (
265
222
  );
266
223
  }
267
224
 
225
+ // Get log collection
226
+ const logCollection = await internalGetLogCollection();
227
+
268
228
  // Get logs
269
229
  const response = await getLogReviewerLogs({
270
230
  pageNumber,
271
231
  filters,
272
232
  countDocuments,
273
- logCollection: _logCollection,
233
+ logCollection,
274
234
  });
275
235
 
276
236
  // Return response
package/src/index.ts CHANGED
@@ -48,14 +48,13 @@ import {
48
48
  } from 'dce-reactkit';
49
49
 
50
50
  // Import helpers
51
- import initCrossServerCredentialCollection from './helpers/initCrossServerCredentialCollection';
52
- import initLogCollection from './helpers/initLogCollection';
53
51
  import initServer from './helpers/initServer';
54
52
  import genRouteHandler from './helpers/genRouteHandler';
55
53
  import handleError from './helpers/handleError';
56
54
  import handleSuccess from './helpers/handleSuccess';
57
55
  import addDBEditorEndpoints from './helpers/addDBEditorEndpoints';
58
56
  import visitEndpointOnAnotherServer from './helpers/visitEndpointOnAnotherServer';
57
+ import initExpressKitCollections from './helpers/initExpressKitCollections';
59
58
 
60
59
  // Import types
61
60
  import CrossServerCredential from './types/CrossServerCredential';
@@ -106,8 +105,7 @@ export {
106
105
  genRouteHandler,
107
106
  handleError,
108
107
  handleSuccess,
109
- initLogCollection,
110
- initCrossServerCredentialCollection,
108
+ initExpressKitCollections,
111
109
  addDBEditorEndpoints,
112
110
  visitEndpointOnAnotherServer,
113
111
  // Types
@@ -32,6 +32,7 @@ enum ExpressKitErrorCode {
32
32
  NoCryptoLib = 'DEK32',
33
33
  InvalidCrossServerCredentialsFormat = 'DEK33',
34
34
  UnknownCrossServerError = 'DEK34',
35
+ NotSelectAdmin = 'DEK35',
35
36
  }
36
37
 
37
38
  export default ExpressKitErrorCode;
@@ -0,0 +1,14 @@
1
+ /**
2
+ * Type describing a LogReviewerAdmin user
3
+ * @author Yuen Ler Chow
4
+ */
5
+ type LogReviewerAdmin = {
6
+ // User Canvas Id
7
+ id: number,
8
+ // User first name
9
+ userFirstName: string,
10
+ // User last name
11
+ userLastName: string,
12
+ };
13
+
14
+ export default LogReviewerAdmin;
@@ -0,0 +1,14 @@
1
+ /**
2
+ * Type describing a SelectAdmin user (subset of Admin)
3
+ * @author Gardenia Liu
4
+ */
5
+ type SelectAdmin = {
6
+ // User Canvas Id
7
+ id: number,
8
+ // User first name
9
+ userFirstName: string,
10
+ // User last name
11
+ userLastName: string,
12
+ };
13
+
14
+ export default SelectAdmin;
@@ -1,19 +0,0 @@
1
- // Import dce-mango
2
- import { Collection as MangoCollection } from 'dce-mango';
3
-
4
- /**
5
- * Initialize a cross-server credential collection given the dce-mango Collection class
6
- * @author Gabe Abrams
7
- * @param Collection the Collection class from dce-mango
8
- * @returns initialized logCollection
9
- */
10
- const initCrossServerCredentialCollection = (Collection: typeof MangoCollection) => {
11
- return new Collection(
12
- 'CrossServerCredential',
13
- {
14
- uniqueIndexKey: 'key',
15
- },
16
- );
17
- };
18
-
19
- export default initCrossServerCredentialCollection;
@@ -1,30 +0,0 @@
1
- // Import dce-mango
2
- import { Collection as MangoCollection } from 'dce-mango';
3
-
4
- /**
5
- * Initialize a log collection given the dce-mango Collection class
6
- * @author Gabe Abrams
7
- * @param Collection the Collection class from dce-mango
8
- * @returns initialized logCollection
9
- */
10
- const initLogCollection = (Collection: typeof MangoCollection) => {
11
- return new Collection(
12
- 'Log',
13
- {
14
- uniqueIndexKey: 'id',
15
- indexKeys: [
16
- 'courseId',
17
- 'context',
18
- 'subcontext',
19
- 'tags',
20
- 'year',
21
- 'month',
22
- 'day',
23
- 'hour',
24
- 'type',
25
- ],
26
- },
27
- );
28
- };
29
-
30
- export default initLogCollection;