* test: added testcases of redux, selector and api * refactor: fixe recommanded issue and improve code cov * refactor: added cases for filter statuses * refactor: updated test description * refactor: add common method of mock data for learner and post * refactor: code and moved test utils in learners folder --------- Co-authored-by: sundasnoreen12 <sundasnoreen12@ggmail.com> Co-authored-by: Awais Ansari <awais.ansari63@gmail.com>
86 lines
2.7 KiB
JavaScript
86 lines
2.7 KiB
JavaScript
/* eslint-disable import/prefer-default-export */
|
|
import snakeCase from 'lodash/snakeCase';
|
|
|
|
import { ensureConfig, getConfig, snakeCaseObject } from '@edx/frontend-platform';
|
|
import { getAuthenticatedHttpClient } from '@edx/frontend-platform/auth';
|
|
|
|
ensureConfig([
|
|
'LMS_BASE_URL',
|
|
], 'Posts API service');
|
|
|
|
export const getCoursesApiUrl = () => `${getConfig().LMS_BASE_URL}/api/discussion/v1/courses/`;
|
|
export const getUserProfileApiUrl = () => `${getConfig().LMS_BASE_URL}/api/user/v1/accounts`;
|
|
export const learnerPostsApiUrl = (courseId) => `${getCoursesApiUrl()}${courseId}/learner/`;
|
|
export const learnersApiUrl = (courseId) => `${getCoursesApiUrl()}${courseId}/activity_stats/`;
|
|
|
|
/**
|
|
* Fetches all the learners in the given course.
|
|
* @param {string} courseId
|
|
* @param {object} params {page, order_by}
|
|
* @returns {Promise<{}>}
|
|
*/
|
|
export async function getLearners(courseId, params) {
|
|
const { data } = await getAuthenticatedHttpClient().get(learnersApiUrl(courseId), { params });
|
|
return data;
|
|
}
|
|
|
|
/**
|
|
* Get user profile
|
|
* @param {string} usernames
|
|
*/
|
|
export async function getUserProfiles(usernames) {
|
|
const url = `${getUserProfileApiUrl()}?username=${usernames.join()}`;
|
|
const { data } = await getAuthenticatedHttpClient().get(url);
|
|
return data;
|
|
}
|
|
|
|
/**
|
|
* Get the posts by a specific user in a course's discussions
|
|
*
|
|
* @param {string} courseId Course ID of the course
|
|
* @param {string} author
|
|
* @param {number} page
|
|
* @param {number} pageSize
|
|
* @param {string} textSearch A search string to match.
|
|
* @param {ThreadOrdering} orderBy The results wil be sorted on this basis.
|
|
* @param {boolean} following If true, only threads followed by the current user will be returned.
|
|
* @param {boolean} flagged If true, only threads that have been reported will be returned.
|
|
* @param {string} threadType Can be 'discussion' or 'question'.
|
|
* @param {ThreadViewStatus} view Set to "unread" on "unanswered" to filter to only those statuses.
|
|
* @param {boolean} countFlagged If true, abuseFlaggedCount will be available.
|
|
* @param {number} cohort
|
|
* @returns API Response object in the format
|
|
* {
|
|
* results: [array of posts],
|
|
* pagination: {count, num_pages, next, previous}
|
|
* }
|
|
*/
|
|
export async function getUserPosts(courseId, {
|
|
page,
|
|
pageSize,
|
|
textSearch,
|
|
orderBy,
|
|
status,
|
|
author,
|
|
threadType,
|
|
countFlagged,
|
|
cohort,
|
|
} = {}) {
|
|
const params = snakeCaseObject({
|
|
page,
|
|
pageSize,
|
|
textSearch,
|
|
threadType,
|
|
orderBy: orderBy && snakeCase(orderBy),
|
|
status,
|
|
requestedFields: 'profile_image',
|
|
username: author,
|
|
countFlagged,
|
|
groupId: cohort,
|
|
});
|
|
|
|
const { data } = await getAuthenticatedHttpClient()
|
|
.get(learnerPostsApiUrl(courseId), { params });
|
|
return data;
|
|
}
|