@adaptic/backend 0.0.1055 → 0.0.1057
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Account.cjs +14 -0
- package/AccountLinkingRequest.cjs +14 -0
- package/AccountRiskMetrics.cjs +11 -0
- package/AdaConversation.cjs +14 -0
- package/Alert.cjs +11 -0
- package/Allocation.cjs +11 -0
- package/AlpacaAccount.cjs +36 -0
- package/Authenticator.cjs +14 -0
- package/AutoFollowRule.cjs +14 -0
- package/BrokerageAccount.cjs +11 -0
- package/Customer.cjs +14 -0
- package/DeskDecision.cjs +11 -0
- package/FeatureFlagOverride.cjs +14 -0
- package/Follow.cjs +14 -0
- package/Fund.cjs +56 -0
- package/FundAssignment.cjs +14 -0
- package/Investor.cjs +14 -0
- package/LinkedProvider.cjs +14 -0
- package/LlmConfiguration.cjs +14 -0
- package/Mandate.cjs +14 -0
- package/MandateApproval.cjs +14 -0
- package/MandateVersion.cjs +14 -0
- package/NotificationDelivery.cjs +14 -0
- package/NotificationPreference.cjs +14 -0
- package/OrgMembership.cjs +14 -0
- package/PolicyOverlay.cjs +64 -0
- package/PushSubscription.cjs +14 -0
- package/RiskEscalationEvent.cjs +33 -0
- package/Session.cjs +14 -0
- package/Strategy.cjs +28 -0
- package/StrategyHealthSnapshot.cjs +11 -0
- package/StrategySubscription.cjs +11 -0
- package/TradingPolicy.cjs +6 -0
- package/User.cjs +11 -0
- package/UserTotp.cjs +14 -0
- package/WaitlistEntry.cjs +14 -0
- package/esm/Account.mjs +14 -0
- package/esm/AccountLinkingRequest.mjs +14 -0
- package/esm/AccountRiskMetrics.mjs +11 -0
- package/esm/AdaConversation.mjs +14 -0
- package/esm/Alert.mjs +11 -0
- package/esm/Allocation.mjs +11 -0
- package/esm/AlpacaAccount.mjs +36 -0
- package/esm/Authenticator.mjs +14 -0
- package/esm/AutoFollowRule.mjs +14 -0
- package/esm/BrokerageAccount.mjs +11 -0
- package/esm/Customer.mjs +14 -0
- package/esm/DeskDecision.mjs +11 -0
- package/esm/FeatureFlagOverride.mjs +14 -0
- package/esm/Follow.mjs +14 -0
- package/esm/Fund.mjs +56 -0
- package/esm/FundAssignment.mjs +14 -0
- package/esm/Investor.mjs +14 -0
- package/esm/LinkedProvider.mjs +14 -0
- package/esm/LlmConfiguration.mjs +14 -0
- package/esm/Mandate.mjs +14 -0
- package/esm/MandateApproval.mjs +14 -0
- package/esm/MandateVersion.mjs +14 -0
- package/esm/NotificationDelivery.mjs +14 -0
- package/esm/NotificationPreference.mjs +14 -0
- package/esm/OrgMembership.mjs +14 -0
- package/esm/PolicyOverlay.mjs +64 -0
- package/esm/PushSubscription.mjs +14 -0
- package/esm/RiskEscalationEvent.mjs +33 -0
- package/esm/Session.mjs +14 -0
- package/esm/Strategy.mjs +28 -0
- package/esm/StrategyHealthSnapshot.mjs +11 -0
- package/esm/StrategySubscription.mjs +11 -0
- package/esm/TradingPolicy.mjs +6 -0
- package/esm/User.mjs +11 -0
- package/esm/UserTotp.mjs +14 -0
- package/esm/WaitlistEntry.mjs +14 -0
- package/esm/auth/membership-write-authority.d.ts +110 -0
- package/esm/auth/membership-write-authority.mjs +125 -0
- package/esm/auth/mutation-authorization.d.ts +19 -3
- package/esm/auth/mutation-authorization.mjs +21 -2
- package/esm/auth/nested-write-policy.d.ts +4 -0
- package/esm/auth/nested-write-policy.mjs +19 -0
- package/esm/auth/tenancy-scope.d.ts +2 -1
- package/esm/auth/tenancy-scope.mjs +2 -1
- package/esm/generated/typeStrings/PolicyOverlay.d.ts +1 -1
- package/esm/generated/typeStrings/PolicyOverlay.mjs +18 -0
- package/esm/generated/typeStrings/TradingPolicy.d.ts +1 -1
- package/esm/generated/typeStrings/TradingPolicy.mjs +18 -0
- package/esm/generated/typeStrings/index.d.ts +2 -2
- package/esm/generated/typegraphql-prisma/enhance.mjs +18 -18
- package/esm/generated/typegraphql-prisma/enums/TradingPolicyScalarFieldEnum.d.ts +1 -0
- package/esm/generated/typegraphql-prisma/enums/TradingPolicyScalarFieldEnum.mjs +1 -0
- package/esm/generated/typegraphql-prisma/models/TradingPolicy.d.ts +20 -0
- package/esm/generated/typegraphql-prisma/models/TradingPolicy.mjs +27 -0
- package/esm/generated/typegraphql-prisma/resolvers/crud/TradingPolicy/args/FindFirstTradingPolicyArgs.d.ts +1 -1
- package/esm/generated/typegraphql-prisma/resolvers/crud/TradingPolicy/args/FindFirstTradingPolicyOrThrowArgs.d.ts +1 -1
- package/esm/generated/typegraphql-prisma/resolvers/crud/TradingPolicy/args/FindManyTradingPolicyArgs.d.ts +1 -1
- package/esm/generated/typegraphql-prisma/resolvers/crud/TradingPolicy/args/GroupByTradingPolicyArgs.d.ts +1 -1
- package/esm/generated/typegraphql-prisma/resolvers/inputs/TradingPolicyCountOrderByAggregateInput.d.ts +1 -0
- package/esm/generated/typegraphql-prisma/resolvers/inputs/TradingPolicyCountOrderByAggregateInput.mjs +7 -0
- package/esm/generated/typegraphql-prisma/resolvers/inputs/TradingPolicyCreateInput.d.ts +1 -0
- package/esm/generated/typegraphql-prisma/resolvers/inputs/TradingPolicyCreateInput.mjs +7 -0
- package/esm/generated/typegraphql-prisma/resolvers/inputs/TradingPolicyCreateManyInput.d.ts +1 -0
- package/esm/generated/typegraphql-prisma/resolvers/inputs/TradingPolicyCreateManyInput.mjs +7 -0
- package/esm/generated/typegraphql-prisma/resolvers/inputs/TradingPolicyCreateWithoutAlpacaAccountInput.d.ts +1 -0
- package/esm/generated/typegraphql-prisma/resolvers/inputs/TradingPolicyCreateWithoutAlpacaAccountInput.mjs +7 -0
- package/esm/generated/typegraphql-prisma/resolvers/inputs/TradingPolicyCreateWithoutOverlaysInput.d.ts +1 -0
- package/esm/generated/typegraphql-prisma/resolvers/inputs/TradingPolicyCreateWithoutOverlaysInput.mjs +7 -0
- package/esm/generated/typegraphql-prisma/resolvers/inputs/TradingPolicyOrderByWithAggregationInput.d.ts +1 -0
- package/esm/generated/typegraphql-prisma/resolvers/inputs/TradingPolicyOrderByWithAggregationInput.mjs +7 -0
- package/esm/generated/typegraphql-prisma/resolvers/inputs/TradingPolicyOrderByWithRelationInput.d.ts +1 -0
- package/esm/generated/typegraphql-prisma/resolvers/inputs/TradingPolicyOrderByWithRelationInput.mjs +7 -0
- package/esm/generated/typegraphql-prisma/resolvers/inputs/TradingPolicyScalarWhereWithAggregatesInput.d.ts +1 -0
- package/esm/generated/typegraphql-prisma/resolvers/inputs/TradingPolicyScalarWhereWithAggregatesInput.mjs +7 -0
- package/esm/generated/typegraphql-prisma/resolvers/inputs/TradingPolicyUpdateInput.d.ts +1 -0
- package/esm/generated/typegraphql-prisma/resolvers/inputs/TradingPolicyUpdateInput.mjs +7 -0
- package/esm/generated/typegraphql-prisma/resolvers/inputs/TradingPolicyUpdateManyMutationInput.d.ts +1 -0
- package/esm/generated/typegraphql-prisma/resolvers/inputs/TradingPolicyUpdateManyMutationInput.mjs +7 -0
- package/esm/generated/typegraphql-prisma/resolvers/inputs/TradingPolicyUpdateWithoutAlpacaAccountInput.d.ts +1 -0
- package/esm/generated/typegraphql-prisma/resolvers/inputs/TradingPolicyUpdateWithoutAlpacaAccountInput.mjs +7 -0
- package/esm/generated/typegraphql-prisma/resolvers/inputs/TradingPolicyUpdateWithoutOverlaysInput.d.ts +1 -0
- package/esm/generated/typegraphql-prisma/resolvers/inputs/TradingPolicyUpdateWithoutOverlaysInput.mjs +7 -0
- package/esm/generated/typegraphql-prisma/resolvers/inputs/TradingPolicyWhereInput.d.ts +1 -0
- package/esm/generated/typegraphql-prisma/resolvers/inputs/TradingPolicyWhereInput.mjs +7 -0
- package/esm/generated/typegraphql-prisma/resolvers/inputs/TradingPolicyWhereUniqueInput.d.ts +1 -0
- package/esm/generated/typegraphql-prisma/resolvers/inputs/TradingPolicyWhereUniqueInput.mjs +7 -0
- package/esm/generated/typegraphql-prisma/resolvers/outputs/CreateManyAndReturnTradingPolicy.d.ts +1 -0
- package/esm/generated/typegraphql-prisma/resolvers/outputs/CreateManyAndReturnTradingPolicy.mjs +7 -0
- package/esm/generated/typegraphql-prisma/resolvers/outputs/TradingPolicyCountAggregate.d.ts +1 -0
- package/esm/generated/typegraphql-prisma/resolvers/outputs/TradingPolicyCountAggregate.mjs +7 -0
- package/esm/generated/typegraphql-prisma/resolvers/outputs/TradingPolicyGroupBy.d.ts +1 -0
- package/esm/generated/typegraphql-prisma/resolvers/outputs/TradingPolicyGroupBy.mjs +7 -0
- package/esm/index.d.ts +1 -0
- package/esm/index.mjs +2 -0
- package/esm/middleware/__tests__/mutation-auth-guard.harness.d.ts +2 -0
- package/esm/middleware/__tests__/mutation-auth-guard.harness.mjs +141 -10
- package/esm/middleware/mutation-account-facts.d.ts +3 -0
- package/esm/middleware/mutation-account-facts.mjs +2 -0
- package/esm/middleware/mutation-auth-guard.d.ts +4 -2
- package/esm/middleware/mutation-auth-guard.mjs +20 -2
- package/esm/middleware/mutation-membership-facts.d.ts +133 -0
- package/esm/middleware/mutation-membership-facts.mjs +271 -0
- package/esm/middleware/mutation-tenant-facts.d.ts +4 -2
- package/esm/middleware/mutation-tenant-facts.mjs +4 -2
- package/esm/middleware/trading-policy-audit.d.ts +16 -4
- package/esm/middleware/trading-policy-audit.mjs +40 -11
- package/esm/readers/index.d.ts +10 -0
- package/esm/readers/index.mjs +10 -0
- package/esm/readers/intraday-lane-prefs.d.ts +45 -0
- package/esm/readers/intraday-lane-prefs.mjs +71 -0
- package/esm/tests/trading-policy-lane-fields-roundtrip.harness.d.ts +14 -0
- package/esm/tests/trading-policy-lane-fields-roundtrip.harness.mjs +107 -0
- package/generated/typeStrings/PolicyOverlay.cjs +18 -0
- package/generated/typeStrings/PolicyOverlay.d.ts +1 -1
- package/generated/typeStrings/TradingPolicy.cjs +18 -0
- package/generated/typeStrings/TradingPolicy.d.ts +1 -1
- package/generated/typeStrings/index.d.ts +2 -2
- package/generated/typegraphql-prisma/enhance.cjs +18 -18
- package/generated/typegraphql-prisma/enums/TradingPolicyScalarFieldEnum.cjs +1 -0
- package/generated/typegraphql-prisma/enums/TradingPolicyScalarFieldEnum.d.ts +1 -0
- package/generated/typegraphql-prisma/models/TradingPolicy.cjs +7 -0
- package/generated/typegraphql-prisma/models/TradingPolicy.d.ts +20 -0
- package/generated/typegraphql-prisma/resolvers/crud/TradingPolicy/args/FindFirstTradingPolicyArgs.d.ts +1 -1
- package/generated/typegraphql-prisma/resolvers/crud/TradingPolicy/args/FindFirstTradingPolicyOrThrowArgs.d.ts +1 -1
- package/generated/typegraphql-prisma/resolvers/crud/TradingPolicy/args/FindManyTradingPolicyArgs.d.ts +1 -1
- package/generated/typegraphql-prisma/resolvers/crud/TradingPolicy/args/GroupByTradingPolicyArgs.d.ts +1 -1
- package/generated/typegraphql-prisma/resolvers/inputs/TradingPolicyCountOrderByAggregateInput.cjs +6 -0
- package/generated/typegraphql-prisma/resolvers/inputs/TradingPolicyCountOrderByAggregateInput.d.ts +1 -0
- package/generated/typegraphql-prisma/resolvers/inputs/TradingPolicyCreateInput.cjs +6 -0
- package/generated/typegraphql-prisma/resolvers/inputs/TradingPolicyCreateInput.d.ts +1 -0
- package/generated/typegraphql-prisma/resolvers/inputs/TradingPolicyCreateManyInput.cjs +6 -0
- package/generated/typegraphql-prisma/resolvers/inputs/TradingPolicyCreateManyInput.d.ts +1 -0
- package/generated/typegraphql-prisma/resolvers/inputs/TradingPolicyCreateWithoutAlpacaAccountInput.cjs +6 -0
- package/generated/typegraphql-prisma/resolvers/inputs/TradingPolicyCreateWithoutAlpacaAccountInput.d.ts +1 -0
- package/generated/typegraphql-prisma/resolvers/inputs/TradingPolicyCreateWithoutOverlaysInput.cjs +6 -0
- package/generated/typegraphql-prisma/resolvers/inputs/TradingPolicyCreateWithoutOverlaysInput.d.ts +1 -0
- package/generated/typegraphql-prisma/resolvers/inputs/TradingPolicyOrderByWithAggregationInput.cjs +6 -0
- package/generated/typegraphql-prisma/resolvers/inputs/TradingPolicyOrderByWithAggregationInput.d.ts +1 -0
- package/generated/typegraphql-prisma/resolvers/inputs/TradingPolicyOrderByWithRelationInput.cjs +6 -0
- package/generated/typegraphql-prisma/resolvers/inputs/TradingPolicyOrderByWithRelationInput.d.ts +1 -0
- package/generated/typegraphql-prisma/resolvers/inputs/TradingPolicyScalarWhereWithAggregatesInput.cjs +6 -0
- package/generated/typegraphql-prisma/resolvers/inputs/TradingPolicyScalarWhereWithAggregatesInput.d.ts +1 -0
- package/generated/typegraphql-prisma/resolvers/inputs/TradingPolicyUpdateInput.cjs +6 -0
- package/generated/typegraphql-prisma/resolvers/inputs/TradingPolicyUpdateInput.d.ts +1 -0
- package/generated/typegraphql-prisma/resolvers/inputs/TradingPolicyUpdateManyMutationInput.cjs +6 -0
- package/generated/typegraphql-prisma/resolvers/inputs/TradingPolicyUpdateManyMutationInput.d.ts +1 -0
- package/generated/typegraphql-prisma/resolvers/inputs/TradingPolicyUpdateWithoutAlpacaAccountInput.cjs +6 -0
- package/generated/typegraphql-prisma/resolvers/inputs/TradingPolicyUpdateWithoutAlpacaAccountInput.d.ts +1 -0
- package/generated/typegraphql-prisma/resolvers/inputs/TradingPolicyUpdateWithoutOverlaysInput.cjs +6 -0
- package/generated/typegraphql-prisma/resolvers/inputs/TradingPolicyUpdateWithoutOverlaysInput.d.ts +1 -0
- package/generated/typegraphql-prisma/resolvers/inputs/TradingPolicyWhereInput.cjs +6 -0
- package/generated/typegraphql-prisma/resolvers/inputs/TradingPolicyWhereInput.d.ts +1 -0
- package/generated/typegraphql-prisma/resolvers/inputs/TradingPolicyWhereUniqueInput.cjs +6 -0
- package/generated/typegraphql-prisma/resolvers/inputs/TradingPolicyWhereUniqueInput.d.ts +1 -0
- package/generated/typegraphql-prisma/resolvers/outputs/CreateManyAndReturnTradingPolicy.cjs +6 -0
- package/generated/typegraphql-prisma/resolvers/outputs/CreateManyAndReturnTradingPolicy.d.ts +1 -0
- package/generated/typegraphql-prisma/resolvers/outputs/TradingPolicyCountAggregate.cjs +6 -0
- package/generated/typegraphql-prisma/resolvers/outputs/TradingPolicyCountAggregate.d.ts +1 -0
- package/generated/typegraphql-prisma/resolvers/outputs/TradingPolicyGroupBy.cjs +6 -0
- package/generated/typegraphql-prisma/resolvers/outputs/TradingPolicyGroupBy.d.ts +1 -0
- package/index.cjs +2 -0
- package/index.d.ts +1 -0
- package/package.json +1 -1
|
@@ -42,6 +42,18 @@ const U_OTHER = '44444444-4444-4444-8444-444444444444';
|
|
|
42
42
|
const FUND_1 = '55555555-5555-4555-8555-555555555555';
|
|
43
43
|
const FUND_2 = '77777777-7777-4777-8777-777777777777';
|
|
44
44
|
const ORG_2 = '88888888-8888-4888-8888-888888888888';
|
|
45
|
+
const ORG_1 = 'org-1';
|
|
46
|
+
/** An organization its creator has just written: no members, no funds. */
|
|
47
|
+
const ORG_NEW = '99999999-9999-4999-8999-999999999999';
|
|
48
|
+
/** An organization that admits sign-ups from one email domain. */
|
|
49
|
+
const ORG_DOMAIN = 'aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa';
|
|
50
|
+
const U_ORG_OWNER = 'b0000000-0000-4000-8000-000000000001';
|
|
51
|
+
const U_ORG_ADMIN = 'b0000000-0000-4000-8000-000000000002';
|
|
52
|
+
const U_ORG_MEMBER = 'b0000000-0000-4000-8000-000000000003';
|
|
53
|
+
const U_FUND_MANAGER = 'b0000000-0000-4000-8000-000000000004';
|
|
54
|
+
const U_FUND_TRADER = 'b0000000-0000-4000-8000-000000000005';
|
|
55
|
+
const U_FOUNDER = 'b0000000-0000-4000-8000-000000000006';
|
|
56
|
+
const U_JOINER = 'b0000000-0000-4000-8000-000000000007';
|
|
45
57
|
const ACCOUNTS = {
|
|
46
58
|
a1: { id: 'a1', userId: U_OWNER, type: 'LIVE', brokerageAccount: null },
|
|
47
59
|
a2: { id: 'a2', userId: U_OTHER, type: 'PAPER', brokerageAccount: { fundId: FUND_1 } },
|
|
@@ -52,17 +64,34 @@ const POLICIES = {
|
|
|
52
64
|
p2: { id: 'p2', alpacaAccountId: 'a2' },
|
|
53
65
|
p3: { id: 'p3', alpacaAccountId: 'a3' },
|
|
54
66
|
};
|
|
55
|
-
/**
|
|
67
|
+
/**
|
|
68
|
+
* The tenancy rows: U_FUND is assigned FUND_1 (org-1) as a VIEWER; FUND_2
|
|
69
|
+
* (ORG_2) bridges the LIVE account a3. org-1 has an OWNER, an ADMIN and a
|
|
70
|
+
* MEMBER, and FUND_1 a MANAGER and a TRADER.
|
|
71
|
+
*/
|
|
56
72
|
const TENANCY = {
|
|
57
|
-
organization: [
|
|
58
|
-
|
|
73
|
+
organization: [
|
|
74
|
+
{ id: ORG_1, deletedAt: null, emailDomains: [] },
|
|
75
|
+
{ id: ORG_2, deletedAt: null, emailDomains: [] },
|
|
76
|
+
{ id: ORG_NEW, deletedAt: null, emailDomains: [] },
|
|
77
|
+
{ id: ORG_DOMAIN, deletedAt: null, emailDomains: ['join.example'] },
|
|
78
|
+
],
|
|
79
|
+
orgMembership: [
|
|
80
|
+
{ id: 'om2', organizationId: ORG_2, userId: U_OTHER, role: 'MEMBER' },
|
|
81
|
+
{ id: 'om-owner', organizationId: ORG_1, userId: U_ORG_OWNER, role: 'OWNER' },
|
|
82
|
+
{ id: 'om-admin', organizationId: ORG_1, userId: U_ORG_ADMIN, role: 'ADMIN' },
|
|
83
|
+
{ id: 'om-member', organizationId: ORG_1, userId: U_ORG_MEMBER, role: 'MEMBER' },
|
|
84
|
+
{ id: 'om-domain', organizationId: ORG_DOMAIN, userId: U_OTHER, role: 'OWNER' },
|
|
85
|
+
],
|
|
59
86
|
fund: [
|
|
60
|
-
{ id: FUND_1, organizationId:
|
|
87
|
+
{ id: FUND_1, organizationId: ORG_1 },
|
|
61
88
|
{ id: FUND_2, organizationId: ORG_2 },
|
|
62
89
|
],
|
|
63
90
|
fundAssignment: [
|
|
64
|
-
{ id: 'fa1', fundId: FUND_1, userId: U_FUND },
|
|
65
|
-
{ id: 'fa2', fundId: FUND_2, userId: U_OTHER },
|
|
91
|
+
{ id: 'fa1', fundId: FUND_1, userId: U_FUND, role: 'VIEWER' },
|
|
92
|
+
{ id: 'fa2', fundId: FUND_2, userId: U_OTHER, role: 'MANAGER' },
|
|
93
|
+
{ id: 'fa-manager', fundId: FUND_1, userId: U_FUND_MANAGER, role: 'MANAGER' },
|
|
94
|
+
{ id: 'fa-trader', fundId: FUND_1, userId: U_FUND_TRADER, role: 'TRADER' },
|
|
66
95
|
],
|
|
67
96
|
brokerageAccount: [
|
|
68
97
|
{ id: 'ba1', fundId: FUND_1, engineAccountId: 'a2' },
|
|
@@ -82,9 +111,41 @@ function matches(row, where) {
|
|
|
82
111
|
if (condition !== null && typeof condition === 'object' && 'in' in condition) {
|
|
83
112
|
return condition.in.includes(row[key]);
|
|
84
113
|
}
|
|
114
|
+
if (condition !== null && typeof condition === 'object' && 'equals' in condition) {
|
|
115
|
+
return row[key] === condition.equals;
|
|
116
|
+
}
|
|
117
|
+
// A compound unique selector (`organizationId_userId: { organizationId, userId }`).
|
|
118
|
+
if (condition !== null && typeof condition === 'object' && !(key in row)) {
|
|
119
|
+
return matches(row, condition);
|
|
120
|
+
}
|
|
85
121
|
return row[key] === condition;
|
|
86
122
|
});
|
|
87
123
|
}
|
|
124
|
+
/** Project a fixture row onto a Prisma `select`, including `_count` and a to-one relation. */
|
|
125
|
+
function project(delegate, row, select) {
|
|
126
|
+
if (!select)
|
|
127
|
+
return { id: row.id };
|
|
128
|
+
const out = {};
|
|
129
|
+
for (const [key, wanted] of Object.entries(select)) {
|
|
130
|
+
if (key === '_count') {
|
|
131
|
+
const counted = wanted.select;
|
|
132
|
+
const counts = {};
|
|
133
|
+
if ('members' in counted)
|
|
134
|
+
counts.members = TENANCY.orgMembership.filter((m) => m.organizationId === row.id).length;
|
|
135
|
+
if ('funds' in counted)
|
|
136
|
+
counts.funds = TENANCY.fund.filter((f) => f.organizationId === row.id).length;
|
|
137
|
+
out._count = counts;
|
|
138
|
+
}
|
|
139
|
+
else if (delegate === 'fundAssignment' && key === 'fund') {
|
|
140
|
+
const fund = TENANCY.fund.find((f) => f.id === row.fundId);
|
|
141
|
+
out.fund = fund ? project('fund', fund, wanted.select) : null;
|
|
142
|
+
}
|
|
143
|
+
else {
|
|
144
|
+
out[key] = row[key];
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
return out;
|
|
148
|
+
}
|
|
88
149
|
const PRINCIPALS = {
|
|
89
150
|
none: null,
|
|
90
151
|
server: { kind: 'server', sub: 'adaptic-engine:host-1:42' },
|
|
@@ -93,6 +154,15 @@ const PRINCIPALS = {
|
|
|
93
154
|
owner: { kind: 'user', sub: U_OWNER, email: 'owner@example.test', roles: [] },
|
|
94
155
|
stranger: { kind: 'user', sub: U_STRANGER, roles: [] },
|
|
95
156
|
fundUser: { kind: 'user', sub: U_FUND, roles: [] },
|
|
157
|
+
orgOwner: { kind: 'user', sub: U_ORG_OWNER, roles: [] },
|
|
158
|
+
orgAdmin: { kind: 'user', sub: U_ORG_ADMIN, roles: [] },
|
|
159
|
+
// The token claims a role the caller does not hold: authority is read from
|
|
160
|
+
// the database, never from the claim.
|
|
161
|
+
orgMember: { kind: 'user', sub: U_ORG_MEMBER, roles: ['fund_operator'] },
|
|
162
|
+
fundManager: { kind: 'user', sub: U_FUND_MANAGER, roles: [] },
|
|
163
|
+
fundTrader: { kind: 'user', sub: U_FUND_TRADER, roles: [] },
|
|
164
|
+
founder: { kind: 'user', sub: U_FOUNDER, roles: [] },
|
|
165
|
+
joiner: { kind: 'user', sub: U_JOINER, roles: [] },
|
|
96
166
|
};
|
|
97
167
|
const FLIP_LIVE = 'mutation Flip { updateOneTradingPolicy(where: { alpacaAccountId: "a1" }, data: { realtimeTradingEnabled: { set: true } }) { id realtimeTradingEnabled } }';
|
|
98
168
|
const FLIP_FUND = 'mutation FlipFund { updateOneTradingPolicy(where: { alpacaAccountId: "a2" }, data: { killSwitchEnabled: { set: true } }) { id } }';
|
|
@@ -113,6 +183,15 @@ const ROOT_ASSIGNMENT_OWN_FUND = `mutation { createOneFundAssignment(data: { fun
|
|
|
113
183
|
const RETENANT_ASSIGNMENT = `mutation { updateOneFundAssignment(where: { id: "fa1" }, data: { fund: { connect: { id: "${FUND_2}" } } }) { id } }`;
|
|
114
184
|
const BROKER_LABEL = (id) => `mutation { updateOneBrokerageAccount(where: { id: "${id}" }, data: { label: { set: "x" } }) { id } }`;
|
|
115
185
|
const ARM_UNDER_USER = (userId, accountId) => `mutation { updateOneUser(where: { id: "${userId}" }, data: { alpacaAccounts: { update: [{ where: { id: "${accountId}" }, data: { tradingPolicy: ${ARM} } }] } }) { id } }`;
|
|
186
|
+
// Entitlement rows: who may write a membership or a fund assignment.
|
|
187
|
+
const SET_MEMBERSHIP_ROLE = (id, role) => `mutation { updateOneOrgMembership(where: { id: "${id}" }, data: { role: { set: ${role} } }) { id } }`;
|
|
188
|
+
const DELETE_MEMBERSHIP = (id) => `mutation { deleteOneOrgMembership(where: { id: "${id}" }) { id } }`;
|
|
189
|
+
const CREATE_MEMBERSHIP = (orgId, userId, role) => `mutation { createOneOrgMembership(data: { organization: { connect: { id: "${orgId}" } }, user: { connect: { id: "${userId}" } }, role: ${role} }) { id } }`;
|
|
190
|
+
const CREATE_ASSIGNMENT = (fundId, userId, role) => `mutation { createOneFundAssignment(data: { fund: { connect: { id: "${fundId}" } }, user: { connect: { id: "${userId}" } }, role: ${role} }) { id } }`;
|
|
191
|
+
const SET_ASSIGNMENT_ROLE = (id, role) => `mutation { updateOneFundAssignment(where: { id: "${id}" }, data: { role: { set: ${role} } }) { id } }`;
|
|
192
|
+
const SET_MEMBERSHIP_ROLE_BY_PAIR = (orgId, userId, role) => `mutation { updateOneOrgMembership(where: { organizationId_userId: { organizationId: "${orgId}", userId: "${userId}" } }, data: { role: { set: ${role} } }) { id } }`;
|
|
193
|
+
/** The platform's fund-creation payload: the creator as operator and as the fund's first MANAGER. */
|
|
194
|
+
const CREATE_FUND_WITH_MANAGER = (userId) => `mutation { createOneFund(data: { name: "F", slug: "f", organization: { connect: { id: "${ORG_1}" } }, operator: { connect: { id: "${userId}" } }, assignments: { create: [{ user: { connect: { id: "${userId}" } }, role: MANAGER }] } }) { id } }`;
|
|
116
195
|
// Blocker 2: walks that leave the root's rows.
|
|
117
196
|
const ACCOUNT_CHAIN = 'mutation { updateOneAlpacaAccount(where: { id: "a1" }, data: { brokerageAccount: { update: { data: { fund: { update: { data: { name: { set: "x" } } } } } } } }) { id } }';
|
|
118
197
|
const MANAGED_FUND_OPERATOR = `mutation { updateOneUser(where: { id: "${U_FUND}" }, data: { managedFunds: { update: [{ where: { id: "${FUND_1}" }, data: { operator: { update: { data: { role: { set: ADMIN } } } } } }] } }) { id } }`;
|
|
@@ -139,6 +218,8 @@ const BROKER_BRIDGE_FOREIGN = `mutation { createOneBrokerageAccount(data: { fund
|
|
|
139
218
|
const BROKER_ROTATE = (userId) => `mutation { updateOneBrokerageAccount(where: { id: "ba1" }, data: { label: { set: "rotated" }, engineAccount: { upsert: { create: { APIKey: "${API_KEY}", APISecret: "${API_SECRET}", user: { connect: { id: "${userId}" } } }, update: { APIKey: { set: "${API_KEY}" } } } } }) { id } }`;
|
|
140
219
|
const SUSPEND_UPSERT = 'mutation { upsertOneTradingPolicy(where: { alpacaAccountId: "a2" }, create: { alpacaAccount: { connect: { id: "a2" } } }, update: { killSwitchEnabled: { set: true } }) { id } }';
|
|
141
220
|
const DISARM_LIVE = 'mutation Disarm { updateOneTradingPolicy(where: { alpacaAccountId: "a1" }, data: { realtimeTradingEnabled: { set: false } }) { id } }';
|
|
221
|
+
const DISARM_LIVE_WITH_LANES = 'mutation DisarmLanes { updateOneTradingPolicy(where: { alpacaAccountId: "a1" }, data: { realtimeTradingEnabled: { set: false }, intradayLanePrefs: { armed: false } }) { id } }';
|
|
222
|
+
const ARM_LANES_LIVE = 'mutation ArmLanes { updateOneTradingPolicy(where: { alpacaAccountId: "a1" }, data: { intradayLanePrefs: { armed: true } }) { id } }';
|
|
142
223
|
// Credential predicates: a `where` over a stored credential must be refused
|
|
143
224
|
// before the guard reads a row with it, or the rows it matched land in the
|
|
144
225
|
// audit trail and the filter becomes an oracle over the credential.
|
|
@@ -188,6 +269,47 @@ export const SCENARIOS = [
|
|
|
188
269
|
enforcedModels: ['TradingPolicy', 'AlpacaAccount'],
|
|
189
270
|
},
|
|
190
271
|
{ name: 'b1/rootAssignment/fundUser/enforce', query: ROOT_ASSIGNMENT_OWN_FUND, principal: 'fundUser', mode: 'enforce' },
|
|
272
|
+
{ name: 'b1/rootAssignment/fundUser/shadow', query: ROOT_ASSIGNMENT_OWN_FUND, principal: 'fundUser', mode: 'shadow' },
|
|
273
|
+
// A member's own promotion, under both modes.
|
|
274
|
+
{ name: 'mem/promoteSelf/orgMember/enforce', query: SET_MEMBERSHIP_ROLE('om-member', 'OWNER'), principal: 'orgMember', mode: 'enforce' },
|
|
275
|
+
{ name: 'mem/promoteSelf/orgMember/shadow', query: SET_MEMBERSHIP_ROLE('om-member', 'OWNER'), principal: 'orgMember', mode: 'shadow' },
|
|
276
|
+
{ name: 'mem/promoteSelfByPair/orgMember/enforce', query: SET_MEMBERSHIP_ROLE_BY_PAIR(ORG_1, U_ORG_MEMBER, 'ADMIN'), principal: 'orgMember', mode: 'enforce' },
|
|
277
|
+
{ name: 'mem/selfManager/orgMember/enforce', query: CREATE_ASSIGNMENT(FUND_1, U_ORG_MEMBER, 'MANAGER'), principal: 'orgMember', mode: 'enforce' },
|
|
278
|
+
{ name: 'mem/selfManager/orgMember/shadow', query: CREATE_ASSIGNMENT(FUND_1, U_ORG_MEMBER, 'MANAGER'), principal: 'orgMember', mode: 'shadow' },
|
|
279
|
+
{ name: 'mem/selfManager/fundUser/enforce', query: SET_ASSIGNMENT_ROLE('fa1', 'MANAGER'), principal: 'fundUser', mode: 'enforce' },
|
|
280
|
+
// OWNER and ADMIN manage the organization's memberships; only OWNER touches an OWNER row.
|
|
281
|
+
{ name: 'mem/ownerSetsRole/orgOwner/enforce', query: SET_MEMBERSHIP_ROLE('om-member', 'ADMIN'), principal: 'orgOwner', mode: 'enforce' },
|
|
282
|
+
{ name: 'mem/ownerGrantsOwner/orgOwner/enforce', query: SET_MEMBERSHIP_ROLE('om-admin', 'OWNER'), principal: 'orgOwner', mode: 'enforce' },
|
|
283
|
+
{ name: 'mem/ownerRemoves/orgOwner/enforce', query: DELETE_MEMBERSHIP('om-member'), principal: 'orgOwner', mode: 'enforce' },
|
|
284
|
+
{ name: 'mem/adminSetsRole/orgAdmin/enforce', query: SET_MEMBERSHIP_ROLE('om-member', 'AUDITOR'), principal: 'orgAdmin', mode: 'enforce' },
|
|
285
|
+
{ name: 'mem/adminGrantsOwner/orgAdmin/enforce', query: SET_MEMBERSHIP_ROLE('om-admin', 'OWNER'), principal: 'orgAdmin', mode: 'enforce' },
|
|
286
|
+
{ name: 'mem/adminDemotesOwner/orgAdmin/enforce', query: SET_MEMBERSHIP_ROLE('om-owner', 'MEMBER'), principal: 'orgAdmin', mode: 'enforce' },
|
|
287
|
+
{ name: 'mem/adminRemovesOwner/orgAdmin/enforce', query: DELETE_MEMBERSHIP('om-owner'), principal: 'orgAdmin', mode: 'enforce' },
|
|
288
|
+
{ name: 'mem/adminAddsMember/orgAdmin/enforce', query: CREATE_MEMBERSHIP(ORG_1, U_STRANGER, 'COMPLIANCE_OFFICER'), principal: 'orgAdmin', mode: 'enforce' },
|
|
289
|
+
{ name: 'mem/otherOrg/orgOwner/enforce', query: SET_MEMBERSHIP_ROLE('om2', 'OWNER'), principal: 'orgOwner', mode: 'enforce' },
|
|
290
|
+
{ name: 'mem/missingRow/orgOwner/enforce', query: SET_MEMBERSHIP_ROLE('om-none', 'ADMIN'), principal: 'orgOwner', mode: 'enforce' },
|
|
291
|
+
// Self-service: the creator of an empty organization. A domain join made as the
|
|
292
|
+
// joiner carries no proof the backend can read (the email is the caller's own column).
|
|
293
|
+
{ name: 'mem/firstOwner/founder/enforce', query: CREATE_MEMBERSHIP(ORG_NEW, U_FOUNDER, 'OWNER'), principal: 'founder', mode: 'enforce' },
|
|
294
|
+
{ name: 'mem/claimOccupied/founder/enforce', query: CREATE_MEMBERSHIP(ORG_1, U_FOUNDER, 'OWNER'), principal: 'founder', mode: 'enforce' },
|
|
295
|
+
{ name: 'mem/firstOwnerForOther/founder/enforce', query: CREATE_MEMBERSHIP(ORG_NEW, U_STRANGER, 'OWNER'), principal: 'founder', mode: 'enforce' },
|
|
296
|
+
{ name: 'mem/domainJoin/joiner/enforce', query: CREATE_MEMBERSHIP(ORG_DOMAIN, U_JOINER, 'MEMBER'), principal: 'joiner', mode: 'enforce' },
|
|
297
|
+
// An invitation's acceptance carries no proof the backend can read.
|
|
298
|
+
{ name: 'mem/inviteAccept/stranger/enforce', query: CREATE_MEMBERSHIP(ORG_1, U_STRANGER, 'ADMIN'), principal: 'stranger', mode: 'enforce' },
|
|
299
|
+
// Fund assignments: org OWNER/ADMIN, or the fund's own MANAGER.
|
|
300
|
+
{ name: 'mem/ownerAssigns/orgOwner/enforce', query: CREATE_ASSIGNMENT(FUND_1, U_STRANGER, 'MANAGER'), principal: 'orgOwner', mode: 'enforce' },
|
|
301
|
+
{ name: 'mem/adminAssigns/orgAdmin/enforce', query: CREATE_ASSIGNMENT(FUND_1, U_STRANGER, 'TRADER'), principal: 'orgAdmin', mode: 'enforce' },
|
|
302
|
+
{ name: 'mem/managerAssigns/fundManager/enforce', query: CREATE_ASSIGNMENT(FUND_1, U_STRANGER, 'ANALYST'), principal: 'fundManager', mode: 'enforce' },
|
|
303
|
+
{ name: 'mem/managerSetsRole/fundManager/enforce', query: SET_ASSIGNMENT_ROLE('fa1', 'ANALYST'), principal: 'fundManager', mode: 'enforce' },
|
|
304
|
+
{ name: 'mem/managerOtherFund/fundManager/enforce', query: CREATE_ASSIGNMENT(FUND_2, U_STRANGER, 'ANALYST'), principal: 'fundManager', mode: 'enforce' },
|
|
305
|
+
{ name: 'mem/managerOtherFundRow/fundManager/enforce', query: SET_ASSIGNMENT_ROLE('fa2', 'VIEWER'), principal: 'fundManager', mode: 'enforce' },
|
|
306
|
+
{ name: 'mem/traderAssigns/fundTrader/enforce', query: CREATE_ASSIGNMENT(FUND_1, U_STRANGER, 'ANALYST'), principal: 'fundTrader', mode: 'enforce' },
|
|
307
|
+
// The authority read fails: refused, and only observed in shadow.
|
|
308
|
+
{ name: 'mem/readDown/orgOwner/enforce', query: SET_MEMBERSHIP_ROLE('om-member', 'ADMIN'), principal: 'orgOwner', mode: 'enforce', membershipReadsDown: true },
|
|
309
|
+
{ name: 'mem/readDown/orgOwner/shadow', query: SET_MEMBERSHIP_ROLE('om-member', 'ADMIN'), principal: 'orgOwner', mode: 'shadow', membershipReadsDown: true },
|
|
310
|
+
// The platform's fund creation writes the creator's MANAGER assignment nested.
|
|
311
|
+
{ name: 'mem/fundCreate/orgOwner/enforce', query: CREATE_FUND_WITH_MANAGER(U_ORG_OWNER), principal: 'orgOwner', mode: 'enforce' },
|
|
312
|
+
{ name: 'mem/ownerSetsRole/server/enforce', query: SET_MEMBERSHIP_ROLE('om-member', 'OWNER'), principal: 'server', mode: 'enforce' },
|
|
191
313
|
{ name: 'b1/retenantAssignment/fundUser/enforce', query: RETENANT_ASSIGNMENT, principal: 'fundUser', mode: 'enforce' },
|
|
192
314
|
{ name: 'b1/crossTenantBroker/stranger/enforce', query: BROKER_LABEL('ba2'), principal: 'stranger', mode: 'enforce' },
|
|
193
315
|
{ name: 'b1/tenantBroker/fundUser/enforce', query: BROKER_LABEL('ba1'), principal: 'fundUser', mode: 'enforce' },
|
|
@@ -241,6 +363,8 @@ export const SCENARIOS = [
|
|
|
241
363
|
mode: 'enforce',
|
|
242
364
|
},
|
|
243
365
|
{ name: 'm/disarmAuditDown/server/enforce', query: DISARM_LIVE, principal: 'server', mode: 'enforce', auditDown: true },
|
|
366
|
+
{ name: 'm/disarmLanesAuditDown/server/enforce', query: DISARM_LIVE_WITH_LANES, principal: 'server', mode: 'enforce', auditDown: true },
|
|
367
|
+
{ name: 'm/armLanesAuditDown/server/enforce', query: ARM_LANES_LIVE, principal: 'server', mode: 'enforce', auditDown: true },
|
|
244
368
|
{ name: 'ctl/brokerCreate/fundUser/enforce', query: BROKER_CREATE, principal: 'fundUser', mode: 'enforce' },
|
|
245
369
|
{ name: 'ctl/brokerCreateOtherOwner/fundUser/enforce', query: BROKER_CREATE_OTHER_OWNER, principal: 'fundUser', mode: 'enforce' },
|
|
246
370
|
{ name: 'ctl/brokerBridgeForeign/fundUser/enforce', query: BROKER_BRIDGE_FOREIGN, principal: 'fundUser', mode: 'enforce' },
|
|
@@ -287,6 +411,7 @@ function policyFacts(p) {
|
|
|
287
411
|
paperTradingOnly: false,
|
|
288
412
|
killSwitchEnabled: false,
|
|
289
413
|
autonomyMode: 'ADVISORY_ONLY',
|
|
414
|
+
intradayLanePrefs: null,
|
|
290
415
|
alpacaAccount: ACCOUNTS[p.alpacaAccountId],
|
|
291
416
|
};
|
|
292
417
|
}
|
|
@@ -295,14 +420,18 @@ function policyFacts(p) {
|
|
|
295
420
|
* fixture rows (the entitlement read and the guard's scope check), and
|
|
296
421
|
* recording reads and writes.
|
|
297
422
|
*/
|
|
298
|
-
function tenancyDelegates(write, read) {
|
|
423
|
+
function tenancyDelegates(write, read, reads, membershipReadsDown) {
|
|
299
424
|
const out = {};
|
|
300
425
|
for (const [delegate, rows] of Object.entries(TENANCY)) {
|
|
301
426
|
out[delegate] = {
|
|
302
427
|
findMany: ({ where }) => read(`${delegate}.findMany`, rows.filter((r) => matches(r, where))),
|
|
303
|
-
findUnique: ({ where }) => {
|
|
428
|
+
findUnique: ({ where, select }) => {
|
|
429
|
+
if (membershipReadsDown() && (delegate === 'orgMembership' || delegate === 'fundAssignment')) {
|
|
430
|
+
reads.push(`${delegate}.findUnique`);
|
|
431
|
+
return Promise.reject(new Error('membership table unavailable'));
|
|
432
|
+
}
|
|
304
433
|
const row = rows.find((r) => matches(r, where));
|
|
305
|
-
return read(`${delegate}.findUnique`, row ?
|
|
434
|
+
return read(`${delegate}.findUnique`, row ? project(delegate, row, select) : null);
|
|
306
435
|
},
|
|
307
436
|
create: () => write(`${delegate}.create`, { id: `${delegate}-new` }),
|
|
308
437
|
update: ({ where }) => write(`${delegate}.update`, { id: where.id }),
|
|
@@ -317,6 +446,7 @@ async function main() {
|
|
|
317
446
|
let enforcedModels = new Set();
|
|
318
447
|
let auditDown = false;
|
|
319
448
|
let credentialMode = 'enforce';
|
|
449
|
+
let membershipReadsDown = false;
|
|
320
450
|
let writes = [];
|
|
321
451
|
let reads = [];
|
|
322
452
|
let audit = [];
|
|
@@ -348,7 +478,7 @@ async function main() {
|
|
|
348
478
|
user: {
|
|
349
479
|
update: ({ where }) => write('user.update', { id: where.id }),
|
|
350
480
|
},
|
|
351
|
-
...tenancyDelegates(write, read),
|
|
481
|
+
...tenancyDelegates(write, read, { push: (name) => reads.push(name) }, () => membershipReadsDown),
|
|
352
482
|
auditLog: {
|
|
353
483
|
create: ({ data }) => {
|
|
354
484
|
if (auditDown)
|
|
@@ -395,6 +525,7 @@ async function main() {
|
|
|
395
525
|
enforcedModels = new Set(scenario.enforcedModels ?? []);
|
|
396
526
|
auditDown = scenario.auditDown === true;
|
|
397
527
|
credentialMode = scenario.credentialMode ?? 'enforce';
|
|
528
|
+
membershipReadsDown = scenario.membershipReadsDown === true;
|
|
398
529
|
writes = [];
|
|
399
530
|
reads = [];
|
|
400
531
|
audit = [];
|
|
@@ -13,6 +13,7 @@
|
|
|
13
13
|
*
|
|
14
14
|
* @module middleware/mutation-account-facts
|
|
15
15
|
*/
|
|
16
|
+
import type { Prisma } from '@prisma/client';
|
|
16
17
|
import type { AccountOwnership, MutationTarget } from '../auth/mutation-authorization';
|
|
17
18
|
import type { NestedWrite } from '../auth/nested-write-inspector';
|
|
18
19
|
import type { TenantScopePrisma } from './mutation-tenant-facts';
|
|
@@ -36,6 +37,7 @@ declare const POLICY_SELECT: {
|
|
|
36
37
|
readonly paperTradingOnly: true;
|
|
37
38
|
readonly killSwitchEnabled: true;
|
|
38
39
|
readonly autonomyMode: true;
|
|
40
|
+
readonly intradayLanePrefs: true;
|
|
39
41
|
readonly alpacaAccount: {
|
|
40
42
|
readonly select: {
|
|
41
43
|
readonly id: true;
|
|
@@ -66,6 +68,7 @@ export interface PolicyFacts {
|
|
|
66
68
|
readonly paperTradingOnly: boolean;
|
|
67
69
|
readonly killSwitchEnabled: boolean;
|
|
68
70
|
readonly autonomyMode: string;
|
|
71
|
+
readonly intradayLanePrefs: Prisma.JsonValue | null;
|
|
69
72
|
readonly alpacaAccount: AccountFacts;
|
|
70
73
|
}
|
|
71
74
|
type Where = Record<string, unknown>;
|
|
@@ -29,6 +29,7 @@ const POLICY_SELECT = {
|
|
|
29
29
|
paperTradingOnly: true,
|
|
30
30
|
killSwitchEnabled: true,
|
|
31
31
|
autonomyMode: true,
|
|
32
|
+
intradayLanePrefs: true,
|
|
32
33
|
alpacaAccount: { select: ACCOUNT_SELECT },
|
|
33
34
|
};
|
|
34
35
|
function asRecord(value) {
|
|
@@ -58,6 +59,7 @@ function policyRow(row, nestedPath = null) {
|
|
|
58
59
|
paperTradingOnly: row.paperTradingOnly,
|
|
59
60
|
killSwitchEnabled: row.killSwitchEnabled,
|
|
60
61
|
autonomyMode: row.autonomyMode,
|
|
62
|
+
intradayLanePrefs: row.intradayLanePrefs,
|
|
61
63
|
},
|
|
62
64
|
};
|
|
63
65
|
}
|
|
@@ -4,8 +4,10 @@
|
|
|
4
4
|
* policy or account row, that account.
|
|
5
5
|
*
|
|
6
6
|
* The decision itself lives in `src/auth/mutation-authorization.ts` (pure);
|
|
7
|
-
* this module installs it on the built schema, resolves the ownership
|
|
8
|
-
* user principal's write needs
|
|
7
|
+
* this module installs it on the built schema, resolves the ownership,
|
|
8
|
+
* tenancy and membership-role facts a user principal's write needs (read
|
|
9
|
+
* from the database for the mutation, never from the token), counts and logs
|
|
10
|
+
* every decision, and writes the
|
|
9
11
|
* attributed TradingPolicy audit rows (`./trading-policy-audit.ts`).
|
|
10
12
|
*
|
|
11
13
|
* ## Why it wraps the Mutation fields instead of being a global middleware
|
|
@@ -4,8 +4,10 @@
|
|
|
4
4
|
* policy or account row, that account.
|
|
5
5
|
*
|
|
6
6
|
* The decision itself lives in `src/auth/mutation-authorization.ts` (pure);
|
|
7
|
-
* this module installs it on the built schema, resolves the ownership
|
|
8
|
-
* user principal's write needs
|
|
7
|
+
* this module installs it on the built schema, resolves the ownership,
|
|
8
|
+
* tenancy and membership-role facts a user principal's write needs (read
|
|
9
|
+
* from the database for the mutation, never from the token), counts and logs
|
|
10
|
+
* every decision, and writes the
|
|
9
11
|
* attributed TradingPolicy audit rows (`./trading-policy-audit.ts`).
|
|
10
12
|
*
|
|
11
13
|
* ## Why it wraps the Mutation fields instead of being a global middleware
|
|
@@ -53,6 +55,7 @@ import { defaultFieldResolver, GraphQLError, } from 'graphql';
|
|
|
53
55
|
import { Counter } from 'prom-client';
|
|
54
56
|
import { redactCredentials } from '../auth/credential-redaction.mjs';
|
|
55
57
|
import { classifyMutationField, effectiveModeFor, evaluateMutationAccess, getEnforcedModels, getMutationAuthMode, touchesAlwaysEnforced, } from '../auth/mutation-authorization.mjs';
|
|
58
|
+
import { isMembershipModel } from '../auth/membership-write-authority.mjs';
|
|
56
59
|
import { findNestedWrites } from '../auth/nested-write-inspector.mjs';
|
|
57
60
|
import { firstNestedRefusal, writesNestedModelRows } from '../auth/nested-write-policy.mjs';
|
|
58
61
|
import { GOVERNED_MODELS } from '../auth/tenancy-scope.mjs';
|
|
@@ -61,6 +64,7 @@ import { metricsRegistry } from '../config/metrics.mjs';
|
|
|
61
64
|
import { logger } from '../utils/logger.mjs';
|
|
62
65
|
import { credentialAccessForbidden, credentialFieldAccessTotal, findCredentialArgumentReferences, getCredentialFieldGuardMode, } from './credential-field-guard.mjs';
|
|
63
66
|
import { loadTouchedAccounts, ownershipOf, resolveNestedPolicyRows, } from './mutation-account-facts.mjs';
|
|
67
|
+
import { membershipWriteVerdict } from './mutation-membership-facts.mjs';
|
|
64
68
|
import { mandateVersionScopeOf, tenantScopeOf } from './mutation-tenant-facts.mjs';
|
|
65
69
|
import { actorFor, changeReasonFor, isDisarmOnlyPolicyWrite, mayTouchLiveAccount, mutationAuditBypassedTotal, touchesTradingSwitch, writeAttemptRow, writeResultRow, } from './trading-policy-audit.mjs';
|
|
66
70
|
/**
|
|
@@ -213,6 +217,20 @@ async function decide(principal, target, nested, args, touched, prisma, relation
|
|
|
213
217
|
return first;
|
|
214
218
|
}
|
|
215
219
|
}
|
|
220
|
+
if (first.reason === 'membership_authority_unresolved' && isMembershipModel(target.model)) {
|
|
221
|
+
try {
|
|
222
|
+
const membership = await membershipWriteVerdict(prisma, target.model, target, args, principal.sub);
|
|
223
|
+
const membershipAuthority = membership.kind === 'decided' ? membership.verdict : 'unresolved';
|
|
224
|
+
return evaluateMutationAccess(principal, target, { ...facts, membershipAuthority });
|
|
225
|
+
}
|
|
226
|
+
catch (error) {
|
|
227
|
+
logger.error('[mutation-auth] membership authority could not be read; refusing as unresolved', {
|
|
228
|
+
mutation: target.fieldName,
|
|
229
|
+
error: error instanceof Error ? error.message : String(error),
|
|
230
|
+
});
|
|
231
|
+
return first;
|
|
232
|
+
}
|
|
233
|
+
}
|
|
216
234
|
if (first.reason === 'tenant_unresolved' && (isGovernedModel(target.model) || target.model === 'MandateVersion')) {
|
|
217
235
|
try {
|
|
218
236
|
const tenantScope = isGovernedModel(target.model)
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The database facts a user principal's OrgMembership or FundAssignment write
|
|
3
|
+
* is decided on (`src/auth/membership-write-authority.ts`).
|
|
4
|
+
*
|
|
5
|
+
* For each row the write reaches — the payload's row for a create, the row the
|
|
6
|
+
* caller's `where` addresses for an update or delete, both for an upsert — it
|
|
7
|
+
* reads the row's organization (and fund), and what the CALLER holds there:
|
|
8
|
+
* their OrgMembership role in that organization and, for a fund assignment,
|
|
9
|
+
* their FundAssignment role on that fund. Roles come from these rows, never
|
|
10
|
+
* from the token's claims, because the token's roles are the platform's
|
|
11
|
+
* persona for the caller and are themselves derived from these rows. Each
|
|
12
|
+
* organization's and fund's standing is read once per mutation.
|
|
13
|
+
*
|
|
14
|
+
* A row that does not exist (an addressed membership, or the fund a payload
|
|
15
|
+
* names) is decided as outside the caller's tenant, the same answer a row in
|
|
16
|
+
* another tenant gets, so the refusal says nothing about which ids exist.
|
|
17
|
+
* Arguments that identify no row at all (no `where`, a payload naming no
|
|
18
|
+
* organization, fund or member) are {@link MembershipFacts} `unresolvable`;
|
|
19
|
+
* a database failure propagates. The guard refuses on both.
|
|
20
|
+
*
|
|
21
|
+
* @module middleware/mutation-membership-facts
|
|
22
|
+
*/
|
|
23
|
+
import type { MutationTarget } from '../auth/mutation-authorization';
|
|
24
|
+
import { type MembershipModel, type MembershipVerdict } from '../auth/membership-write-authority';
|
|
25
|
+
type Where = Record<string, unknown>;
|
|
26
|
+
declare const MEMBERSHIP_SELECT: {
|
|
27
|
+
readonly organizationId: true;
|
|
28
|
+
readonly userId: true;
|
|
29
|
+
readonly role: true;
|
|
30
|
+
};
|
|
31
|
+
declare const ASSIGNMENT_SELECT: {
|
|
32
|
+
readonly fundId: true;
|
|
33
|
+
readonly userId: true;
|
|
34
|
+
readonly role: true;
|
|
35
|
+
readonly fund: {
|
|
36
|
+
readonly select: {
|
|
37
|
+
readonly organizationId: true;
|
|
38
|
+
};
|
|
39
|
+
};
|
|
40
|
+
};
|
|
41
|
+
declare const MEMBERSHIP_ROLE_SELECT: {
|
|
42
|
+
readonly role: true;
|
|
43
|
+
};
|
|
44
|
+
declare const ASSIGNMENT_ROLE_SELECT: {
|
|
45
|
+
readonly role: true;
|
|
46
|
+
};
|
|
47
|
+
declare const FUND_SELECT: {
|
|
48
|
+
readonly organizationId: true;
|
|
49
|
+
};
|
|
50
|
+
declare const ORGANIZATION_SELECT: {
|
|
51
|
+
readonly deletedAt: true;
|
|
52
|
+
readonly _count: {
|
|
53
|
+
readonly select: {
|
|
54
|
+
readonly members: true;
|
|
55
|
+
readonly funds: true;
|
|
56
|
+
};
|
|
57
|
+
};
|
|
58
|
+
};
|
|
59
|
+
/**
|
|
60
|
+
* The slice of the Prisma client the facts are read through. Declared
|
|
61
|
+
* structurally so the decision can be exercised against a fake; the guard
|
|
62
|
+
* passes the server's PrismaClient.
|
|
63
|
+
*/
|
|
64
|
+
export interface MembershipFactsPrisma {
|
|
65
|
+
orgMembership: {
|
|
66
|
+
findUnique(args: {
|
|
67
|
+
where: Where;
|
|
68
|
+
select: typeof MEMBERSHIP_SELECT | typeof MEMBERSHIP_ROLE_SELECT;
|
|
69
|
+
}): Promise<{
|
|
70
|
+
organizationId?: string;
|
|
71
|
+
userId?: string;
|
|
72
|
+
role: string;
|
|
73
|
+
} | null>;
|
|
74
|
+
};
|
|
75
|
+
fundAssignment: {
|
|
76
|
+
findUnique(args: {
|
|
77
|
+
where: Where;
|
|
78
|
+
select: typeof ASSIGNMENT_SELECT | typeof ASSIGNMENT_ROLE_SELECT;
|
|
79
|
+
}): Promise<{
|
|
80
|
+
fundId?: string;
|
|
81
|
+
userId?: string;
|
|
82
|
+
role: string;
|
|
83
|
+
fund?: {
|
|
84
|
+
organizationId: string;
|
|
85
|
+
};
|
|
86
|
+
} | null>;
|
|
87
|
+
};
|
|
88
|
+
fund: {
|
|
89
|
+
findUnique(args: {
|
|
90
|
+
where: {
|
|
91
|
+
id: string;
|
|
92
|
+
};
|
|
93
|
+
select: typeof FUND_SELECT;
|
|
94
|
+
}): Promise<{
|
|
95
|
+
organizationId: string;
|
|
96
|
+
} | null>;
|
|
97
|
+
};
|
|
98
|
+
organization: {
|
|
99
|
+
findUnique(args: {
|
|
100
|
+
where: {
|
|
101
|
+
id: string;
|
|
102
|
+
};
|
|
103
|
+
select: typeof ORGANIZATION_SELECT;
|
|
104
|
+
}): Promise<{
|
|
105
|
+
deletedAt: Date | null;
|
|
106
|
+
_count: {
|
|
107
|
+
members: number;
|
|
108
|
+
funds: number;
|
|
109
|
+
};
|
|
110
|
+
} | null>;
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
/** The outcome of {@link membershipWriteVerdict}. */
|
|
114
|
+
export type MembershipFacts = {
|
|
115
|
+
readonly kind: 'decided';
|
|
116
|
+
readonly verdict: MembershipVerdict;
|
|
117
|
+
} | {
|
|
118
|
+
readonly kind: 'unresolvable';
|
|
119
|
+
};
|
|
120
|
+
/**
|
|
121
|
+
* Decide a user principal's write to an entitlement row from the database.
|
|
122
|
+
* Every row the write reaches must be admitted; the first refusal is returned.
|
|
123
|
+
*
|
|
124
|
+
* @param prisma - A client exposing {@link MembershipFactsPrisma}.
|
|
125
|
+
* @param model - The entitlement model written.
|
|
126
|
+
* @param target - The classified root mutation (`one` cardinality).
|
|
127
|
+
* @param args - The mutation's arguments.
|
|
128
|
+
* @param callerId - The user principal's subject.
|
|
129
|
+
* @throws Whatever the database raised; the caller refuses on it.
|
|
130
|
+
*/
|
|
131
|
+
export declare function membershipWriteVerdict(prisma: MembershipFactsPrisma, model: MembershipModel, target: MutationTarget, args: Record<string, unknown>, callerId: string): Promise<MembershipFacts>;
|
|
132
|
+
export {};
|
|
133
|
+
//# sourceMappingURL=mutation-membership-facts.d.ts.map
|