36 lines
1.5 KiB
PL/PgSQL
36 lines
1.5 KiB
PL/PgSQL
-- Brings back what 000094 and 000098 set up. The outlet settings removed by the up
|
|||
|
|
-- migration are not restored: every outlet falls back to not accepting EnakPoint.
|
||
|
|
|
||
|
|
ALTER TABLE payments
|
||
|
|
ADD COLUMN IF NOT EXISTS points_used BIGINT,
|
||
|
|
ADD COLUMN IF NOT EXISTS point_value DECIMAL(10,2),
|
||
|
|
ADD CONSTRAINT chk_payments_point_pair CHECK (
|
||
|
|
(points_used IS NULL) = (point_value IS NULL)
|
||
|
|
AND (points_used IS NULL OR (points_used > 0 AND point_value > 0)));
|
||
|
|
|
||
|
|
ALTER TABLE payment_methods DROP CONSTRAINT IF EXISTS payment_methods_type_check;
|
||
|
|
ALTER TABLE payment_methods ADD CONSTRAINT payment_methods_type_check
|
||
|
|
CHECK (type IN ('cash', 'card', 'digital_wallet', 'qr', 'edc', 'delivery', 'point'));
|
||
|
|
|
||
|
|
CREATE UNIQUE INDEX uq_payment_methods_point_per_organization ON payment_methods(organization_id)
|
||
|
|
WHERE type = 'point';
|
||
|
|
|
||
|
|
INSERT INTO payment_methods (organization_id, name, type, is_active)
|
||
|
|
SELECT id, 'EnakPoint', 'point', TRUE FROM organizations
|
||
|
|
ON CONFLICT (organization_id) WHERE type = 'point' DO NOTHING;
|
||
|
|
|
||
|
|
CREATE OR REPLACE FUNCTION create_point_payment_method()
|
||
|
|
RETURNS TRIGGER AS $$
|
||
|
|
BEGIN
|
||
|
|
INSERT INTO payment_methods (organization_id, name, type, is_active)
|
||
|
|
VALUES (NEW.id, 'EnakPoint', 'point', TRUE)
|
||
|
|
ON CONFLICT (organization_id) WHERE type = 'point' DO NOTHING;
|
||
|
|
RETURN NEW;
|
||
|
|
END;
|
||
|
|
$$ LANGUAGE plpgsql;
|
||
|
|
|
||
|
|
CREATE TRIGGER trigger_create_point_payment_method
|
||
|
|
AFTER INSERT ON organizations
|
||
|
|
FOR EACH ROW
|
||
|
|
EXECUTE FUNCTION create_point_payment_method();
|