ilk
This commit is contained in:
238
ui/src/stores/UserDetailStore.js
Normal file
238
ui/src/stores/UserDetailStore.js
Normal file
@@ -0,0 +1,238 @@
|
||||
// src/stores/userDetailStore.js
|
||||
import { defineStore } from 'pinia'
|
||||
import api, { get, post, put } from 'src/services/api'
|
||||
|
||||
export const useUserDetailStore = defineStore('userDetail', {
|
||||
state: () => ({
|
||||
sendingPasswordMail: false,
|
||||
lastPasswordMailSentAt: null,
|
||||
hasPassword: false,
|
||||
|
||||
/* ================= FLAGS ================= */
|
||||
loading: false,
|
||||
saving: false,
|
||||
error: null,
|
||||
|
||||
/* ================= FORM ================= */
|
||||
form: {
|
||||
id: null,
|
||||
code: '',
|
||||
full_name: '',
|
||||
email: '',
|
||||
mobile: '',
|
||||
is_active: true,
|
||||
address: '',
|
||||
roles: [],
|
||||
departments: [],
|
||||
piyasalar: [],
|
||||
nebim_users: []
|
||||
},
|
||||
|
||||
/* ================= LOOKUPS ================= */
|
||||
roleOptions: [],
|
||||
departmentOptions: [],
|
||||
piyasaOptions: [],
|
||||
nebimUserOptions: []
|
||||
}),
|
||||
|
||||
actions: {
|
||||
/* =====================================================
|
||||
🔄 RESET (NEW MODE)
|
||||
===================================================== */
|
||||
resetForm () {
|
||||
this.form = {
|
||||
id: null,
|
||||
code: '',
|
||||
full_name: '',
|
||||
email: '',
|
||||
mobile: '',
|
||||
is_active: true,
|
||||
address: '',
|
||||
roles: [],
|
||||
departments: [],
|
||||
piyasalar: [],
|
||||
nebim_users: []
|
||||
}
|
||||
this.error = null
|
||||
this.hasPassword = false
|
||||
this.lastPasswordMailSentAt = null
|
||||
},
|
||||
|
||||
/* =====================================================
|
||||
🔐 ADMIN RESET PASSWORD
|
||||
===================================================== */
|
||||
async adminResetPassword (id, payload) {
|
||||
// token otomatik (interceptor)
|
||||
await post(`/users/${id}/admin-reset-password`, payload)
|
||||
this.hasPassword = true
|
||||
},
|
||||
|
||||
/* =====================================================
|
||||
✉️ SEND PASSWORD MAIL
|
||||
===================================================== */
|
||||
async sendPasswordMail (id) {
|
||||
this.sendingPasswordMail = true
|
||||
this.error = null
|
||||
|
||||
try {
|
||||
await post(`/users/${id}/send-password-mail`, {})
|
||||
|
||||
// UI takip (DB’siz): sadece “son gönderim” gösterir
|
||||
this.lastPasswordMailSentAt = new Date().toLocaleString('tr-TR')
|
||||
} catch (e) {
|
||||
this.error = 'Parola maili gönderilemedi'
|
||||
throw e
|
||||
} finally {
|
||||
this.sendingPasswordMail = false
|
||||
}
|
||||
},
|
||||
|
||||
/* =====================================================
|
||||
📦 PAYLOAD BUILDER (BACKEND SÖZLEŞMESİYLE UYUMLU)
|
||||
===================================================== */
|
||||
buildPayload () {
|
||||
return {
|
||||
code: this.form.code,
|
||||
full_name: this.form.full_name,
|
||||
email: this.form.email,
|
||||
mobile: this.form.mobile,
|
||||
is_active: this.form.is_active,
|
||||
address: this.form.address,
|
||||
|
||||
roles: this.form.roles,
|
||||
|
||||
// ✅ TEK DEPARTMAN (string → backend array)
|
||||
departments: this.form.departments
|
||||
? [{ code: this.form.departments }]
|
||||
: [],
|
||||
|
||||
piyasalar: (this.form.piyasalar || []).map(code => ({ code })),
|
||||
|
||||
nebim_users: (this.form.nebim_users || []).map(username => {
|
||||
const opt = (this.nebimUserOptions || []).find(x => x.value === username)
|
||||
return {
|
||||
username,
|
||||
user_group_code: opt?.user_group_code || ''
|
||||
}
|
||||
})
|
||||
}
|
||||
},
|
||||
|
||||
/* =====================================================
|
||||
📥 GET USER (EDIT MODE)
|
||||
===================================================== */
|
||||
async fetchUser (id) {
|
||||
this.loading = true
|
||||
this.error = null
|
||||
|
||||
try {
|
||||
const data = await get(`/users/${id}`)
|
||||
|
||||
this.form.id = data.id
|
||||
this.form.code = data.code || ''
|
||||
this.form.full_name = data.full_name || ''
|
||||
this.form.email = data.email || ''
|
||||
this.form.mobile = data.mobile || ''
|
||||
this.form.is_active = !!data.is_active
|
||||
this.form.address = data.address || ''
|
||||
|
||||
this.form.roles = data.roles || []
|
||||
this.form.departments = (data.departments || []).map(x => x.code)
|
||||
this.form.piyasalar = (data.piyasalar || []).map(x => x.code)
|
||||
this.form.nebim_users = (data.nebim_users || []).map(x => x.username)
|
||||
|
||||
this.hasPassword = !!data.has_password
|
||||
} catch (e) {
|
||||
this.error = 'Kullanıcı bilgileri alınamadı'
|
||||
throw e
|
||||
} finally {
|
||||
this.loading = false
|
||||
}
|
||||
},
|
||||
|
||||
/* =====================================================
|
||||
✍️ UPDATE USER (PUT)
|
||||
===================================================== */
|
||||
async saveUser (id) {
|
||||
this.saving = true
|
||||
this.error = null
|
||||
|
||||
try {
|
||||
console.log('🟦 saveUser() START', id)
|
||||
|
||||
const payload = this.buildPayload()
|
||||
console.log('📤 PUT payload', payload)
|
||||
|
||||
await put(`/users/${id}`, payload)
|
||||
|
||||
console.log('✅ PUT OK → REFETCH USER')
|
||||
await this.fetchUser(id)
|
||||
|
||||
console.log('🔄 USER REFRESHED', {
|
||||
hasPassword: this.hasPassword,
|
||||
roles: this.form.roles,
|
||||
departments: this.form.departments
|
||||
})
|
||||
} catch (e) {
|
||||
console.error('❌ saveUser FAILED', e)
|
||||
this.error = 'Kullanıcı güncellenemedi'
|
||||
throw e
|
||||
} finally {
|
||||
this.saving = false
|
||||
}
|
||||
},
|
||||
|
||||
/* =====================================================
|
||||
➕ CREATE USER (POST)
|
||||
===================================================== */
|
||||
async createUser () {
|
||||
this.saving = true
|
||||
this.error = null
|
||||
|
||||
try {
|
||||
console.log('🟢 createUser() START')
|
||||
|
||||
const payload = this.buildPayload()
|
||||
console.log('📤 POST payload', payload)
|
||||
|
||||
const data = await post('/users', payload)
|
||||
|
||||
console.log('✅ CREATE OK response', data)
|
||||
|
||||
const newId = data?.id
|
||||
if (!newId) {
|
||||
throw new Error('CREATE response id yok')
|
||||
}
|
||||
|
||||
console.log('🔁 FETCH NEW USER id=', newId)
|
||||
await this.fetchUser(newId)
|
||||
|
||||
return newId
|
||||
} catch (e) {
|
||||
console.error('❌ createUser FAILED', e)
|
||||
this.error = 'Kullanıcı oluşturulamadı'
|
||||
throw e
|
||||
} finally {
|
||||
this.saving = false
|
||||
}
|
||||
},
|
||||
|
||||
/* =====================================================
|
||||
📚 LOOKUPS (NEW + EDIT ORTAK)
|
||||
===================================================== */
|
||||
async fetchLookups () {
|
||||
// token otomatik
|
||||
const [roles, depts, piyasalar, nebims] = await Promise.all([
|
||||
api.get('/lookups/roles'),
|
||||
api.get('/lookups/departments'),
|
||||
api.get('/lookups/piyasalar'),
|
||||
api.get('/lookups/nebim-users')
|
||||
])
|
||||
|
||||
this.roleOptions = roles?.data || roles || []
|
||||
this.departmentOptions = depts?.data || depts || []
|
||||
this.piyasaOptions = piyasalar?.data || piyasalar || []
|
||||
this.nebimUserOptions = nebims?.data || nebims || []
|
||||
}
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user