Mal Sorumlusu kaydetme mantığı değişti
This commit is contained in:
141
src/server.js
141
src/server.js
@@ -396,7 +396,7 @@ app.get('/api/units', (req, res) => {
|
||||
});
|
||||
|
||||
// POST - Yeni birlik ekle
|
||||
app.post('/api/units', (req, res) => {
|
||||
app.post('/api/units', async (req, res) => {
|
||||
// Yetki kontrolü (temporary - will be implemented with proper session)
|
||||
|
||||
try {
|
||||
@@ -414,9 +414,9 @@ app.post('/api/units', (req, res) => {
|
||||
}
|
||||
|
||||
// Komutan validasyonu
|
||||
const { full_name, rank, registration_number, tc_kimlik, phone } = commander;
|
||||
if (!full_name || !rank || !registration_number || !tc_kimlik || !phone) {
|
||||
return res.status(400).json({ message: 'Birlik sorumlusunun tüm bilgileri zorunludur.' });
|
||||
const { full_name, rank, registration_number, tc_kimlik, phone, username, password } = commander;
|
||||
if (!full_name || !rank || !registration_number || !tc_kimlik || !phone || !username || !password) {
|
||||
return res.status(400).json({ message: 'Mal sorumlusunun tüm bilgileri zorunludur.' });
|
||||
}
|
||||
|
||||
// TC Kimlik numarası validasyonu
|
||||
@@ -436,11 +436,44 @@ app.post('/api/units', (req, res) => {
|
||||
rank: rank.trim(),
|
||||
registration_number: registration_number.trim(),
|
||||
tc_kimlik: tc_kimlik.trim(),
|
||||
phone: phone.trim()
|
||||
phone: phone.trim(),
|
||||
username: username.trim(),
|
||||
password: password.trim()
|
||||
},
|
||||
created_at: new Date().toISOString()
|
||||
};
|
||||
|
||||
// Mal sorumlusu için kullanıcı oluştur
|
||||
try {
|
||||
const hashedPassword = await bcrypt.hash(password, 10);
|
||||
|
||||
// Kullanıcı adı benzersizliğini kontrol et
|
||||
const existingUser = await new Promise((resolve, reject) => {
|
||||
db.get('SELECT id FROM users WHERE username = ?', [username], (err, row) => {
|
||||
if (err) reject(err);
|
||||
else resolve(row);
|
||||
});
|
||||
});
|
||||
|
||||
if (existingUser) {
|
||||
return res.status(400).json({ message: 'Bu kullanıcı adı zaten kullanımda.' });
|
||||
}
|
||||
|
||||
// Mal sorumlusu kullanıcı olarak ekle
|
||||
await new Promise((resolve, reject) => {
|
||||
db.run('INSERT INTO users (username, password, role, full_name) VALUES (?, ?, ?, ?)',
|
||||
[username, hashedPassword, 'goods_manager', full_name], (err) => {
|
||||
if (err) reject(err);
|
||||
else resolve();
|
||||
});
|
||||
});
|
||||
|
||||
console.log(`✅ Mal sorumlusu kullanıcı oluşturuldu: ${username}`);
|
||||
} catch (userError) {
|
||||
console.error('❌ Mal sorumlusu kullanıcı oluşturma hatası:', userError);
|
||||
return res.status(500).json({ message: 'Mal sorumlusu kullanıcı oluşturulamadı.' });
|
||||
}
|
||||
|
||||
units.push(newUnit);
|
||||
|
||||
res.json({
|
||||
@@ -454,7 +487,7 @@ app.post('/api/units', (req, res) => {
|
||||
});
|
||||
|
||||
// PUT - Birlik güncelle
|
||||
app.put('/api/units', (req, res) => {
|
||||
app.put('/api/units', async (req, res) => {
|
||||
// Yetki kontrolü (temporary - will be implemented with proper session)
|
||||
|
||||
try {
|
||||
@@ -473,9 +506,9 @@ app.put('/api/units', (req, res) => {
|
||||
}
|
||||
|
||||
// Komutan validasyonu
|
||||
const { full_name, rank, registration_number, tc_kimlik, phone } = commander;
|
||||
if (!full_name || !rank || !registration_number || !tc_kimlik || !phone) {
|
||||
return res.status(400).json({ message: 'Birlik sorumlusunun tüm bilgileri zorunludur.' });
|
||||
const { full_name, rank, registration_number, tc_kimlik, phone, username, password } = commander;
|
||||
if (!full_name || !rank || !registration_number || !tc_kimlik || !phone || !username || !password) {
|
||||
return res.status(400).json({ message: 'Mal sorumlusunun tüm bilgileri zorunludur.' });
|
||||
}
|
||||
|
||||
// TC Kimlik numarası validasyonu
|
||||
@@ -490,21 +523,52 @@ app.put('/api/units', (req, res) => {
|
||||
}
|
||||
|
||||
// Birlik güncelle
|
||||
const updatedCommander = {
|
||||
full_name: full_name.trim(),
|
||||
rank: rank.trim(),
|
||||
registration_number: registration_number.trim(),
|
||||
tc_kimlik: tc_kimlik.trim(),
|
||||
phone: phone.trim(),
|
||||
username: username.trim(),
|
||||
password: password.trim()
|
||||
};
|
||||
|
||||
units[unitIndex] = {
|
||||
...units[unitIndex],
|
||||
name: name.trim(),
|
||||
address: address.trim(),
|
||||
stk: stk.trim().toUpperCase(),
|
||||
btk: btk.trim().toUpperCase(),
|
||||
commander: {
|
||||
full_name: full_name.trim(),
|
||||
rank: rank.trim(),
|
||||
registration_number: registration_number.trim(),
|
||||
tc_kimlik: tc_kimlik.trim(),
|
||||
phone: phone.trim()
|
||||
}
|
||||
commander: updatedCommander
|
||||
};
|
||||
|
||||
// Eğer kullanıcı adı veya şifre değişmişse, kullanıcıyı güncelle
|
||||
const oldCommander = units[unitIndex].commander;
|
||||
if (oldCommander.username !== username || password !== oldCommander.password) {
|
||||
try {
|
||||
const hashedPassword = await bcrypt.hash(password, 10);
|
||||
|
||||
// Kullanıcıyı güncelle
|
||||
await new Promise((resolve, reject) => {
|
||||
db.run('UPDATE users SET username = ?, password = ?, full_name = ? WHERE username = ?',
|
||||
[username, hashedPassword, full_name, oldCommander.username], (err) => {
|
||||
if (err) reject(err);
|
||||
else resolve();
|
||||
});
|
||||
});
|
||||
|
||||
console.log(`✅ Mal sorumlusu kullanıcı güncellendi: ${username}`);
|
||||
} catch (userError) {
|
||||
console.error('❌ Mal sorumlusu kullanıcı güncelleme hatası:', userError);
|
||||
// Kullanıcı güncellenemezse, birlik güncellemesi geri alınsın
|
||||
units[unitIndex] = {
|
||||
...units[unitIndex],
|
||||
commander: oldCommander
|
||||
};
|
||||
return res.status(500).json({ message: 'Mal sorumlusu kullanıcı güncellenemedi.' });
|
||||
}
|
||||
}
|
||||
|
||||
res.json({
|
||||
message: 'Birlik başarıyla güncellendi.',
|
||||
unit: units[unitIndex]
|
||||
@@ -687,7 +751,7 @@ app.get('/api/fuel-personnel', (req, res) => {
|
||||
});
|
||||
|
||||
// POST - Yeni yakıt personeli ekle
|
||||
app.post('/api/fuel-personnel', (req, res) => {
|
||||
app.post('/api/fuel-personnel', async (req, res) => {
|
||||
// Yetki kontrolü (temporary - will be implemented with proper session)
|
||||
|
||||
try {
|
||||
@@ -697,11 +761,12 @@ app.post('/api/fuel-personnel', (req, res) => {
|
||||
registration_number,
|
||||
tc_kimlik,
|
||||
phone,
|
||||
unit_id,
|
||||
is_active = true
|
||||
} = req.body;
|
||||
|
||||
// Validasyon
|
||||
if (!full_name || !rank || !registration_number || !tc_kimlik || !phone) {
|
||||
if (!full_name || !rank || !registration_number || !tc_kimlik || !phone || !unit_id) {
|
||||
return res.status(400).json({ message: 'Tüm alanlar zorunludur.' });
|
||||
}
|
||||
|
||||
@@ -734,10 +799,46 @@ app.post('/api/fuel-personnel', (req, res) => {
|
||||
registration_number: registration_number.trim(),
|
||||
tc_kimlik: tc_kimlik.trim(),
|
||||
phone: phone.trim(),
|
||||
unit_id: parseInt(unit_id),
|
||||
is_active: Boolean(is_active),
|
||||
created_at: new Date().toISOString()
|
||||
};
|
||||
|
||||
// Birlik personeli için kullanıcı oluştur
|
||||
try {
|
||||
// Kullanıcı adını TC kimlik numarasından oluştur
|
||||
const username = `bp_${tc_kimlik}`;
|
||||
const defaultPassword = tc_kimlik.substring(0, 6); // İlk 6 hane varsayılan şifre
|
||||
|
||||
const hashedPassword = await bcrypt.hash(defaultPassword, 10);
|
||||
|
||||
// Kullanıcı adı benzersizliğini kontrol et
|
||||
const existingUser = await new Promise((resolve, reject) => {
|
||||
db.get('SELECT id FROM users WHERE username = ?', [username], (err, row) => {
|
||||
if (err) reject(err);
|
||||
else resolve(row);
|
||||
});
|
||||
});
|
||||
|
||||
if (existingUser) {
|
||||
return res.status(400).json({ message: 'Bu personel için zaten kullanıcı hesabı mevcut.' });
|
||||
}
|
||||
|
||||
// Birlik personeli olarak kullanıcı ekle
|
||||
await new Promise((resolve, reject) => {
|
||||
db.run('INSERT INTO users (username, password, role, full_name) VALUES (?, ?, ?, ?)',
|
||||
[username, hashedPassword, 'birlik_personeli', full_name], (err) => {
|
||||
if (err) reject(err);
|
||||
else resolve();
|
||||
});
|
||||
});
|
||||
|
||||
console.log(`✅ Birlik personeli kullanıcı oluşturuldu: ${username}`);
|
||||
} catch (userError) {
|
||||
console.error('❌ Birlik personeli kullanıcı oluşturma hatası:', userError);
|
||||
return res.status(500).json({ message: 'Personel için kullanıcı hesabı oluşturulamadı.' });
|
||||
}
|
||||
|
||||
fuelPersonnel.push(newPersonnel);
|
||||
|
||||
res.json({
|
||||
@@ -762,11 +863,12 @@ app.put('/api/fuel-personnel', (req, res) => {
|
||||
registration_number,
|
||||
tc_kimlik,
|
||||
phone,
|
||||
unit_id,
|
||||
is_active
|
||||
} = req.body;
|
||||
|
||||
// Validasyon
|
||||
if (!id || !full_name || !rank || !registration_number || !tc_kimlik || !phone) {
|
||||
if (!id || !full_name || !rank || !registration_number || !tc_kimlik || !phone || !unit_id) {
|
||||
return res.status(400).json({ message: 'Tüm alanlar zorunludur.' });
|
||||
}
|
||||
|
||||
@@ -805,6 +907,7 @@ app.put('/api/fuel-personnel', (req, res) => {
|
||||
registration_number: registration_number.trim(),
|
||||
tc_kimlik: tc_kimlik.trim(),
|
||||
phone: phone.trim(),
|
||||
unit_id: parseInt(unit_id),
|
||||
is_active: Boolean(is_active)
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user