-- Add 'mark-as-paid' action to department_expense_claims permission for positions
-- that already have the 'approve' action on it.
-- This is intended for rolling out the new "mark expense claim as paid" feature
-- to managers/accountants who currently approve claims, without granting it to everyone.
--
-- Safe to run multiple times: only updates rows that don't already have the action.
--
-- Run on server:
--   docker-compose exec -T postgres psql -U postgres -d mind14_crm -f - < backend/prisma/add-expense-mark-paid-permission.sql
-- Or from backend:
--   npx prisma db execute --file prisma/add-expense-mark-paid-permission.sql

UPDATE position_permissions
SET
  actions    = actions || '["mark-as-paid"]'::jsonb,
  "updatedAt" = NOW()
WHERE module   = 'department_expense_claims'
  AND resource = '*'
  AND NOT (actions @> '["mark-as-paid"]'::jsonb)
  AND NOT (actions @> '["*"]'::jsonb)
  AND NOT (actions @> '["all"]'::jsonb)
  AND actions @> '["approve"]'::jsonb;
