Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: avoid import action break subscription restriction #714

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
refactor: move 'checkPlanTypeFromUserSubscription' from user service …
…to plan service
danshuitaihejie committed Jun 29, 2024
commit cd96746b2891f7134b0fda3a80bf54fff16fe444
23 changes: 23 additions & 0 deletions src/services/planService.js
Original file line number Diff line number Diff line change
@@ -45,6 +45,29 @@ const getPlanByType = (planType) => {

return planMap[planType] || planMap['free'];
};

const checkPlanTypeFromUserSubscription = (
isSubscribed,
getSubscriptionPassthroughFunc,
) => {
if (!isSubscribed) return 'free';
// Compatible with previous pro users, before subscription.passthrough only stored userId
const passthrough = getSubscriptionPassthroughFunc();
return isJSONString(passthrough)
? JSON.parse(passthrough).planType
: 'basic-monthly';
};

function isJSONString(str) {
try {
JSON.parse(str);
return true;
} catch (e) {
return false;
}
}

export default {
checkPlanTypeFromUserSubscription: checkPlanTypeFromUserSubscription,
getPlanByType: getPlanByType,
};
24 changes: 4 additions & 20 deletions src/services/user_service.js
Original file line number Diff line number Diff line change
@@ -6,35 +6,19 @@ export default {
user: user,
subscription: subscription,
isSubscribed: function () {
//console.debug('subscription', subscription());
return (
subscription() &&
(subscription().status === 'active' ||
subscription().status === 'trialing')
);
},
getPlanType: function () {
if (!this.isSubscribed()) return 'free';
const currentSubscription = subscription();
return getPlanTypeFromPassthrough(currentSubscription.passthrough);
return planService.checkPlanTypeFromUserSubscription(
this.isSubscribed(),
() => subscription().passthrough,
);
},
getPlan: function () {
return planService.getPlanByType(this.getPlanType());
},
};

// Compatible with previous pro users, before subscription.passthrough only stored userId
function getPlanTypeFromPassthrough(passthrough) {
return isJSONString(passthrough)
? JSON.parse(passthrough).planType
: 'basic-monthly';
}

function isJSONString(str) {
try {
JSON.parse(str);
return true;
} catch (e) {
return false;
}
}