@openstax/ts-utils 1.5.2 → 1.5.4

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.
@@ -60,6 +60,12 @@ export declare type SearchUsersResponse = {
60
60
  } & JsonCompatibleStruct>;
61
61
  total_count: number;
62
62
  };
63
+ export declare type MappedUserInfo<T> = {
64
+ data: T;
65
+ fullName: string;
66
+ platformUserId?: string;
67
+ uuid: string;
68
+ };
63
69
  export declare const accountsGateway: <C extends string = "accounts">(initializer: Initializer<C>) => (configProvider: { [key in C]: {
64
70
  accountsBase: import("../../config").ConfigValueProvider<string>;
65
71
  accountsAuthToken: import("../../config").ConfigValueProvider<string>;
@@ -70,7 +76,7 @@ export declare const accountsGateway: <C extends string = "accounts">(initialize
70
76
  linkUser: (body: LinkUserPayload) => Promise<LinkUserResponse>;
71
77
  mapUserUuids: <T>(userUuidsMap: {
72
78
  [uuid: string]: T;
73
- }, logger: Logger, platformId?: string | undefined) => Promise<[string, T][]>;
79
+ }, logger: Logger, platformId?: string | undefined) => Promise<MappedUserInfo<T>[]>;
74
80
  searchUsers: (payload: SearchUsersPayload) => Promise<SearchUsersResponse>;
75
81
  };
76
82
  export declare type AccountsGateway = ReturnType<ReturnType<typeof accountsGateway>>;
@@ -91,16 +91,23 @@ const accountsGateway = (initializer) => (configProvider) => {
91
91
  });
92
92
  }
93
93
  items.forEach((user) => {
94
- const userId = platformId ? getPlatformUserId(user.external_ids, platformId) : user.full_name;
95
- if (!userId) {
96
- const missing = platformId ? 'external_id matching the given platformId' : 'full_name';
94
+ const platformUserId = platformId ? getPlatformUserId(user.external_ids, platformId) : undefined;
95
+ if (platformId && !platformUserId) {
97
96
  logger.logEvent(logger_1.Level.Warn, {
98
- message: `Accounts user has no ${missing}`,
97
+ message: 'Accounts user has no external_id matching the given platformId',
99
98
  accountsUuid: user.uuid,
100
99
  platformId,
101
100
  });
102
101
  }
103
- results.push([userId || 'N/A', userUuidsMap[user.uuid]]);
102
+ if (!user.full_name) {
103
+ logger.logEvent(logger_1.Level.Warn, {
104
+ message: 'Accounts user has no full_name',
105
+ accountsUuid: user.uuid,
106
+ });
107
+ }
108
+ results.push({
109
+ data: userUuidsMap[user.uuid], fullName: user.full_name, platformUserId, uuid: user.uuid,
110
+ });
104
111
  });
105
112
  }));
106
113
  return results;
@@ -1,4 +1,4 @@
1
- import { AccountsGateway } from '../accountsGateway';
1
+ import { AccountsGateway, MappedUserInfo } from '../accountsGateway';
2
2
  import { AuthProvider } from '../authProvider';
3
3
  import { Logger } from '../logger';
4
4
  import { ActivityState } from './attempt-utils';
@@ -13,22 +13,45 @@ export interface Grade {
13
13
  }
14
14
  export declare const getRegistrationAttemptInfo: (lrs: LrsGateway, registration: string, options?: {
15
15
  anyUser?: boolean | undefined;
16
+ currentPreference?: "latest" | "oldest" | undefined;
17
+ user?: string | undefined;
16
18
  } | undefined) => Promise<{
17
19
  [key: string]: ActivityState;
18
20
  }>;
21
+ export declare const getScoreGrade: (score: {
22
+ scaled?: number;
23
+ raw?: number;
24
+ min?: number;
25
+ max?: number;
26
+ }, completed: boolean, userId: string, maxScore?: number | undefined) => Grade;
27
+ export declare type Progress = {
28
+ scaled: number;
29
+ max?: number;
30
+ raw?: number;
31
+ };
32
+ export declare type GradeAndProgress = {
33
+ grade: Grade;
34
+ progress: Progress;
35
+ };
19
36
  export declare const getCurrentGrade: (services: {
20
37
  lrs: LrsGateway;
21
38
  ltiAuthProvider: AuthProvider;
22
39
  }, registration: string, options?: {
40
+ currentPreference?: "latest" | "oldest" | undefined;
41
+ incompleteAttemptCallback?: ((info: ActivityState) => Promise<GradeAndProgress>) | undefined;
23
42
  scoreMaximum?: number | undefined;
24
43
  userId?: string | undefined;
25
- } | undefined) => Promise<Grade | null>;
26
- export declare const getAllGradesForAssignment: (services: {
44
+ } | undefined) => Promise<GradeAndProgress | null>;
45
+ export declare type UserActivityInfo = MappedUserInfo<ActivityState>;
46
+ export declare const getAssignmentGrades: (services: {
27
47
  accountsGateway: AccountsGateway;
28
48
  lrs: LrsGateway;
29
49
  logger: Logger;
30
50
  }, registration: string, options?: {
31
- incompleteAttemptsCallback?: ((mappedInfo: [string, ActivityState][]) => Promise<Grade[]>) | undefined;
51
+ anyUser?: boolean | undefined;
52
+ currentPreference?: "latest" | "oldest" | undefined;
53
+ incompleteAttemptsCallback?: ((mappedInfo: UserActivityInfo[]) => Promise<GradeAndProgress[]>) | undefined;
32
54
  platformId?: string | undefined;
33
55
  scoreMaximum?: number | undefined;
34
- } | undefined) => Promise<Grade[]>;
56
+ user?: string | undefined;
57
+ } | undefined) => Promise<GradeAndProgress[]>;
@@ -3,12 +3,13 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.getAllGradesForAssignment = exports.getCurrentGrade = exports.getRegistrationAttemptInfo = void 0;
6
+ exports.getAssignmentGrades = exports.getCurrentGrade = exports.getScoreGrade = exports.getRegistrationAttemptInfo = void 0;
7
7
  const partition_1 = __importDefault(require("lodash/fp/partition"));
8
8
  const __1 = require("../..");
9
9
  const attempt_utils_1 = require("./attempt-utils");
10
10
  const getRegistrationAttemptInfo = async (lrs, registration, options) => {
11
- const allStatements = await lrs.getAllXapiStatements({ ...options, registration, ensureSync: true });
11
+ const { currentPreference, ...xapiOptions } = options !== null && options !== void 0 ? options : {};
12
+ const allStatements = await lrs.getAllXapiStatements({ ...xapiOptions, registration, ensureSync: true });
12
13
  // Partition statements for each user
13
14
  const statementsPerUser = {};
14
15
  allStatements.forEach((statement) => {
@@ -20,7 +21,7 @@ const getRegistrationAttemptInfo = async (lrs, registration, options) => {
20
21
  });
21
22
  const result = {};
22
23
  for (const [userUuid, userStatements] of Object.entries(statementsPerUser)) {
23
- result[userUuid] = (0, attempt_utils_1.resolveAttemptInfo)(userStatements);
24
+ result[userUuid] = (0, attempt_utils_1.resolveAttemptInfo)(userStatements, { currentPreference });
24
25
  }
25
26
  return result;
26
27
  };
@@ -28,10 +29,8 @@ exports.getRegistrationAttemptInfo = getRegistrationAttemptInfo;
28
29
  // generates a payload that can be sent to the LMS, documentation here:
29
30
  // lti: http://www.imsglobal.org/spec/lti-ags/v2p0#score-publish-service
30
31
  // ltijs: https://cvmcosta.me/ltijs/#/grading
31
- const getInfoGrade = (info, userId, maxScore) => {
32
- var _a;
33
- const completed = info.currentAttemptCompleted;
34
- const { raw, scaled, max } = ((_a = completed === null || completed === void 0 ? void 0 : completed.result) === null || _a === void 0 ? void 0 : _a.score) || {};
32
+ const getScoreGrade = (score, completed, userId, maxScore) => {
33
+ const { raw, scaled, max } = score;
35
34
  const scoreMaximum = maxScore !== null && maxScore !== void 0 ? maxScore : 100;
36
35
  const scoreGiven = raw && max
37
36
  ? scoreMaximum / max * raw
@@ -46,25 +45,45 @@ const getInfoGrade = (info, userId, maxScore) => {
46
45
  scoreGiven: (0, __1.roundToPrecision)(scoreGiven, -2),
47
46
  };
48
47
  };
48
+ exports.getScoreGrade = getScoreGrade;
49
+ // These methods assigns 0's to incomplete activities
50
+ const getCompletedActivityStateGradeAndProgress = (state, userId, maxScore) => {
51
+ var _a, _b;
52
+ return ({
53
+ grade: (0, exports.getScoreGrade)(((_b = (_a = state.currentAttemptCompleted) === null || _a === void 0 ? void 0 : _a.result) === null || _b === void 0 ? void 0 : _b.score) || {}, !!state.currentAttemptCompleted, userId, maxScore),
54
+ progress: {
55
+ scaled: state.currentAttemptCompleted ? 1 : 0,
56
+ },
57
+ });
58
+ };
59
+ const getCompletedUserInfosGradeAndProgress = (infos, scoreMaximum) => infos.map(({ data, fullName, platformUserId }) => getCompletedActivityStateGradeAndProgress(data, platformUserId !== null && platformUserId !== void 0 ? platformUserId : fullName, scoreMaximum));
49
60
  const getCurrentGrade = async (services, registration, options) => {
50
61
  var _a;
51
62
  const user = await services.ltiAuthProvider.getUser();
52
63
  if (!user) {
53
64
  return null;
54
65
  }
55
- const infoPerUser = await (0, exports.getRegistrationAttemptInfo)(services.lrs, registration);
66
+ const userId = (_a = options === null || options === void 0 ? void 0 : options.userId) !== null && _a !== void 0 ? _a : user.uuid;
67
+ const { currentPreference, incompleteAttemptCallback, scoreMaximum } = options !== null && options !== void 0 ? options : {};
68
+ const infoPerUser = await (0, exports.getRegistrationAttemptInfo)(services.lrs, registration, { currentPreference });
56
69
  const userInfo = infoPerUser[user.uuid];
57
- return getInfoGrade(userInfo !== null && userInfo !== void 0 ? userInfo : (0, attempt_utils_1.resolveAttemptInfo)([]), (_a = options === null || options === void 0 ? void 0 : options.userId) !== null && _a !== void 0 ? _a : user.uuid, options === null || options === void 0 ? void 0 : options.scoreMaximum);
70
+ if (!userInfo) {
71
+ return getCompletedActivityStateGradeAndProgress((0, attempt_utils_1.resolveAttemptInfo)([]), userId, scoreMaximum);
72
+ }
73
+ if (userInfo.currentAttemptCompleted || !incompleteAttemptCallback) {
74
+ return getCompletedActivityStateGradeAndProgress(userInfo, userId, scoreMaximum);
75
+ }
76
+ return incompleteAttemptCallback(userInfo);
58
77
  };
59
78
  exports.getCurrentGrade = getCurrentGrade;
60
- const getAllGradesForAssignment = async (services, registration, options) => {
61
- const infoPerUserUuid = await (0, exports.getRegistrationAttemptInfo)(services.lrs, registration, { anyUser: true });
62
- const mappedInfo = await services.accountsGateway.mapUserUuids(infoPerUserUuid, services.logger, options === null || options === void 0 ? void 0 : options.platformId);
63
- const gradeCompletedAttemptsOnly = (results) => results.map(([userId, userInfo]) => getInfoGrade(userInfo, userId, options === null || options === void 0 ? void 0 : options.scoreMaximum));
64
- if (!(options === null || options === void 0 ? void 0 : options.incompleteAttemptsCallback)) {
65
- return gradeCompletedAttemptsOnly(mappedInfo);
79
+ const getAssignmentGrades = async (services, registration, options) => {
80
+ const { anyUser, currentPreference, incompleteAttemptsCallback, platformId, scoreMaximum, user } = options !== null && options !== void 0 ? options : {};
81
+ const infoPerUserUuid = await (0, exports.getRegistrationAttemptInfo)(services.lrs, registration, { anyUser, currentPreference, user });
82
+ const mappedInfo = await services.accountsGateway.mapUserUuids(infoPerUserUuid, services.logger, platformId);
83
+ if (!incompleteAttemptsCallback) {
84
+ return getCompletedUserInfosGradeAndProgress(mappedInfo, scoreMaximum);
66
85
  }
67
- const [incompleteInfo, completedInfo] = (0, partition_1.default)((info) => info[1].currentAttemptCompleted === undefined)(mappedInfo);
68
- return gradeCompletedAttemptsOnly(completedInfo).concat(await options.incompleteAttemptsCallback(incompleteInfo));
86
+ const [incompleteInfo, completedInfo] = (0, partition_1.default)((info) => info.data.currentAttemptCompleted === undefined)(mappedInfo);
87
+ return getCompletedUserInfosGradeAndProgress(completedInfo, scoreMaximum).concat(await incompleteAttemptsCallback(incompleteInfo));
69
88
  };
70
- exports.getAllGradesForAssignment = getAllGradesForAssignment;
89
+ exports.getAssignmentGrades = getAssignmentGrades;