:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
import { MakeLogicType, actions, afterMount, connect, kea, listeners, path, reducers } from 'kea'
import { loaders } from 'kea-loaders'
import api from 'lib/api'
import { lemonToast } from 'lib/lemon-ui/LemonToast/LemonToast'
import { teamLogic } from 'scenes/teamLogic'
import { userLogic } from 'scenes/userLogic'
import { ApprovalPolicy, AvailableFeature } from '~/types'
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface approvalPoliciesLogicValues {
hasAvailableFeature: (feature: AvailableFeature, currentUsage?: number | undefined) => boolean // userLogic
policies: ApprovalPolicy[]
policiesLoading: boolean
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface approvalPoliciesLogicActions {
createPolicy: (policy: Partial<ApprovalPolicy>) => {
policy: Partial<ApprovalPolicy>
}
deletePolicy: (id: string) => {
id: string
}
loadPolicies: () => {
value: true
}
loadPoliciesFailure: (
error: string,
errorObject?: any
) => {
error: string
errorObject?: any
}
loadPoliciesSuccess: (
policies: ApprovalPolicy[],
payload?: {
value: true
}
) => {
policies: ApprovalPolicy[]
payload?: {
value: true
}
}
updatePolicy: (
id: string,
policy: Partial<ApprovalPolicy>
) => {
id: string
policy: Partial<ApprovalPolicy>
}
}
export type approvalPoliciesLogicType = MakeLogicType<approvalPoliciesLogicValues, approvalPoliciesLogicActions>
export const approvalPoliciesLogic = kea<approvalPoliciesLogicType>([
path(['scenes', 'settings', 'organization', 'Approvals', 'approvalPoliciesLogic']),
connect(() => ({
values: [userLogic, ['hasAvailableFeature']],
})),
actions({
loadPolicies: true,
createPolicy: (policy: Partial<ApprovalPolicy>) => ({ policy }),
updatePolicy: (id: string, policy: Partial<ApprovalPolicy>) => ({ id, policy }),
deletePolicy: (id: string) => ({ id }),
}),
loaders(({ values }) => ({
policies: [
[] as ApprovalPolicy[],
{
loadPolicies: async () => {
const teamId = teamLogic.values.currentTeamId
if (!teamId || !values.hasAvailableFeature(AvailableFeature.APPROVALS)) {
return []
}
// nosemgrep: prefer-codegen-api -- Legacy raw API call with a hand-written URL and an unchecked response type. Use approvalPoliciesList() from 'products/platform_features/frontend/generated/api' instead.
const response = await api.get<{ results: ApprovalPolicy[] }>(
`api/projects/${teamId}/approval_policies/`
)
return response.results || []
},
},
],
})),
reducers({
policies: {
updatePolicy: (state, { id, policy }) =>
state.map((p) => (p.id === id ? { ...p, ...policy } : p)) as ApprovalPolicy[],
deletePolicy: (state, { id }) => state.filter((p) => p.id !== id),
},
}),
listeners(({ actions }) => ({
createPolicy: async ({ policy }) => {
try {
const teamId = teamLogic.values.currentTeamId
if (!teamId) {
throw new Error('No team selected')
}
// nosemgrep: prefer-codegen-api -- Legacy raw API call with a hand-written URL and an unchecked response type. Use approvalPoliciesCreate() from 'products/platform_features/frontend/generated/api' instead.
await api.create(`api/projects/${teamId}/approval_policies/`, policy)
lemonToast.success('Approval policy created')
actions.loadPolicies()
} catch (error: any) {
lemonToast.error(error.detail || 'Failed to create approval policy')
}
},
updatePolicy: async ({ id, policy }) => {
try {
const teamId = teamLogic.values.currentTeamId
if (!teamId) {
throw new Error('No team selected')
}
// nosemgrep: prefer-codegen-api -- Legacy raw API call with a hand-written URL and an unchecked response type. Use approvalPoliciesPartialUpdate() from 'products/platform_features/frontend/generated/api' instead.
await api.update(`api/projects/${teamId}/approval_policies/${id}/`, policy)
lemonToast.success('Approval policy updated')
actions.loadPolicies()
} catch (error: any) {
lemonToast.error(error.detail || 'Failed to update approval policy')
}
},
deletePolicy: async ({ id }) => {
try {
const teamId = teamLogic.values.currentTeamId
if (!teamId) {
throw new Error('No team selected')
}
// nosemgrep: prefer-codegen-api -- Legacy raw API call with a hand-written URL and an unchecked response type. approvalPoliciesDestroy() from 'products/platform_features/frontend/generated/api' serves this route, but its generated types do not describe this call yet, so fix the endpoint's OpenAPI schema first.
await api.delete(`api/projects/${teamId}/approval_policies/${id}/`)
lemonToast.success('Approval policy deleted')
actions.loadPolicies()
} catch (error: any) {
lemonToast.error(error.detail || 'Failed to delete approval policy')
}
},
})),
afterMount(({ actions }) => {
actions.loadPolicies()
}),
])