diff --git a/apps/OpenSign/src/constant/Utils.js b/apps/OpenSign/src/constant/Utils.js index 849a59c32..599370ff5 100644 --- a/apps/OpenSign/src/constant/Utils.js +++ b/apps/OpenSign/src/constant/Utils.js @@ -13,25 +13,44 @@ export const openInNewTab = (url) => { window.open(url, "_blank", "noopener,noreferrer"); }; +export async function fetchSubscription() { + try { + const extClass = localStorage.getItem("Extand_Class"); + const jsonSender = JSON.parse(extClass); + const baseURL = localStorage.getItem("baseUrl"); + const url = `${baseURL}functions/getsubscriptions`; + const headers = { + "Content-Type": "application/json", + "X-Parse-Application-Id": localStorage.getItem("parseAppId"), + sessionToken: localStorage.getItem("accesstoken") + }; + const params = { extUserId: jsonSender[0].objectId }; + const tenatRes = await axios.post(url, params, { headers: headers }); + const plan = tenatRes.data?.result?.result?.PlanName; + const billingDate = tenatRes.data?.result?.result?.Next_billing_date?.iso; + return { plan, billingDate }; + } catch (err) { + console.log("Err in fetch subscription", err); + return { plan: "", billingDate: "" }; + } +} //function to get subcripition details from Extand user class export async function checkIsSubscribed() { - const extClass = localStorage.getItem("Extand_Class"); - const jsonSender = JSON.parse(extClass); - const user = await Parse.Cloud.run("getUserDetails", { - email: jsonSender[0].Email - }); - const freeplan = user?.get("Plan") && user?.get("Plan")?.plan_code; - const billingDate = - user?.get("Next_billing_date") && user?.get("Next_billing_date"); - if (freeplan === "freeplan") { - return false; - } else if (billingDate) { - if (billingDate > new Date()) { - return true; + try { + const res = await fetchSubscription(); + if (res.plan === "freeplan") { + return false; + } else if (res.billingDate) { + if (new Date(res.billingDate) > new Date()) { + return true; + } else { + return false; + } } else { return false; } - } else { + } catch (err) { + console.log("Err in fetch subscription", err); return false; } } @@ -1229,8 +1248,8 @@ export const multiSignEmbed = async ( position.type === radioButtonWidget ? 10 : position.type === "checkbox" - ? 10 - : newUpdateHeight; + ? 10 + : newUpdateHeight; const newHeight = ind ? (ind > 0 ? widgetHeight : 0) : widgetHeight; if (signyourself) { diff --git a/apps/OpenSign/src/layout/HomeLayout.js b/apps/OpenSign/src/layout/HomeLayout.js index 26a386455..4a40ee111 100644 --- a/apps/OpenSign/src/layout/HomeLayout.js +++ b/apps/OpenSign/src/layout/HomeLayout.js @@ -11,6 +11,7 @@ import ModalUi from "../primitives/ModalUi"; import { useNavigate, useLocation, Outlet } from "react-router-dom"; import { isEnableSubscription } from "../constant/const"; import { useCookies } from "react-cookie"; +import { fetchSubscription } from "../constant/Utils"; const HomeLayout = () => { const navigate = useNavigate(); @@ -36,7 +37,7 @@ const HomeLayout = () => { sessionToken: localStorage.getItem("accesstoken") }); if (user) { - localStorage.setItem("profileImg", user.get('ProfilePic')); + localStorage.setItem("profileImg", user.get("ProfilePic") || ""); checkIsSubscribed(); } else { setIsUserValid(false); @@ -69,19 +70,13 @@ const HomeLayout = () => { }; async function checkIsSubscribed() { - const currentUser = Parse.User.current(); - const user = await Parse.Cloud.run("getUserDetails", { - email: currentUser.get("email") - }); if (isEnableSubscription) { - const freeplan = user?.get("Plan") && user?.get("Plan").plan_code; - const billingDate = - user?.get("Next_billing_date") && user?.get("Next_billing_date"); - if (freeplan === "freeplan") { + const res = await fetchSubscription(); + if (res.plan === "freeplan") { setIsUserValid(true); setIsLoader(false); - } else if (billingDate) { - if (billingDate > new Date()) { + } else if (res.billingDate) { + if (new Date(res.billingDate) > new Date()) { setIsUserValid(true); setIsLoader(false); } else { diff --git a/apps/OpenSign/src/pages/Login.js b/apps/OpenSign/src/pages/Login.js index 0ffce338c..2b050dab1 100644 --- a/apps/OpenSign/src/pages/Login.js +++ b/apps/OpenSign/src/pages/Login.js @@ -19,7 +19,7 @@ import Alert from "../primitives/Alert"; import { appInfo } from "../constant/appinfo"; import { fetchAppInfo } from "../redux/reducers/infoReducer"; import { showTenant } from "../redux/reducers/ShowTenant"; -import { getAppLogo } from "../constant/Utils"; +import { fetchSubscription, getAppLogo } from "../constant/Utils"; function Login() { const navigate = useNavigate(); const location = useLocation(); @@ -169,7 +169,7 @@ function Login() { await Parse.Cloud.run("getUserDetails", { email: currentUser.get("email") }).then( - (result) => { + async (result) => { let tenentInfo = []; const results = [result]; if (results) { @@ -270,16 +270,15 @@ function Login() { "userDetails", JSON.stringify(LocalUserDetails) ); - const freeplan = - results[0].get("Plan") && - results[0].get("Plan").plan_code; - const billingDate = - results[0].get("Next_billing_date") && - results[0].get("Next_billing_date"); + const res = await fetchSubscription(); + const freeplan = res.plan; + const billingDate = res.billingDate; if (freeplan === "freeplan") { navigate(redirectUrl); } else if (billingDate) { - if (billingDate > new Date()) { + if ( + new Date(billingDate) > new Date() + ) { localStorage.removeItem( "userDetails" ); @@ -814,7 +813,7 @@ function Login() { await Parse.Cloud.run("getUserDetails", { email: currentUser.get("email") }).then( - (result) => { + async (result) => { let tenentInfo = []; const results = [result]; if (results) { @@ -866,17 +865,13 @@ function Login() { "userDetails", JSON.stringify(LocalUserDetails) ); - const billingDate = - results[0].get("Next_billing_date") && - results[0].get("Next_billing_date"); - const freeplan = - results[0]?.get("Plan") && - results[0]?.get("Plan").plan_code; - + const res = await fetchSubscription(); + const billingDate = res.billingDate; + const freeplan = res.plan; if (freeplan === "freeplan") { navigate(redirectUrl); } else if (billingDate) { - if (billingDate > new Date()) { + if (new Date(billingDate) > new Date()) { localStorage.removeItem("userDetails"); // Redirect to the appropriate URL after successful login navigate(redirectUrl); diff --git a/apps/OpenSign/src/pages/PdfRequestFiles.js b/apps/OpenSign/src/pages/PdfRequestFiles.js index 30598b1c3..71db81ab4 100644 --- a/apps/OpenSign/src/pages/PdfRequestFiles.js +++ b/apps/OpenSign/src/pages/PdfRequestFiles.js @@ -22,7 +22,8 @@ import { onSaveImage, addDefaultSignatureImg, radioButtonWidget, - replaceMailVaribles + replaceMailVaribles, + fetchSubscription } from "../constant/Utils"; import Loader from "../primitives/LoaderWithMsg"; import HandleError from "../primitives/HandleError"; @@ -32,7 +33,6 @@ import PdfDeclineModal from "../primitives/PdfDeclineModal"; import Title from "../components/Title"; import DefaultSignature from "../components/pdf/DefaultSignature"; import ModalUi from "../primitives/ModalUi"; -import Parse from "parse"; function PdfRequestFiles() { const { docId } = useParams(); @@ -135,17 +135,14 @@ function PdfRequestFiles() { // eslint-disable-next-line react-hooks/exhaustive-deps }, [divRef.current]); - async function checkIsSubscribed(email) { - const user = await Parse.Cloud.run("getUserDetails", { - email: email - }); - const freeplan = user?.get("Plan") && user?.get("Plan").plan_code; - const billingDate = - user?.get("Next_billing_date") && user?.get("Next_billing_date"); + async function checkIsSubscribed() { + const res = await fetchSubscription(); + const freeplan = res.plan; + const billingDate = res.billingDate; if (freeplan === "freeplan") { return true; } else if (billingDate) { - if (billingDate > new Date()) { + if (new Date(billingDate) > new Date()) { return true; } else { if (location.pathname.includes("/load/")) { @@ -1095,9 +1092,9 @@ function PdfRequestFiles() { isDecline.currnt === "Sure" ? "Are you sure want to decline this document ?" : isDecline.currnt === "YouDeclined" - ? "You have declined this document!" - : isDecline.currnt === "another" && - "You cannot sign this document as it has been declined by one or more recipient(s)." + ? "You have declined this document!" + : isDecline.currnt === "another" && + "You cannot sign this document as it has been declined by one or more recipient(s)." } footerMessage={isDecline.currnt === "Sure"} declineDoc={declineDoc} diff --git a/apps/OpenSign/src/pages/PlaceHolderSign.js b/apps/OpenSign/src/pages/PlaceHolderSign.js index 59cae9f50..b38e8133f 100644 --- a/apps/OpenSign/src/pages/PlaceHolderSign.js +++ b/apps/OpenSign/src/pages/PlaceHolderSign.js @@ -32,7 +32,8 @@ import { color, getTenantDetails, replaceMailVaribles, - copytoData + copytoData, + fetchSubscription } from "../constant/Utils"; import RenderPdf from "../components/pdf/RenderPdf"; import { useNavigate } from "react-router-dom"; @@ -238,17 +239,14 @@ function PlaceHolderSign() { // eslint-disable-next-line react-hooks/exhaustive-deps }, [divRef.current]); - async function checkIsSubscribed(email) { - const user = await Parse.Cloud.run("getUserDetails", { - email: email - }); - const freeplan = user?.get("Plan") && user?.get("Plan").plan_code; - const billingDate = - user?.get("Next_billing_date") && user?.get("Next_billing_date"); + async function checkIsSubscribed() { + const res = await fetchSubscription(); + const freeplan = res.plan; + const billingDate = res.billingDate; if (freeplan === "freeplan") { return true; } else if (billingDate) { - if (billingDate > new Date()) { + if (new Date(billingDate) > new Date()) { setIsSubscribe(true); return true; } else { diff --git a/apps/OpenSign/src/pages/TemplatePlaceholder.js b/apps/OpenSign/src/pages/TemplatePlaceholder.js index 3870d86db..477eb9a02 100644 --- a/apps/OpenSign/src/pages/TemplatePlaceholder.js +++ b/apps/OpenSign/src/pages/TemplatePlaceholder.js @@ -23,7 +23,8 @@ import { defaultWidthHeight, addWidgetOptions, textInputWidget, - radioButtonWidget + radioButtonWidget, + fetchSubscription } from "../constant/Utils"; import RenderPdf from "../components/pdf/RenderPdf"; import "../styles/AddUser.css"; @@ -35,7 +36,6 @@ import AddRoleModal from "../components/pdf/AddRoleModal"; import PlaceholderCopy from "../components/pdf/PlaceholderCopy"; import TourContentWithBtn from "../primitives/TourContentWithBtn"; import DropdownWidgetOption from "../components/pdf/DropdownWidgetOption"; -import Parse from "parse"; const TemplatePlaceholder = () => { const navigate = useNavigate(); const { templateId } = useParams(); @@ -170,17 +170,14 @@ const TemplatePlaceholder = () => { } // eslint-disable-next-line react-hooks/exhaustive-deps }, [divRef.current]); - async function checkIsSubscribed(email) { - const user = await Parse.Cloud.run("getUserDetails", { - email: email - }); - const freeplan = user?.get("Plan") && user?.get("Plan").plan_code; - const billingDate = - user?.get("Next_billing_date") && user?.get("Next_billing_date"); + async function checkIsSubscribed() { + const res = await fetchSubscription(); + const freeplan = res.plan; + const billingDate = res.billingDate; if (freeplan === "freeplan") { return true; } else if (billingDate) { - if (billingDate > new Date()) { + if (new Date(billingDate) > new Date()) { setIsSubscribe(true); return true; } else { diff --git a/apps/OpenSignServer/cloud/customRoute/saveInvoice.js b/apps/OpenSignServer/cloud/customRoute/saveInvoice.js index 615d3b301..63e914c5f 100644 --- a/apps/OpenSignServer/cloud/customRoute/saveInvoice.js +++ b/apps/OpenSignServer/cloud/customRoute/saveInvoice.js @@ -31,6 +31,13 @@ export default async function saveInvoice(request, response) { className: '_User', objectId: extUser.get('UserId').id, }); + if (extUser?.get('TenantId')?.id) { + createInvoice.set('TenantId', { + __type: 'Pointer', + className: 'partners_Tenant', + objectId: extUser.get('TenantId').id, + }); + } await createInvoice.save(null, { useMasterKey: true }); return response.status(200).json({ status: 'create invoice!' }); } diff --git a/apps/OpenSignServer/cloud/customRoute/savePayments.js b/apps/OpenSignServer/cloud/customRoute/savePayments.js index f032cdbc2..30ac4eee4 100644 --- a/apps/OpenSignServer/cloud/customRoute/savePayments.js +++ b/apps/OpenSignServer/cloud/customRoute/savePayments.js @@ -30,6 +30,13 @@ export default async function savePayments(request, response) { className: '_User', objectId: extUser.get('UserId').id, }); + if (extUser?.get('TenantId')?.id) { + createPayment.set('TenantId', { + __type: 'Pointer', + className: 'partners_Tenant', + objectId: extUser.get('TenantId').id, + }); + } await createPayment.save(null, { useMasterKey: true }); return response.status(200).json({ status: 'create payments!' }); } diff --git a/apps/OpenSignServer/cloud/customRoute/saveSubscription.js b/apps/OpenSignServer/cloud/customRoute/saveSubscription.js index b7203be10..bf396bc58 100644 --- a/apps/OpenSignServer/cloud/customRoute/saveSubscription.js +++ b/apps/OpenSignServer/cloud/customRoute/saveSubscription.js @@ -9,7 +9,11 @@ export default async function saveSubscription(request, response) { const extUser = await extUserCls.first({ useMasterKey: true }); if (extUser) { const subcriptionCls = new Parse.Query('contracts_Subscriptions'); - subcriptionCls.equalTo('SubscriptionId', SubscriptionId); + subcriptionCls.equalTo('TenantId', { + __type: 'Pointer', + className: 'partners_Tenant', + objectId: extUser.get('TenantId').id, + }); const subscription = await subcriptionCls.first({ useMasterKey: true }); if (subscription) { const updateSubscription = new Parse.Object('contracts_Subscriptions'); @@ -31,6 +35,11 @@ export default async function saveSubscription(request, response) { className: '_User', objectId: extUser.get('UserId').id, }); + createSubscription.set('TenantId', { + __type: 'Pointer', + className: 'partners_Tenant', + objectId: extUser.get('TenantId').id, + }); await createSubscription.save(null, { useMasterKey: true }); return response.status(200).json({ status: 'create subscription!' }); } diff --git a/apps/OpenSignServer/cloud/parsefunction/SubscribeFree.js b/apps/OpenSignServer/cloud/parsefunction/SubscribeFree.js index 03c4fb95f..a51f40ebb 100644 --- a/apps/OpenSignServer/cloud/parsefunction/SubscribeFree.js +++ b/apps/OpenSignServer/cloud/parsefunction/SubscribeFree.js @@ -6,27 +6,51 @@ export default async function SubscribeFree(request) { extQuery.equalTo('UserId', userPtr); const extUser = await extQuery.first({ useMasterKey: true }); if (extUser) { - if (extUser?.get('Plan')?.plan_code === 'freeplan') { + const subscriptionCls = new Parse.Query('contracts_Subscriptions'); + subscriptionCls.equalTo('TenantId', { + __type: 'Pointer', + className: 'partners_Tenant', + objectId: extUser.get('TenantId').id, + }); + subscriptionCls.descending('createdAt'); + const subcripitions = await subscriptionCls.first({ useMasterKey: true }); + if (subcripitions?.get('PlanName') === 'freeplan') { return { status: 'success', result: 'already subscribed!' }; - } else if (extUser?.get('Next_billing_date') < new Date()) { + } else if (subcripitions?.get('Next_billing_date') < new Date()) { try { - const extUpdate = new Parse.Object('contracts_Users'); - extUpdate.id = extUser.id; - extUpdate.set('Plan', { plan_code: 'freeplan' }); - await extUpdate.save(null, { useMasterKey: true }); + const updateSubscription = new Parse.Object('contracts_Subscriptions'); + updateSubscription.id = subcripitions.id; + updateSubscription.set('PlanName', 'freeplan'); + await updateSubscription.save(null, { useMasterKey: true }); return { status: 'success', result: 'subscribed!' }; } catch (err) { console.log('err ', err); return { status: 'error', result: err.message }; } - } else if (extUser?.get('Next_billing_date') > new Date()) { + } else if (subcripitions?.get('Next_billing_date') > new Date()) { return { status: 'success', result: 'already subscribed!' }; } else { try { - const extUpdate = new Parse.Object('contracts_Users'); - extUpdate.id = extUser.id; - extUpdate.set('Plan', { plan_code: 'freeplan' }); - await extUpdate.save(null, { useMasterKey: true }); + const createSubscription = new Parse.Object('contracts_Subscriptions'); + createSubscription.set('PlanName', 'freeplan'); + createSubscription.set('ExtUserPtr', { + __type: 'Pointer', + className: 'contracts_Users', + objectId: extUser.id, + }); + createSubscription.set('CreatedBy', { + __type: 'Pointer', + className: '_User', + objectId: extUser.get('UserId').id, + }); + if (extUser?.get('TenantId')) { + createSubscription.set('TenantId', { + __type: 'Pointer', + className: 'partners_Tenant', + objectId: extUser.get('TenantId').id, + }); + } + await createSubscription.save(null, { useMasterKey: true }); return { status: 'success', result: 'subscribed!' }; } catch (err) { console.log('err ', err); diff --git a/apps/OpenSignServer/cloud/parsefunction/getSubscriptions.js b/apps/OpenSignServer/cloud/parsefunction/getSubscriptions.js index b7b2b7ada..f094143ba 100644 --- a/apps/OpenSignServer/cloud/parsefunction/getSubscriptions.js +++ b/apps/OpenSignServer/cloud/parsefunction/getSubscriptions.js @@ -12,19 +12,25 @@ export default async function getSubscription(request) { }); const userId = userRes.data && userRes.data.objectId; if (userId) { - const subscriptionCls = new Parse.Query('contracts_Subscriptions'); - subscriptionCls.equalTo('ExtUserPtr', { - __type: 'Pointer', - className: 'contracts_Users', - objectId: extUserId, - }); - subscriptionCls.descending('createdAt'); - const subcripitions = await subscriptionCls.first({ useMasterKey: true }); - if (subcripitions) { - const _subcripitions = JSON.parse(JSON.stringify(subcripitions)); - return { status: 'success', result: _subcripitions }; + const subcriptionCls = new Parse.Query('contracts_Users'); + const exUser = await subcriptionCls.get(extUserId, { useMasterKey: true }); + if (exUser) { + const subscriptionCls = new Parse.Query('contracts_Subscriptions'); + subscriptionCls.equalTo('TenantId', { + __type: 'Pointer', + className: 'partners_Tenant', + objectId: exUser.get('TenantId').id, + }); + subscriptionCls.descending('createdAt'); + const subcripitions = await subscriptionCls.first({ useMasterKey: true }); + if (subcripitions) { + const _subcripitions = JSON.parse(JSON.stringify(subcripitions)); + return { status: 'success', result: _subcripitions }; + } else { + return { status: 'success', result: {} }; + } } else { - return { status: 'success', result: {} }; + return { status: 'error', result: 'User not found!' }; } } else { return { status: 'error', result: 'Invalid session token!' };