Files
FEDEO/spaces/stores/data.ts
flfeders 5503c572f1 Added Logo
Added Document Download
Added zipjs
2023-12-11 12:04:32 +01:00

199 lines
7.4 KiB
TypeScript

import {defineStore} from 'pinia'
import {createClient} from '@supabase/supabase-js'
const supabase = createClient('https://uwppvcxflrcsibuzsbil.supabase.co','eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6InV3cHB2Y3hmbHJjc2lidXpzYmlsIiwicm9sZSI6ImFub24iLCJpYXQiOjE3MDA5MzgxOTQsImV4cCI6MjAxNjUxNDE5NH0.CkxYSQH0uLfwx9GVUlO6AYMU2FMLAxGMrwEKvyPv7Oo')
// @ts-ignore
export const useDataStore = defineStore('data', {
state: () => ({
loaded: false,
ownTenant: {
calendarConfig: {
eventTypes: [] as any[]
},
timeConfig: {
timeTypes: [] as any[]
}
},
profiles: [] as any[],
events: [] as any[],
customers: [] as any[],
tasks: [] as any[],
projects: [] as any[],
documents: [] as any[],
spaces: [] as any[],
units: [] as any[],
times: [] as any[],
products: [] as any[],
movements: [] as any[],
forms: [] as any[],
jobs: [] as any[],
formSubmits: [] as any[],
contacts: [] as any[],
vehicles: [] as any[],
}),
actions: {
async fetchData() {
await this.fetchOwnTenant()
await this.fetchProfiles()
await this.fetchEvents()
await this.fetchCustomers()
await this.fetchTasks()
await this.fetchForms()
await this.fetchFormSubmits()
await this.fetchProducts()
await this.fetchUnits()
await this.fetchProjects()
await this.fetchDocuments()
await this.fetchMovements()
await this.fetchTimes()
await this.fetchJobs()
await this.fetchSpaces()
await this.fetchVehicles()
this.loaded = true
},
async fetchOwnTenant() {
//TODO: Tenant ID Dynamisch machen
// @ts-ignore
this.ownTenant = (await supabase.from("tenants").select().eq('id', 1)).data[0]
},
async fetchProfiles() {
// @ts-ignore
this.profiles = (await supabase.from("profiles").select()).data
},
async fetchEvents() {
// @ts-ignore
this.events = (await supabase.from("events").select()).data
},
async fetchCustomers() {
// @ts-ignore
this.customers = (await supabase.from("customers").select().order('customerNumber', {ascending: true})).data
},
async fetchTasks() {
// @ts-ignore
this.tasks = (await supabase.from("tasks").select()).data
},
async fetchForms() {
// @ts-ignore
this.forms = (await supabase.from("forms").select()).data
},
async fetchFormSubmits() {
// @ts-ignore
this.formSubmits = (await supabase.from("formSubmits").select()).data
},
async fetchProducts() {
// @ts-ignore
this.products = (await supabase.from("products").select()).data
},
async fetchUnits() {
// @ts-ignore
this.units = (await supabase.from("units").select()).data
},
async fetchProjects() {
// @ts-ignore
this.projects = (await supabase.from("projects").select()).data
},
async fetchSpaces() {
// @ts-ignore
this.spaces = (await supabase.from("spaces").select().order('spaceNumber', {ascending: true})).data
},
async fetchMovements() {
// @ts-ignore
this.movements = (await supabase.from("movements").select()).data
},
async fetchVehicles() {
// @ts-ignore
this.vehicles = (await supabase.from("vehicles").select()).data
},
async fetchTimes() {
// @ts-ignore
this.times = (await supabase.from("times").select()).data
},
async fetchJobs() {
// @ts-ignore
this.jobs = (await supabase.from("jobs").select()).data
},
async fetchDocuments() {
// @ts-ignore
this.documents = (await supabase.from("documents").select()).data
for(const [index,doc] of this.documents.entries()){
// @ts-ignore
this.documents[index].url = (await supabase.storage.from('documents').createSignedUrl(doc.path, 60 * 60)).data.signedUrl
}
}
},
getters: {
getProfileById: (state) => (userUid:string) => state.profiles.find(profile => profile.id === userUid),
getOpenTasksCount: (state) => state.tasks.filter(task => task.categorie != "Erledigt").length,
movementsBySpace: (state) => (spaceId:number) => state.movements.filter(move => move.spaceId === spaceId),
getProductById: (state) => (productId:number) => state.products.find(product => product.id === productId),
getProjectById: (state) => (projectId:number) => {
let project = state.projects.find(project => project.id === projectId)
let projectHours = 0
let projectTimes = state.times.filter(time => time.projectId === projectId)
projectTimes.forEach(time => projectHours += time.duration)
project.projectHours = projectHours
return project
},
getCustomerById: (state) => (customerId:number) => state.customers.find(customer => customer.id === customerId),
getJobById: (state) => (jobId:number) => state.jobs.find(job => job.id === jobId),
getTimesByProjectId: (state) => (projectId:number) => {
let times = state.times.filter(time => time.projectId === projectId)
console.log(times.length)
/*const mapNumRange = (num, inMin, inMax, outMin, outMax) =>
((num - inMin) * (outMax - outMin)) / (inMax - inMin) + outMin;
times.forEach((time,index) => {
times[index].duration = Math.round(mapNumRange(Math.abs(new Date(time.end) - new Date(time.start))/1000/60,0,60,0,1)*100)/100
})*/
return times
},
getFormSubmitsWithLabelProp: (state) => (state.formSubmits.map(submit => {return{...submit, label: submit.id}})),
getResources: (state) => {
return [
...state.profiles.map(profile => {
return {
type: 'person',
title: profile.firstName + ' ' + profile.lastName,
id: profile.id
}
}),
...state.vehicles.map(vehicle => {
return {
type: 'vehicle',
title: vehicle.licensePlate,
id: vehicle.licensePlate
}
})
]
},
getEvents: (state) => {
return [
...state.events.map(event => {
let eventColor = state.ownTenant.calendarConfig.eventTypes.find(type => type.label === event.type).color
return {
...event,
borderColor: eventColor,
textColor: eventColor,
backgroundColor: "black"
}
}),
]
},
getEventTypes: (state) => state.ownTenant.calendarConfig.eventTypes,
getTimeTypes: (state) => state.ownTenant.timeConfig.timeTypes
}
})