Skip to content

Commit

Permalink
feat(api): merge existing policy when drafting policy
Browse files Browse the repository at this point in the history
  • Loading branch information
hbriese committed Jul 29, 2024
1 parent fce7023 commit 98aebdd
Show file tree
Hide file tree
Showing 6 changed files with 174 additions and 165 deletions.
140 changes: 70 additions & 70 deletions api/dbschema/edgeql-js/__spec__.ts

Large diffs are not rendered by default.

154 changes: 77 additions & 77 deletions api/dbschema/edgeql-js/modules/default.ts

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions api/src/feat/policies/existing-policies.edgeql
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ with account := (select Account filter .address = <UAddress>$account),
keys := array_unpack(<array<uint16>>$policyKeys)
select Policy {
key,
name,
approvers: { address },
threshold,
actions: {
Expand Down
2 changes: 2 additions & 0 deletions api/src/feat/policies/existing-policies.query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export type ExistingPoliciesArgs = {

export type ExistingPoliciesReturns = Array<{
"key": number;
"name": string;
"approvers": Array<{
"address": string;
}>;
Expand Down Expand Up @@ -46,6 +47,7 @@ with account := (select Account filter .address = <UAddress>$account),
keys := array_unpack(<array<uint16>>$policyKeys)
select Policy {
key,
name,
approvers: { address },
threshold,
actions: {
Expand Down
38 changes: 22 additions & 16 deletions api/src/feat/policies/policies.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ import {
validateMessage,
validateTransaction,
Policy,
Address,
UAddress,
PLACEHOLDER_ACCOUNT_ADDRESS,
Tx,
UUID,
asUUID,
Expand All @@ -31,10 +29,9 @@ import { ShapeFunc } from '~/core/database';
import {
policyStateAsPolicy,
PolicyShape,
policyInputAsStateShape,
inputAsPolicyState,
selectPolicy,
latestPolicy2,
inputAsPolicy,
} from './policies.util';
import { NameTaken, PolicyEvent, Policy as PolicyModel, ValidationError } from './policies.model';
import { TX_SHAPE, transactionAsTx, ProposalTxShape } from '../transactions/transactions.util';
Expand Down Expand Up @@ -119,14 +116,22 @@ export class PoliciesService {
const currentPolicies = await this.db.exec(existingPolicies, { account, policyKeys });
const changedPolicies = policiesWithKeys
.map((input) => {
const policy = inputAsPolicy(input.key, input);
const existing = currentPolicies.find((p) => p.key === policy.key);
return (
(!existing || encodePolicy(policy) !== encodePolicy(policyStateAsPolicy(existing))) && {
input,
policy,
}
);
// Merge exisiting policy state (draft or latest) with input
const existing = currentPolicies.find((p) => p.key === input.key);
const policyState = inputAsPolicyState(input.key, input, existing);
const policy = policyStateAsPolicy(policyState);

// Ignore unchanged policies
if (existing && encodePolicy(policy) === encodePolicy(policyStateAsPolicy(existing)))
return;

return {
policy,
state: {
name: 'Policy ' + input.key,
...policyState,
},
};
})
.filter(Boolean);
if (!changedPolicies.length) return [];
Expand All @@ -152,15 +157,16 @@ export class PoliciesService {
await this.db.exec(insertPolicies, {
account,
transaction,
policies: changedPolicies.map(({ input }) => ({
policies: changedPolicies.map(({ state }) => ({
...(isInitialization && { activationBlock: 0n }),
...policyInputAsStateShape(input.key, input),
name: input.name || 'Policy ' + input.key,
...state,
})),
})
).map((p) => ({ ...p, id: asUUID(p.id), key: asPolicyKey(p.key) }));

const approvers = new Set(changedPolicies.flatMap(({ input }) => input.approvers));
const approvers = new Set(
changedPolicies.flatMap(({ state }) => state.approvers.map((a) => asAddress(a.address))),
);
this.userAccounts.invalidateApproversCache(...approvers);

newPolicies.forEach(({ id }) =>
Expand Down
4 changes: 2 additions & 2 deletions api/src/feat/policies/policies.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ export const policyStateAsPolicy = <S extends PolicyShape>(state: S) =>
})
: null) as S extends null ? Policy | null : Policy;

export const policyInputAsStateShape = (
export const inputAsPolicyState = (
key: PolicyKey,
p: Partial<PolicyInput>,
defaults: NonNullable<PolicyShape> = {
Expand Down Expand Up @@ -183,7 +183,7 @@ export const policyInputAsStateShape = (
};

export const inputAsPolicy = (key: PolicyKey, p: PolicyInput) =>
policyStateAsPolicy(policyInputAsStateShape(key, p));
policyStateAsPolicy(inputAsPolicyState(key, p));

export const asTransfersConfig = (c: TransfersConfigInput): TransfersConfig => ({
defaultAllow: c.defaultAllow,
Expand Down

0 comments on commit 98aebdd

Please sign in to comment.