@openstax/ts-utils 1.5.3 → 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>>;
@@ -85,16 +85,23 @@ export const accountsGateway = (initializer) => (configProvider) => {
85
85
  });
86
86
  }
87
87
  items.forEach((user) => {
88
- const userId = platformId ? getPlatformUserId(user.external_ids, platformId) : user.full_name;
89
- if (!userId) {
90
- const missing = platformId ? 'external_id matching the given platformId' : 'full_name';
88
+ const platformUserId = platformId ? getPlatformUserId(user.external_ids, platformId) : undefined;
89
+ if (platformId && !platformUserId) {
91
90
  logger.logEvent(Level.Warn, {
92
- message: `Accounts user has no ${missing}`,
91
+ message: 'Accounts user has no external_id matching the given platformId',
93
92
  accountsUuid: user.uuid,
94
93
  platformId,
95
94
  });
96
95
  }
97
- results.push([userId || 'N/A', userUuidsMap[user.uuid]]);
96
+ if (!user.full_name) {
97
+ logger.logEvent(Level.Warn, {
98
+ message: 'Accounts user has no full_name',
99
+ accountsUuid: user.uuid,
100
+ });
101
+ }
102
+ results.push({
103
+ data: userUuidsMap[user.uuid], fullName: user.full_name, platformUserId, uuid: user.uuid,
104
+ });
98
105
  });
99
106
  }));
100
107
  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,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[]>;
@@ -2,7 +2,8 @@ import partition from 'lodash/fp/partition';
2
2
  import { roundToPrecision } from '../..';
3
3
  import { resolveAttemptInfo } from './attempt-utils';
4
4
  export const getRegistrationAttemptInfo = async (lrs, registration, options) => {
5
- const allStatements = await lrs.getAllXapiStatements({ ...options, registration, ensureSync: true });
5
+ const { currentPreference, ...xapiOptions } = options !== null && options !== void 0 ? options : {};
6
+ const allStatements = await lrs.getAllXapiStatements({ ...xapiOptions, registration, ensureSync: true });
6
7
  // Partition statements for each user
7
8
  const statementsPerUser = {};
8
9
  allStatements.forEach((statement) => {
@@ -14,7 +15,7 @@ export const getRegistrationAttemptInfo = async (lrs, registration, options) =>
14
15
  });
15
16
  const result = {};
16
17
  for (const [userUuid, userStatements] of Object.entries(statementsPerUser)) {
17
- result[userUuid] = resolveAttemptInfo(userStatements, { currentPreference: 'oldest' });
18
+ result[userUuid] = resolveAttemptInfo(userStatements, { currentPreference });
18
19
  }
19
20
  return result;
20
21
  };
@@ -37,10 +38,17 @@ export const getScoreGrade = (score, completed, userId, maxScore) => {
37
38
  scoreGiven: roundToPrecision(scoreGiven, -2),
38
39
  };
39
40
  };
40
- const getInfoGrade = (info, userId, maxScore) => {
41
+ // These methods assigns 0's to incomplete activities
42
+ const getCompletedActivityStateGradeAndProgress = (state, userId, maxScore) => {
41
43
  var _a, _b;
42
- return 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);
44
+ return ({
45
+ grade: 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),
46
+ progress: {
47
+ scaled: state.currentAttemptCompleted ? 1 : 0,
48
+ },
49
+ });
43
50
  };
51
+ const getCompletedUserInfosGradeAndProgress = (infos, scoreMaximum) => infos.map(({ data, fullName, platformUserId }) => getCompletedActivityStateGradeAndProgress(data, platformUserId !== null && platformUserId !== void 0 ? platformUserId : fullName, scoreMaximum));
44
52
  export const getCurrentGrade = async (services, registration, options) => {
45
53
  var _a;
46
54
  const user = await services.ltiAuthProvider.getUser();
@@ -48,24 +56,24 @@ export const getCurrentGrade = async (services, registration, options) => {
48
56
  return null;
49
57
  }
50
58
  const userId = (_a = options === null || options === void 0 ? void 0 : options.userId) !== null && _a !== void 0 ? _a : user.uuid;
51
- const scoreMaximum = options === null || options === void 0 ? void 0 : options.scoreMaximum;
52
- const infoPerUser = await getRegistrationAttemptInfo(services.lrs, registration);
59
+ const { currentPreference, incompleteAttemptCallback, scoreMaximum } = options !== null && options !== void 0 ? options : {};
60
+ const infoPerUser = await getRegistrationAttemptInfo(services.lrs, registration, { currentPreference });
53
61
  const userInfo = infoPerUser[user.uuid];
54
62
  if (!userInfo) {
55
- return getInfoGrade(resolveAttemptInfo([]), userId, scoreMaximum);
63
+ return getCompletedActivityStateGradeAndProgress(resolveAttemptInfo([]), userId, scoreMaximum);
56
64
  }
57
- if (userInfo.currentAttemptCompleted || !(options === null || options === void 0 ? void 0 : options.incompleteAttemptCallback)) {
58
- return getInfoGrade(userInfo, userId, scoreMaximum);
65
+ if (userInfo.currentAttemptCompleted || !incompleteAttemptCallback) {
66
+ return getCompletedActivityStateGradeAndProgress(userInfo, userId, scoreMaximum);
59
67
  }
60
- return options.incompleteAttemptCallback(userInfo);
68
+ return incompleteAttemptCallback(userInfo);
61
69
  };
62
70
  export const getAssignmentGrades = async (services, registration, options) => {
63
- const infoPerUserUuid = await getRegistrationAttemptInfo(services.lrs, registration, { anyUser: options === null || options === void 0 ? void 0 : options.anyUser, user: options === null || options === void 0 ? void 0 : options.user });
64
- const mappedInfo = await services.accountsGateway.mapUserUuids(infoPerUserUuid, services.logger, options === null || options === void 0 ? void 0 : options.platformId);
65
- const gradeCompletedAttemptsOnly = (results) => results.map(([userId, userInfo]) => getInfoGrade(userInfo, userId, options === null || options === void 0 ? void 0 : options.scoreMaximum));
66
- if (!(options === null || options === void 0 ? void 0 : options.incompleteAttemptsCallback)) {
67
- return gradeCompletedAttemptsOnly(mappedInfo);
71
+ const { anyUser, currentPreference, incompleteAttemptsCallback, platformId, scoreMaximum, user } = options !== null && options !== void 0 ? options : {};
72
+ const infoPerUserUuid = await getRegistrationAttemptInfo(services.lrs, registration, { anyUser, currentPreference, user });
73
+ const mappedInfo = await services.accountsGateway.mapUserUuids(infoPerUserUuid, services.logger, platformId);
74
+ if (!incompleteAttemptsCallback) {
75
+ return getCompletedUserInfosGradeAndProgress(mappedInfo, scoreMaximum);
68
76
  }
69
- const [incompleteInfo, completedInfo] = partition((info) => info[1].currentAttemptCompleted === undefined)(mappedInfo);
70
- return gradeCompletedAttemptsOnly(completedInfo).concat(await options.incompleteAttemptsCallback(incompleteInfo));
77
+ const [incompleteInfo, completedInfo] = partition((info) => info.data.currentAttemptCompleted === undefined)(mappedInfo);
78
+ return getCompletedUserInfosGradeAndProgress(completedInfo, scoreMaximum).concat(await incompleteAttemptsCallback(incompleteInfo));
71
79
  };