@devx-commerce/plugin-loyalty
Version:
A starter for Medusa plugins.
61 lines • 15.3 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Migration20250707060644 = void 0;
const migrations_1 = require("@mikro-orm/migrations");
class Migration20250707060644 extends migrations_1.Migration {
async up() {
this.addSql(`alter table if exists "loyalty_transaction_bucket" drop constraint if exists "loyalty_transaction_bucket_loyalty_transaction_id_unique";`);
this.addSql(`create table if not exists "loyalty_config" ("id" text not null, "points_per_rupee" integer not null default 1, "spend_per_order" integer not null, "spend_per_order_type" text check ("spend_per_order_type" in ('percentage', 'fixed')) not null, "max_spend_per_order" integer null, "min_order_value" integer not null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "deleted_at" timestamptz null, constraint "loyalty_config_pkey" primary key ("id"));`);
this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_loyalty_config_deleted_at" ON "loyalty_config" (deleted_at) WHERE deleted_at IS NULL;`);
this.addSql(`create table if not exists "loyalty_event" ("id" text not null, "name" text not null, "description" text null, "status" text check ("status" in ('active', 'draft')) not null default 'draft', "expiry_days" integer null, "earn_per_order" integer null, "earn_per_order_type" text check ("earn_per_order_type" in ('percentage', 'fixed')) null, "max_earn_per_order" integer null, "min_order_value" integer null, "referee_bonus_per_order" integer null, "referee_bonus_per_order_type" text check ("referee_bonus_per_order_type" in ('percentage', 'fixed')) null, "referrer_bonus_per_order" integer null, "referrer_bonus_per_order_type" text check ("referrer_bonus_per_order_type" in ('percentage', 'fixed')) null, "referrer_bonus" integer null, "referee_bonus" integer null, "trigger_event" text check ("trigger_event" in ('order_placed', 'order_delivered', 'referral_order_placed', 'referral_link_redeemed')) not null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "deleted_at" timestamptz null, constraint "loyalty_event_pkey" primary key ("id"));`);
this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_loyalty_event_deleted_at" ON "loyalty_event" (deleted_at) WHERE deleted_at IS NULL;`);
this.addSql(`create table if not exists "loyalty_referral" ("id" text not null, "referral_code" text not null default 'sp7rpj', "status" text check ("status" in ('active', 'expired')) not null default 'active', "expires_at" timestamptz null, "used_links" text[] not null default '{}', "customer_id" text not null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "deleted_at" timestamptz null, constraint "loyalty_referral_pkey" primary key ("id"));`);
this.addSql(`CREATE UNIQUE INDEX IF NOT EXISTS "IDX_LOYALTY_REFERRAL_CODE" ON "loyalty_referral" (referral_code) WHERE deleted_at IS NULL;`);
this.addSql(`CREATE UNIQUE INDEX IF NOT EXISTS "IDX_LOYALTY_CUSTOMER_ID" ON "loyalty_referral" (customer_id) WHERE deleted_at IS NULL;`);
this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_loyalty_referral_deleted_at" ON "loyalty_referral" (deleted_at) WHERE deleted_at IS NULL;`);
this.addSql(`create table if not exists "loyalty_wallet" ("id" text not null, "points" integer not null default 0, "customer_id" text not null, "used_referrals" text[] null, "total_earned_points" integer not null default 0, "total_used_points" integer not null default 0, "total_expired_points" integer not null default 0, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "deleted_at" timestamptz null, constraint "loyalty_wallet_pkey" primary key ("id"));`);
this.addSql(`CREATE UNIQUE INDEX IF NOT EXISTS "IDX_LOYALTY_CUSTOMER_ID" ON "loyalty_wallet" (customer_id) WHERE deleted_at IS NULL;`);
this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_loyalty_wallet_deleted_at" ON "loyalty_wallet" (deleted_at) WHERE deleted_at IS NULL;`);
this.addSql(`create table if not exists "loyalty_transaction" ("id" text not null, "loyalty_wallet_id" text not null, "points" integer not null, "customer_id" text not null, "order_id" text null, "type" text check ("type" in ('credit', 'debit', 'reversed')) not null, "reference_type" text check ("reference_type" in ('order', 'manual', 'expired', 'order_return', 'referral', 'referral_order')) not null, "status" text check ("status" in ('active', 'reversed')) not null default 'active', "expires_at" timestamptz null, "description" text not null, "referrer_id" text null, "referee_id" text null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "deleted_at" timestamptz null, constraint "loyalty_transaction_pkey" primary key ("id"));`);
this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_loyalty_transaction_loyalty_wallet_id" ON "loyalty_transaction" (loyalty_wallet_id) WHERE deleted_at IS NULL;`);
this.addSql(`CREATE UNIQUE INDEX IF NOT EXISTS "IDX_LOYALTY_CUSTOMER_ID" ON "loyalty_transaction" (customer_id) WHERE deleted_at IS NULL;`);
this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_loyalty_transaction_referrer_id" ON "loyalty_transaction" (referrer_id) WHERE deleted_at IS NULL;`);
this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_loyalty_transaction_referee_id" ON "loyalty_transaction" (referee_id) WHERE deleted_at IS NULL;`);
this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_loyalty_transaction_deleted_at" ON "loyalty_transaction" (deleted_at) WHERE deleted_at IS NULL;`);
this.addSql(`create table if not exists "loyalty_transaction_bucket" ("id" text not null, "loyalty_transaction_id" text not null, "loyalty_wallet_id" text not null, "customer_id" text not null, "original_points" integer not null, "remaining_points" integer not null, "status" text check ("status" in ('active', 'expired', 'depleted')) not null default 'active', "reference_type" text check ("reference_type" in ('order', 'referral', 'manual', 'expired', 'referral_order')) not null, "expires_at" timestamptz null, "description" text not null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "deleted_at" timestamptz null, constraint "loyalty_transaction_bucket_pkey" primary key ("id"));`);
this.addSql(`CREATE UNIQUE INDEX IF NOT EXISTS "IDX_loyalty_transaction_bucket_loyalty_transaction_id_unique" ON "loyalty_transaction_bucket" (loyalty_transaction_id) WHERE deleted_at IS NULL;`);
this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_loyalty_transaction_bucket_loyalty_wallet_id" ON "loyalty_transaction_bucket" (loyalty_wallet_id) WHERE deleted_at IS NULL;`);
this.addSql(`CREATE UNIQUE INDEX IF NOT EXISTS "IDX_LOYALTY_CUSTOMER_ID" ON "loyalty_transaction_bucket" (customer_id) WHERE deleted_at IS NULL;`);
this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_loyalty_transaction_bucket_deleted_at" ON "loyalty_transaction_bucket" (deleted_at) WHERE deleted_at IS NULL;`);
this.addSql(`create table if not exists "loyalty_transaction_history" ("id" text not null, "loyalty_transaction_bucket_id" text not null, "loyalty_wallet_id" text not null, "customer_id" text not null, "order_id" text null, "loyalty_transaction_id" text null, "referral_id" text null, "points_used" integer not null, "type" text check ("type" in ('credit', 'debit')) not null, "status" text check ("status" in ('active', 'expired', 'depleted', 'reversed')) not null default 'active', "reference_type" text check ("reference_type" in ('order', 'referral', 'manual', 'expired', 'order_return')) not null, "created_at" timestamptz not null default now(), "updated_at" timestamptz not null default now(), "deleted_at" timestamptz null, constraint "loyalty_transaction_history_pkey" primary key ("id"));`);
this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_loyalty_transaction_history_loyalty_transaction_bucket_id" ON "loyalty_transaction_history" (loyalty_transaction_bucket_id) WHERE deleted_at IS NULL;`);
this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_loyalty_transaction_history_loyalty_wallet_id" ON "loyalty_transaction_history" (loyalty_wallet_id) WHERE deleted_at IS NULL;`);
this.addSql(`CREATE UNIQUE INDEX IF NOT EXISTS "IDX_LOYALTY_CUSTOMER_ID" ON "loyalty_transaction_history" (customer_id) WHERE deleted_at IS NULL;`);
this.addSql(`CREATE INDEX IF NOT EXISTS "IDX_loyalty_transaction_history_deleted_at" ON "loyalty_transaction_history" (deleted_at) WHERE deleted_at IS NULL;`);
this.addSql(`alter table if exists "loyalty_transaction" add constraint "loyalty_transaction_loyalty_wallet_id_foreign" foreign key ("loyalty_wallet_id") references "loyalty_wallet" ("id") on update cascade;`);
this.addSql(`alter table if exists "loyalty_transaction" add constraint "loyalty_transaction_referrer_id_foreign" foreign key ("referrer_id") references "loyalty_wallet" ("id") on update cascade on delete set null;`);
this.addSql(`alter table if exists "loyalty_transaction" add constraint "loyalty_transaction_referee_id_foreign" foreign key ("referee_id") references "loyalty_wallet" ("id") on update cascade on delete set null;`);
this.addSql(`alter table if exists "loyalty_transaction_bucket" add constraint "loyalty_transaction_bucket_loyalty_transaction_id_foreign" foreign key ("loyalty_transaction_id") references "loyalty_transaction" ("id") on update cascade;`);
this.addSql(`alter table if exists "loyalty_transaction_bucket" add constraint "loyalty_transaction_bucket_loyalty_wallet_id_foreign" foreign key ("loyalty_wallet_id") references "loyalty_wallet" ("id") on update cascade;`);
this.addSql(`alter table if exists "loyalty_transaction_history" add constraint "loyalty_transaction_history_loyalty_transaction__d0241_foreign" foreign key ("loyalty_transaction_bucket_id") references "loyalty_transaction_bucket" ("id") on update cascade;`);
this.addSql(`alter table if exists "loyalty_transaction_history" add constraint "loyalty_transaction_history_loyalty_wallet_id_foreign" foreign key ("loyalty_wallet_id") references "loyalty_wallet" ("id") on update cascade;`);
}
async down() {
this.addSql(`alter table if exists "loyalty_transaction" drop constraint if exists "loyalty_transaction_loyalty_wallet_id_foreign";`);
this.addSql(`alter table if exists "loyalty_transaction" drop constraint if exists "loyalty_transaction_referrer_id_foreign";`);
this.addSql(`alter table if exists "loyalty_transaction" drop constraint if exists "loyalty_transaction_referee_id_foreign";`);
this.addSql(`alter table if exists "loyalty_transaction_bucket" drop constraint if exists "loyalty_transaction_bucket_loyalty_wallet_id_foreign";`);
this.addSql(`alter table if exists "loyalty_transaction_history" drop constraint if exists "loyalty_transaction_history_loyalty_wallet_id_foreign";`);
this.addSql(`alter table if exists "loyalty_transaction_bucket" drop constraint if exists "loyalty_transaction_bucket_loyalty_transaction_id_foreign";`);
this.addSql(`alter table if exists "loyalty_transaction_history" drop constraint if exists "loyalty_transaction_history_loyalty_transaction__d0241_foreign";`);
this.addSql(`drop table if exists "loyalty_config" cascade;`);
this.addSql(`drop table if exists "loyalty_event" cascade;`);
this.addSql(`drop table if exists "loyalty_referral" cascade;`);
this.addSql(`drop table if exists "loyalty_wallet" cascade;`);
this.addSql(`drop table if exists "loyalty_transaction" cascade;`);
this.addSql(`drop table if exists "loyalty_transaction_bucket" cascade;`);
this.addSql(`drop table if exists "loyalty_transaction_history" cascade;`);
}
}
exports.Migration20250707060644 = Migration20250707060644;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiTWlncmF0aW9uMjAyNTA3MDcwNjA2NDQuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi8uLi8uLi8uLi8uLi9zcmMvbW9kdWxlcy9sb3lhbHR5L21pZ3JhdGlvbnMvTWlncmF0aW9uMjAyNTA3MDcwNjA2NDQudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7O0FBQUEsc0RBQWtEO0FBRWxELE1BQWEsdUJBQXdCLFNBQVEsc0JBQVM7SUFDM0MsS0FBSyxDQUFDLEVBQUU7UUFDZixJQUFJLENBQUMsTUFBTSxDQUNULDBJQUEwSSxDQUMzSSxDQUFDO1FBQ0YsSUFBSSxDQUFDLE1BQU0sQ0FDVCxvZkFBb2YsQ0FDcmYsQ0FBQztRQUNGLElBQUksQ0FBQyxNQUFNLENBQ1QsdUhBQXVILENBQ3hILENBQUM7UUFFRixJQUFJLENBQUMsTUFBTSxDQUNULHVrQ0FBdWtDLENBQ3hrQyxDQUFDO1FBQ0YsSUFBSSxDQUFDLE1BQU0sQ0FDVCxxSEFBcUgsQ0FDdEgsQ0FBQztRQUVGLElBQUksQ0FBQyxNQUFNLENBQ1QsdWVBQXVlLENBQ3hlLENBQUM7UUFDRixJQUFJLENBQUMsTUFBTSxDQUNULCtIQUErSCxDQUNoSSxDQUFDO1FBQ0YsSUFBSSxDQUFDLE1BQU0sQ0FDVCwySEFBMkgsQ0FDNUgsQ0FBQztRQUNGLElBQUksQ0FBQyxNQUFNLENBQ1QsMkhBQTJILENBQzVILENBQUM7UUFFRixJQUFJLENBQUMsTUFBTSxDQUNULDhlQUE4ZSxDQUMvZSxDQUFDO1FBQ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