Add vehicle repair functionality in Falukant module
- Implemented a new repairVehicle method in FalukantService to handle vehicle repairs, including cost calculation and precondition checks. - Updated FalukantController to expose the repairVehicle endpoint, allowing users to initiate repairs via the API. - Enhanced FalukantRouter to include a new route for vehicle repairs. - Modified BranchView component to add UI elements for repairing vehicles, including a dialog for repair confirmation and displaying repair details. - Updated German localization files to include translations related to vehicle repair actions, improving user experience for German-speaking users.
This commit is contained in:
@@ -1118,6 +1118,93 @@ class FalukantService extends BaseService {
|
||||
return { success: true };
|
||||
}
|
||||
|
||||
async repairVehicle(hashedUserId, vehicleId) {
|
||||
const user = await getFalukantUserOrFail(hashedUserId);
|
||||
const now = new Date();
|
||||
|
||||
// Fahrzeug laden mit Typ-Informationen
|
||||
const vehicle = await Vehicle.findOne({
|
||||
where: {
|
||||
id: vehicleId,
|
||||
falukantUserId: user.id,
|
||||
},
|
||||
include: [
|
||||
{
|
||||
model: VehicleType,
|
||||
as: 'type',
|
||||
required: true,
|
||||
},
|
||||
{
|
||||
model: Transport,
|
||||
as: 'transports',
|
||||
required: false,
|
||||
attributes: ['id'],
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
if (!vehicle) {
|
||||
throw new Error('Vehicle not found or does not belong to user');
|
||||
}
|
||||
|
||||
// Prüfen, ob Fahrzeug in Benutzung ist
|
||||
const hasActiveTransport = Array.isArray(vehicle.transports) && vehicle.transports.length > 0;
|
||||
const isBuilding = vehicle.availableFrom && new Date(vehicle.availableFrom).getTime() > now.getTime();
|
||||
|
||||
if (hasActiveTransport) {
|
||||
throw new PreconditionError('vehicleInUse');
|
||||
}
|
||||
|
||||
if (isBuilding) {
|
||||
throw new PreconditionError('vehicleAlreadyBuilding');
|
||||
}
|
||||
|
||||
// Prüfen, ob Reparatur nötig ist (Zustand < 100)
|
||||
if (vehicle.condition >= 100) {
|
||||
throw new PreconditionError('vehicleAlreadyPerfect');
|
||||
}
|
||||
|
||||
// Kosten berechnen: Baupreis * 0.8 * (100 - zustand) / 100
|
||||
const baseCost = vehicle.type.cost;
|
||||
const repairCost = Math.round(baseCost * 0.8 * (100 - vehicle.condition) / 100);
|
||||
|
||||
if (user.money < repairCost) {
|
||||
throw new PreconditionError('insufficientFunds');
|
||||
}
|
||||
|
||||
// Bauzeit verwenden (buildTimeMinutes)
|
||||
const buildTimeMinutes = vehicle.type.buildTimeMinutes || 0;
|
||||
const buildMs = buildTimeMinutes * 60 * 1000;
|
||||
const availableFrom = new Date(now.getTime() + buildMs);
|
||||
|
||||
// Reparatur durchführen
|
||||
await sequelize.transaction(async (tx) => {
|
||||
// Geld abziehen
|
||||
const moneyResult = await updateFalukantUserMoney(
|
||||
user.id,
|
||||
-repairCost,
|
||||
'repair_vehicle',
|
||||
user.id
|
||||
);
|
||||
if (!moneyResult.success) {
|
||||
throw new Error('Failed to update money');
|
||||
}
|
||||
|
||||
// Fahrzeug reparieren: Zustand auf 100, availableFrom auf jetzt + Bauzeit
|
||||
await vehicle.update({
|
||||
condition: 100,
|
||||
availableFrom: availableFrom,
|
||||
}, { transaction: tx });
|
||||
});
|
||||
|
||||
return {
|
||||
success: true,
|
||||
repairCost,
|
||||
buildTimeMinutes,
|
||||
availableFrom,
|
||||
};
|
||||
}
|
||||
|
||||
async createStock(hashedUserId, branchId, stockData) {
|
||||
const u = await getFalukantUserOrFail(hashedUserId);
|
||||
const b = await getBranchOrFail(u.id, branchId);
|
||||
|
||||
Reference in New Issue
Block a user