@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>>;
@@ -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,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[]>;
@@ -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,17 +15,15 @@ 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);
18
+ result[userUuid] = resolveAttemptInfo(userStatements, { currentPreference });
18
19
  }
19
20
  return result;
20
21
  };
21
22
  // generates a payload that can be sent to the LMS, documentation here:
22
23
  // lti: http://www.imsglobal.org/spec/lti-ags/v2p0#score-publish-service
23
24
  // ltijs: https://cvmcosta.me/ltijs/#/grading
24
- const getInfoGrade = (info, userId, maxScore) => {
25
- var _a;
26
- const completed = info.currentAttemptCompleted;
27
- const { raw, scaled, max } = ((_a = completed === null || completed === void 0 ? void 0 : completed.result) === null || _a === void 0 ? void 0 : _a.score) || {};
25
+ export const getScoreGrade = (score, completed, userId, maxScore) => {
26
+ const { raw, scaled, max } = score;
28
27
  const scoreMaximum = maxScore !== null && maxScore !== void 0 ? maxScore : 100;
29
28
  const scoreGiven = raw && max
30
29
  ? scoreMaximum / max * raw
@@ -39,23 +38,42 @@ const getInfoGrade = (info, userId, maxScore) => {
39
38
  scoreGiven: roundToPrecision(scoreGiven, -2),
40
39
  };
41
40
  };
41
+ // These methods assigns 0's to incomplete activities
42
+ const getCompletedActivityStateGradeAndProgress = (state, userId, maxScore) => {
43
+ var _a, _b;
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
+ });
50
+ };
51
+ const getCompletedUserInfosGradeAndProgress = (infos, scoreMaximum) => infos.map(({ data, fullName, platformUserId }) => getCompletedActivityStateGradeAndProgress(data, platformUserId !== null && platformUserId !== void 0 ? platformUserId : fullName, scoreMaximum));
42
52
  export const getCurrentGrade = async (services, registration, options) => {
43
53
  var _a;
44
54
  const user = await services.ltiAuthProvider.getUser();
45
55
  if (!user) {
46
56
  return null;
47
57
  }
48
- const infoPerUser = await getRegistrationAttemptInfo(services.lrs, registration);
58
+ const userId = (_a = options === null || options === void 0 ? void 0 : options.userId) !== null && _a !== void 0 ? _a : user.uuid;
59
+ const { currentPreference, incompleteAttemptCallback, scoreMaximum } = options !== null && options !== void 0 ? options : {};
60
+ const infoPerUser = await getRegistrationAttemptInfo(services.lrs, registration, { currentPreference });
49
61
  const userInfo = infoPerUser[user.uuid];
50
- return getInfoGrade(userInfo !== null && userInfo !== void 0 ? userInfo : 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);
62
+ if (!userInfo) {
63
+ return getCompletedActivityStateGradeAndProgress(resolveAttemptInfo([]), userId, scoreMaximum);
64
+ }
65
+ if (userInfo.currentAttemptCompleted || !incompleteAttemptCallback) {
66
+ return getCompletedActivityStateGradeAndProgress(userInfo, userId, scoreMaximum);
67
+ }
68
+ return incompleteAttemptCallback(userInfo);
51
69
  };
52
- export const getAllGradesForAssignment = async (services, registration, options) => {
53
- const infoPerUserUuid = await getRegistrationAttemptInfo(services.lrs, registration, { anyUser: true });
54
- const mappedInfo = await services.accountsGateway.mapUserUuids(infoPerUserUuid, services.logger, options === null || options === void 0 ? void 0 : options.platformId);
55
- const gradeCompletedAttemptsOnly = (results) => results.map(([userId, userInfo]) => getInfoGrade(userInfo, userId, options === null || options === void 0 ? void 0 : options.scoreMaximum));
56
- if (!(options === null || options === void 0 ? void 0 : options.incompleteAttemptsCallback)) {
57
- return gradeCompletedAttemptsOnly(mappedInfo);
70
+ export const getAssignmentGrades = async (services, registration, options) => {
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);
58
76
  }
59
- const [incompleteInfo, completedInfo] = partition((info) => info[1].currentAttemptCompleted === undefined)(mappedInfo);
60
- 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));
61
79
  };