Files
apskel-pos-backend/migrations/000094_add_point_payment_method.up.sql
T
efrilmandClaude Opus 5.5 cf5332c281 feat(loyalty): EnakPoint payment method
Adds the system payment method for paying with EnakPoint
(docs/prd-point-coin.md F9, §8, §10.5, PC-303).

Migration 000094 allows the point type, keeps one per organization with a
partial unique index, creates it for every existing organization, and adds
a trigger that creates it for new ones, as the walk-in customer is. It adds
payments.points_used and point_value. Their CHECK is written so it can
never be NULL: the PRD form, (both NULL) OR (both > 0), is NULL for
points_used with a NULL point_value, which a CHECK lets through, so a
payment could have lost the value a refund depends on. A test caught it.

The API cannot create, delete or retype the EnakPoint method, nor turn
another method into one; that answers 400. Renaming it is allowed. The
method list takes the outlet from ?outlet_id= or the user's outlet and
leaves EnakPoint out when that outlet does not accept it, filtered in the
query so the count stays right. The organization-wide active list is
unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-30 11:33:25 +07:00

46 lines
2.0 KiB
PL/PgSQL

-- Paying with EnakPoint (docs/prd-point-coin.md F9, §8, §10.5).
-- A new payment method type. Every organization has exactly one method of it, made by
-- the system, which cannot be deleted or change type.
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', '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;
-- New organizations get theirs the same way they get their walk-in customer, whatever
-- code path creates them.
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();
-- A payment made with EnakPoint records how many were used and the rupiah value of one
-- at that moment. The value is frozen so a refund returns exactly the EnakPoint used,
-- whatever the value is by then.
--
-- Written so it never evaluates to NULL: the form in the PRD, (both NULL) OR (both
-- > 0), is NULL for points_used = 1000 with point_value NULL, and a CHECK only rejects
-- FALSE, so a payment could lose its frozen value.
ALTER TABLE payments
ADD COLUMN points_used BIGINT,
ADD COLUMN 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)));