Included user-information in employee model

This commit is contained in:
Sockenklaus
2021-11-04 14:00:52 +01:00
parent cfcfeba170
commit b41ff6515d
3 changed files with 70 additions and 116 deletions

View File

@@ -4,21 +4,17 @@ import { useUser } from './user'
import { useNotifications } from './notifications'
type ResultData = {
employee: {
id: number,
firstName: string,
lastName: string,
shorthand: string,
phone: string,
mobile: string,
email: string,
contractHours: number,
hasUser: boolean,
},
user?: {
id?: number,
username?: string
}
id: number,
firstName: string,
lastName: string | undefined,
shorthand: string,
phone: string | undefined,
mobile: string | undefined,
email: string | undefined,
contractHours: number | undefined,
isActive: boolean,
username: string | undefined,
role: string
}
const user = useUser()
@@ -39,14 +35,11 @@ export const useEmployee = defineStore({
mobile: '',
email: '',
contractHours: NaN,
hasUser: false
},
user: {
id: NaN,
isActive: false,
username: '',
password: '',
passwordConfirm: '',
}
passwordConfirm: ''
},
},
employee: {
@@ -58,15 +51,11 @@ export const useEmployee = defineStore({
mobile: '',
email: '',
contractHours: NaN,
hasUser: false
},
user: {
id: NaN,
isActive: false,
username: '',
password: '',
passwordConfirm: ''
}
},
}
},
@@ -75,15 +64,14 @@ export const useEmployee = defineStore({
try {
const data : ResultData = await <ResultData>(await axios.get('employees/'+id, {
headers: {
'Authorization': 'Bearer '+user.token
}
headers: user.header
})).data
Object.assign(this.clean.employee, data.employee)
Object.assign(this.clean.user, data.user)
Object.assign(this.employee, data.employee),
Object.assign(this.user, data.user)
console.log(this.clean.employee)
console.log(data)
Object.assign(this.clean.employee, data)
Object.assign(this.employee, data)
}
catch(err){
if(err instanceof Error) notifications.add('danger', err.message, -1)
@@ -92,7 +80,6 @@ export const useEmployee = defineStore({
},
reset() {
Object.assign(this.user, this.clean.user)
Object.assign(this.employee, this.clean.employee)
},
@@ -101,30 +88,6 @@ export const useEmployee = defineStore({
try {
let result
if(isNaN(this.user.id) && this.user.username.length > 0){
result = await axios.post(
'users',
{
username: this.user.username,
password: this.user.password
},
{
headers: user.header
}
)
}
else if (this.user.password.length > 0 && this.user.password === this.user.passwordConfirm){
result = await axios.patch(
'users/'+this.user.id,
{
password: this.user.password
},
{
headers: user.header
}
)
}
result = await axios.patch(
'employees/'+this.employee.id,
this.employee,
@@ -134,7 +97,6 @@ export const useEmployee = defineStore({
)
Object.assign(this.clean.employee, this.employee)
Object.assign(this.clean.user, this.user)
notifications.add('success', result.statusText)
}
catch(error) {