Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into 550_Setup_heroku_we…
Browse files Browse the repository at this point in the history
…bworkers
  • Loading branch information
alexeykosinski committed Dec 17, 2019
2 parents 740cbc2 + 482b171 commit 19b3ccb
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .env.example
Expand Up @@ -53,3 +53,6 @@ NUMBER_OF_ADMIN_WALLET_ACCOUNTS=1
ADMIN_MIN_BALANCE=
#password to sign and encrypt jwt token for market (should be 32 symbols)
MARKET_PASSWORD=
# You can use it to test Hanuka bonus. Required. Format: { start: 23/12/2019, end: 30/12/2019 }
HANUKA_START_DATE=
HANUKA_END_DATE=
4 changes: 4 additions & 0 deletions src/server/db/mongo/models/user-private.js
Expand Up @@ -53,6 +53,10 @@ export const UserPrivateSchema = new mongoose.Schema({
mnemonic: {
type: String
},
hanukaBonus: {
type: mongoose.Schema.Types.Mixed,
default: {}
},
isCompleted: {
whiteList: {
type: Boolean,
Expand Down
12 changes: 12 additions & 0 deletions src/server/server.config.js
Expand Up @@ -361,6 +361,18 @@ const conf = convict({
format: Boolean,
env: 'ETORO',
default: false
},
hanukaStartDate: {
doc: 'Hanuka Start Day',
format: '*',
env: 'HANUKA_START_DATE',
default: undefined
},
hanukaEndDate: {
doc: 'Hanuka End Day',
format: '*',
env: 'HANUKA_END_DATE',
default: undefined
}
})

Expand Down
81 changes: 81 additions & 0 deletions src/server/verification/verificationAPI.js
Expand Up @@ -2,6 +2,7 @@
import { Router } from 'express'
import passport from 'passport'
import _ from 'lodash'
import moment from 'moment'
import multer from 'multer'
import type { LoggedUser, StorageAPI, UserRecord, VerificationAPI } from '../../imports/types'
import AdminWallet from '../blockchain/AdminWallet'
Expand Down Expand Up @@ -390,6 +391,86 @@ const setup = (app: Router, verifier: VerificationAPI, storage: StorageAPI) => {
})
)

/**
* @api {post} /verify/hanuka-bonus Check hanuka bonus availability
* @apiName Hanuka Bonus
* @apiGroup Verification
*
* @apiSuccess {Number} ok
* @ignore
*/
app.get(
'/verify/hanuka-bonus',
passport.authenticate('jwt', { session: false }),
wrapAsync(async (req, res, next) => {
const log = req.log.child({ from: 'verificationAPI - verify/hanuka-bonus' })
const { user } = req
const now = moment().utcOffset('+0200')
const startHanuka = moment(conf.hanukaStartDate, 'DD/MM/YYYY')
const endHanuka = moment(conf.hanukaEndDate, 'DD/MM/YYYY').endOf('day')

if (startHanuka.isAfter(now) || now.isAfter(endHanuka)) {
log.info('That is no the period of Hanuka bonus')

return res.json({
ok: 1
})
}

const startsFromDay = startHanuka.format('DD')
const currentDay = now.format('DD')
const currentDayNumber = currentDay.diff(startsFromDay, 'days') + 1
const dayField = `day${currentDayNumber}`

if (user.hanukaBonus && user.hanukaBonus[dayField]) {
log.info('The user already get Hanuka bonus today', { date: now, dayNumber: dayField, user })

return res.json({
ok: 1
})
}

const bonusInWei = gdToWei(currentDayNumber)

log.debug('Hanuka Dates/Data for calculations', {
now,
currentDay,
currentDayNumber,
dayField,
bonusInWei,
bonus: currentDayNumber
})

const { release, fail } = await txManager.lock(user.gdAddress, 0)

AdminWallet.redeemBonuses(user.gdAddress, bonusInWei, {
onTransactionHash: r => {
return res.status(200).json({
ok: 1
})
},
onReceipt: async r => {
log.info('Bonus redeem - receipt received', r)

await storage.updateUser({
identifier: user.loggedInAs,
hanukaBonus: {
...user.hanukaBonus,
[dayField]: true
}
})

release()
},
onError: e => {
log.error('Bonuses charge failed', e.message, e, user)

fail()
}
})
})
)

/**
* @api {get} /verify/w3/email Verify email to be equal with email provided by token from web3
* @apiName Web3 Email Verify
Expand Down

0 comments on commit 19b3ccb

Please sign in to comment.