@egalteam/framework
Version:
Egal
226 lines (225 loc) • 9.51 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ActionConstructor = void 0;
var GetItemsFilterParams_1 = require("../Actions/GetItemsAction/GetItemsFilterParams");
var GetItemsSortingParams_1 = require("../Actions/GetItemsAction/GetItemsSortingParams");
var GlobalVariables_1 = require("../GlobalVariables");
var CRUDAction_1 = require("../Actions/CRUDActions/CRUDAction");
var GetModelMetadataAction_1 = require("../Actions/GetMetadataAction/GetModelMetadataAction");
var CustomAction_1 = require("../Actions/CustomAction/CustomAction");
var GetItemsAction_1 = require("../Actions/GetItemsAction/GetItemsAction");
var ActionConstructor = /** @class */ (function () {
function ActionConstructor(url, refreshTokenName) {
this.filterArr = [];
this.ordersArr = [];
this.withsArr = [];
this.microserviceName = '';
this.modelName = '';
this.actionName = '';
this.calledAction = '';
this.actionParams = [];
this.pagination = { per_page: undefined, page: undefined };
this.id = '';
this.url = url;
this.refreshTokenName = refreshTokenName;
this.setBaseUrl(this.url);
}
ActionConstructor.prototype.setBaseUrl = function (url) {
GlobalVariables_1.GlobalVariables.httpBaseUrl = url;
};
ActionConstructor.prototype.clearParams = function () {
this.filterArr = [];
this.ordersArr = [];
this.withsArr = [];
this.pagination = { per_page: undefined, page: undefined };
this.id = '';
this.calledAction = '';
};
ActionConstructor.prototype.getMetadata = function (microserviceName, modelName) {
this.microserviceName = microserviceName;
this.modelName = modelName;
this.actionName = 'getMetadata';
this.calledAction = 'getMetadata';
return this;
};
ActionConstructor.prototype.getItems = function (microserviceName, modelName) {
this.clearParams();
this.microserviceName = microserviceName;
this.modelName = modelName;
this.actionName = 'getItems';
this.calledAction = 'getItems';
return this;
};
ActionConstructor.prototype.getItem = function (microserviceName, modelName, id) {
this.clearParams();
this.microserviceName = microserviceName;
this.modelName = modelName;
this.actionName = 'getItem';
this.calledAction = 'getItem';
this.id = id;
return this;
};
ActionConstructor.prototype.create = function (microserviceName, modelName, actionParams) {
this.microserviceName = microserviceName;
this.modelName = modelName;
this.actionParams = actionParams;
this.actionName = 'create';
this.calledAction = 'create';
return this;
};
ActionConstructor.prototype.update = function (microserviceName, modelName, actionParams) {
this.microserviceName = microserviceName;
this.modelName = modelName;
this.actionParams = actionParams;
this.actionName = 'update';
this.calledAction = 'update';
return this;
};
ActionConstructor.prototype.delete = function (microserviceName, modelName, actionParams) {
this.microserviceName = microserviceName;
this.modelName = modelName;
this.actionParams = actionParams;
this.actionName = 'delete';
this.calledAction = 'delete';
return this;
};
ActionConstructor.prototype.deleteMany = function (microserviceName, modelName, actionParams) {
this.microserviceName = microserviceName;
this.modelName = modelName;
this.actionParams = actionParams;
this.actionName = 'deleteMany';
this.calledAction = 'deleteMany';
return this;
};
ActionConstructor.prototype.createMany = function (microserviceName, modelName, actionParams) {
this.microserviceName = microserviceName;
this.modelName = modelName;
this.actionParams = actionParams;
this.actionName = 'createMany';
this.calledAction = 'createMany';
return this;
};
ActionConstructor.prototype.updateMany = function (microserviceName, modelName, actionParams) {
this.microserviceName = microserviceName;
this.modelName = modelName;
this.actionParams = actionParams;
this.actionName = 'updateMany';
this.calledAction = 'updateMany';
return this;
};
ActionConstructor.prototype.updateManyWithFilter = function (microserviceName, modelName, actionParams) {
this.microserviceName = microserviceName;
this.modelName = modelName;
this.actionParams = actionParams;
this.actionName = 'updateManyRaw';
this.calledAction = 'updateManyRaw';
return this;
};
ActionConstructor.prototype.deleteManyWithFilter = function (microserviceName, modelName, actionParams) {
this.microserviceName = microserviceName;
this.modelName = modelName;
this.actionParams = actionParams;
this.actionName = 'deleteManyRaw';
this.calledAction = 'deleteManyRaw';
return this;
};
ActionConstructor.prototype.custom = function (microserviceName, modelName, actionName, actionParams) {
this.clearParams();
this.microserviceName = microserviceName;
this.modelName = modelName;
this.actionParams = actionParams;
this.actionName = actionName;
this.calledAction = 'custom';
return this;
};
ActionConstructor.prototype.getCount = function (microserviceName, modelName) {
this.clearParams();
this.microserviceName = microserviceName;
this.modelName = modelName;
this.actionName = 'getCount';
this.calledAction = 'getCount';
return this;
};
ActionConstructor.prototype.filter = function (filterObject, custom) {
if (custom && custom === 'custom') {
this.filterArr = filterObject;
}
else {
var newFilterObj = new GetItemsFilterParams_1.GetItemsFilterParams(filterObject);
newFilterObj.checkFilterType();
this.filterArr = newFilterObj.formFilterObject();
}
return this;
};
ActionConstructor.prototype.withs = function (withs) {
Array.isArray(withs) ? (this.withsArr = withs) : this.withsArr.push(withs);
return this;
};
ActionConstructor.prototype.order = function (orders) {
this.ordersArr = new GetItemsSortingParams_1.GetItemsSortingParams().createOrderObj(orders);
return this;
};
ActionConstructor.prototype.setPagination = function (perPage, page) {
this.pagination = { per_page: perPage, page: page };
return this;
};
ActionConstructor.prototype.call = function () {
var _this = this;
return new Promise(function (resolve, reject) {
var result;
var actionParameters = {
pagination: _this.pagination,
filter: _this.filterArr,
withs: _this.withsArr,
order: _this.ordersArr,
id: _this.id
};
GlobalVariables_1.GlobalVariables.tokenUST = _this.microserviceName;
switch (_this.calledAction) {
case 'getItems':
case 'getItem':
case 'getCount':
result = new GetItemsAction_1.GetItemsAction(_this.microserviceName, _this.modelName, _this.actionName, actionParameters)
.axiosConnect(true, _this.refreshTokenName)
.then(function (data) {
resolve(data);
})
.catch(function (error) {
reject(error);
});
break;
case 'getMetadata':
result = new GetModelMetadataAction_1.GetModelMetadataAction(_this.microserviceName, _this.actionName, _this.modelName)
.axiosConnect(true, _this.refreshTokenName)
.then(function (data) {
resolve(data);
})
.catch(function (error) {
reject(error);
});
break;
case 'custom':
result = new CustomAction_1.CustomAction(_this.microserviceName, _this.modelName, _this.actionName, _this.actionParams, actionParameters)
.axiosConnect(true, _this.refreshTokenName)
.then(function (data) {
resolve(data);
})
.catch(function (error) {
reject(error);
});
break;
default:
result = new CRUDAction_1.CRUDAction(_this.microserviceName, _this.modelName, _this.actionName, _this.actionParams)
.axiosConnect(true, _this.refreshTokenName)
.then(function (data) {
resolve(data);
})
.catch(function (error) {
reject(error);
});
}
});
};
return ActionConstructor;
}());
exports.ActionConstructor = ActionConstructor;