UNPKG

atix-internetmarke

Version:

A node wrapper for the Internetmarke web service of the Deutsche Post.

153 lines (129 loc) 3.05 kB
/** * internetmarke * Copyright (c) 2018 Manuel Schächinger * MIT Licensed */ 'use strict'; const errors = require('../errors'); const _CREDENTIALS = Symbol('credentials'), _TOKEN = Symbol('token'); class User { /** * The representation of an user containing credentials, wallet and the token. * * @constructor * @param {Object} credentials - The credentials of the account. * @param {string} credentials.username - The username (email address). * @param {string} credentials.password - The corresponding password. */ constructor({ username, password } = {}) { if (!username || !password) { throw new Error(errors.usage.missingUserCredentials); } /** @type {Object} */ this[_CREDENTIALS] = { username, password }; /** @type {(string|null)} */ this[_TOKEN] = null; /** @type {number[]} */ this._orderIds = []; /** @type {number} */ this._balance = 0; /** @type {boolean} */ this._showTermsAndConditions = false; /** @type {(string|null)} */ this._infoMessage = null; } /** * @returns {Object} - The credentials of the user. */ getCredentials() { return Object.assign({}, this[_CREDENTIALS]); } /** * Retrieve the current user token to perform the next request. * * @returns {(string|null)} */ getToken() { return this[_TOKEN]; } /** * Set the token that authenticates the user for the current session. * * @param {string} token - The token generated by the API. * @returns {User} */ setToken(token) { this[_TOKEN] = token; return this; } /** * @returns {number} */ getBalance() { return this._balance; } /** * @param {number} balance - The amount of money that are available in the * wallet in Euro cents. * @returns {User} */ setBalance(balance) { this._balance = balance; return this; } /** * @returns {(number|null)} */ getOrderId() { return this._orderIds[0] || null; } /** * Add an order id to the user to assign it to him. * * @param {number} orderId * @returns {User} */ addOrderId(orderId) { this._orderIds.unshift(orderId); return this; } /** * @returns {boolean} */ getTerms() { return this._showTermsAndConditions; } /** * @param {boolean} showTermsAndConditions - Indicates whether the user did * see the latest version of the terms. * @returns {User} */ setTerms(showTermsAndConditions) { this._showTermsAndConditions = showTermsAndConditions; return this; } /** * @returns {(string|null)} */ getInfoMessage() { return this._infoMessage; } /** * @param {string} message - The message as provided by the API. * @returns {User} */ setInfoMessage(message) { this._infoMessage = message; return this; } /** * Used to check whether the user is authenticated. * * @returns {boolean} */ isAuthenticated() { return !!this[_TOKEN]; } } module.exports = User;