Skip to content

Commit ff4d0c7

Browse files
ghassanmasarbrandes
authored andcommitted
feat: ensure lms api synced with latest value in config
This change make it possible if LMS url to be changed, that the last value will be picked. This is simlair openedx/frontend-app-authoring/pull/389 which issue overhangio/tutor-mfe/issues/86, the fixes is needed so that dynamic config would work with tutor: overhangio/tutor-mfe/pull/69
1 parent c4846f9 commit ff4d0c7

File tree

5 files changed

+52
-57
lines changed

5 files changed

+52
-57
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/data/services/lms/api.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ const { get, post, stringifyUrl } = utils;
1414
/*********************************************************************************
1515
* GET Actions
1616
*********************************************************************************/
17-
const assignmentTypes = () => get(urls.assignmentTypes);
18-
const cohorts = () => get(urls.cohorts);
19-
const roles = () => get(urls.roles);
20-
const tracks = () => get(urls.tracks);
17+
const assignmentTypes = () => get(urls.getAssignmentTypesUrl());
18+
const cohorts = () => get(urls.getCohortsUrl());
19+
const roles = () => get(urls.getRolesUrl());
20+
const tracks = () => get(urls.getTracksUrl());
2121

2222
/**
2323
* fetch.gradebookData(searchText, cohort, track, options)
@@ -45,15 +45,15 @@ const gradebookData = (searchText, cohort, track, options = {}) => {
4545
[paramKeys.assignmentGradeMax]: options.assignmentGradeMax,
4646
[paramKeys.assignmentGradeMin]: options.assignmentGradeMin,
4747
};
48-
return get(stringifyUrl(urls.gradebook, queryParams));
48+
return get(stringifyUrl(urls.getGradebookUrl(), queryParams));
4949
};
5050

5151
/**
5252
* fetch.gradeBulkOperationHistory()
5353
* fetches bulk operation history and raises an error if the operation fails
5454
* @return {Promise} - get response
5555
*/
56-
const gradeBulkOperationHistory = () => get(urls.bulkHistory)
56+
const gradeBulkOperationHistory = () => get(urls.getBulkHistoryUrl())
5757
.then(response => response.data)
5858
.catch(() => Promise.reject(Error(messages.errors.unhandledResponse)));
5959

@@ -87,7 +87,7 @@ const gradeOverrideHistory = (subsectionId, userId) => (
8787
* }
8888
* @return {Promise} - post response
8989
*/
90-
const updateGradebookData = (updateData) => post(urls.bulkUpdate, updateData);
90+
const updateGradebookData = (updateData) => post(urls.getBulkUpdateUrl(), updateData);
9191

9292
/**
9393
* uploadGradeCsv(formData)

src/data/services/lms/api.test.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,28 +35,28 @@ describe('lms service api', () => {
3535
describe('fetch.assignmentTypes', () => {
3636
testSimpleFetch(
3737
api.fetch.assignmentTypes,
38-
urls.assignmentTypes,
38+
urls.getAssignmentTypesUrl(),
3939
'fetches from urls.assignmentTypes',
4040
);
4141
});
4242
describe('fetch.cohorts', () => {
4343
testSimpleFetch(
4444
api.fetch.cohorts,
45-
urls.cohorts,
45+
urls.getCohortsUrl(),
4646
'fetches from urls.cohorts',
4747
);
4848
});
4949
describe('fetch.roles', () => {
5050
testSimpleFetch(
5151
api.fetch.roles,
52-
urls.roles,
52+
urls.getRolesUrl(),
5353
'fetches from urls.roles',
5454
);
5555
});
5656
describe('fetch.tracks', () => {
5757
testSimpleFetch(
5858
api.fetch.tracks,
59-
urls.tracks,
59+
urls.getTracksUrl(),
6060
'fetches from urls.tracks',
6161
);
6262
});
@@ -98,7 +98,7 @@ describe('lms service api', () => {
9898
});
9999
test('loads only passed values if options is empty', () => (
100100
api.fetch.gradebookData(searchText, cohort, track).then(({ data }) => {
101-
expect(data).toEqual(utils.stringifyUrl(urls.gradebook, {
101+
expect(data).toEqual(utils.stringifyUrl(urls.getGradebookUrl(), {
102102
[paramKeys.pageSize]: pageSize,
103103
[paramKeys.userContains]: searchText,
104104
[paramKeys.cohortId]: cohort,
@@ -114,7 +114,7 @@ describe('lms service api', () => {
114114
));
115115
test('loads ["all"] for excludedCorseRoles if not includeCourseRoles', () => (
116116
api.fetch.gradebookData(searchText, cohort, track, options).then(({ data }) => {
117-
expect(data).toEqual(utils.stringifyUrl(urls.gradebook, {
117+
expect(data).toEqual(utils.stringifyUrl(urls.getGradebookUrl(), {
118118
[paramKeys.pageSize]: pageSize,
119119
[paramKeys.userContains]: searchText,
120120
[paramKeys.cohortId]: cohort,
@@ -130,7 +130,7 @@ describe('lms service api', () => {
130130
));
131131
test('loads null for excludedCorseRoles if includeCourseRoles', () => (
132132
api.fetch.gradebookData(searchText, cohort, track, options).then(({ data }) => {
133-
expect(data).toEqual(utils.stringifyUrl(urls.gradebook, {
133+
expect(data).toEqual(utils.stringifyUrl(urls.getGradebookUrl(), {
134134
[paramKeys.pageSize]: pageSize,
135135
[paramKeys.userContains]: searchText,
136136
[paramKeys.cohortId]: cohort,
@@ -153,7 +153,7 @@ describe('lms service api', () => {
153153
});
154154
it('fetches from urls.bulkHistory and returns the data', () => (
155155
api.fetch.gradeBulkOperationHistory().then(url => {
156-
expect(url).toEqual(urls.bulkHistory);
156+
expect(url).toEqual(urls.getBulkHistoryUrl());
157157
})
158158
));
159159
});
@@ -195,7 +195,7 @@ describe('lms service api', () => {
195195
});
196196
test('posts to urls.bulkUpdate with passed data', () => (
197197
api.updateGradebookData(updateData).then(({ data }) => {
198-
expect(data).toEqual({ url: urls.bulkUpdate, data: updateData });
198+
expect(data).toEqual({ url: urls.getBulkUpdateUrl(), data: updateData });
199199
})
200200
));
201201
});

src/data/services/lms/urls.js

Lines changed: 28 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,57 +3,52 @@ import { StrictDict } from 'utils';
33
import { historyRecordLimit } from './constants';
44
import { filterQuery, stringifyUrl } from './utils';
55

6-
const baseUrl = `${getConfig().LMS_BASE_URL}`;
7-
86
const courseId = window.location.pathname.split('/').filter(Boolean).pop() || '';
97

10-
const api = `${baseUrl}/api/`;
11-
const bulkGrades = `${api}bulk_grades/course/${courseId}/`;
12-
const enrollment = `${api}enrollment/v1/`;
13-
const grades = `${api}grades/v1/`;
14-
const gradebook = `${grades}gradebook/${courseId}/`;
15-
const bulkUpdate = `${gradebook}bulk-update`;
16-
const intervention = `${bulkGrades}intervention/`;
17-
18-
const cohorts = `${baseUrl}/courses/${courseId}/cohorts/`;
19-
const tracks = `${enrollment}course/${courseId}?include_expired=1`;
20-
const bulkHistory = `${bulkGrades}history/`;
21-
22-
const assignmentTypes = stringifyUrl(`${gradebook}grading-info`, { graded_only: true });
23-
const roles = stringifyUrl(`${enrollment}roles/`, { courseId });
24-
8+
export const getUrlPrefix = () => `${getConfig().LMS_BASE_URL}/api/`;
9+
export const getBulkGradesUrl = () => `${getUrlPrefix()}bulk_grades/course/${courseId}/`;
10+
export const getEnrollmentUrl = () => `${getUrlPrefix()}enrollment/v1/`;
11+
export const getGradesUrl = () => `${getUrlPrefix()}grades/v1/`;
12+
export const getGradebookUrl = () => `${getGradesUrl()}gradebook/${courseId}/`;
13+
export const getBulkUpdateUrl = () => `${getGradebookUrl()}bulk-update`;
14+
export const getInterventionUrl = () => `${getBulkGradesUrl()}intervention/`;
15+
export const getCohortsUrl = () => `${getUrlPrefix()}courses/${courseId}/cohorts/`;
16+
export const getTracksUrl = () => `${getEnrollmentUrl()}course/${courseId}?include_expired=1`;
17+
export const getBulkHistoryUrl = () => `${getBulkUpdateUrl()}history/`;
18+
export const getAssignmentTypesUrl = () => stringifyUrl(`${getGradebookUrl()}grading-info`, { graded_only: true });
19+
export const getRolesUrl = () => stringifyUrl(`${getEnrollmentUrl()}roles/`, { courseId });
2520
/**
2621
* bulkGradesUrlByCourseAndRow(courseId, rowId)
2722
* returns the bulkGrades url with the given rowId.
2823
* @param {string} rowId - row/error identifier
2924
* @return {string} - bulk grades fetch url
3025
*/
31-
export const bulkGradesUrlByRow = (rowId) => stringifyUrl(bulkGrades, { error_id: rowId });
26+
export const bulkGradesUrlByRow = (rowId) => stringifyUrl(getBulkGradesUrl(), { error_id: rowId });
3227

33-
export const gradeCsvUrl = (options = {}) => stringifyUrl(bulkGrades, filterQuery(options));
28+
export const gradeCsvUrl = (options = {}) => stringifyUrl(getBulkGradesUrl(), filterQuery(options));
3429

3530
export const interventionExportCsvUrl = (options = {}) => (
36-
stringifyUrl(intervention, filterQuery(options))
31+
stringifyUrl(getInterventionUrl(), filterQuery(options))
3732
);
3833

3934
export const sectionOverrideHistoryUrl = (subsectionId, userId) => stringifyUrl(
40-
`${grades}subsection/${subsectionId}/`,
35+
`${getGradesUrl()}subsection/${subsectionId}/`,
4136
{ user_id: userId, history_record_limit: historyRecordLimit },
4237
);
4338

4439
export default StrictDict({
45-
assignmentTypes,
46-
bulkGrades,
47-
bulkHistory,
48-
bulkUpdate,
49-
cohorts,
50-
enrollment,
51-
grades,
52-
gradebook,
53-
intervention,
54-
roles,
55-
tracks,
56-
40+
getUrlPrefix,
41+
getBulkGradesUrl,
42+
getEnrollmentUrl,
43+
getGradesUrl,
44+
getGradebookUrl,
45+
getBulkUpdateUrl,
46+
getInterventionUrl,
47+
getCohortsUrl,
48+
getTracksUrl,
49+
getBulkHistoryUrl,
50+
getAssignmentTypesUrl,
51+
getRolesUrl,
5752
bulkGradesUrlByRow,
5853
gradeCsvUrl,
5954
interventionExportCsvUrl,

src/data/services/lms/urls.test.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,33 +17,33 @@ describe('lms api url methods', () => {
1717
it('returns bulkGrades url with error_id', () => {
1818
const id = 'heyo';
1919
expect(bulkGradesUrlByRow(id)).toEqual(
20-
utils.stringifyUrl(urls.bulkGrades, { error_id: id }),
20+
utils.stringifyUrl(urls.getBulkGradesUrl(), { error_id: id }),
2121
);
2222
});
2323
});
2424
describe('gradeCsvUrl', () => {
2525
it('returns bulkGrades with filterQuery-loaded options as query', () => {
2626
const options = { some: 'fun', query: 'options' };
2727
expect(gradeCsvUrl(options)).toEqual(
28-
utils.stringifyUrl(urls.bulkGrades, utils.filterQuery(options)),
28+
utils.stringifyUrl(urls.getBulkGradesUrl(), utils.filterQuery(options)),
2929
);
3030
});
3131
it('defaults options to empty object', () => {
3232
expect(gradeCsvUrl()).toEqual(
33-
utils.stringifyUrl(urls.bulkGrades, utils.filterQuery({})),
33+
utils.stringifyUrl(urls.getBulkGradesUrl(), utils.filterQuery({})),
3434
);
3535
});
3636
});
3737
describe('interventionExportCsvUrl', () => {
3838
it('returns intervention url with filterQuery-loaded options as query', () => {
3939
const options = { some: 'fun', query: 'options' };
4040
expect(interventionExportCsvUrl(options)).toEqual(
41-
utils.stringifyUrl(urls.intervention, utils.filterQuery(options)),
41+
utils.stringifyUrl(urls.getInterventionUrl(), utils.filterQuery(options)),
4242
);
4343
});
4444
it('defaults options to empty object', () => {
4545
expect(interventionExportCsvUrl()).toEqual(
46-
utils.stringifyUrl(urls.intervention, utils.filterQuery({})),
46+
utils.stringifyUrl(urls.getInterventionUrl(), utils.filterQuery({})),
4747
);
4848
});
4949
});
@@ -53,7 +53,7 @@ describe('lms api url methods', () => {
5353
const userId = 'Tom';
5454
expect(sectionOverrideHistoryUrl(subsectionId, userId)).toEqual(
5555
utils.stringifyUrl(
56-
`${urls.grades}subsection/${subsectionId}/`,
56+
`${urls.getGradesUrl()}subsection/${subsectionId}/`,
5757
{ user_id: userId, history_record_limit: historyRecordLimit },
5858
),
5959
);

0 commit comments

Comments
 (0)