Introduced ProfileStore

Corrected All Links to DataStore
This commit is contained in:
2024-12-21 22:33:42 +01:00
parent 813944fc23
commit b465f4a75a
64 changed files with 508 additions and 959 deletions

View File

@@ -18,6 +18,7 @@ import unit from "~/components/columnRenderings/unit.vue";
export const useDataStore = defineStore('data', () => {
const supabase = useSupabaseClient()
const profileStore = useProfileStore()
const user = useSupabaseUser()
const toast = useToast()
const router = useRouter()
@@ -477,28 +478,6 @@ export const useDataStore = defineStore('data', () => {
})
const loaded = ref(false)
const showProfileSelection = ref(false)
const ownTenant = ref({
calendarConfig: {
eventTypes: []
},
timeConfig: {
timeTypes: []
},
tags: {
documents: [] ,
products: []
},
measures: []
})
const profiles = ref([])
const ownProfiles = ref([])
const activeProfile = ref([])
const tenants = ref([])
const currentTenant = ref(null)
const events = ref([])
const customers = ref([])
const tasks = ref([])
@@ -520,7 +499,6 @@ export const useDataStore = defineStore('data', () => {
const bankstatements = ref([])
const bankrequisitions = ref([])
const historyItems = ref([])
const numberRanges = ref([])
const notifications = ref([])
const absencerequests = ref([])
const accounts = ref([])
@@ -538,126 +516,8 @@ export const useDataStore = defineStore('data', () => {
const servicecategories =ref([])
const resources =ref([])
/*const rights = ref({
createUser: {label: "Benutzer erstellen"},
modifyUser: {label: "Benutzer bearbeiten"},
deactivateUser: {label: "Benutzer sperren"},
createProject: {label: "Projekt erstellen"},
viewOwnProjects: {label: "Eigene Projekte sehen"},
viewAllProjects: {label: "Alle Projekte sehen"},
createTask: {label: "Aufgabe erstellen"},
viewOwnTasks: {label:"Eigene Aufgaben sehen"},
viewAllTasks: {label: "Alle Aufgaben sehen"},
trackOwnTime: {label:"Eigene Zeite erfassen"},
createOwnTime: {label:"Eigene Zeiten erstellen"},
createTime: {label:"Zeiten erstellen"},
viewOwnTimes: {label:"Eigene Zeiten anzeigen"},
viewTimes: {label:"Zeiten anzeigen"},
})*/
/*const roles = ref([
{
key: "tenantAdmin",
label: "Firmenadministrator",
rights: [
...Object.keys(rights.value)
]
},
{
key:"worker",
label: "Monteur",
rights: [
"viewOwnProjects",
"createTasks",
"viewOwnTasks",
"viewOwnTimes",
"createOwnTime"
]
},
{
key:"manager",
label: "Vorarbeiter",
rights: [
"createProjects",
"viewOwnProjects",
"createTasks",
"viewOwnTasks",
]
},
{
key:"booker",
label: "Buchhalter",
rights: [
"createTasks",
"viewOwnTasks",
"createTime",
"viewAllTimes"
]
}
])*/
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) {
activeProfile.value = profiles.find(i => i.id === activeProfileConnection.profile_id)
currentTenant.value = activeProfile.value.tenant
await fetchData()
//loaded.value = true
} else {
await fetchOwnProfiles()
await fetchTenants()
showProfileSelection.value = true
}
}
async function changeProfile(newActiveProfileId) {
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 clearStore()
await fetchData()
router.push("/")
loaded.value = true*/
}
}
async function fetchData () {
await fetchOwnProfiles()
await fetchProfiles()
await fetchTenants()
await fetchOwnTenant()
//loaded.value = true
await fetchDocuments()
await fetchDocuments()
await fetchEvents()
await fetchTasks()
await fetchProjects()
@@ -678,7 +538,6 @@ export const useDataStore = defineStore('data', () => {
await fetchBankStatements()
await fetchBankRequisitions()
await fetchHistoryItems()
await fetchNumberRanges()
await fetchNotifications()
await fetchAbsenceRequests()
await fetchAccounts()
@@ -695,18 +554,9 @@ export const useDataStore = defineStore('data', () => {
await fetchServices()
await fetchServiceCategories()
await fetchResources()
loaded.value = true
}
function clearStore () {
console.log("Clear")
loaded.value = false
ownTenant.value = {}
profiles.value = []
ownProfiles.value = []
tenants.value = []
events.value= []
customers.value= []
tasks.value= []
@@ -728,7 +578,6 @@ export const useDataStore = defineStore('data', () => {
bankstatements.value= []
bankrequisitions.value= []
historyItems.value = []
numberRanges.value = []
notifications.value = []
absencerequests.value = []
accounts.value = []
@@ -747,16 +596,6 @@ export const useDataStore = defineStore('data', () => {
resources.value = []
}
/*function hasRight (right) {
const role = profiles.value.find(i => i.id === activeProfile.value.id).role
const grantedRights = roles.value.find(i => i.key === role).rights
if(grantedRights.includes(right)){
return true
} else return false
}*/
var deepDiffMapper = function () {
return {
VALUE_CREATED: 'created',
@@ -831,8 +670,6 @@ export const useDataStore = defineStore('data', () => {
}
}();
const generateHistoryItems = async (dataType,newData, oldData=null) => {
//console.log(oldData)
//console.log(newData)
@@ -1022,7 +859,7 @@ export const useDataStore = defineStore('data', () => {
createdBy: activeProfile.value.id,
oldVal: prop.data.o,
newVal: prop.data.n,
tenant: currentTenant.value
tenant: profileStore.currentTenant
}
historyItem[dataTypes[dataType].historyItemHolder] = newData.id
@@ -1056,7 +893,7 @@ export const useDataStore = defineStore('data', () => {
let historyItem = {
text: `${dataTypes[dataType].labelSingle} erstellt`,
createdBy: activeProfile.value.id,
tenant: currentTenant.value
tenant: profileStore.currentTenant
}
historyItem[dataTypes[dataType].historyItemHolder] = newData.id
@@ -1075,38 +912,14 @@ export const useDataStore = defineStore('data', () => {
}
}
//Realtime Update
/*const channelA = supabase
.channel('schema-db-changes')
.on(
'postgres_changes',
{
event: '*',
schema: 'public',
},
(payload) => {
//console.log(payload)
/!*if(payload.eventType === 'INSERT') {
const c = payload.table + '.value.push(' + JSON.stringify(payload.new) + ')'
eval(c)
} else if(payload.eventType === 'UPDATE'){
const c = payload.table + '.value[' + payload.table + '.value.findIndex(i => i.id === ' + JSON.stringify(payload.old.id) + ')] = ' + JSON.stringify(payload.new)
eval(c)
}*!/
}
)
.subscribe()*/
async function createNewItem (dataType,data){
if(typeOf(data) === 'object') {
data = {...data, tenant: currentTenant.value}
data = {...data, tenant: profileStore.currentTenant}
} else if(typeOf(data) === 'array') {
data.map(i => {
return {
...i,
tenant: currentTenant.value
tenant: profileStore.currentTenant
}
})
}
@@ -1226,7 +1039,7 @@ export const useDataStore = defineStore('data', () => {
const {data, error} = await supabase
.storage
.from("files")
.upload(`${currentTenant.value}/${file.name}`, file, {upsert: upsert})
.upload(`${profileStore.currentTenant}/${file.name}`, file, {upsert: upsert})
if (error) {
console.log(error)
@@ -1246,7 +1059,7 @@ export const useDataStore = defineStore('data', () => {
} else if (data) {
const returnPath = data.path
documentsToInsert.push({...formData, path: returnPath, tenant: currentTenant.value})
documentsToInsert.push({...formData, path: returnPath, tenant: profileStore.currentTenant})
}
//console.log(data)
@@ -1285,95 +1098,72 @@ export const useDataStore = defineStore('data', () => {
}
async function fetchOwnTenant () {
ownTenant.value = (await supabase.from("tenants").select().eq('id', currentTenant.value).single()).data
}
async function fetchProfiles () {
profiles.value = (await supabase.from("profiles").select().eq("tenant",currentTenant.value).order("lastName")).data
}
async function fetchOwnProfiles () {
let profiles = (await supabase.from("profiles").select().order("fullName")).data
let conns = (await supabase.from("profileconnections").select()).data.map(i => i.profile_id)
//console.log(conns)
//console.log(profiles.filter(i => conns.includes(i.id)))
ownProfiles.value = profiles.filter(i => conns.includes(i.id))
}
async function fetchTenants () {
tenants.value = (await supabase.from("tenants").select()).data
}
async function fetchBankAccounts () {
bankAccounts.value = (await supabase.from("bankaccounts").select().eq('tenant', currentTenant.value)).data
bankAccounts.value = (await supabase.from("bankaccounts").select().eq('tenant', profileStore.currentTenant)).data
}
async function fetchBankStatements () {
bankstatements.value = (await supabase.from("bankstatements").select().eq('tenant', currentTenant.value).order("date", {ascending:false})).data
bankstatements.value = (await supabase.from("bankstatements").select().eq('tenant', profileStore.currentTenant).order("date", {ascending:false})).data
}
async function fetchBankRequisitions () {
bankrequisitions.value = (await supabase.from("bankrequisitions").select().eq('status', "LN")).data
}
async function fetchEvents () {
events.value = (await supabase.from("events").select().eq('tenant', currentTenant.value)).data
events.value = (await supabase.from("events").select().eq('tenant', profileStore.currentTenant)).data
}
async function fetchContracts () {
contracts.value = (await supabase.from("contracts").select().eq('tenant', currentTenant.value)).data
contracts.value = (await supabase.from("contracts").select().eq('tenant', profileStore.currentTenant)).data
}
async function fetchContacts () {
contacts.value = (await supabase.from("contacts").select().eq('tenant', currentTenant.value)).data
contacts.value = (await supabase.from("contacts").select().eq('tenant', profileStore.currentTenant)).data
}
async function fetchCustomers () {
customers.value = (await supabase.from("customers").select().eq('tenant', currentTenant.value).order("customerNumber", {ascending:true})).data
customers.value = (await supabase.from("customers").select().eq('tenant', profileStore.currentTenant).order("customerNumber", {ascending:true})).data
}
async function fetchTasks () {
tasks.value = (await supabase.from("tasks").select().eq('tenant', currentTenant.value)).data
tasks.value = (await supabase.from("tasks").select().eq('tenant', profileStore.currentTenant)).data
}
async function fetchForms () {
forms.value = (await supabase.from("forms").select().eq('tenant', currentTenant.value)).data
forms.value = (await supabase.from("forms").select().eq('tenant', profileStore.currentTenant)).data
}
async function fetchFormSubmits () {
formSubmits.value = (await supabase.from("formSubmits").select().eq('tenant', currentTenant.value)).data
formSubmits.value = (await supabase.from("formSubmits").select().eq('tenant', profileStore.currentTenant)).data
}
async function fetchProducts () {
products.value = (await supabase.from("products").select().eq('tenant', currentTenant.value)).data
products.value = (await supabase.from("products").select().eq('tenant', profileStore.currentTenant)).data
}
async function fetchUnits () {
units.value = (await supabase.from("units").select()).data
}
async function fetchProjects () {
projects.value = (await supabase.from("projects").select().eq("tenant",currentTenant.value)).data
projects.value = (await supabase.from("projects").select().eq("tenant",profileStore.currentTenant)).data
}
async function fetchSpaces () {
spaces.value = (await supabase.from("spaces").select().eq('tenant', currentTenant.value).order("spaceNumber", {ascending:true})).data
spaces.value = (await supabase.from("spaces").select().eq('tenant', profileStore.currentTenant).order("spaceNumber", {ascending:true})).data
}
async function fetchMovements () {
movements.value = (await supabase.from("movements").select().eq('tenant', currentTenant.value)).data
movements.value = (await supabase.from("movements").select().eq('tenant', profileStore.currentTenant)).data
}
async function fetchVehicles () {
vehicles.value = (await supabase.from("vehicles").select().eq('tenant', currentTenant.value)).data
vehicles.value = (await supabase.from("vehicles").select().eq('tenant', profileStore.currentTenant)).data
}
async function fetchTimes () {
times.value = (await supabase.from("times").select().eq('tenant', currentTenant.value).order("start", {ascending:false})).data
times.value = (await supabase.from("times").select().eq('tenant', profileStore.currentTenant).order("start", {ascending:false})).data
}
async function fetchHistoryItems () {
historyItems.value = (await supabase.from("historyitems").select().eq('tenant', currentTenant.value)).data
historyItems.value = (await supabase.from("historyitems").select().eq('tenant', profileStore.currentTenant)).data
}
async function fetchVendors () {
vendors.value = (await supabase.from("vendors").select().eq('tenant', currentTenant.value).order("vendorNumber", {ascending:true})).data
vendors.value = (await supabase.from("vendors").select().eq('tenant', profileStore.currentTenant).order("vendorNumber", {ascending:true})).data
}
async function fetchIncomingInvoices () {
incominginvoices.value = (await supabase.from("incominginvoices").select().eq('tenant', currentTenant.value)).data
}
async function fetchNumberRanges () {
numberRanges.value = (await supabase.from("numberranges").select().eq('tenant', currentTenant.value)).data
incominginvoices.value = (await supabase.from("incominginvoices").select().eq('tenant', profileStore.currentTenant)).data
}
async function fetchNotifications () {
notifications.value = (await supabase.from("notifications").select().eq('tenant', currentTenant.value).order("created_at", {ascending: false})).data
notifications.value = (await supabase.from("notifications").select().eq('tenant', profileStore.currentTenant).order("created_at", {ascending: false})).data
}
async function fetchAbsenceRequests () {
absencerequests.value = (await supabase.from("absencerequests").select().eq('tenant', currentTenant.value)).data
absencerequests.value = (await supabase.from("absencerequests").select().eq('tenant', profileStore.currentTenant)).data
}
async function fetchAccounts () {
accounts.value = (await supabase.from("accounts").select()).data
@@ -1382,51 +1172,51 @@ export const useDataStore = defineStore('data', () => {
taxTypes.value = (await supabase.from("taxtypes").select()).data
}
async function fetchPlants () {
plants.value = (await supabase.from("plants").select().eq('tenant', currentTenant.value)).data
plants.value = (await supabase.from("plants").select().eq('tenant', profileStore.currentTenant)).data
}
async function fetchInventoryItems () {
inventoryitems.value = (await supabase.from("inventoryitems").select().eq('tenant', currentTenant.value)).data
inventoryitems.value = (await supabase.from("inventoryitems").select().eq('tenant', profileStore.currentTenant)).data
}
async function fetchChats() {
chats.value = (await supabase.from("chats").select()).data
}
async function fetchMessages() {
messages.value = (await supabase.from("messages").select().eq('tenant', currentTenant.value).order('created_at', {ascending:true})).data
messages.value = (await supabase.from("messages").select().eq('tenant', profileStore.currentTenant).order('created_at', {ascending:true})).data
}
async function fetchCreatedDocuments() {
createddocuments.value = (await supabase.from("createddocuments").select().eq('tenant', currentTenant.value).order('created_at', {ascending:true})).data
createddocuments.value = (await supabase.from("createddocuments").select().eq('tenant', profileStore.currentTenant).order('created_at', {ascending:true})).data
}
async function fetchWorkingTimes() {
workingtimes.value = (await supabase.from("workingtimes").select().eq('tenant', currentTenant.value).order('created_at', {ascending:true})).data
workingtimes.value = (await supabase.from("workingtimes").select().eq('tenant', profileStore.currentTenant).order('created_at', {ascending:true})).data
}
async function fetchPhasesTemplates() {
phasesTemplates.value = (await supabase.from("phasesTemplates").select().eq('tenant', currentTenant.value).order('created_at', {ascending:true})).data
phasesTemplates.value = (await supabase.from("phasesTemplates").select().eq('tenant', profileStore.currentTenant).order('created_at', {ascending:true})).data
}
async function fetchEmailAccounts() {
emailAccounts.value = (await supabase.from("emailAccounts").select().eq('tenant', currentTenant.value)).data
emailAccounts.value = (await supabase.from("emailAccounts").select().eq('tenant', profileStore.currentTenant)).data
}
async function fetchTextTemplates() {
texttemplates.value = (await supabase.from("texttemplates").select().eq('tenant', currentTenant.value)).data
texttemplates.value = (await supabase.from("texttemplates").select().eq('tenant', profileStore.currentTenant)).data
}
async function fetchServices() {
services.value = (await supabase.from("services").select().eq('tenant', currentTenant.value)).data
services.value = (await supabase.from("services").select().eq('tenant', profileStore.currentTenant)).data
}
async function fetchServiceCategories() {
servicecategories.value = (await supabase.from("servicecategories").select().eq('tenant', currentTenant.value)).data
servicecategories.value = (await supabase.from("servicecategories").select().eq('tenant', profileStore.currentTenant)).data
}
async function fetchResources() {
resources.value = (await supabase.from("resources").select().eq('tenant', currentTenant.value)).data
resources.value = (await supabase.from("resources").select().eq('tenant', profileStore.currentTenant)).data
}
async function fetchDocuments () {
let tempDocuments = (await supabase.from("documents").select().eq('tenant', currentTenant.value)).data
let tempDocuments = (await supabase.from("documents").select().eq('tenant', profileStore.currentTenant)).data
if(tempDocuments.length > 0){
let paths = []
@@ -1482,22 +1272,17 @@ export const useDataStore = defineStore('data', () => {
return tasks.value.filter(task => task.categorie != "Erledigt").length
})
const getOwnProfile = computed(() => {
return profiles.value.find(i => i.id === user.value.id)
})
const getMovementsBySpace = computed(() => (spaceId) => {
return movements.value.filter(movement => movement.spaceId === spaceId)
})
const getContactsByCustomerId = computed(() => (customerId) => {
return contacts.value.filter(item => item.customer === customerId)
})
async function getContactsByCustomerId (itemId) {
return (await supabase.from("contacts").select().eq("customer", itemId)).data
}
const getProjectsByCustomerId = computed(() => (customerId) => {
return projects.value.filter(item => item.customer === customerId)
})
async function getProjectsByCustomerId (itemId) {
return (await supabase.from("projects").select().eq("customer",itemId)).data
}
const getPlantsByCustomerId = computed(() => (customerId) => {
return plants.value.filter(item => item.customer === customerId)
@@ -1673,7 +1458,7 @@ export const useDataStore = defineStore('data', () => {
const getResources = computed(() => {
return [
...profiles.value.filter(i => i.tenant === currentTenant.value).map(profile => {
...profiles.value.filter(i => i.tenant === profileStore.currentTenant).map(profile => {
return {
type: 'Mitarbeiter',
title: profile.fullName,
@@ -1699,7 +1484,7 @@ export const useDataStore = defineStore('data', () => {
const getResourcesList = computed(() => {
return [
...profiles.value.filter(i => i.tenant === currentTenant.value).map(profile => {
...profiles.value.filter(i => i.tenant === profileStore.currentTenant).map(profile => {
return {
type: 'Mitarbeiter',
title: profile.fullName,
@@ -1886,10 +1671,6 @@ export const useDataStore = defineStore('data', () => {
return absencerequests.value.find(item => item.id === itemId)
})
const getProfileById = computed(() => (itemId) => {
return profiles.value.find(item => item.id === itemId)
})
const getAccountById = computed(() => (accountId) => {
return accounts.value.find(item => item.id === accountId)
})
@@ -1910,34 +1691,9 @@ export const useDataStore = defineStore('data', () => {
return bankAccounts.value.find(item => item.id === itemId)
})
const getEventById = computed(() => (itemId) => {
return events.value.find(item => item.id === itemId)
})
const getWorkingTimeById = computed(() => (itemId) => {
return workingtimes.value.find(item => item.id === itemId)
})
/*const getOpenDocuments = computed(() => (itemId) => {
let items = createddocuments.value.filter(i => i.type === "invoices" || i.type === "advanceInvoices")
items = items.filter(i => bankstatements.value.filter(x => x.assignments.find(y => y.type === 'createdDocument' && y.id === i.id)).length === 0)
/!*let finalItems = []
items.forEach(item => {
if(bankstatements.value.filter(i => i.assignments.find(x => x.id === item.id))){
finalItems.push(item)
}
})
return finalItems*!/
return items
//return createddocuments.value.filter(i => (i.type === "invoices" || i.type === "advanceInvoices") && !bankstatements.value.some(x => x.amount -))
})*/
const getOpenIncomingInvoices = computed(() => (itemId) => {
return incominginvoices.value.filter(i => bankstatements.value.filter(x => x.assignments.find(y => y.type === 'incomingInvoice' && y.id === i.id)).length === 0)
@@ -1967,22 +1723,11 @@ export const useDataStore = defineStore('data', () => {
})
return {
//General
currentTenant,
loaded,
showProfileSelection,
ownTenant,
initializeData,
changeProfile,
uploadFiles,
generateHistoryItems,
dataTypes,
//Data
profiles,
ownProfiles,
activeProfile,
tenants,
events,
customers,
tasks,
@@ -2004,7 +1749,6 @@ export const useDataStore = defineStore('data', () => {
bankstatements,
bankrequisitions,
historyItems,
numberRanges,
notifications,
absencerequests,
accounts,
@@ -2027,42 +1771,12 @@ export const useDataStore = defineStore('data', () => {
updateItem,
fetchData,
clearStore,
fetchOwnTenant,
fetchProfiles,
fetchOwnProfiles,
fetchBankAccounts,
fetchBankStatements,
fetchBankRequisitions,
fetchEvents,
fetchContracts,
fetchContacts,
fetchCustomers,
fetchTasks,
fetchForms,
fetchFormSubmits,
fetchProducts,
fetchUnits,
fetchProjects,
fetchSpaces,
fetchMovements,
fetchVehicles,
fetchTimes,
fetchHistoryItems,
fetchVendors,
fetchIncomingInvoices,
fetchNumberRanges,
fetchNotifications,
fetchDocuments,
fetchAbsenceRequests,
fetchPlants,
fetchInventoryItems,
fetchChats,
fetchMessages,
fetchWorkingTimes,
addHistoryItem,
//Getters
getOpenTasksCount,
getOwnProfile,
getMovementsBySpace,
getContactsByCustomerId,
getProjectsByCustomerId,
@@ -2115,17 +1829,12 @@ export const useDataStore = defineStore('data', () => {
getTaskById,
getAbsenceRequestById,
getProjectById,
getProfileById,
getAccountById,
getPlantById,
getCreatedDocumentById,
getInventoryItemById,
getBankAccountById,
getEventById,
getWorkingTimeById,
//getOpenDocuments,
getOpenIncomingInvoices
}
})