All files / src/stores user-group.store.ts

0% Statements 0/138
0% Branches 0/1
0% Functions 0/1
0% Lines 0/138

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                                                                                                                                                                                                                                                                                                                                   
import { defineStore } from 'pinia'
import { userGroupApi } from '@/api/user-group.api'
import type { UserGroup, UserGroupCreate, UserGroupUpdate } from '@/types'
 
export const useUserGroupStore = defineStore('userGroup', {
  state: () => ({
    userGroups: [] as UserGroup[],
    currentUserGroup: null as UserGroup | null,
    isLoading: false,
    error: null as string | null,
  }),
 
  getters: {
    getUserGroupById: (state) => (id: string) => {
      return state.userGroups.find((g) => g.userGroupId === id)
    },
    
    getUserGroupsByCourse: (state) => (courseId: string) => {
      return state.userGroups.filter((g) => g.courseIds?.includes(courseId))
    },
  },
 
  actions: {
    async fetchUserGroups() {
      this.isLoading = true
      this.error = null
      
      try {
        const response = await userGroupApi.list()
        this.userGroups = response.data
      } catch (err: any) {
        this.error = err.response?.data?.detail || 'Failed to fetch user groups'
        throw err
      } finally {
        this.isLoading = false
      }
    },
 
    async fetchUserGroupById(id: string) {
      this.isLoading = true
      this.error = null
      
      try {
        const response = await userGroupApi.getById(id)
        this.currentUserGroup = response.data
        const index = this.userGroups.findIndex((g) => g.userGroupId === id)
        if (index !== -1) {
          this.userGroups[index] = response.data
        }
        
        return response.data
      } catch (err: any) {
        this.error = err.response?.data?.detail || 'Failed to fetch user group'
        throw err
      } finally {
        this.isLoading = false
      }
    },
 
    async createUserGroup(data: UserGroupCreate) {
      this.isLoading = true
      this.error = null
      
      try {
        const response = await userGroupApi.create(data)
        this.userGroups.push(response.data)
        return response.data
      } catch (err: any) {
        this.error = err.response?.data?.detail || 'Failed to create user group'
        throw err
      } finally {
        this.isLoading = false
      }
    },
 
    async updateUserGroup(id: string, data: UserGroupUpdate) {
      this.isLoading = true
      this.error = null
      
      try {
        const response = await userGroupApi.update(id, data)
        const index = this.userGroups.findIndex((g) => g.userGroupId === id)
        if (index !== -1) {
          this.userGroups[index] = response.data
        }
        if (this.currentUserGroup?.userGroupId === id) {
          this.currentUserGroup = response.data
        }
        
        return response.data
      } catch (err: any) {
        this.error = err.response?.data?.detail || 'Failed to update user group'
        throw err
      } finally {
        this.isLoading = false
      }
    },
 
    async deleteUserGroup(id: string) {
      this.isLoading = true
      this.error = null
      
      try {
        await userGroupApi.delete(id)
        this.userGroups = this.userGroups.filter((g) => g.userGroupId !== id)
        if (this.currentUserGroup?.userGroupId === id) {
          this.currentUserGroup = null
        }
      } catch (err: any) {
        this.error = err.response?.data?.detail || 'Failed to delete user group'
        throw err
      } finally {
        this.isLoading = false
      }
    },
 
    async addMember(userGroupId: string, userId: string) {
      this.isLoading = true
      this.error = null
      
      try {
        const response = await userGroupApi.addMember(userGroupId, userId)
        const index = this.userGroups.findIndex((g) => g.userGroupId === userGroupId)
        if (index !== -1) {
          this.userGroups[index] = response.data
        }
        
        return response.data
      } catch (err: any) {
        this.error = err.response?.data?.detail || 'Failed to add member'
        throw err
      } finally {
        this.isLoading = false
      }
    },
 
    async removeMember(userGroupId: string, userId: string) {
      this.isLoading = true
      this.error = null
      
      try {
        const response = await userGroupApi.removeMember(userGroupId, userId)
        const index = this.userGroups.findIndex((g) => g.userGroupId === userGroupId)
        if (index !== -1) {
          this.userGroups[index] = response.data
        }
        
        return response.data
      } catch (err: any) {
        this.error = err.response?.data?.detail || 'Failed to remove member'
        throw err
      } finally {
        this.isLoading = false
      }
    },
 
    clearError() {
      this.error = null
    },
  },
})