Supabase Removals Frontend

This commit is contained in:
2026-02-14 12:48:59 +01:00
parent 053f184a33
commit 55699da42c
28 changed files with 135 additions and 222 deletions

View File

@@ -1,152 +1,117 @@
import {defineStore} from 'pinia'
import OneSignal from "onesignal-cordova-plugin";
import {Capacitor} from "@capacitor/core";
// @ts-ignore
export const useProfileStore = defineStore('profile', () => {
import { defineStore } from "pinia"
import OneSignal from "onesignal-cordova-plugin"
import { Capacitor } from "@capacitor/core"
const supabase = useSupabaseClient()
export const useProfileStore = defineStore("profile", () => {
const auth = useAuthStore()
const dataStore = useDataStore()
const user = useSupabaseUser()
const toast = useToast()
const router = useRouter()
const tempStore = useTempStore()
const loaded = ref(false)
const showProfileSelection = ref(false)
const ownTenant = ref({
calendarConfig: {
eventTypes: []
},
timeConfig: {
timeTypes: []
},
tags: {
documents: [] ,
products: []
},
calendarConfig: { eventTypes: [] },
timeConfig: { timeTypes: [] },
tags: { documents: [], products: [] },
measures: []
})
const profiles = ref([])
const ownProfiles = ref([])
const activeProfile = ref([])
const activeProfile = ref(null)
const tenants = ref([])
const currentTenant = ref(null)
const syncFromAuth = () => {
currentTenant.value = auth.activeTenant || null
activeProfile.value = auth.profile || null
tenants.value = auth.tenants || []
}
async function initializeData (userId) {
let profileconnections = (await supabase.from("profileconnections").select()).data
let profiles = (await supabase.from("profiles").select("*, role(*)")).data
let activeProfileConnection = profileconnections.find(i => i.active)
if(activeProfileConnection) {
if(!false) {
toast.add({title: 'Aktives Profil ausgewählt'})
}
console.log("Active Profile selected")
activeProfile.value = profiles.find(i => i.id === activeProfileConnection.profile_id)
currentTenant.value = activeProfile.value.tenant
if(Capacitor.getPlatform() === "ios") {
OneSignal.initialize("1295d5ff-28f8-46a6-9c62-fe5d090016d7");
OneSignal.Location.setShared(false)
OneSignal.Notifications.requestPermission();
OneSignal.login(activeProfileConnection.user_id)
}
async function initializeData() {
await auth.fetchMe()
syncFromAuth()
if (activeProfile.value?.temp_config) {
tempStore.setStoredTempConfig(activeProfile.value.temp_config)
}
if (currentTenant.value) {
await fetchData()
await dataStore.fetchData()
if (Capacitor.getPlatform() === "ios" && activeProfile.value?.user_id) {
OneSignal.initialize("1295d5ff-28f8-46a6-9c62-fe5d090016d7")
OneSignal.Location.setShared(false)
OneSignal.Notifications.requestPermission()
OneSignal.login(activeProfile.value.user_id)
}
} else {
toast.add({title: 'Kein aktives Profil', color: 'orange'})
await fetchOwnProfiles()
await fetchTenants()
toast.add({ title: "Kein aktiver Tenant", color: "orange" })
showProfileSelection.value = true
}
}
async function changeProfile(newActiveProfileId) {
async function changeProfile(newActiveTenantId) {
loaded.value = false
let profileconnections = (await supabase.from("profileconnections").select()).data
let oldActiveProfileConnection = profileconnections.find(i => i.active)
const {error} = await supabase.from("profileconnections").update({active: true}).eq("profile_id", newActiveProfileId)
if(error) {
console.log(error)
} else {
if(oldActiveProfileConnection){
const {error} = await supabase.from("profileconnections").update({active: false}).eq("profile_id", oldActiveProfileConnection.profile_id)
}
reloadNuxtApp({
path:"/",
ttl: 10000
})
}
await auth.switchTenant(String(newActiveTenantId))
}
async function fetchData () {
async function fetchData() {
await fetchOwnProfiles()
await fetchProfiles()
await fetchTenants()
await fetchOwnTenant()
loaded.value = true
console.log("Finished Loading")
}
function clearStore () {
function clearStore() {
loaded.value = false
ownTenant.value = {}
ownTenant.value = {}
profiles.value = []
ownProfiles.value = []
tenants.value = []
}
async function fetchOwnTenant () {
ownTenant.value = (await supabase.from("tenants").select().eq('id', currentTenant.value).single()).data
async function fetchOwnTenant() {
syncFromAuth()
ownTenant.value = auth.activeTenantData || ownTenant.value
}
async function fetchProfiles () {
profiles.value = (await supabase.from("profiles").select().eq("tenant",currentTenant.value).order("lastName")).data
async function fetchProfiles() {
try {
const res = await useNuxtApp().$api("/api/tenant/profiles")
profiles.value = res?.data || []
} catch (e) {
profiles.value = activeProfile.value ? [activeProfile.value] : []
}
}
async function fetchOwnProfiles () {
let profiles = (await supabase.from("profiles").select().order("tenant")).data
let conns = (await supabase.from("profileconnections").select()).data.map(i => i.profile_id)
ownProfiles.value = profiles.filter(i => conns.includes(i.id))
async function fetchOwnProfiles() {
ownProfiles.value = profiles.value
}
async function fetchTenants () {
tenants.value = (await supabase.from("tenants").select().order("id",{ascending: true})).data
async function fetchTenants() {
syncFromAuth()
}
const getOwnProfile = computed(() => {
return profiles.value.find(i => i.id === user.value.id)
const getOwnProfile = computed(() => {
return activeProfile.value
})
const getProfileById = computed(() => (itemId) => {
return profiles.value.find(item => item.id === itemId)
return profiles.value.find((item) => item.id === itemId || item.user_id === itemId)
})
return {
//General
currentTenant,
loaded,
showProfileSelection,
ownTenant,
initializeData,
changeProfile,
//Data
profiles,
ownProfiles,
activeProfile,
@@ -157,6 +122,4 @@ export const useProfileStore = defineStore('profile', () => {
getOwnProfile,
getProfileById
}
})
})