Skip to content

Commit

Permalink
feat: system to identify already subscribed plan
Browse files Browse the repository at this point in the history
  • Loading branch information
AldemirLucas committed Sep 3, 2024
1 parent 317b8c4 commit 3de57bc
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 4 deletions.
59 changes: 59 additions & 0 deletions next/pages/api/user/getAlreadySubscribed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import axios from "axios";

const API_URL= `${process.env.NEXT_PUBLIC_API_URL}/api/v1/graphql`

async function getAlreadySubscribed(id, token) {
try {
const res = await axios({
url: API_URL,
method: "POST",
headers: {
Authorization: `Bearer ${token}`
},
data: {
query: `
query {
allAccount (id: "${id}"){
edges {
node {
id
internalSubscription {
edges {
node {
canceledAt
createdAt
isActive
stripeSubscription
planInterval
nextBillingCycle
}
}
}
}
}
}
}
`
}
})
const data = res.data
return data
} catch (error) {
console.error(error)
return "err"
}
}

export default async function handler(req, res) {
const token = () => {
if(req.query.q) return atob(req.query.q)
return req.cookies.token
}

const result = await getAlreadySubscribed(atob(req.query.p), token())

if(result.errors) return res.status(500).json({error: result.errors})
if(result === "err") return res.status(500).json({error: "err"})

res.status(200).json(result?.data?.allAccount?.edges[0]?.node?.internalSubscription)
}
19 changes: 17 additions & 2 deletions next/pages/precos.js
Original file line number Diff line number Diff line change
Expand Up @@ -281,11 +281,26 @@ export function SectionPrice() {
const [username, setUsername] = useState(null)
const [isBDPro, setIsBDPro] = useState(false)
const [isBDEmp, setIsBDEmp] = useState(false)
const [hasSubscribed, setHasSubscribed] = useState(true)

async function alreadySubscribed(id) {
const result = await fetch(`/api/user/getAlreadySubscribed?p=${btoa(id)}`)
.then(res => res.json())
setHasSubscribed(result?.edges.length > 0)
}

useEffect(() => {
let user = null
if(cookies.get("userBD")) user = JSON.parse(cookies.get("userBD"))

if(user) {
const reg = new RegExp("(?<=:).*")
const [ id ] = reg.exec(user.id)
alreadySubscribed(id)
} else {
setHasSubscribed(false)
}

const stripeSubscription = user?.internalSubscription?.edges?.[0]?.node?.stripeSubscription

if(user != null) {
Expand Down Expand Up @@ -412,7 +427,7 @@ export function SectionPrice() {
{name: "Download direto até 1GB (80% das tabelas da plataforma)", tooltip: "Tabelas maiores que 1 GB não estão disponíveis para download parcial ou completo. Esse limite não se aplica ao acesso via SQL, Python e R."}
]}
button={{
text: isBDPro ? "Plano atual" : `Iniciar teste grátis`,
text: isBDPro ? "Plano atual" : hasSubscribed ? "Assinar" : "Iniciar teste grátis",
href: username === null ? `/user/login?q=pro&i=${plans?.[`bd_pro_${toggleAnual ? "year" : "month"}`]._id}` :`/user/${username}?plans_and_payment&q=pro&i=${plans?.[`bd_pro_${toggleAnual ? "year" : "month"}`]._id}`,
isCurrentPlan: isBDPro,
}}
Expand All @@ -429,7 +444,7 @@ export function SectionPrice() {
{name: "Suporte prioritário via email e Discord"}
]}
button={{
text: isBDEmp ? "Plano atual" : "Iniciar teste grátis",
text: isBDEmp ? "Plano atual" : hasSubscribed ? "Assinar" : "Iniciar teste grátis",
href: username === null ? `/user/login?q=empresas&i=${plans?.[`bd_empresas_${toggleAnual ? "year" : "month"}`]._id}` :`/user/${username}?plans_and_payment&q=empresas&i=${plans?.[`bd_empresas_${toggleAnual ? "year" : "month"}`]._id}`,
isCurrentPlan: isBDEmp,
}}
Expand Down
18 changes: 16 additions & 2 deletions next/pages/user/[username].js
Original file line number Diff line number Diff line change
Expand Up @@ -1450,12 +1450,26 @@ const PlansAndPayment = ({ userData }) => {
const [isLoading, setIsLoading] = useState(false)
const [isLoadingH, setIsLoadingH] = useState(false)
const [isLoadingCanSub, setIsLoadingCanSub] = useState(false)
const [hasSubscribed, setHasSubscribed] = useState(true)

const [plans, setPlans] = useState(null)
const [toggleAnual, setToggleAnual] = useState(false)

const subscriptionInfo = userData?.internalSubscription?.edges?.[0]?.node

async function alreadySubscribed(id) {
const result = await fetch(`/api/user/getAlreadySubscribed?p=${btoa(id)}`)
.then(res => res.json())
setHasSubscribed(result?.edges.length > 0)
}

useEffect(() => {
const reg = new RegExp("(?<=:).*")
const [ id ] = reg.exec(userData.id)

alreadySubscribed(id)
}, [userData?.id])

useEffect(() => {
if(PlansModal.isOpen === false) return

Expand Down Expand Up @@ -2010,7 +2024,7 @@ const PlansAndPayment = ({ userData }) => {
{name: "Download direto até 1GB (80% das tabelas da plataforma)", tooltip: "Tabelas maiores que 1 GB não estão disponíveis para download parcial ou completo. Esse limite não se aplica ao acesso via SQL, Python e R."}
]}
button={{
text: `${subscriptionInfo?.stripeSubscription === "bd_pro" ? "Plano atual" : "Assinar"}`,
text: `${subscriptionInfo?.stripeSubscription === "bd_pro" ? "Plano atual" : hasSubscribed ? "Assinar" : "Iniciar teste grátis"}`,
onClick: subscriptionInfo?.stripeSubscription === "bd_pro" ? () => {} : () => {
setPlan({id: plans?.[`bd_pro_${toggleAnual ? "year" : "month"}`]._id})
PlansModal.onClose()
Expand All @@ -2031,7 +2045,7 @@ const PlansAndPayment = ({ userData }) => {
{name: "Suporte prioritário via email e Discord"}
]}
button={{
text: `${subscriptionInfo?.stripeSubscription === "bd_pro_empresas" ? "Plano atual" : "Assinar"}`,
text: `${subscriptionInfo?.stripeSubscription === "bd_pro_empresas" ? "Plano atual" : hasSubscribed ? "Assinar" : "Iniciar teste grátis"}`,
onClick: subscriptionInfo?.stripeSubscription === "bd_pro_empresas" ? () => {} : () => {
setPlan({id: plans?.[`bd_empresas_${toggleAnual ? "year" : "month"}`]._id})
PlansModal.onClose()
Expand Down

0 comments on commit 3de57bc

Please sign in to comment.