Skip to content

Commit

Permalink
Merge pull request #410 from bigcapitalhq/seed-free-subscription-to-t…
Browse files Browse the repository at this point in the history
…enants

feat: seed free subscription to tenants that have no subscription.
  • Loading branch information
abouolia authored Apr 19, 2024
2 parents bd9717f + b44c318 commit 571a332
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
exports.up = function (knex) {
return knex.seed.run({
specific: 'seed_tenants_free_subscription.js',
});
};

exports.down = function (knex) {};
26 changes: 26 additions & 0 deletions packages/server/src/system/seeds/seed_tenants_free_subscription.js
Original file line number Diff line number Diff line change
@@ -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(),
});
}
}
});
};

0 comments on commit 571a332

Please sign in to comment.