Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@availity/api-core

Package Overview
Dependencies
Maintainers
14
Versions
121
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@availity/api-core - npm Package Compare versions

Comparing version 6.3.0 to 6.4.0-alpha.9

244

lib/resources/organizations.js

@@ -10,4 +10,2 @@ "use strict";

var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));

@@ -31,2 +29,4 @@

var _qs = _interopRequireDefault(require("qs"));
var _api = _interopRequireDefault(require("../api"));

@@ -97,3 +97,3 @@

var _postGet = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee(data, config, additionalPostGetArgs) {
var _yield$_get$call, organizationsData;
var permissionIds, dataTemp, _yield$_get$call, organizationsData;

@@ -105,10 +105,24 @@ return _regenerator["default"].wrap(function _callee$(_context) {

if (!additionalPostGetArgs) {
_context.next = 6;
_context.next = 8;
break;
}
_context.next = 3;
permissionIds = additionalPostGetArgs.permissionIds;
if (permissionIds) {
if (typeof data === 'string') {
dataTemp = _qs["default"].parse(data);
dataTemp.permissionId = permissionIds;
data = _qs["default"].stringify(dataTemp, {
arrayFormat: 'repeat'
});
} else if (typeof data === 'object') {
data.permissionId = permissionIds;
}
}
_context.next = 5;
return (0, _get2["default"])((0, _getPrototypeOf2["default"])(AvOrganizations.prototype), "postGet", this).call(this, data, config);
case 3:
case 5:
_yield$_get$call = _context.sent;

@@ -118,6 +132,6 @@ organizationsData = _yield$_get$call.data;

case 6:
case 8:
return _context.abrupt("return", (0, _get2["default"])((0, _getPrototypeOf2["default"])(AvOrganizations.prototype), "postGet", this).call(this, data, config));
case 7:
case 9:
case "end":

@@ -140,4 +154,6 @@ return _context.stop();

var _getFilteredOrganizations = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee2(organizationsData, additionalPostGetArgs, data) {
var resourceIds, permissionId, region, organizations, orgLimit, orgOffset, totalOrgCount, resourceIdsArray, _yield$this$avUserPer, userPermissions, authorizedOrgs, authorizedFilteredOrgs;
var _this3 = this;
var _additionalPostGetArg, resourceIds, permissionIds, permissionId, region, organizations, orgLimit, orgOffset, totalOrgCount, permissionIdsToUse, resourceIdsToUse, resourceIdsArray, permissionIdsOR, _yield$this$avUserPer, userPermissions, authorizedOrgs, authorizedFilteredOrgs;
return _regenerator["default"].wrap(function _callee2$(_context2) {

@@ -147,26 +163,14 @@ while (1) {

case 0:
resourceIds = additionalPostGetArgs.resourceIds;
// for filtered orgs, can pass both permissions and resources in postGetArgs, and we will use the permissionIds here over the data.permissionId
_additionalPostGetArg = additionalPostGetArgs.resourceIds, resourceIds = _additionalPostGetArg === void 0 ? [] : _additionalPostGetArg, permissionIds = additionalPostGetArgs.permissionIds;
permissionId = data.permissionId, region = data.region;
organizations = organizationsData.organizations, orgLimit = organizationsData.limit, orgOffset = organizationsData.offset, totalOrgCount = organizationsData.totalCount;
permissionIdsToUse = permissionIds || permissionId;
permissionIdsToUse = this.sanitizeIds(permissionIdsToUse);
resourceIdsToUse = this.sanitizeIds(resourceIds); // resourceIds is passed as readOnly, convert so that we can use Array methods on it
if (!(typeof permissionId !== 'string' && !Array.isArray(permissionId))) {
_context2.next = 5;
break;
}
resourceIdsArray = Array.isArray(resourceIdsToUse) ? resourceIdsToUse : [resourceIdsToUse];
permissionIdsOR = Array.isArray(permissionIdsToUse) ? permissionIdsToUse : [permissionIdsToUse];
throw new TypeError('permissionId must be either an array of ids or a string');
case 5:
if (!(typeof resourceIds !== 'string' && !Array.isArray(resourceIds))) {
_context2.next = 7;
break;
}
throw new TypeError('resourceIds must be either an array of ids or a string');
case 7:
// resourceIds is passed as readOnly, convert so that we can use Array methods on it
resourceIdsArray = typeof resourceIds === 'string' ? [resourceIds] : resourceIds;
if (!(region !== this.previousRegionId || !this.arePermissionsEqual(permissionId))) {
if (!(region !== this.previousRegionId || !this.arePermissionsEqual(permissionIdsOR))) {
_context2.next = 20;

@@ -178,5 +182,5 @@ break;

return this.avUserPermissions.postGet({
permissionId: permissionId,
permissionId: permissionIdsOR,
region: region,
limit: permissionId.length
limit: permissionIdsOR.length
});

@@ -193,4 +197,7 @@

this.userPermissions = userPermissions;
this.previousPermissionIds = permissionId;
this.userPermissions = userPermissions.reduce(function (accum, cur) {
accum[cur.id] = cur;
return accum;
}, {});
this.previousPermissionIds = permissionIdsOR;
this.previousRegionId = region;

@@ -204,31 +211,94 @@ _context2.next = 20;

case 20:
// Reduce the userPermissions result into a collection of orgs that contain a valid resource
authorizedOrgs = this.userPermissions.reduce(function (accum, userPermission) {
userPermission.organizations.forEach(function (userOrg) {
var isDuplicate = accum.some(function (item) {
return item.id === userOrg.id;
// loop thru the permissionId list of ORs, finding and adding matching orgs in the userPermissions. ANDs are beneath/within the ORs
authorizedOrgs = permissionIdsOR.reduce(function (accum, permissionIdOR) {
if (Array.isArray(permissionIdOR)) {
var matchedOrgs = permissionIdOR.reduce(function (matchedANDOrgsByPerm, permissionIdAND, index) {
if (_this3.userPermissions[permissionIdAND]) {
_this3.userPermissions[permissionIdAND].organizations.forEach(function (org) {
if (index === 0) {
// add the orgs for the first permission
matchedANDOrgsByPerm[org.id] = org;
} else if (matchedANDOrgsByPerm[org.id]) {
// if duplicate, add resources
matchedANDOrgsByPerm[org.id].resources = matchedANDOrgsByPerm[org.id].resources.concat(org.resources);
}
});
} // filter unmatched orgs out
matchedANDOrgsByPerm = Object.keys(matchedANDOrgsByPerm).filter(function (orgId) {
if (_this3.userPermissions[permissionIdAND]) {
return _this3.userPermissions[permissionIdAND].organizations.some(function (org) {
return org.id === orgId;
});
}
return false;
}).reduce(function (obj, orgId) {
obj[orgId] = matchedANDOrgsByPerm[orgId];
return obj;
}, {});
return matchedANDOrgsByPerm;
}, {});
Object.keys(matchedOrgs).forEach(function (orgId) {
if (!accum[orgId]) {
accum[orgId] = matchedOrgs[orgId];
accum[orgId].match = false;
}
});
} else if (_this3.userPermissions[permissionIdOR]) {
_this3.userPermissions[permissionIdOR].organizations.forEach(function (org) {
if (!accum[org.id]) {
accum[org.id] = org;
accum[org.id].match = false;
} else {
// add the resources
accum[org.id].resources = accum[org.id].resources.concat(org.resources);
}
});
}
if (!isDuplicate) {
// If this org contains one of the passed in resourceIds, it is an authorized org
var match = userOrg.resources.some(function (userResource) {
return resourceIdsArray.some(function (resource) {
return Number(resource) === Number(userResource.id);
return accum;
}, {}); // loop thru the orgs from permission filtering and check resourceIds list to further filter
if (resourceIdsArray.length === 0) {
Object.keys(authorizedOrgs).forEach(function (orgId) {
authorizedOrgs[orgId].match = true;
});
} else {
resourceIdsArray.forEach(function (resourceIdOR) {
if (Array.isArray(resourceIdOR)) {
// there is AND logic
Object.keys(authorizedOrgs).forEach(function (orgId) {
if (authorizedOrgs[orgId]) {
var isMatch = resourceIdOR.every(function (resId) {
return authorizedOrgs[orgId].resources.some(function (res) {
return res.id === resId;
});
});
if (isMatch) {
authorizedOrgs[orgId].match = true;
}
}
});
} else {
Object.keys(authorizedOrgs).forEach(function (orgId) {
var isMatch = authorizedOrgs[orgId].resources.some(function (res) {
return res.id === resourceIdOR;
});
if (isMatch || !resourceIdOR) {
authorizedOrgs[orgId].match = true;
}
});
if (match) {
accum.push({
id: userOrg.id
});
}
}
});
return accum;
}, []); // avUserPermissions call doesn't return much useful organization data
}, {});
} // avUserPermissions call doesn't return much useful organization data
// but we can match valid ids to useful data returned from avOrganizations
authorizedFilteredOrgs = organizations.filter(function (org) {
return authorizedOrgs.some(function (authorizedOrg) {
return authorizedOrg.id === org.id;
return Object.keys(authorizedOrgs).some(function (orgId) {
return authorizedOrgs[orgId] && authorizedOrgs[orgId].match && orgId === org.id;
});

@@ -246,3 +316,3 @@ }); // Transform back into data object that ResourceSelect can use and paginate

case 23:
case 24:
case "end":

@@ -264,18 +334,60 @@ return _context2.stop();

value: function arePermissionsEqual(permissionId) {
if (typeof permissionId !== typeof this.previousPermissionIds) return false;
if (typeof permissionId === 'string') return permissionId === this.previousPermissionIds;
// handle nested arrays by collecting all permission values for both new and previous, then Set-ing them
var permissionArray = [];
if (Array.isArray(permissionId) && Array.isArray(this.previousPermissionIds)) {
if (permissionId.length !== this.previousPermissionIds.length) return false; // if lengths are equal, need a way to check if values are the same or not
// Sets won't allow duplicate values
// if size of Set is greater than length of original arrays
// then a different value was inserted and they are not equal
if (typeof permissionId === 'string' || typeof permissionId === 'number') {
permissionArray.push(permissionId);
} else if (Array.isArray(permissionId)) {
permissionId.forEach(function (permissionOR) {
if (Array.isArray(permissionOR)) {
permissionOR.forEach(function (permissionAND) {
permissionArray.push(permissionAND);
});
} else {
permissionArray.push(permissionOR);
}
});
}
var idSet = new Set((0, _toConsumableArray2["default"])(permissionId), (0, _toConsumableArray2["default"])(this.previousPermissionIds));
if (idSet.size !== permissionId.length) return false;
return true;
var prevPermissionArray = [];
if (typeof this.previousPermissionIds === 'string' || typeof this.previousPermissionIds === 'number') {
prevPermissionArray.push(this.previousPermissionIds);
} else if (Array.isArray(this.previousPermissionIds)) {
this.previousPermissionIds.forEach(function (permissionOR) {
if (Array.isArray(permissionOR)) {
permissionOR.forEach(function (permissionAND) {
prevPermissionArray.push(permissionAND);
});
} else {
prevPermissionArray.push(permissionOR);
}
});
}
return false;
var idSet = new Set([].concat(permissionArray));
var idSetCombined = new Set([].concat(permissionArray, prevPermissionArray));
return idSet.size === idSetCombined.size;
}
}, {
key: "sanitizeIds",
value: function sanitizeIds(unsanitized) {
var _this4 = this;
if (typeof unsanitized === 'string') {
return unsanitized;
}
if (typeof unsanitized === 'number') {
return "".concat(unsanitized);
}
if (Array.isArray(unsanitized)) {
return unsanitized.map(function (dirty) {
return _this4.sanitizeIds(dirty);
});
}
throw new TypeError('permission/resourcesId(s) must be either an array of ids, a string, or a number');
}
}]);

@@ -282,0 +394,0 @@ return AvOrganizations;

{
"name": "@availity/api-core",
"version": "6.3.0",
"version": "6.4.0-alpha.9+f0d746e",
"description": "Base API definitions for the Availity REST API",

@@ -24,3 +24,3 @@ "keywords": [

},
"gitHead": "f6de97e3e88bc33e29ffd2fba2d78f6d144a02a2"
"gitHead": "f0d746e72dc44112fc306105c9708767644c6372"
}

@@ -0,1 +1,2 @@

import qs from 'qs';
import AvApi from '../api';

@@ -49,2 +50,12 @@

if (additionalPostGetArgs) {
const { permissionIds } = additionalPostGetArgs;
if (permissionIds) {
if (typeof data === 'string') {
const dataTemp = qs.parse(data);
dataTemp.permissionId = permissionIds;
data = qs.stringify(dataTemp, { arrayFormat: 'repeat' });
} else if (typeof data === 'object') {
data.permissionId = permissionIds;
}
}
const { data: organizationsData } = await super.postGet(data, config);

@@ -68,3 +79,4 @@

) {
const { resourceIds } = additionalPostGetArgs;
// for filtered orgs, can pass both permissions and resources in postGetArgs, and we will use the permissionIds here over the data.permissionId
const { resourceIds = [], permissionIds } = additionalPostGetArgs;
const { permissionId, region } = data;

@@ -78,20 +90,18 @@ const {

if (typeof permissionId !== 'string' && !Array.isArray(permissionId)) {
throw new TypeError(
'permissionId must be either an array of ids or a string'
);
}
if (typeof resourceIds !== 'string' && !Array.isArray(resourceIds)) {
throw new TypeError(
'resourceIds must be either an array of ids or a string'
);
}
let permissionIdsToUse = permissionIds || permissionId;
permissionIdsToUse = this.sanitizeIds(permissionIdsToUse);
const resourceIdsToUse = this.sanitizeIds(resourceIds);
// resourceIds is passed as readOnly, convert so that we can use Array methods on it
const resourceIdsArray =
typeof resourceIds === 'string' ? [resourceIds] : resourceIds;
const resourceIdsArray = Array.isArray(resourceIdsToUse)
? resourceIdsToUse
: [resourceIdsToUse];
const permissionIdsOR = Array.isArray(permissionIdsToUse)
? permissionIdsToUse
: [permissionIdsToUse];
if (
region !== this.previousRegionId ||
!this.arePermissionsEqual(permissionId)
!this.arePermissionsEqual(permissionIdsOR)
) {

@@ -105,10 +115,13 @@ // avUserPermissions will return a list of user organizations that match given permission and region

} = await this.avUserPermissions.postGet({
permissionId,
permissionId: permissionIdsOR,
region,
limit: permissionId.length,
limit: permissionIdsOR.length,
});
if (userPermissions) {
this.userPermissions = userPermissions;
this.previousPermissionIds = permissionId;
this.userPermissions = userPermissions.reduce((accum, cur) => {
accum[cur.id] = cur;
return accum;
}, {});
this.previousPermissionIds = permissionIdsOR;
this.previousRegionId = region;

@@ -120,24 +133,96 @@ } else {

// Reduce the userPermissions result into a collection of orgs that contain a valid resource
const authorizedOrgs = this.userPermissions.reduce(
(accum, userPermission) => {
userPermission.organizations.forEach(userOrg => {
const isDuplicate = accum.some(item => item.id === userOrg.id);
if (!isDuplicate) {
// If this org contains one of the passed in resourceIds, it is an authorized org
const match = userOrg.resources.some(userResource => {
return resourceIdsArray.some(
resource => Number(resource) === Number(userResource.id)
// loop thru the permissionId list of ORs, finding and adding matching orgs in the userPermissions. ANDs are beneath/within the ORs
const authorizedOrgs = permissionIdsOR.reduce((accum, permissionIdOR) => {
if (Array.isArray(permissionIdOR)) {
const matchedOrgs = permissionIdOR.reduce(
(matchedANDOrgsByPerm, permissionIdAND, index) => {
if (this.userPermissions[permissionIdAND]) {
this.userPermissions[permissionIdAND].organizations.forEach(
org => {
if (index === 0) {
// add the orgs for the first permission
matchedANDOrgsByPerm[org.id] = org;
} else if (matchedANDOrgsByPerm[org.id]) {
// if duplicate, add resources
matchedANDOrgsByPerm[
org.id
].resources = matchedANDOrgsByPerm[org.id].resources.concat(
org.resources
);
}
}
);
});
if (match) {
accum.push({ id: userOrg.id });
}
// filter unmatched orgs out
matchedANDOrgsByPerm = Object.keys(matchedANDOrgsByPerm)
.filter(orgId => {
if (this.userPermissions[permissionIdAND]) {
return this.userPermissions[
permissionIdAND
].organizations.some(org => org.id === orgId);
}
return false;
})
.reduce((obj, orgId) => {
obj[orgId] = matchedANDOrgsByPerm[orgId];
return obj;
}, {});
return matchedANDOrgsByPerm;
},
{}
);
Object.keys(matchedOrgs).forEach(orgId => {
if (!accum[orgId]) {
accum[orgId] = matchedOrgs[orgId];
accum[orgId].match = false;
}
});
} else if (this.userPermissions[permissionIdOR]) {
this.userPermissions[permissionIdOR].organizations.forEach(org => {
if (!accum[org.id]) {
accum[org.id] = org;
accum[org.id].match = false;
} else {
// add the resources
accum[org.id].resources = accum[org.id].resources.concat(
org.resources
);
}
});
}
return accum;
}, {});
return accum;
},
[]
);
// loop thru the orgs from permission filtering and check resourceIds list to further filter
if (resourceIdsArray.length === 0) {
Object.keys(authorizedOrgs).forEach(orgId => {
authorizedOrgs[orgId].match = true;
});
} else {
resourceIdsArray.forEach(resourceIdOR => {
if (Array.isArray(resourceIdOR)) {
// there is AND logic
Object.keys(authorizedOrgs).forEach(orgId => {
if (authorizedOrgs[orgId]) {
const isMatch = resourceIdOR.every(resId =>
authorizedOrgs[orgId].resources.some(res => res.id === resId)
);
if (isMatch) {
authorizedOrgs[orgId].match = true;
}
}
});
} else {
Object.keys(authorizedOrgs).forEach(orgId => {
const isMatch = authorizedOrgs[orgId].resources.some(
res => res.id === resourceIdOR
);
if (isMatch || !resourceIdOR) {
authorizedOrgs[orgId].match = true;
}
});
}
}, {});
}

@@ -147,3 +232,8 @@ // avUserPermissions call doesn't return much useful organization data

const authorizedFilteredOrgs = organizations.filter(org =>
authorizedOrgs.some(authorizedOrg => authorizedOrg.id === org.id)
Object.keys(authorizedOrgs).some(
orgId =>
authorizedOrgs[orgId] &&
authorizedOrgs[orgId].match &&
orgId === org.id
)
);

@@ -163,26 +253,56 @@

arePermissionsEqual(permissionId) {
if (typeof permissionId !== typeof this.previousPermissionIds) return false;
// handle nested arrays by collecting all permission values for both new and previous, then Set-ing them
const permissionArray = [];
if (typeof permissionId === 'string' || typeof permissionId === 'number') {
permissionArray.push(permissionId);
} else if (Array.isArray(permissionId)) {
permissionId.forEach(permissionOR => {
if (Array.isArray(permissionOR)) {
permissionOR.forEach(permissionAND => {
permissionArray.push(permissionAND);
});
} else {
permissionArray.push(permissionOR);
}
});
}
if (typeof permissionId === 'string')
return permissionId === this.previousPermissionIds;
const prevPermissionArray = [];
if (
Array.isArray(permissionId) &&
Array.isArray(this.previousPermissionIds)
typeof this.previousPermissionIds === 'string' ||
typeof this.previousPermissionIds === 'number'
) {
if (permissionId.length !== this.previousPermissionIds.length)
return false;
prevPermissionArray.push(this.previousPermissionIds);
} else if (Array.isArray(this.previousPermissionIds)) {
this.previousPermissionIds.forEach(permissionOR => {
if (Array.isArray(permissionOR)) {
permissionOR.forEach(permissionAND => {
prevPermissionArray.push(permissionAND);
});
} else {
prevPermissionArray.push(permissionOR);
}
});
}
// if lengths are equal, need a way to check if values are the same or not
// Sets won't allow duplicate values
// if size of Set is greater than length of original arrays
// then a different value was inserted and they are not equal
const idSet = new Set([...permissionId], [...this.previousPermissionIds]);
if (idSet.size !== permissionId.length) return false;
const idSet = new Set([...permissionArray]);
const idSetCombined = new Set([...permissionArray, ...prevPermissionArray]);
return true;
return idSet.size === idSetCombined.size;
}
sanitizeIds(unsanitized) {
if (typeof unsanitized === 'string') {
return unsanitized;
}
return false;
if (typeof unsanitized === 'number') {
return `${unsanitized}`;
}
if (Array.isArray(unsanitized)) {
return unsanitized.map(dirty => this.sanitizeIds(dirty));
}
throw new TypeError(
'permission/resourcesId(s) must be either an array of ids, a string, or a number'
);
}
}

@@ -62,3 +62,3 @@ # Availity API's

Service that allows you to get logged=in user's active organizations.
Service that allows you to get logged in user's active organizations.

@@ -75,2 +75,6 @@ #### Methods

##### `postGet(data, config, additionalPostGetArgs)`
Filters the returned organizations by permissions/resources if additionalPostGetArgs are passed
### `AvProviders`

@@ -77,0 +81,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc