Update database schema and tests for subscription and order management

This commit includes a significant change in the way subscriptions and orders are handled. It introduces the ability to update existing orders and subscriptions, including adding and deleting items. The diff also adds checks to ensure that only valid items can be read, and adds corresponding test cases to verify these changes.
This commit is contained in:
giancarlo
2024-05-14 15:38:53 +07:00
parent f07610ad8e
commit fe86b04d95
5 changed files with 150 additions and 42 deletions

View File

@@ -12,7 +12,7 @@ INSERT INTO public.billing_customers(account_id, provider, customer_id)
VALUES (tests.get_supabase_uid('primary_owner'), 'stripe', 'cus_test');
-- Call the upsert_order function
SELECT public.upsert_order(tests.get_supabase_uid('primary_owner'), 'cus_test', 'order_test', 'pending', 'stripe', 100, 'usd', '[{"id":"order_item_1", "product_id": "prod_test", "variant_id": "var_test", "price_amount": 100, "quantity": 1}]');
SELECT public.upsert_order(tests.get_supabase_uid('primary_owner'), 'cus_test', 'order_test', 'pending', 'stripe', 100, 'usd', '[{"id":"order_item_1", "product_id": "prod_test", "variant_id": "var_test", "price_amount": 100, "quantity": 1}, {"id":"order_item_2", "product_id": "prod_test", "variant_id": "var_test_2", "price_amount": 100, "quantity": 1}]');
-- Verify that the order was created correctly
SELECT is(
@@ -24,37 +24,42 @@ SELECT is(
-- Verify that the subscription items were created correctly
SELECT row_eq(
$$ select count(*) from order_items where order_id = 'order_test' $$,
row(1::bigint),
row(2::bigint),
'The order items should be created'
);
-- Call the upsert_order function again to update the order
SELECT public.upsert_order(tests.get_supabase_uid('primary_owner'), 'cus_test', 'order_test', 'succeeded', 'stripe', 100, 'usd', '[{"id":"order_item_1", "product_id": "prod_test", "variant_id": "var_test", "price_amount": 100, "quantity": 10}]');
select public.upsert_order(tests.get_supabase_uid('primary_owner'), 'cus_test', 'order_test', 'succeeded', 'stripe', 100, 'usd', '[{"id":"order_item_1", "product_id": "prod_test", "variant_id": "var_test", "price_amount": 100, "quantity": 10}]');
-- Verify that the order was updated correctly
SELECT is(
(SELECT status FROM public.orders WHERE id = 'order_test'),
select is(
(select status FROM public.orders WHERE id = 'order_test'),
'succeeded',
'The order status should be succeeded'
);
SELECT row_eq(
select row_eq(
$$ select quantity from order_items where variant_id = 'var_test' $$,
row(10::int),
'The subscription items should be updated'
);
select is_empty(
$$ select * from order_items where id = 'order_item_2' $$,
'The order item should be deleted when the order is updated'
);
select tests.authenticate_as('primary_owner');
-- account can read their own subscription
SELECT isnt_empty(
select isnt_empty(
$$ select 1 from orders where id = 'order_test' $$,
'The account can read their own order'
);
SELECT isnt_empty(
select isnt_empty(
$$ select * from order_items where order_id = 'order_test' $$,
'The account can read their own order'
'The account can read their own orders items'
);
-- foreigners
@@ -62,17 +67,18 @@ select tests.create_supabase_user('foreigner');
select tests.authenticate_as('foreigner');
-- account cannot read other's subscription
SELECT is_empty(
select is_empty(
$$ select 1 from orders where id = 'order_test' $$,
'The account cannot read the other account orders'
);
SELECT is_empty(
select is_empty(
$$ select 1 from order_items where order_id = 'order_test' $$,
'The account cannot read the other account order items'
);
-- Finish the tests and clean up
SELECT * FROM finish();
ROLLBACK;
select * from finish();
rollback;