@openstax/ts-utils 1.5.3 → 1.5.5

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;
@@ -18,6 +18,11 @@ export interface ApiUser extends TokenUser {
18
18
  is_verified: boolean;
19
19
  is_guessed_preferred: boolean;
20
20
  }>;
21
+ applications: Array<{
22
+ id: number;
23
+ name: string;
24
+ roles: string[];
25
+ }>;
21
26
  external_ids: string[];
22
27
  is_not_gdpr_location: boolean;
23
28
  signed_contract_names: string[];
@@ -24,8 +24,10 @@ export declare const resolveAttemptInfo: (statements: XapiStatement[], options?:
24
24
  currentPreference?: "latest" | "oldest" | undefined;
25
25
  } | undefined) => ActivityState;
26
26
  export declare const loadStatementsForActivityAndFirstChildren: (gateway: LrsGateway, activityIRI: string, options?: {
27
+ anyUser?: boolean | undefined;
27
28
  attempt?: string | undefined;
28
29
  ensureSync?: boolean | undefined;
30
+ user?: string | undefined;
29
31
  } | undefined) => Promise<XapiStatement[]>;
30
32
  export declare const loadActivityAttemptInfo: (gateway: LrsGateway, activityIRI: string, options?: {
31
33
  currentAttempt?: string | undefined;
@@ -117,7 +117,7 @@ ${await response.text()}`);
117
117
  throw new Error(`xAPI consistent through ${consistentThrough}; not in sync with current date ${date}.`);
118
118
  }
119
119
  return formatGetXapiStatementsResponse(responsePromise);
120
- });
120
+ }, { retries: 4 });
121
121
  }
122
122
  else {
123
123
  return formatGetXapiStatementsResponse(fetchXapiStatements(fetchParams));
@@ -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,6 +13,7 @@ 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;
16
17
  user?: string | undefined;
17
18
  } | undefined) => Promise<{
18
19
  [key: string]: ActivityState;
@@ -23,22 +24,34 @@ export declare const getScoreGrade: (score: {
23
24
  min?: number;
24
25
  max?: number;
25
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
+ };
26
36
  export declare const getCurrentGrade: (services: {
27
37
  lrs: LrsGateway;
28
38
  ltiAuthProvider: AuthProvider;
29
39
  }, registration: string, options?: {
30
- incompleteAttemptCallback?: ((info: ActivityState) => Promise<Grade>) | undefined;
40
+ currentPreference?: "latest" | "oldest" | undefined;
41
+ incompleteAttemptCallback?: ((info: ActivityState) => Promise<GradeAndProgress>) | undefined;
31
42
  scoreMaximum?: number | undefined;
32
43
  userId?: string | undefined;
33
- } | undefined) => Promise<Grade | null>;
44
+ } | undefined) => Promise<GradeAndProgress | null>;
45
+ export declare type UserActivityInfo = MappedUserInfo<ActivityState>;
34
46
  export declare const getAssignmentGrades: (services: {
35
47
  accountsGateway: AccountsGateway;
36
48
  lrs: LrsGateway;
37
49
  logger: Logger;
38
50
  }, registration: string, options?: {
39
51
  anyUser?: boolean | undefined;
40
- incompleteAttemptsCallback?: ((mappedInfo: [string, ActivityState][]) => Promise<Grade[]>) | undefined;
52
+ currentPreference?: "latest" | "oldest" | undefined;
53
+ incompleteAttemptsCallback?: ((mappedInfo: UserActivityInfo[]) => Promise<GradeAndProgress[]>) | undefined;
41
54
  platformId?: string | undefined;
42
55
  scoreMaximum?: number | undefined;
43
56
  user?: string | undefined;
44
- } | undefined) => Promise<Grade[]>;
57
+ } | undefined) => Promise<GradeAndProgress[]>;
@@ -8,7 +8,8 @@ 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, { currentPreference: 'oldest' });
24
+ result[userUuid] = (0, attempt_utils_1.resolveAttemptInfo)(userStatements, { currentPreference });
24
25
  }
25
26
  return result;
26
27
  };
@@ -45,10 +46,17 @@ const getScoreGrade = (score, completed, userId, maxScore) => {
45
46
  };
46
47
  };
47
48
  exports.getScoreGrade = getScoreGrade;
48
- const getInfoGrade = (info, userId, maxScore) => {
49
+ // These methods assigns 0's to incomplete activities
50
+ const getCompletedActivityStateGradeAndProgress = (state, userId, maxScore) => {
49
51
  var _a, _b;
50
- return (0, exports.getScoreGrade)(((_b = (_a = info.currentAttemptCompleted) === null || _a === void 0 ? void 0 : _a.result) === null || _b === void 0 ? void 0 : _b.score) || {}, !!info.currentAttemptCompleted, userId, maxScore);
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
+ });
51
58
  };
59
+ const getCompletedUserInfosGradeAndProgress = (infos, scoreMaximum) => infos.map(({ data, fullName, platformUserId }) => getCompletedActivityStateGradeAndProgress(data, platformUserId !== null && platformUserId !== void 0 ? platformUserId : fullName, scoreMaximum));
52
60
  const getCurrentGrade = async (services, registration, options) => {
53
61
  var _a;
54
62
  const user = await services.ltiAuthProvider.getUser();
@@ -56,26 +64,26 @@ const getCurrentGrade = async (services, registration, options) => {
56
64
  return null;
57
65
  }
58
66
  const userId = (_a = options === null || options === void 0 ? void 0 : options.userId) !== null && _a !== void 0 ? _a : user.uuid;
59
- const scoreMaximum = options === null || options === void 0 ? void 0 : options.scoreMaximum;
60
- const infoPerUser = await (0, exports.getRegistrationAttemptInfo)(services.lrs, registration);
67
+ const { currentPreference, incompleteAttemptCallback, scoreMaximum } = options !== null && options !== void 0 ? options : {};
68
+ const infoPerUser = await (0, exports.getRegistrationAttemptInfo)(services.lrs, registration, { currentPreference });
61
69
  const userInfo = infoPerUser[user.uuid];
62
70
  if (!userInfo) {
63
- return getInfoGrade((0, attempt_utils_1.resolveAttemptInfo)([]), userId, scoreMaximum);
71
+ return getCompletedActivityStateGradeAndProgress((0, attempt_utils_1.resolveAttemptInfo)([]), userId, scoreMaximum);
64
72
  }
65
- if (userInfo.currentAttemptCompleted || !(options === null || options === void 0 ? void 0 : options.incompleteAttemptCallback)) {
66
- return getInfoGrade(userInfo, userId, scoreMaximum);
73
+ if (userInfo.currentAttemptCompleted || !incompleteAttemptCallback) {
74
+ return getCompletedActivityStateGradeAndProgress(userInfo, userId, scoreMaximum);
67
75
  }
68
- return options.incompleteAttemptCallback(userInfo);
76
+ return incompleteAttemptCallback(userInfo);
69
77
  };
70
78
  exports.getCurrentGrade = getCurrentGrade;
71
79
  const getAssignmentGrades = async (services, registration, options) => {
72
- const infoPerUserUuid = await (0, exports.getRegistrationAttemptInfo)(services.lrs, registration, { anyUser: options === null || options === void 0 ? void 0 : options.anyUser, user: options === null || options === void 0 ? void 0 : options.user });
73
- const mappedInfo = await services.accountsGateway.mapUserUuids(infoPerUserUuid, services.logger, options === null || options === void 0 ? void 0 : options.platformId);
74
- const gradeCompletedAttemptsOnly = (results) => results.map(([userId, userInfo]) => getInfoGrade(userInfo, userId, options === null || options === void 0 ? void 0 : options.scoreMaximum));
75
- if (!(options === null || options === void 0 ? void 0 : options.incompleteAttemptsCallback)) {
76
- return gradeCompletedAttemptsOnly(mappedInfo);
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);
77
85
  }
78
- const [incompleteInfo, completedInfo] = (0, partition_1.default)((info) => info[1].currentAttemptCompleted === undefined)(mappedInfo);
79
- 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));
80
88
  };
81
89
  exports.getAssignmentGrades = getAssignmentGrades;