Skip to content

Commit

Permalink
chore: start research for migration
Browse files Browse the repository at this point in the history
  • Loading branch information
grub authored and grub committed Feb 29, 2024
1 parent d5ee96e commit 54619ef
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
5 changes: 5 additions & 0 deletions api/migrations/20240229105707_seeding.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-- Add down migration script here
TRUNCATE TABLE cost_shares;
TRUNCATE TABLE costs;
TRUNCATE TABLE users;
TRUNCATE TABLE wgs;
45 changes: 45 additions & 0 deletions api/migrations/20240229105707_seeding.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
-- Add up migration script here
INSERT INTO wgs
(id, url, name) VALUES
(1, 'lamako', 'LAMAKO WGMBH');

INSERT INTO users
(id, name, username, password_hash, revoke_before, wg) VALUES
(1, 'Konni', 'kartonrad', '$pbkdf2-sha256$i=10000,l=32$R+UA953d9tO8VFqh8vF7TA$wmDkCKb9ScX6MfPJxKNf4dBM9MMHwEeVCsgzMvkqm48',
'NOW', 1);

INSERT INTO users
(id, name, username, password_hash, revoke_before, wg) VALUES
(2, 'Laura', 'lamaura', '$pbkdf2-sha256$i=10000,l=32$R+UA953d9tO8VFqh8vF7TA$wmDkCKb9ScX6MfPJxKNf4dBM9MMHwEeVCsgzMvkqm48',
'NOW', 1);

INSERT INTO users
(id, name, username, password_hash, revoke_before, wg) VALUES
(3, 'Marie', 'marieee', '$pbkdf2-sha256$i=10000,l=32$R+UA953d9tO8VFqh8vF7TA$wmDkCKb9ScX6MfPJxKNf4dBM9MMHwEeVCsgzMvkqm48',
'NOW', 1);


INSERT INTO costs (id, wg_id, name, amount, creditor_id, added_on) VALUES
(1, 1, 'Apotheke IBU/Tests/Masken', 15.0, 1, 'NOW');
INSERT INTO cost_shares (cost_id, debtor_id, paid) VALUES
(1, 1, true);
INSERT INTO cost_shares (cost_id, debtor_id, paid) VALUES
(1, 2, false);
INSERT INTO cost_shares (cost_id, debtor_id, paid) VALUES
(1, 3, false);

INSERT INTO costs (id, wg_id, name, amount, creditor_id, added_on) VALUES
(2, 1, 'Einkauf whatever', 22.0, 2, 'NOW');
INSERT INTO cost_shares (cost_id, debtor_id, paid) VALUES
(2, 1, false);
INSERT INTO cost_shares (cost_id, debtor_id, paid) VALUES
(2, 2, true);

INSERT INTO costs (id, wg_id, name, amount, creditor_id, added_on) VALUES
(3, 1, 'DM Kirschkernkissen', 9.90, 1, 'NOW');
INSERT INTO cost_shares (cost_id, debtor_id, paid) VALUES
(3, 1, true);
INSERT INTO cost_shares (cost_id, debtor_id, paid) VALUES
(3, 2, false);
INSERT INTO cost_shares (cost_id, debtor_id, paid) VALUES
(3, 3, false);
18 changes: 18 additions & 0 deletions api/src/sql/costs.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
SELECT debtor_id, creditor_id, (amount/nr_shares)::NUMERIC(16,2) as owed, paid, id as cost_id , wg_id, equal_balances
FROM cost_shares
LEFT JOIN (
SELECT
costs.id,
amount,
creditor_id,
wg_id,
equal_balances,
count(*) as nr_shares,
sum( CASE WHEN shares.paid = false AND shares.debtor_id != creditor_id THEN 1 ELSE 0 END ) as nr_unpaid_shares
FROM costs
LEFT JOIN cost_shares as shares ON costs.id = shares.cost_id --multiple per row
GROUP BY costs.id
) AS cost_agg ON cost_agg.id = cost_shares.cost_id



--- for GET /costs
SELECT id, wg_id, name, amount, creditor_id, receit_id, added_on, equal_balances, ROW(my_share.cost_id, my_share.debtor_id, my_share.paid) as my_share,
count(*) as nr_shares, sum( CASE WHEN shares.paid = false AND shares.debtor_id != creditor_id THEN 1 ELSE 0 END ) as nr_unpaid_shares
Expand Down

0 comments on commit 54619ef

Please sign in to comment.