magister.js
Version:
A JavaScript implementation of the Magister 6 API
169 lines (140 loc) • 3.89 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _magisterThing = _interopRequireDefault(require("./magisterThing"));
var _file = _interopRequireDefault(require("./file"));
var _util = require("./util");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
class ActivityElement extends _magisterThing.default {
/**
* @private
* @param {Magister} magister
* @param {Activity} activity
* @param {Object} raw
*/
constructor(magister, activity, raw) {
super(magister);
/**
* @type {String}
* @readonly
*/
this.id = (0, _util.toString)(raw.Id);
/**
* @type {String}
* @readonly
*/
this.title = raw.Titel;
/**
* @type {String}
* @readonly
*/
this.description = (0, _util.cleanHtmlContent)(raw.Details);
/**
* @type {Date}
* @readonly
*/
this.signinStart = (0, _util.parseDate)(raw.StartInschrijfdatum);
/**
* @type {Date}
* @readonly
*/
this.signinEnd = (0, _util.parseDate)(raw.EindeInschrijfdatum);
/**
* @type {Number}
* @readonly
*/
this.minParticipants = raw.MinAantalDeelnemers;
/**
* @type {Number}
* @readonly
*/
this.maxParticipants = raw.MaxAantalDeelnemers;
/**
* @type {Number}
* @readonly
*/
this.placesLeft = raw.AantalPlaatsenBeschikbaar;
/**
* @type {Boolean}
* @readonly
*/
this.signedup = raw.IsIngeschreven;
/**
* @type {Boolean}
* @readonly
*/
this.ableToSignup = raw.IsOpInTeSchrijven;
/**
* @type {Boolean}
* @readonly
*/
this.mandatorilySignedup = raw.IsVerplichtIngeschreven;
/**
* @type {File[]}
* @readonly
*/
this.attachments = (raw.Bijlagen || []).map(a => new _file.default(this._magister, undefined, a)); // REVIEW: do we want a reference to the parent activity?
// this._activity = activity
/**
* @type {String}
* @readonly
*/
this.activityId = (0, _util.toString)(activity.id);
/**
* @type {String}
* @private
* @readonly
*/
this._url = `${activity._url}/onderdelen/${this.id}`;
}
/**
* @param {boolean} val
* @returns {ActivityElement}
*/
signup(val) {
// TODO: handle errors, I have no idea how an error looks like, though.
if (!this.ableToSignup || this.signedup === val) {
return;
}
return Promise.resolve().then(() => {
const url = `${this._url}/inschrijvingen`;
if (val) {
const payload = {
persoonId: this._magister.profileInfo.id,
activiteitId: Number.parseInt(this.activityId, 10),
onderdeelId: Number.parseInt(this.id, 10)
};
return this._magister._privileges.needs('activiteiten', 'update').then(() => this._magister.http.post(url, payload));
} else {
return this._magister._privileges.needs('activiteiten', 'delete').then(() => this._magister.http.delete(url));
}
}).then(() => {
this.signedup = val;
return this;
});
}
/**
* @private
* @returns {Object}
*/
_toMagister() {
return {
Id: Number.parseInt(this.id, 10),
Titel: this.title,
Details: this.description,
StartInschrijfdatum: this.signinStart.toJSON(),
EindeInschrijfdatum: this.signinEnd.toJSON(),
MinAantalDeelnemers: this.minParticipants,
MaxAantalDeelnemers: this.maxParticipants,
AantalPlaatsenBeschikbaar: this.placesLeft,
IsIngeschreven: this.signedup,
IsOpInTeSchrijven: this.ableToSignup,
IsVerplichtIngeschreven: this.mandatorilySignedup,
activiteitId: Number.parseInt(this.activityId, 10)
};
}
}
var _default = ActivityElement;
exports.default = _default;