v1.0.1
Rozliczenia
This commit is contained in:
@@ -35,4 +35,76 @@ router.post('/', (req, res) => {
|
||||
res.status(201).json({ transactions: current.transactions });
|
||||
});
|
||||
|
||||
function validateManualSettlement(req, res, { fromUserId, toUserId, amount }) {
|
||||
if (!fromUserId || !toUserId || !amount) {
|
||||
res.status(400).json({ error: 'fromUserId, toUserId i amount są wymagane' });
|
||||
return false;
|
||||
}
|
||||
if (fromUserId === toUserId) {
|
||||
res.status(400).json({ error: 'Płacący i odbiorca muszą być różnymi osobami' });
|
||||
return false;
|
||||
}
|
||||
if (Number(amount) <= 0) {
|
||||
res.status(400).json({ error: 'Kwota musi być większa od zera' });
|
||||
return false;
|
||||
}
|
||||
const memberIds = getMembers(req.household.id).map((m) => m.id);
|
||||
if (!memberIds.includes(fromUserId) || !memberIds.includes(toUserId)) {
|
||||
res.status(400).json({ error: 'Obie osoby muszą być członkami gospodarstwa' });
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
router.post('/manual', (req, res) => {
|
||||
const { fromUserId, toUserId, amount } = req.body || {};
|
||||
if (!validateManualSettlement(req, res, { fromUserId, toUserId, amount })) return;
|
||||
|
||||
const settlement = {
|
||||
id: uuid(),
|
||||
household_id: req.household.id,
|
||||
from_user_id: fromUserId,
|
||||
to_user_id: toUserId,
|
||||
amount: Number(amount),
|
||||
};
|
||||
db.prepare(
|
||||
'INSERT INTO settlements (id, household_id, from_user_id, to_user_id, amount) VALUES (?, ?, ?, ?, ?)'
|
||||
).run(settlement.id, settlement.household_id, settlement.from_user_id, settlement.to_user_id, settlement.amount);
|
||||
|
||||
res.status(201).json({ settlement });
|
||||
});
|
||||
|
||||
router.put('/:id', (req, res) => {
|
||||
const existing = db
|
||||
.prepare('SELECT * FROM settlements WHERE id = ? AND household_id = ?')
|
||||
.get(req.params.id, req.household.id);
|
||||
if (!existing) {
|
||||
return res.status(404).json({ error: 'Rozliczenie nie znalezione' });
|
||||
}
|
||||
|
||||
const fromUserId = req.body?.fromUserId ?? existing.from_user_id;
|
||||
const toUserId = req.body?.toUserId ?? existing.to_user_id;
|
||||
const amount = req.body?.amount ?? existing.amount;
|
||||
if (!validateManualSettlement(req, res, { fromUserId, toUserId, amount })) return;
|
||||
|
||||
db.prepare('UPDATE settlements SET from_user_id = ?, to_user_id = ?, amount = ? WHERE id = ?').run(
|
||||
fromUserId,
|
||||
toUserId,
|
||||
Number(amount),
|
||||
existing.id
|
||||
);
|
||||
|
||||
res.json({ settlement: db.prepare('SELECT * FROM settlements WHERE id = ?').get(existing.id) });
|
||||
});
|
||||
|
||||
router.delete('/:id', (req, res) => {
|
||||
const result = db
|
||||
.prepare('DELETE FROM settlements WHERE id = ? AND household_id = ?')
|
||||
.run(req.params.id, req.household.id);
|
||||
if (result.changes === 0) {
|
||||
return res.status(404).json({ error: 'Rozliczenie nie znalezione' });
|
||||
}
|
||||
res.status(204).end();
|
||||
});
|
||||
|
||||
module.exports = { router };
|
||||
|
||||
Reference in New Issue
Block a user