 DROP FUNCTION get_total_tasks_by_user(uuid) 

CREATE OR REPLACE FUNCTION get_total_tasks_by_user(userId UUID)
RETURNS TABLE(
    total_tasks bigINT
) AS $$
BEGIN
    RETURN QUERY
    SELECT 
        COALESCE(SUM(s.soh_no_sales), 0) + 
        COALESCE(SUM(s.oos), 0) + 
        COALESCE(SUM(s.negative_stock), 0) + 
        COALESCE(SUM(s.no_sales_100_days), 0) + 
        COALESCE(SUM(s.seven_day_cover), 0) AS total_tasks
    FROM store s
    JOIN store_user_link sul ON s.id = sul.store_id
    WHERE sul.user_id = userId;
END;
$$
 LANGUAGE plpgsql;
 

select * from get_total_tasks_by_user('c2a19dcb-a8b2-4c9c-b2b1-5c209a4efd02');


CREATE TABLE user_store AS
SELECT user_id, store_id
FROM store_user_link;