Files
cal.diy/packages/app-store/stripepayment/lib/downgrade.ts
T
Omar Lópezandkodiakhq[bot] 464343f5ab Refactors EE code (#3490)
* WIP

* WIP

* Type and migration fixes

* Adds missing default import

* Fixes import

* Fixes tRPC imports in App Store

* Migrate stripe helpers

* WIP

* Type fixes

* Type fix?

* WIP

* WIP

* Update index.ts

* Fixes

* Update workflow.tsx

* Moved queries to lib

* Moves QueryCell

* Migrates MultiSelectCheckboxes

* WIP

* CryptoSection type fixes

* WIP

* Import fixes

* Build fixes

* Update app-providers.tsx

* Build fixes

* Upgrades hookform zod resolvers

* Build fixes

* Cleanup

* Build fixes

* Relocates QueryCell to ui

* Moved List and SkeletonLoader

* Revert QueryCell migration

* Can't use QueryCell here

* oops

* CryptoSection cleanup

* Update app-providers.tsx

* Moved ee to features

* ee to features/ee

* Removes @calcom/ee

* Adds possible feature locations

* Build fixes

* Migrates stripe to app-store lib

* Colocates stripe imports

* Update subscription.ts

* Submodule sync

Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
2022-07-28 13:58:26 -06:00

96 lines
2.9 KiB
TypeScript
Executable File

#!/usr/bin/env ts-node
// To run this script: `yarn downgrade 2>&1 | tee result.log`
import { Prisma, UserPlan } from "@prisma/client";
import dayjs from "@calcom/dayjs";
import { TRIAL_LIMIT_DAYS } from "@calcom/lib/constants";
import prisma from "@calcom/prisma";
// import { isPremiumUserName } from "../../apps/website/lib/username";
import { getStripeCustomerIdFromUserId } from "./customer";
import stripe from "./server";
import { getPremiumPlanPrice, getProPlanPrice, getProPlanProduct } from "./utils";
export async function downgradeIllegalProUsers() {
const illegalProUsers = await prisma.user.findMany({
where: {
plan: UserPlan.PRO,
},
select: {
id: true,
name: true,
email: true,
username: true,
plan: true,
metadata: true,
},
});
const usersDowngraded: Partial<typeof illegalProUsers[number]>[] = [];
const downgrade = async (user: typeof illegalProUsers[number]) => {
await prisma.user.update({
where: { id: user.id },
data: {
plan: UserPlan.TRIAL,
trialEndsAt: dayjs().add(TRIAL_LIMIT_DAYS, "day").toDate(),
},
});
console.log(`Downgraded: ${user.email}`);
usersDowngraded.push({
id: user.id,
username: user.username,
name: user.name,
email: user.email,
plan: user.plan,
metadata: user.metadata,
});
};
for (const suspectUser of illegalProUsers) {
console.log(`Checking: ${suspectUser.email}`);
const metadata = (suspectUser.metadata as Prisma.JsonObject) ?? {};
// if their pro is already sponsored by a team, do not downgrade
if (metadata.proPaidForByTeamId !== undefined) continue;
const stripeCustomerId = await getStripeCustomerIdFromUserId(suspectUser.id);
const customer = await stripe.customers.retrieve(stripeCustomerId, {
expand: ["subscriptions.data.plan"],
});
if (!customer || customer.deleted) {
await downgrade(suspectUser);
continue;
}
const subscription = customer.subscriptions?.data[0];
if (!subscription) {
await downgrade(suspectUser);
continue;
}
const hasProPlan = !!subscription.items.data.find(
(item) =>
item.plan.product === getProPlanProduct() ||
[getProPlanPrice(), getPremiumPlanPrice()].includes(item.plan.id)
);
// if they're pro, do not downgrade
if (hasProPlan) continue;
// If they already have a premium username, do not downgrade
// if (suspectUser.username && isPremiumUserName(suspectUser.username)) continue;
await downgrade(suspectUser);
}
return {
usersDowngraded,
usersDowngradedAmount: usersDowngraded.length,
};
}
downgradeIllegalProUsers()
.then(({ usersDowngraded, usersDowngradedAmount }) => {
console.log(`Downgraded ${usersDowngradedAmount} illegal pro users`);
console.table(usersDowngraded);
})
.catch((e) => {
console.error(e);
process.exit(1);
});