@mobitel.ltd/orion-pro-api
Version:
my local service
200 lines (199 loc) • 8.44 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
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) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
Object.defineProperty(exports, "__esModule", { value: true });
/* eslint new-cap: ["error", { "properties": false }]*/
const Soap = require("soap");
const parser = require("./parser");
const lodash_1 = require("lodash");
const consts_1 = require("./consts");
class OrionApi {
constructor(url, logger = console, lib = Soap) {
this.url = url;
this.logger = logger;
this.lib = lib;
}
start() {
return __awaiter(this, void 0, void 0, function* () {
const client = yield this.lib.createClientAsync(this.url);
this.logger.log('Connected to Orion!!!');
this.client = client;
return client;
});
}
getPersons(options = {}) {
return __awaiter(this, void 0, void 0, function* () {
try {
const client = this.client || (yield this.start());
const data = yield client.GetPersonsAsync(Object.assign({ count: 0, offset: 0, withoutPhoto: true }, options));
this.logger.debug('Request for getting person info is succeded!!!');
return parser.allPersonHandle(data);
}
catch (err) {
this.logger.error(err);
}
});
}
getPersonByTabNumber(options) {
return __awaiter(this, void 0, void 0, function* () {
try {
const client = this.client || (yield this.start());
this.logger.debug(`Start request for getting info about person by tub number ${options.tabNum}`);
const data = yield client.GetPersonByTabNumberAsync(Object.assign({ withoutPhoto: true }, options));
this.logger.debug(`Request for getting info about person by tub number ${options.tabNum} is succeded!!!`);
return parser.personHandle(data);
}
catch (err) {
this.logger.error(err);
}
});
}
getPersonByPersonId(id) {
return __awaiter(this, void 0, void 0, function* () {
const persons = yield this.getPersons();
return persons && persons.find(person => person.Id === id);
});
}
getPersonForPutCard(options) {
return __awaiter(this, void 0, void 0, function* () {
const data = yield this.getPersonByTabNumber(Object.assign({}, options, { withoutPhoto: false }));
return data && lodash_1.pick(data, consts_1.default.PERSON_PUT_KEYS_LIST);
});
}
getEvents(options) {
return __awaiter(this, void 0, void 0, function* () {
const { beginTime, endTime, offset = 0, count = 0, accessPoints = [], eventTypes = [] } = options;
const client = this.client || (yield this.start());
const date = Date.now();
const data = yield client.GetEventsAsync({
beginTime,
endTime,
offset,
count,
});
this.logger.debug(`Get events from ${beginTime} till ${endTime} is succeded for events: ${JSON.stringify(eventTypes)}`);
return parser.getEvents(eventTypes, accessPoints)(data);
});
}
getCard(options) {
return __awaiter(this, void 0, void 0, function* () {
try {
const client = this.client || (yield this.start());
const data = yield client.GetKeyDataAsync(options);
this.logger.debug(`Get info about card by cardno ${options.cardNo} is succeded!!!`);
const parsedData = parser.getCard(data);
if (lodash_1.isEmpty(parsedData)) {
this.logger.debug(`No user use card ${options.cardNo}`);
return;
}
return parsedData;
}
catch (err) {
this.logger.error(err);
}
});
}
getAllCards() {
return __awaiter(this, void 0, void 0, function* () {
const client = this.client || (yield this.start());
const data = yield client.GetKeysAsync();
return parser.getAllCards(data);
});
}
// Just simple ping request
ping() {
return __awaiter(this, void 0, void 0, function* () {
try {
const date = Date.now();
const client = this.client || (yield this.start());
yield client.GetPersonByTabNumberAsync();
return Date.now() - date;
}
catch (error) {
const msg = 'Connection failed!!!';
this.logger.error(msg);
throw new Error('Connection failed!!!');
}
});
}
getPersonAccessId(tabNum, personData) {
return __awaiter(this, void 0, void 0, function* () {
try {
const person = personData || (yield this.getPersonByTabNumber({ tabNum }));
if (!person) {
return;
}
const allCards = yield this.getAllCards();
const personCardData = allCards.find(card => card.PersonId === person.Id);
return personCardData && personCardData.AccessLevelId;
}
catch (err) {
this.logger.error(err);
}
});
}
putPass(_a) {
var { tabNum, defaultAccessLevel } = _a, options = __rest(_a, ["tabNum", "defaultAccessLevel"]);
return __awaiter(this, void 0, void 0, function* () {
try {
const client = this.client || (yield this.start());
const personData = yield this.getPersonForPutCard({ tabNum });
// TODO change until fix access user id
// const personAccessLevelId = await this.getPersonAccessId(tabNum, personData);
// const accessLevels = {
// AccessLevel: {
// Id: personAccessLevelId || defaultAccessLevel,
// },
// };
const accessLevels = {
AccessLevel: {
Id: defaultAccessLevel,
},
};
const params = Object.assign({}, options, { personData,
accessLevels });
// this data is Person
const data = yield client.PutPassWithAccLevelsAsync(params);
this.logger.debug(`Setting access for user ${tabNum} to card ${options.cardNo} is succeded!!!`);
return Boolean(data);
}
catch (err) {
this.logger.error(err);
return false;
}
});
}
deletePass(options) {
return __awaiter(this, void 0, void 0, function* () {
try {
const client = this.client || (yield this.start());
// this data is Person
const data = yield client.DeletePassAsync(options);
this.logger.debug(`Removing data access for card number ${options.cardNo} is succeded!!!`);
return !!data;
}
catch (err) {
this.logger.error(err);
return false;
}
});
}
}
exports.OrionApi = OrionApi;