internetmarke
Version:
A node implementation to use the Internetmarke web service of Deutsche Post.
92 lines (91 loc) • 3.15 kB
JavaScript
"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.User = void 0;
const inversify_1 = require("inversify");
const amount_1 = require("./utils/amount");
const CREDENTIALS = Symbol('credentials');
const TOKEN = Symbol('token');
/**
* The Portokasse user that is billed for ordered vouchers.
*/
let User = class User {
constructor() {
this.orderIds = [];
this.showTermsAndCondition = false;
}
/**
* Set the credentials to use the services.
*
* @param credentials The credentials that authenticate the user.
*/
setCredentials(credentials) {
this[CREDENTIALS] = Object.assign({}, credentials);
this[TOKEN] = null;
}
/**
* Update the user data as retrieved from the service.
*
* @param data The user data from the service.
* @param initialData Determines whether to reset the token in case non is
* provided.
*/
load(data, initialData = false) {
if (data.userToken || data.email || initialData) {
this[TOKEN] = data.userToken || data.email || null;
}
// only update data for authenticated users
if (!this[TOKEN]) {
return;
}
if (undefined !== data.walletBalance) {
this.walletBalance = (0, amount_1.parseAmount)(data.walletBalance);
}
if (undefined !== data.showTermsAndCondition) {
this.showTermsAndCondition = data.showTermsAndCondition;
}
if (data.infoMessage) {
this.infoMessage = data.infoMessage;
}
}
getCredentials() {
return Object.assign({}, this[CREDENTIALS]);
}
isAuthenticated() {
return !!this.getToken();
}
/**
* Add an order id to the user to assign it to him.
*
* @param orderIdThe order id that has been added to the user.
*/
addOrderId(orderId) {
this.orderIds.unshift(orderId);
}
getToken() {
return this[TOKEN];
}
getInfo() {
const isAuthenticated = this.isAuthenticated();
const info = {
isAuthenticated
};
if (isAuthenticated) {
info.username = this[CREDENTIALS].username;
info.walletBalance = this.walletBalance;
info.infoMessage = this.infoMessage;
info.showTermsAndCondition = this.showTermsAndCondition;
info.orderIds = this.orderIds;
}
return info;
}
};
User = __decorate([
(0, inversify_1.injectable)()
], User);
exports.User = User;