-
-
Notifications
You must be signed in to change notification settings - Fork 245
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #410 from bigcapitalhq/seed-free-subscription-to-t…
…enants feat: seed free subscription to tenants that have no subscription.
- Loading branch information
Showing
2 changed files
with
33 additions
and
0 deletions.
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
packages/server/src/system/migrations/20240222134235_seed_free_subscription_to_tenants.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
26
packages/server/src/system/seeds/seed_tenants_free_subscription.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(), | ||
}); | ||
} | ||
} | ||
}); | ||
}; |