Skip to content

Commit

Permalink
plans upd funcs
Browse files Browse the repository at this point in the history
  • Loading branch information
ksydex committed Apr 18, 2020
1 parent 244057b commit becf793
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 81 deletions.
106 changes: 55 additions & 51 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion controllers/Kassa/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ const Kassa = {
}
})
} catch (e) {
console.log('err', e)
return utils.response.error(res, 'Ты не Яндекс.Касса')
}

Expand Down
66 changes: 41 additions & 25 deletions controllers/user/Auth/SignUp.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ async function isHashAndPaymentDone(hash) {
return { success: true, status: 'succeeded', user_id: info.user_id }
else if (info.p_status.indexOf('promo_code') !== -1)
return { success: true, status: info.p_status, user_id: info.user_id }
else if (info.p_status.indexOf('trial') !== -1)
return { success: true, status: info.p_status, user_id: info.user_id }

let kassaPayment = await kassa.getPaymentStatus(info.p_key)
if (info.p_status !== kassaPayment.status)
Expand Down Expand Up @@ -67,37 +69,48 @@ const SignUp = {

let dbpayment = null,
kassaPayment = null
if (!code) {
kassaPayment = await kassa.createPayment({
value: plan.cost,
description: `Оплата пакета "${plan.name} в приложении Культ Тела"`,
return_url: 'first-login/' + hash,
metadata: {
type: 'PLAN_BUY',
hash: hash
}
})

if (!kassaPayment)
return utils.response.error(res, 'Ошибка при создании платежа')
if (typeof plan.trial !== 'number') {
if (!code) {
kassaPayment = await kassa.createPayment({
value: plan.cost,
description: `Оплата пакета "${plan.name} в приложении Культ Тела"`,
return_url: 'first-login/' + hash,
metadata: {
type: 'PLAN_BUY',
hash: hash
}
})

if (!kassaPayment)
return utils.response.error(res, 'Ошибка при создании платежа')

dbpayment = await User.Payment.create(
user_id,
kassaPayment.id,
'PLAN_BUY',
parseInt(plan.cost)
)
} else if (code) {
const codeStatus = await User.Promo.getStatus(code, true)
if (codeStatus && codeStatus.plan_id === plan_id) {
dbpayment = await User.Payment.create(
user_id,
null,
kassaPayment.id,
'PLAN_BUY',
0,
'promo_code ' + codeStatus.key
parseInt(plan.cost)
)
} else if (code) {
const codeStatus = await User.Promo.getStatus(code, true)
if (codeStatus && codeStatus.plan_id === plan_id) {
dbpayment = await User.Payment.create(
user_id,
null,
'PLAN_BUY',
0,
'promo_code ' + codeStatus.key
)
}
}
} else {
dbpayment = await User.Payment.create(
user_id,
null,
'PLAN_BUY',
0,
'trial ' + plan.trial
)
}

if (!dbpayment) {
Expand Down Expand Up @@ -192,14 +205,17 @@ const SignUp = {
if (
isOk.success === true &&
(isOk.status === 'succeeded' ||
isOk.status.indexOf('promo_code') !== -1)
isOk.status.indexOf('promo_code') !== -1 ||
isOk.status.indexOf('trial') !== -1)
) {
let subscription_duration
if (isOk.status.indexOf('promo_code') !== -1) {
const promoCode = await User.Promo.getStatus(
isOk.status.replace('promo_code ', '')
)
subscription_duration = promoCode.subscription_duration || 31
} else if (isOk.status.indexOf('trial') !== -1) {
subscription_duration = parseInt(isOk.status.replace('trial ', ''))
} else subscription_duration = 31

const passwordHashed = await bcrypt.hash(password, SALT_ROUNDS)
Expand Down
5 changes: 4 additions & 1 deletion controllers/user/User/Subscription.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ const { copyDATA } = require('../../../data/')
const Subscription = {
async extend(req, res) {
const { plan_id } = req.body
const plans = copyDATA('plans')
const plans = copyDATA('plans').map(e => {
if (e.trial) delete e.trial
return e
})
const plan = plans.filter(e => parseInt(e.id) === plan_id)[0]
const metadata = {
type: 'PLAN_EXTEND',
Expand Down
6 changes: 4 additions & 2 deletions data/json/plans.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@
"name": "Пехота",
"description": "Видео упражнений и секретные советы по питанию!",
"cost": 800,
"color": "#b4bac2"
"color": "#b4bac2",
"disabled": true
},
{
"id": 1,
"name": "Артиллерия",
"description": "Видео всех упражнений для безопасного похудения и оздоровления, программа тренировок в зависимости от индивидуальных параметров, меню и секретные советы по питанию!",
"cost": 1200,
"color": "#9635db"
"color": "#9635db",
"trial": 7
},
{
"id": 2,
Expand Down
5 changes: 4 additions & 1 deletion utils/user/Plan.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ const Plan = {
const { rows } = await db.query(query, values)
let data = rows[0]
if (data && typeof data.plan_id === 'number') {
const plans = copyDATA('plans')
const plans = copyDATA('plans').map(e => {
if (e.trial) delete e.trial
return e
})
if (data.payment_status === 'promo_code') {
return plans
.map(e => {
Expand Down

0 comments on commit becf793

Please sign in to comment.