Skip to content

: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.

10.4 KB
Raw Blame
import { MakeLogicType, actions, afterMount, connect, kea, key, listeners, path, props, reducers, selectors } from 'kea'
import { loaders } from 'kea-loaders'

import api from 'lib/api'
import { lemonToast } from 'lib/lemon-ui/LemonToast/LemonToast'

import { EventDefinition, SchemaEnforcementMode } from '~/types'

import type { Definition } from '../../../types'
import { definitionLogic } from '../definition/definitionLogic'
import type { SetDefinitionProps } from '../definition/definitionLogic'
import { type SchemaPropertyGroup, schemaManagementLogic } from '../schema/schemaManagementLogic'

export type { SchemaPropertyGroup }

function getErrorMessage(error: any, defaultMessage: string): string {
    if (error.detail) {
        const detail = error.detail

        // Handle "property group is already added to this event schema"
        if (
            typeof detail === 'string' &&
            (detail.includes('already added to this event schema') || detail.includes('already exists'))
        ) {
            // Extract property group name if available
            const nameMatch = detail.match(/Property group '([^']+)'/)
            if (nameMatch) {
                return `Property group "${nameMatch[1]}" is already added to this event`
            }
            return detail
        }

        // Handle team mismatch errors
        if (typeof detail === 'string' && detail.includes('must belong to the same team')) {
            return 'Property group must belong to the same team as the event'
        }

        // Return the detail if it's a user-friendly string
        if (typeof detail === 'string' && !detail.includes('IntegrityError') && !detail.includes('Key (')) {
            return detail
        }
    }

    if (error.message && !error.message.includes('IntegrityError')) {
        return error.message
    }

    return defaultMessage
}

export interface EventSchema {
    id: string
    event_definition: string
    property_group_id: string
    property_group: SchemaPropertyGroup
    created_at: string
    updated_at: string
}

export interface EventDefinitionSchemaLogicProps {
    eventDefinitionId: string
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface eventDefinitionSchemaLogicValues {
    definition: Definition // definitionLogic
    allPropertyGroups: SchemaPropertyGroup[]
    allPropertyGroupsLoading: boolean
    availablePropertyGroups: SchemaPropertyGroup[]
    eventSchemas: EventSchema[]
    eventSchemasLoading: boolean
    schemaEnforcementMode: SchemaEnforcementMode
    schemaEnforcementModeUpdating: boolean
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface eventDefinitionSchemaLogicActions {
    setDefinition: (
        definition: Partial<Definition>,
        options?: SetDefinitionProps | undefined
    ) => {
        definition: Partial<Definition>
        options: SetDefinitionProps
    } // definitionLogic
    createPropertyGroupSuccess: (
        propertyGroups: any[],
        payload?: Partial<SchemaPropertyGroup> | undefined
    ) => {
        payload?: Partial<SchemaPropertyGroup>
        propertyGroups: any[]
    } // schemaManagementLogic
    updatePropertyGroupSuccess: (
        propertyGroups: any[],
        payload?:
            | {
                  data: Partial<SchemaPropertyGroup>
                  id: string
              }
            | undefined
    ) => {
        payload?: {
            data: Partial<SchemaPropertyGroup>
            id: string
        }
        propertyGroups: any[]
    } // schemaManagementLogic
    addPropertyGroup: (propertyGroupId: string) => {
        propertyGroupId: string
    }
    loadAllPropertyGroups: () => any
    loadAllPropertyGroupsFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    loadAllPropertyGroupsSuccess: (
        allPropertyGroups: SchemaPropertyGroup[],
        payload?: any
    ) => {
        allPropertyGroups: SchemaPropertyGroup[]
        payload?: any
    }
    loadEventSchemas: () => any
    loadEventSchemasFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    loadEventSchemasSuccess: (
        eventSchemas: EventSchema[],
        payload?: any
    ) => {
        eventSchemas: EventSchema[]
        payload?: any
    }
    removePropertyGroup: (eventSchemaId: string) => {
        eventSchemaId: string
    }
    setSchemaEnforcementModeUpdating: (updating: boolean) => {
        updating: boolean
    }
    updateSchemaEnforcementMode: (mode: SchemaEnforcementMode) => {
        mode: SchemaEnforcementMode
    }
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface eventDefinitionSchemaLogicMeta {
    key: string
    __keaTypeGenInternalSelectorTypes: {
        schemaEnforcementMode: (definition: Definition) => SchemaEnforcementMode
        availablePropertyGroups: (
            allPropertyGroups: SchemaPropertyGroup[],
            eventSchemas: EventSchema[]
        ) => SchemaPropertyGroup[]
    }
}

export type eventDefinitionSchemaLogicType = MakeLogicType<
    eventDefinitionSchemaLogicValues,
    eventDefinitionSchemaLogicActions,
    EventDefinitionSchemaLogicProps,
    eventDefinitionSchemaLogicMeta
>

export const eventDefinitionSchemaLogic = kea<eventDefinitionSchemaLogicType>([
    path(['scenes', 'data-management', 'events', 'eventDefinitionSchemaLogic']),
    props({} as EventDefinitionSchemaLogicProps),
    key((props) => props.eventDefinitionId),
    connect((props: EventDefinitionSchemaLogicProps) => ({
        values: [definitionLogic({ id: props.eventDefinitionId }), ['definition']],
        actions: [
            definitionLogic({ id: props.eventDefinitionId }),
            ['setDefinition'],
            schemaManagementLogic({ key: `event-${props.eventDefinitionId}` }),
            ['updatePropertyGroupSuccess', 'createPropertyGroupSuccess'],
        ],
    })),
    actions({
        addPropertyGroup: (propertyGroupId: string) => ({ propertyGroupId }),
        removePropertyGroup: (eventSchemaId: string) => ({ eventSchemaId }),
        updateSchemaEnforcementMode: (mode: SchemaEnforcementMode) => ({ mode }),
        setSchemaEnforcementModeUpdating: (updating: boolean) => ({ updating }),
    }),
    loaders(({ props }) => ({
        eventSchemas: {
            __default: [] as EventSchema[],
            loadEventSchemas: async () => {
                const response = await api.eventSchemas.list(props.eventDefinitionId)
                return response.results || []
            },
        },
        allPropertyGroups: {
            __default: [] as SchemaPropertyGroup[],
            loadAllPropertyGroups: async () => {
                const response = await api.schemaPropertyGroups.list()
                return response.results || []
            },
        },
    })),
    reducers({
        eventSchemas: [
            [] as EventSchema[],
            {
                removePropertyGroup: (state, { eventSchemaId }) =>
                    state.filter((schema: EventSchema) => schema.id !== eventSchemaId),
            },
        ],
        schemaEnforcementModeUpdating: [
            false,
            {
                setSchemaEnforcementModeUpdating: (_, { updating }) => updating,
            },
        ],
    }),
    selectors({
        schemaEnforcementMode: [
            (s) => [s.definition],
            (definition: import('~/types').Definition): SchemaEnforcementMode =>
                (definition as EventDefinition).enforcement_mode ?? SchemaEnforcementMode.Allow,
        ],
        availablePropertyGroups: [
            (s) => [s.allPropertyGroups, s.eventSchemas],
            (allPropertyGroups: SchemaPropertyGroup[], eventSchemas: EventSchema[]): SchemaPropertyGroup[] => {
                const usedGroupIds = new Set(eventSchemas.map((schema: EventSchema) => schema.property_group_id))
                return allPropertyGroups.filter((group: SchemaPropertyGroup) => !usedGroupIds.has(group.id))
            },
        ],
    }),
    listeners(({ actions, props }) => ({
        addPropertyGroup: async ({ propertyGroupId }) => {
            try {
                await api.eventSchemas.create({
                    event_definition: props.eventDefinitionId,
                    property_group_id: propertyGroupId,
                })
                await actions.loadEventSchemas()
                lemonToast.success('Property group added to event schema')
            } catch (error: any) {
                const errorMessage = getErrorMessage(error, 'Failed to add property group')
                lemonToast.error(errorMessage)
            }
        },
        removePropertyGroup: async ({ eventSchemaId }) => {
            try {
                await api.eventSchemas.delete(eventSchemaId)
                lemonToast.success('Property group removed from event schema')
            } catch (error: any) {
                const errorMessage = getErrorMessage(error, 'Failed to remove property group')
                lemonToast.error(errorMessage)
            }
        },
        updateSchemaEnforcementMode: async ({ mode }) => {
            actions.setSchemaEnforcementModeUpdating(true)
            try {
                await api.eventDefinitions.update({
                    eventDefinitionId: props.eventDefinitionId,
                    eventDefinitionData: { enforcement_mode: mode },
                })
                actions.setDefinition({ enforcement_mode: mode }, { merge: true })
                if (mode === SchemaEnforcementMode.Reject) {
                    lemonToast.success('Schema enforcement enabled. Events that fail validation will be rejected.')
                } else {
                    lemonToast.success('Schema enforcement disabled. All events will be accepted.')
                }
            } catch (error: any) {
                const errorMessage = getErrorMessage(error, 'Failed to update schema enforcement mode')
                lemonToast.error(errorMessage)
            } finally {
                actions.setSchemaEnforcementModeUpdating(false)
            }
        },
        updatePropertyGroupSuccess: async () => {
            await actions.loadEventSchemas()
        },
        createPropertyGroupSuccess: async () => {
            await actions.loadEventSchemas()
        },
    })),
    afterMount(({ actions }) => {
        actions.loadEventSchemas()
        actions.loadAllPropertyGroups()
    }),
])