Skip to content

Commit

Permalink
mixed groups in typescript entities
Browse files Browse the repository at this point in the history
  • Loading branch information
Ptroger committed Oct 24, 2024
1 parent ba1cca9 commit 82ab9a2
Show file tree
Hide file tree
Showing 8 changed files with 144 additions and 155 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ import {
SerializedTransactionRequest,
SerializedUserOperationV6,
accountEntitySchema,
accountGroupEntitySchema,
addressBookAccountEntitySchema,
groupEntitySchema,
tokenEntitySchema,
userEntitySchema,
userGroupEntitySchema
userEntitySchema
} from '@narval/policy-engine-shared'
import { Intent } from '@narval/transaction-request-intent'
import { loadPolicy } from '@open-policy-agent/opa-wasm'
Expand Down Expand Up @@ -39,31 +38,25 @@ export type Input = {
// many Rego rules performing a look up on the dataset.
const Id = z.string().toLowerCase()

export const UserGroup = userGroupEntitySchema.extend({
id: Id,
users: z.array(Id)
})
export type UserGroup = z.infer<typeof UserGroup>

export const Account = accountEntitySchema.extend({
id: Id,
assignees: z.array(Id)
})
export type Account = z.infer<typeof Account>

export const AccountGroup = accountGroupEntitySchema.extend({
export const Group = groupEntitySchema.extend({
id: Id,
users: z.array(Id),
accounts: z.array(Id)
})
export type AccountGroup = z.infer<typeof AccountGroup>
export type Group = z.infer<typeof Group>

export const Data = z.object({
entities: z.object({
addressBook: z.record(Id, addressBookAccountEntitySchema.extend({ id: Id })),
tokens: z.record(Id, tokenEntitySchema.extend({ id: Id })),
users: z.record(Id, userEntitySchema.extend({ id: Id })),
accountGroups: z.record(Id, AccountGroup),
userGroups: z.record(Id, UserGroup),
groups: z.record(Id, Group),
accounts: z.record(Id, Account)
})
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -346,24 +346,15 @@ describe('toData', () => {
})
})

it('indexes user groups with members by lower case id', () => {
it('indexes groups with members by lower case id', () => {
const { entities } = toData(FIXTURE.ENTITIES)
const group = FIXTURE.USER_GROUP.Engineering
const group = FIXTURE.GROUP.Engineering

expect(entities.userGroups[group.id.toLowerCase()]).toEqual({
expect(entities.groups[group.id.toLowerCase()]).toEqual({
id: group.id.toLowerCase(),
users: FIXTURE.USER_GROUP_MEMBER.filter(({ groupId }) => groupId === group.id).map(({ userId }) =>
userId.toLowerCase()
)
})
})

it('indexes account groups with members by lower case id', () => {
const { entities } = toData(FIXTURE.ENTITIES)
const group = FIXTURE.ACCOUNT_GROUP.Treasury

expect(entities.accountGroups[group.id.toLowerCase()]).toEqual({
id: group.id.toLowerCase(),
),
accounts: FIXTURE.ACCOUNT_GROUP_MEMBER.filter(({ groupId }) => groupId === group.id).map(({ accountId }) =>
accountId.toLowerCase()
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { InputType, safeDecode } from '@narval/transaction-request-intent'
import { HttpStatus } from '@nestjs/common'
import { indexBy } from 'lodash/fp'
import { OpenPolicyAgentException } from '../exception/open-policy-agent.exception'
import { Account, AccountGroup, Data, Input, UserGroup } from '../type/open-policy-agent.type'
import { Account, Data, Group, Input } from '../type/open-policy-agent.type'

type Mapping<R extends Request> = (
request: R,
Expand Down Expand Up @@ -192,19 +192,33 @@ export const toInput = (params: {
}

export const toData = (entities: Entities): Data => {
const userGroups = entities.userGroupMembers.reduce((groups, { userId, groupId }) => {
const groups = new Map<string, Group>()

// Process user group members
entities.userGroupMembers.forEach(({ userId, groupId }) => {
const id = groupId.toLowerCase()
const group = groups.get(id)
const group = groups.get(id) || {
id: groupId,
users: [],
accounts: []
}

if (group) {
return groups.set(id, {
id: groupId,
users: group.users.concat(userId)
})
} else {
return groups.set(groupId, { id: groupId, users: [userId] })
group.users.push(userId)
groups.set(id, group)
})

// Process account group members
entities.accountGroupMembers.forEach(({ accountId, groupId }) => {
const id = groupId.toLowerCase()
const group = groups.get(id) || {
id: groupId,
users: [],
accounts: []
}
}, new Map<string, UserGroup>())

group.accounts.push(accountId)
groups.set(id, group)
})

const accountAssignees = entities.userAccounts.reduce((assignees, { userId, accountId }) => {
const account = assignees.get(accountId)
Expand All @@ -221,27 +235,13 @@ export const toData = (entities: Entities): Data => {
assignees: accountAssignees.get(account.id) || []
}))

const accountGroups = entities.accountGroupMembers.reduce((groups, { accountId, groupId }) => {
const group = groups.get(groupId)

if (group) {
return groups.set(groupId, {
id: groupId,
accounts: group.accounts.concat(accountId)
})
} else {
return groups.set(groupId, { id: groupId, accounts: [accountId] })
}
}, new Map<string, AccountGroup>())

const data: Data = {
entities: {
addressBook: indexBy('id', entities.addressBook),
tokens: indexBy('id', entities.tokens),
users: indexBy('id', entities.users),
userGroups: Object.fromEntries(userGroups),
accounts: indexBy('id', accounts),
accountGroups: Object.fromEntries(accountGroups)
groups: Object.fromEntries(groups),
accounts: indexBy('id', accounts)
}
}

Expand Down
111 changes: 71 additions & 40 deletions packages/armory-sdk/src/lib/http/client/auth/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1672,12 +1672,6 @@ export interface EntityDataStoreDtoEntityData {
* @memberof EntityDataStoreDtoEntityData
*/
'userGroupMembers': Array<EntityDataStoreDtoEntityDataUserGroupMembersInner>;
/**
*
* @type {Array<EntityDataStoreDtoEntityDataUserGroupsInner>}
* @memberof EntityDataStoreDtoEntityData
*/
'userGroups': Array<EntityDataStoreDtoEntityDataUserGroupsInner>;
/**
*
* @type {Array<EntityDataStoreDtoEntityDataUserAccountsInner>}
Expand All @@ -1698,10 +1692,10 @@ export interface EntityDataStoreDtoEntityData {
'accountGroupMembers': Array<EntityDataStoreDtoEntityDataAccountGroupMembersInner>;
/**
*
* @type {Array<EntityDataStoreDtoEntityDataUserGroupsInner>}
* @type {Array<EntityDataStoreDtoEntityDataGroupsInner>}
* @memberof EntityDataStoreDtoEntityData
*/
'accountGroups': Array<EntityDataStoreDtoEntityDataUserGroupsInner>;
'groups': Array<EntityDataStoreDtoEntityDataGroupsInner>;
/**
*
* @type {Array<EntityDataStoreDtoEntityDataAccountsInner>}
Expand Down Expand Up @@ -1833,6 +1827,19 @@ export interface EntityDataStoreDtoEntityDataCredentialsInner {
*/
'key': CreateClientResponseDtoPolicyEngineNodesInnerPublicKey;
}
/**
*
* @export
* @interface EntityDataStoreDtoEntityDataGroupsInner
*/
export interface EntityDataStoreDtoEntityDataGroupsInner {
/**
*
* @type {string}
* @memberof EntityDataStoreDtoEntityDataGroupsInner
*/
'id': string;
}
/**
*
* @export
Expand Down Expand Up @@ -1908,19 +1915,6 @@ export interface EntityDataStoreDtoEntityDataUserGroupMembersInner {
*/
'groupId': string;
}
/**
*
* @export
* @interface EntityDataStoreDtoEntityDataUserGroupsInner
*/
export interface EntityDataStoreDtoEntityDataUserGroupsInner {
/**
*
* @type {string}
* @memberof EntityDataStoreDtoEntityDataUserGroupsInner
*/
'id': string;
}
/**
*
* @export
Expand Down Expand Up @@ -3748,57 +3742,94 @@ export interface PolicyDataStoreDtoPolicyDataInnerWhenInnerOneOf43ArgsFilters {
*/
'accountGroups'?: Array<string>;
}
/**
* @type PolicyDataStoreDtoPolicyDataInnerWhenInnerOneOf43ArgsTimeWindow
* @export
*/
export type PolicyDataStoreDtoPolicyDataInnerWhenInnerOneOf43ArgsTimeWindow = PolicyDataStoreDtoPolicyDataInnerWhenInnerOneOf43ArgsTimeWindowOneOf | PolicyDataStoreDtoPolicyDataInnerWhenInnerOneOf43ArgsTimeWindowOneOf1;

/**
*
* @export
* @interface PolicyDataStoreDtoPolicyDataInnerWhenInnerOneOf43ArgsTimeWindow
* @interface PolicyDataStoreDtoPolicyDataInnerWhenInnerOneOf43ArgsTimeWindowOneOf
*/
export interface PolicyDataStoreDtoPolicyDataInnerWhenInnerOneOf43ArgsTimeWindow {
export interface PolicyDataStoreDtoPolicyDataInnerWhenInnerOneOf43ArgsTimeWindowOneOf {
/**
*
* @type {string}
* @memberof PolicyDataStoreDtoPolicyDataInnerWhenInnerOneOf43ArgsTimeWindow
* @memberof PolicyDataStoreDtoPolicyDataInnerWhenInnerOneOf43ArgsTimeWindowOneOf
*/
'type'?: PolicyDataStoreDtoPolicyDataInnerWhenInnerOneOf43ArgsTimeWindowTypeEnum;
'type': PolicyDataStoreDtoPolicyDataInnerWhenInnerOneOf43ArgsTimeWindowOneOfTypeEnum;
/**
*
* @type {string}
* @memberof PolicyDataStoreDtoPolicyDataInnerWhenInnerOneOf43ArgsTimeWindow
* @memberof PolicyDataStoreDtoPolicyDataInnerWhenInnerOneOf43ArgsTimeWindowOneOf
*/
'period'?: PolicyDataStoreDtoPolicyDataInnerWhenInnerOneOf43ArgsTimeWindowPeriodEnum;
'period': PolicyDataStoreDtoPolicyDataInnerWhenInnerOneOf43ArgsTimeWindowOneOfPeriodEnum;
/**
*
* @type {number}
* @memberof PolicyDataStoreDtoPolicyDataInnerWhenInnerOneOf43ArgsTimeWindow
*/
'value'?: number;
/**
*
* @type {number}
* @memberof PolicyDataStoreDtoPolicyDataInnerWhenInnerOneOf43ArgsTimeWindow
* @memberof PolicyDataStoreDtoPolicyDataInnerWhenInnerOneOf43ArgsTimeWindowOneOf
*/
'startDate'?: number;
/**
*
* @type {number}
* @memberof PolicyDataStoreDtoPolicyDataInnerWhenInnerOneOf43ArgsTimeWindow
* @memberof PolicyDataStoreDtoPolicyDataInnerWhenInnerOneOf43ArgsTimeWindowOneOf
*/
'endDate'?: number;
}

export const PolicyDataStoreDtoPolicyDataInnerWhenInnerOneOf43ArgsTimeWindowTypeEnum = {
Rolling: 'rolling',
export const PolicyDataStoreDtoPolicyDataInnerWhenInnerOneOf43ArgsTimeWindowOneOfTypeEnum = {
Fixed: 'fixed'
} as const;

export type PolicyDataStoreDtoPolicyDataInnerWhenInnerOneOf43ArgsTimeWindowTypeEnum = typeof PolicyDataStoreDtoPolicyDataInnerWhenInnerOneOf43ArgsTimeWindowTypeEnum[keyof typeof PolicyDataStoreDtoPolicyDataInnerWhenInnerOneOf43ArgsTimeWindowTypeEnum];
export const PolicyDataStoreDtoPolicyDataInnerWhenInnerOneOf43ArgsTimeWindowPeriodEnum = {
export type PolicyDataStoreDtoPolicyDataInnerWhenInnerOneOf43ArgsTimeWindowOneOfTypeEnum = typeof PolicyDataStoreDtoPolicyDataInnerWhenInnerOneOf43ArgsTimeWindowOneOfTypeEnum[keyof typeof PolicyDataStoreDtoPolicyDataInnerWhenInnerOneOf43ArgsTimeWindowOneOfTypeEnum];
export const PolicyDataStoreDtoPolicyDataInnerWhenInnerOneOf43ArgsTimeWindowOneOfPeriodEnum = {
_1d: '1d',
_1m: '1m',
_1y: '1y'
} as const;

export type PolicyDataStoreDtoPolicyDataInnerWhenInnerOneOf43ArgsTimeWindowPeriodEnum = typeof PolicyDataStoreDtoPolicyDataInnerWhenInnerOneOf43ArgsTimeWindowPeriodEnum[keyof typeof PolicyDataStoreDtoPolicyDataInnerWhenInnerOneOf43ArgsTimeWindowPeriodEnum];
export type PolicyDataStoreDtoPolicyDataInnerWhenInnerOneOf43ArgsTimeWindowOneOfPeriodEnum = typeof PolicyDataStoreDtoPolicyDataInnerWhenInnerOneOf43ArgsTimeWindowOneOfPeriodEnum[keyof typeof PolicyDataStoreDtoPolicyDataInnerWhenInnerOneOf43ArgsTimeWindowOneOfPeriodEnum];

/**
*
* @export
* @interface PolicyDataStoreDtoPolicyDataInnerWhenInnerOneOf43ArgsTimeWindowOneOf1
*/
export interface PolicyDataStoreDtoPolicyDataInnerWhenInnerOneOf43ArgsTimeWindowOneOf1 {
/**
*
* @type {string}
* @memberof PolicyDataStoreDtoPolicyDataInnerWhenInnerOneOf43ArgsTimeWindowOneOf1
*/
'type': PolicyDataStoreDtoPolicyDataInnerWhenInnerOneOf43ArgsTimeWindowOneOf1TypeEnum;
/**
*
* @type {number}
* @memberof PolicyDataStoreDtoPolicyDataInnerWhenInnerOneOf43ArgsTimeWindowOneOf1
*/
'value': number;
/**
*
* @type {number}
* @memberof PolicyDataStoreDtoPolicyDataInnerWhenInnerOneOf43ArgsTimeWindowOneOf1
*/
'startDate'?: number;
/**
*
* @type {number}
* @memberof PolicyDataStoreDtoPolicyDataInnerWhenInnerOneOf43ArgsTimeWindowOneOf1
*/
'endDate'?: number;
}

export const PolicyDataStoreDtoPolicyDataInnerWhenInnerOneOf43ArgsTimeWindowOneOf1TypeEnum = {
Rolling: 'rolling'
} as const;

export type PolicyDataStoreDtoPolicyDataInnerWhenInnerOneOf43ArgsTimeWindowOneOf1TypeEnum = typeof PolicyDataStoreDtoPolicyDataInnerWhenInnerOneOf43ArgsTimeWindowOneOf1TypeEnum[keyof typeof PolicyDataStoreDtoPolicyDataInnerWhenInnerOneOf43ArgsTimeWindowOneOf1TypeEnum];

/**
*
Expand Down
Loading

0 comments on commit 82ab9a2

Please sign in to comment.