All files / src/stores entities.ts

92.18% Statements 59/64
83.87% Branches 26/31
88.88% Functions 8/9
93.54% Lines 58/62

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200                                                                                                7x 115x 115x 115x 115x 115x 115x     14x 10x 10x 10x     14x 2x     12x 12x 12x 12x 12x 2x     12x 9x     9x   3x 3x       3x     6x 4x   2x   6x 6x     3x 3x 3x   3x   12x         3x         3x 1x               1x 1x     2x 2x   2x 2x                           2x 2x     2x   2x       3x 3x     3x 3x         3x       3x       1x     1x     1x         115x                              
import { defineStore } from 'pinia'
import { ref } from 'vue'
import api from '@/services/api'
import { ENTITY_SCHEMA, type WorkflowDefinition } from '@/schemas/entity-schema'
 
export interface EntityField {
  fieldId?: string // Stable internal identifier for the field (auto-generated if not provided)
  name: string | Record<string, string>
  type?:
    | 'text'
    | 'number'
    | 'email'
    | 'password'
    | 'select'
    | 'date'
    | 'boolean'
    | 'textarea'
    | 'image'
    | 'image_set'
    | 'workflow'
  length?: number
  mandatory?: boolean
  disabled?: boolean
  options?: string[] | { value: string; label: string }[]
  placeholder?: string
  defaultValue?: any
  maxImages?: number // For image_set: maximum number of images
  workflowDefinition?: WorkflowDefinition // For workflow type fields
}
 
export interface Entity {
  id: string
  tenantId: string
  visibility: 'flow-members' | 'restricted' | 'public'
  isPublic: string // Deprecated, kept for backward compatibility
  name: string | Record<string, string>
  description?: string | Record<string, string>
  fields: EntityField[]
  defaultLanguage?: string
  enabledLanguages?: string[]
  picture?: string
  icon?: string
  publicPermissions?: string[]
  createdBy: string
  createdAt: string
  updatedAt: string
}
 
export const useEntitiesStore = defineStore('entities', () => {
  const entities = ref<Entity[]>([])
  const currentEntity = ref<Entity | null>(null)
  const loading = ref(false)
  const error = ref<string | null>(null)
  const nextToken = ref<string | null>(null)
  const hasMore = ref(true)
 
  async function fetchEntities(reset = true) {
    if (reset) {
      entities.value = []
      nextToken.value = null
      hasMore.value = true
    }
 
    if (loading.value || (!reset && !hasMore.value)) {
      return
    }
 
    loading.value = true
    error.value = null
    try {
      const params: any = { limit: 30 }
      if (!reset && nextToken.value) {
        params.nextToken = nextToken.value
      }
 
      const response = await api.get('/v1/entities', { params })
      const data = response.data
 
      // Handle both old format (array) and new format (object with items)
      if (Array.isArray(data)) {
        // Old format - no pagination
        if (reset) {
          entities.value = data
        } else E{
          entities.value = [...entities.value, ...data]
        }
        hasMore.value = false
      } else {
        // New format with pagination
        if (reset) {
          entities.value = data.items
        } else {
          entities.value = [...entities.value, ...data.items]
        }
        nextToken.value = data.nextToken
        hasMore.value = data.hasMore
      }
    } catch (err: any) {
      error.value = err.response?.data?.error || err.message || 'Failed to fetch entities'
      Eif (reset) {
        entities.value = []
      }
      throw err
    } finally {
      loading.value = false
    }
  }
 
  async function loadMore() {
    await fetchEntities(false)
  }
 
  async function fetchEntity(id: string) {
    // Check if requesting system entity schema
    if (id === 'entity') {
      const systemEntity = {
        ...ENTITY_SCHEMA,
        tenantId: 'SYSTEM',
        isPublic: 'false', // Backward compatibility
        createdBy: 'SYSTEM',
        createdAt: '2026-01-01T00:00:00Z',
        updatedAt: '2026-01-01T00:00:00Z'
      } as any as Entity // Cast to Entity type (system schema has extended field types)
      currentEntity.value = systemEntity
      return systemEntity
    }
 
    const response = await api.get(`/v1/entities/${id}`)
    const entity = response.data
 
    currentEntity.value = entity
    return entity
  }
 
  async function createEntity(data: {
    name: string | Record<string, string>
    fields: any[]
    visibility?: 'flow-members' | 'restricted' | 'public'
    isPublic?: boolean // Deprecated, kept for backward compatibility
    picture?: string
    icon?: string
    publicPermissions?: string[]
    defaultLanguage?: string
    enabledLanguages?: string[]
  }) {
    const response = await api.post('/v1/entities', data)
    const newEntity = response.data
 
    // Add new entity to the beginning of the list
    entities.value.unshift(newEntity)
 
    return newEntity
  }
 
  async function updateEntity(id: string, data: Partial<Entity>) {
    const response = await api.put(`/v1/entities/${id}`, data)
    const updatedEntity = response.data
 
    // Replace the entity in the array
    const index = entities.value.findIndex((e) => e.id === id)
    Iif (index !== -1) {
      entities.value[index] = updatedEntity
    }
 
    // Update currentEntity if it's the one being updated
    Iif (currentEntity.value?.id === id) {
      currentEntity.value = updatedEntity
    }
 
    return updatedEntity
  }
 
  async function deleteEntity(id: string) {
    await api.delete(`/v1/entities/${id}`)
 
    // Remove entity from local list
    entities.value = entities.value.filter((e) => e.id !== id)
 
    // Clear currentEntity if it's the one being deleted
    Iif (currentEntity.value?.id === id) {
      currentEntity.value = null
    }
  }
 
  return {
    entities,
    currentEntity,
    loading,
    error,
    nextToken,
    hasMore,
    fetchEntities,
    loadMore,
    fetchEntity,
    createEntity,
    updateEntity,
    deleteEntity
  }
})