From b44c318a5d44f3b5d6f2ed85bd76a40a2f841a68 Mon Sep 17 00:00:00 2001 From: Ahmed Bouhuolia Date: Fri, 19 Apr 2024 09:34:47 +0200 Subject: [PATCH] feat: seed free subscription to tenants that have no subscription. --- ...34235_seed_free_subscription_to_tenants.js | 7 +++++ .../seeds/seed_tenants_free_subscription.js | 26 +++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 packages/server/src/system/migrations/20240222134235_seed_free_subscription_to_tenants.js create mode 100644 packages/server/src/system/seeds/seed_tenants_free_subscription.js diff --git a/packages/server/src/system/migrations/20240222134235_seed_free_subscription_to_tenants.js b/packages/server/src/system/migrations/20240222134235_seed_free_subscription_to_tenants.js new file mode 100644 index 000000000..5368db3c0 --- /dev/null +++ b/packages/server/src/system/migrations/20240222134235_seed_free_subscription_to_tenants.js @@ -0,0 +1,7 @@ +exports.up = function (knex) { + return knex.seed.run({ + specific: 'seed_tenants_free_subscription.js', + }); +}; + +exports.down = function (knex) {}; diff --git a/packages/server/src/system/seeds/seed_tenants_free_subscription.js b/packages/server/src/system/seeds/seed_tenants_free_subscription.js new file mode 100644 index 000000000..0c08a41b1 --- /dev/null +++ b/packages/server/src/system/seeds/seed_tenants_free_subscription.js @@ -0,0 +1,26 @@ +exports.seed = (knex) => { + // Deletes ALL existing entries + return knex('subscription_plan_subscriptions') + .then(async () => { + const tenants = await knex('tenants'); + + for (const tenant of tenants) { + const existingSubscription = await knex('subscription_plan_subscriptions') + .where('tenantId', tenant.id) + .first(); + + if (!existingSubscription) { + const freePlan = await knex('subscription_plans').where('slug', 'free').first(); + + await knex('subscription_plan_subscriptions').insert({ + tenantId: tenant.id, + planId: freePlan.id, + slug: 'main', + startsAt: knex.fn.now(), + endsAt: null, + createdAt: knex.fn.now(), + }); + } + } + }); +};