All files / services/config-service/src/migrations initPlans.js

40% Statements 14/35
66.66% Branches 2/3
100% Functions 1/1
40% Lines 14/35

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 361x 1x 1x 1x 1x 1x 1x 1x 1x                                           1x 1x 1x 1x 1x  
const Organization = require('../models/Organization');
const OrganizationPlan = require('../models/OrganizationPlan');
const UsageTracking = require('../models/UsageTracking');
 
async function migrate() {
  const orgs = await Organization.find({});
  let migrated = 0;
 
  for (const org of orgs) {
    const existingPlan = await OrganizationPlan.findOne({ organizationId: org._id });
    if (!existingPlan) {
      const periodEnd = new Date(org.createdAt.getTime() + 30 * 24 * 60 * 60 * 1000);
      await OrganizationPlan.create({
        organizationId: org._id,
        planCode: 'free',
        status: 'active',
        currentPeriodStart: org.createdAt,
        currentPeriodEnd: periodEnd,
      });
      await UsageTracking.create({
        organizationId: org._id,
        periodStart: org.createdAt,
        periodEnd: periodEnd,
        kbItemCount: 0,
        llmCostUsed: 0,
        ocrCostUsed: 0,
      });
      migrated++;
    }
  }
 
  return migrated;
}
 
module.exports = { migrate };