qus-canvas
Version:
client api for canvas
145 lines (144 loc) • 5.29 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.AssignmentsFactory = void 0;
const endpoints_1 = require("../config/endpoints");
const case_converter_1 = require("../../utils/case.converter");
class AssignmentsFactory {
constructor(connector, version, accountId) {
this.connector = connector;
this.version = version;
this.accountId = accountId;
}
/**
* Fetch assignments
*
* @param endpoint
* @private
*/
fetchAssignments(endpoint) {
return __awaiter(this, void 0, void 0, function* () {
try {
const response = yield this.connector.get(endpoint);
if (response) {
const assignments = (0, case_converter_1.toCamelCase)(response.data);
const assignmentPromises = assignments.map((assignment) => __awaiter(this, void 0, void 0, function* () { return this.mapAssignment(assignment); }));
return yield Promise.all(assignmentPromises);
}
}
catch (error) {
console.error(`Error fetching assignments: ${error}`);
}
return [];
});
}
/**
* Map assignment
*
* @param data
*/
mapAssignment(data) {
return Object.assign(Object.assign({}, data), { id: '', assignmentNumber: data.id, createdAtRemote: data.createdAt, updatedAtRemote: data.updatedAt, rubric: data.rubric ? this.mapRubric(data.rubric) : null });
}
/**
* Map data
*
* @param data
*/
mapData(data) {
var _a;
return Object.assign(Object.assign({}, data), { ratings: (_a = data.ratings) === null || _a === void 0 ? void 0 : _a.map((rating) => this.mapRating(rating)) });
}
/**
* Map rubric
*
* @param data
*/
mapRubric(data) {
var _a;
return Object.assign(Object.assign({}, data), { data: (_a = data.data) === null || _a === void 0 ? void 0 : _a.map((item) => this.mapData(item)) });
}
/**
* Map rating
*
* @param data
*/
mapRating(data) {
return Object.assign({}, data);
}
/**
* List assignments by course
*
* @param courseId
* @param page
* @param size
*/
listAssignmentsByCourse(courseId, page, size) {
return __awaiter(this, void 0, void 0, function* () {
const endpoint = new endpoints_1.Endpoints().LIST_ASSIGNMENTS
.replace(':version', this.version)
.replace(':course_id', courseId)
.replace(':page', page.toString())
.replace(':size', size.toString());
return this.fetchAssignments(endpoint);
});
}
/**
* List assignments by course with due dates
*
* @param courseId
* @param page
* @param size
*/
listAssignmentsByCourseWithDueDates(courseId, page, size) {
return __awaiter(this, void 0, void 0, function* () {
const endpoint = new endpoints_1.Endpoints().LIST_ASSIGNMENTS_WITH_DUE_DATES
.replace(':version', this.version)
.replace(':course_id', courseId)
.replace(':page', page.toString())
.replace(':size', size.toString());
return this.fetchAssignments(endpoint);
});
}
/**
* List assignments by user and course
*
* @param courseId
* @param userId
* @param page
* @param size
*/
listAssignmentsByUserAndCourse(courseId, userId, page, size) {
return __awaiter(this, void 0, void 0, function* () {
const endpoint = new endpoints_1.Endpoints().LIST_ASSIGNMENTS_BY_USER_AND_COURSE
.replace(':version', this.version)
.replace(':user_id', userId)
.replace(':course_id', courseId)
.replace(':page', page.toString())
.replace(':size', size.toString());
return this.fetchAssignments(endpoint);
});
}
/**
* List assignments by course with script
*
* @param courseId
*/
listAssignmentsByCourseWithScript(courseId) {
return __awaiter(this, void 0, void 0, function* () {
const endpoint = new endpoints_1.Endpoints().LIST_ASSIGNMENTS_BY_COURSE_WITH_SCRIPT
.replace(':version', this.version)
.replace(':course_id', courseId);
return this.fetchAssignments(endpoint);
});
}
}
exports.AssignmentsFactory = AssignmentsFactory;