magister.js
Version:
A JavaScript implementation of the Magister 6 API
97 lines (80 loc) • 2.49 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _magisterThing = _interopRequireDefault(require("./magisterThing"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
class ProfileSettings extends _magisterThing.default {
/**
* @private
* @param {Magister} magister
* @param {Object} raw
*/
constructor(magister, raw) {
super(magister);
/**
* @type {Boolean}
*/
this.redirectMagisterMessages = raw.EloBerichtenDoorsturen;
/**
* @type {String}
*/
this.emailAddress = raw.EmailAdres;
/**
* @type {String}
*/
this.mobileNumber = raw.Mobiel;
}
/**
* Update the server to reflect the changes made on the properties of this
* ProfileSettings instance.
* @returns {Promise<Error|undefined>}
*/
async saveChanges() {
await this._magister._privileges.needs('profiel', 'update');
const url = `${this._magister._personUrl}/profiel`;
await this._magister.http.put(url, this._toMagister());
}
/**
* Change the user password,
* seperate function because requires verification.
*
* @param {string} changed
* @param {string} [original] - Not required, defaults to password set on auth
* @returns {Promise}
*/
async changePassword(changed, original) {
original = original || this._magister._options.password;
const schoolUrl = this._magister.school.url;
await this._magister._privileges.needs('wachtwoordwijzigen', 'update');
const profile = await this._magister.http.post(`${schoolUrl}/api/sessies/huidige/valideer`, {
'wachtwoord': original
}).then(res => res.json());
if (profile.isVerified) {
const selfUrl = profile.links.account.href;
const status = await this._magister.http.put(`${schoolUrl}/${selfUrl}/wachtwoord`, {
'wachtwoord': changed,
'wachtwoordControle': original
}).then(res => res.status);
if (status !== 204) {
throw new Error(`Changing password failed with status code ${status}!`);
}
} else {
throw new Error('Original password incorrect');
}
}
/**
* @private
* @returns {Object}
*/
_toMagister() {
return {
EloBerichtenDoorsturen: this.redirectMagisterMessages,
EmailAdres: this.emailAddress,
Mobiel: this.mobileNumber
};
}
}
var _default = ProfileSettings;
exports.default = _default;