Skip to content

Commit

Permalink
promo codes with days of subsc exceeding
Browse files Browse the repository at this point in the history
  • Loading branch information
ksydex committed Apr 14, 2020
1 parent 942e9c0 commit 244057b
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 63 deletions.
113 changes: 61 additions & 52 deletions .idea/workspace.xml

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

7 changes: 4 additions & 3 deletions controllers/admin/Promo.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@ const { copyDATA } = require('../../data')

const Promo = {
async create(req, res) {
const { key, infinite, plan_id } = req.body
const { key, infinite, plan_id, subscription_duration } = req.body

const query = `INSERT INTO promo_codes(key, infinite, plan_id) VALUES($1,$2,$3) RETURNING *`
const values = [key, infinite, plan_id]
const query = `INSERT INTO promo_codes(key, infinite, plan_id, subscription_duration) VALUES($1,$2,$3,$4) RETURNING *`
const values = [key, infinite, plan_id, subscription_duration]

try {
const { rows } = await db.query(query, values)
return utils.response.success(res, rows[0])
} catch (e) {
console.log(e)
return utils.response.error(res, 'Промокод с таким ключом уже существует')
}
},
Expand Down
19 changes: 14 additions & 5 deletions controllers/user/Auth/SignUp.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ async function isHashAndPaymentDone(hash) {
if (info && info.used === false) {
if (info.p_status === 'succeeded')
return { success: true, status: 'succeeded', user_id: info.user_id }
else if (info.p_status === 'promo_code')
return { success: true, status: 'promo_code', 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 }

let kassaPayment = await kassa.getPaymentStatus(info.p_key)
if (info.p_status !== kassaPayment.status)
Expand Down Expand Up @@ -95,7 +95,7 @@ const SignUp = {
null,
'PLAN_BUY',
0,
'promo_code'
'promo_code ' + codeStatus.key
)
}
}
Expand Down Expand Up @@ -191,12 +191,21 @@ const SignUp = {

if (
isOk.success === true &&
(isOk.status === 'succeeded' || isOk.status === 'promo_code')
(isOk.status === 'succeeded' ||
isOk.status.indexOf('promo_code') !== -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 subscription_duration = 31

const passwordHashed = await bcrypt.hash(password, SALT_ROUNDS)

let query = `UPDATE users SET password=$1, first_name=$2, last_name=$3, patronymic=$4, weight_start=$5, avatar_src=$6, height=$8, age=$9,
date_signup=current_timestamp, subscription_exp = current_timestamp + '31 days' WHERE id=$7 RETURNING TRUE, plan_id`
date_signup=current_timestamp, subscription_exp = current_timestamp + '${subscription_duration} days' WHERE id=$7 RETURNING TRUE, plan_id`

let values = [
passwordHashed,
Expand Down
3 changes: 1 addition & 2 deletions data/json/plans.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
"name": "Супер-солдат с супер-наставником",
"description": "Похудение в паре с опытным офицером Армии. Он смог и вы сможете! К пакету «Супер-солдат» добавляется - личный чат с офицером похудения + 2 онлайн видео-консультации!",
"cost": 3900,
"color": "#f6b543",
"locked_message": "Доступно с 1 апреля!"
"color": "#f6b543"
}
]
1 change: 1 addition & 0 deletions models/promo_codes.sql
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ CREATE TABLE IF NOT EXISTS promo_codes (
plan_id INTEGER NOT NULL,
used BOOLEAN DEFAULT FALSE,
date_expire timestamp DEFAULT NULL,
subscription_duration INTEGER DEFAULT NULL,
date timestamp DEFAULT current_timestamp
);
2 changes: 1 addition & 1 deletion utils/user/Promo.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const Promo = {
const values = [code]
try {
const { rows } = await db.query(query, values)
if (rows && rows[0]) return { plan_id: rows[0].plan_id }
if (rows && rows[0]) return rows[0]
else return false
} catch (e) {
return false
Expand Down

0 comments on commit 244057b

Please sign in to comment.