Merge pull request #873 from chaotixkilla/feat-integrate-hedging-and-accounting-with-pazuz-admin
Integrate hedging and accounting with pazuz related screens
This commit is contained in:
commit
d138b26903
40 changed files with 761 additions and 386 deletions
|
|
@ -1,5 +1,6 @@
|
|||
#!/usr/bin/env node
|
||||
|
||||
const { asyncLocalStorage, defaultStore } = require('../lib/async-storage')
|
||||
const authentication = require('../lib/new-admin/graphql/modules/authentication')
|
||||
const options = require('../lib/options')
|
||||
|
||||
|
|
@ -29,7 +30,8 @@ if (role !== 'user' && role !== 'superuser') {
|
|||
process.exit(2)
|
||||
}
|
||||
|
||||
authentication.createRegisterToken(name, role).then(token => {
|
||||
asyncLocalStorage.run(defaultStore(), () => {
|
||||
authentication.createRegisterToken(name, role).then(token => {
|
||||
if (!token) {
|
||||
console.log(`A user named ${name} already exists!`)
|
||||
process.exit(2)
|
||||
|
|
@ -42,7 +44,8 @@ authentication.createRegisterToken(name, role).then(token => {
|
|||
}
|
||||
|
||||
process.exit(0)
|
||||
}).catch(err => {
|
||||
}).catch(err => {
|
||||
console.log('Error: %s', err)
|
||||
process.exit(3)
|
||||
})
|
||||
})
|
||||
|
|
|
|||
|
|
@ -64,8 +64,7 @@ function loadSanctions (settings) {
|
|||
function startServer (settings) {
|
||||
return Promise.resolve()
|
||||
.then(() => {
|
||||
poller.start(settings)
|
||||
|
||||
poller.setup(['public'])
|
||||
const httpsServerOptions = {
|
||||
key: fs.readFileSync(options.keyPath),
|
||||
cert: fs.readFileSync(options.certPath),
|
||||
|
|
|
|||
8
lib/compute-schema.js
Normal file
8
lib/compute-schema.js
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
const { asyncLocalStorage, defaultStore } = require('./async-storage')
|
||||
|
||||
const computeSchema = (req, res, next) => {
|
||||
const store = defaultStore()
|
||||
return asyncLocalStorage.run(store, () => next())
|
||||
}
|
||||
|
||||
module.exports = computeSchema
|
||||
|
|
@ -1,3 +1,5 @@
|
|||
const T = require('./time')
|
||||
|
||||
const anonymousCustomer = {
|
||||
uuid: '47ac1184-8102-11e7-9079-8f13a7117867',
|
||||
name: 'anonymous'
|
||||
|
|
@ -8,6 +10,8 @@ const cassetteMaxCapacity = 500
|
|||
const AUTHENTICATOR_ISSUER_ENTITY = 'Lamassu'
|
||||
const AUTH_TOKEN_EXPIRATION_TIME = '30 minutes'
|
||||
const REGISTRATION_TOKEN_EXPIRATION_TIME = '30 minutes'
|
||||
const USER_SESSIONS_TABLE_NAME = 'user_sessions'
|
||||
const USER_SESSIONS_CLEAR_INTERVAL = 1 * T.hour
|
||||
|
||||
const AUTOMATIC = 'automatic'
|
||||
const MANUAL = 'manual'
|
||||
|
|
@ -19,5 +23,7 @@ module.exports = {
|
|||
AUTH_TOKEN_EXPIRATION_TIME,
|
||||
REGISTRATION_TOKEN_EXPIRATION_TIME,
|
||||
AUTOMATIC,
|
||||
MANUAL
|
||||
MANUAL,
|
||||
USER_SESSIONS_TABLE_NAME,
|
||||
USER_SESSIONS_CLEAR_INTERVAL
|
||||
}
|
||||
|
|
|
|||
|
|
@ -21,7 +21,8 @@ const stripDefaultDbFuncs = dbCtx => {
|
|||
tx: dbCtx.$tx,
|
||||
task: dbCtx.$task,
|
||||
batch: dbCtx.batch,
|
||||
multi: dbCtx.$multi
|
||||
multi: dbCtx.$multi,
|
||||
connect: dbCtx.connect
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -37,7 +38,10 @@ const _task = (obj, opts, cb) => {
|
|||
})
|
||||
}
|
||||
|
||||
const getSchema = () => 'public'
|
||||
const getSchema = () => {
|
||||
const store = asyncLocalStorage.getStore() ?? defaultStore()
|
||||
return asyncLocalStorage.run(store, () => store.get('schema'))
|
||||
}
|
||||
const getDefaultSchema = () => 'ERROR_SCHEMA'
|
||||
|
||||
const searchPathWrapper = (t, cb) => {
|
||||
|
|
|
|||
|
|
@ -157,18 +157,37 @@ function unpair (rec) {
|
|||
}
|
||||
|
||||
function reboot (rec) {
|
||||
return axios.post(`http://localhost:3030/reboot?device_id=${rec.deviceId}`)
|
||||
return db.none('NOTIFY $1:name, $2', ['poller', JSON.stringify(
|
||||
{
|
||||
type: 'machineAction',
|
||||
action: 'reboot',
|
||||
value: _.pick(['deviceId', 'operatorId', 'action'], rec)
|
||||
}
|
||||
)])
|
||||
}
|
||||
|
||||
function shutdown (rec) {
|
||||
return axios.post(`http://localhost:3030/shutdown?device_id=${rec.deviceId}`)
|
||||
return db.none('NOTIFY $1:name, $2', ['poller', JSON.stringify(
|
||||
{
|
||||
type: 'machineAction',
|
||||
action: 'shutdown',
|
||||
value: _.pick(['deviceId', 'operatorId', 'action'], rec)
|
||||
}
|
||||
)])
|
||||
}
|
||||
|
||||
function restartServices (rec) {
|
||||
return axios.post(`http://localhost:3030/restartServices?device_id=${rec.deviceId}`)
|
||||
return db.none('NOTIFY $1:name, $2', ['poller', JSON.stringify(
|
||||
{
|
||||
type: 'machineAction',
|
||||
action: 'restartServices',
|
||||
value: _.pick(['deviceId', 'operatorId', 'action'], rec)
|
||||
}
|
||||
)])
|
||||
}
|
||||
|
||||
function setMachine (rec) {
|
||||
function setMachine (rec, operatorId) {
|
||||
rec.operatorId = operatorId
|
||||
switch (rec.action) {
|
||||
case 'rename': return renameMachine(rec)
|
||||
case 'emptyCashInBills': return emptyCashInBills(rec)
|
||||
|
|
|
|||
|
|
@ -16,9 +16,12 @@ const options = require('../options')
|
|||
const users = require('../users')
|
||||
const logger = require('../logger')
|
||||
|
||||
const session = require('./middlewares/session')
|
||||
const { AuthDirective } = require('./graphql/directives')
|
||||
const { typeDefs, resolvers } = require('./graphql/schema')
|
||||
const findOperatorId = require('../middlewares/operatorId')
|
||||
const computeSchema = require('../compute-schema')
|
||||
const { USER_SESSIONS_CLEAR_INTERVAL } = require('../constants')
|
||||
const { session, cleanUserSessions, buildApolloContext } = require('./middlewares')
|
||||
|
||||
const devMode = require('minimist')(process.argv.slice(2)).dev
|
||||
const idPhotoCardBasedir = _.get('idPhotoCardDir', options)
|
||||
|
|
@ -32,6 +35,7 @@ if (!hostname) {
|
|||
}
|
||||
|
||||
const app = express()
|
||||
|
||||
app.use(helmet())
|
||||
app.use(compression())
|
||||
app.use(nocache())
|
||||
|
|
@ -39,6 +43,9 @@ app.use(cookieParser())
|
|||
app.use(express.json())
|
||||
app.use(express.urlencoded({ extended: true })) // support encoded bodies
|
||||
app.use(express.static(path.resolve(__dirname, '..', '..', 'public')))
|
||||
app.use(cleanUserSessions(USER_SESSIONS_CLEAR_INTERVAL))
|
||||
app.use(computeSchema)
|
||||
app.use(findOperatorId)
|
||||
app.use(session)
|
||||
|
||||
const apolloServer = new ApolloServer({
|
||||
|
|
@ -53,27 +60,7 @@ const apolloServer = new ApolloServer({
|
|||
logger.error(error)
|
||||
return error
|
||||
},
|
||||
context: async ({ req, res }) => {
|
||||
if (!req.session.user) return { req }
|
||||
|
||||
const user = await users.verifyAndUpdateUser(
|
||||
req.session.user.id,
|
||||
req.headers['user-agent'] || 'Unknown',
|
||||
req.ip
|
||||
)
|
||||
if (!user || !user.enabled) throw new AuthenticationError('Authentication failed')
|
||||
|
||||
req.session.ua = req.headers['user-agent'] || 'Unknown'
|
||||
req.session.ipAddress = req.ip
|
||||
req.session.lastUsed = new Date(Date.now()).toISOString()
|
||||
req.session.user.id = user.id
|
||||
req.session.user.role = user.role
|
||||
|
||||
res.set('role', user.role)
|
||||
res.set('Access-Control-Expose-Headers', 'role')
|
||||
|
||||
return { req }
|
||||
}
|
||||
context: async (obj) => buildApolloContext(obj)
|
||||
})
|
||||
|
||||
apolloServer.applyMiddleware({
|
||||
|
|
|
|||
|
|
@ -30,8 +30,7 @@ const authenticateUser = (username, password) => {
|
|||
|
||||
const destroySessionIfSameUser = (context, user) => {
|
||||
const sessionUser = getUserFromCookie(context)
|
||||
if (sessionUser && user.id === sessionUser.id)
|
||||
context.req.session.destroy()
|
||||
if (sessionUser && user.id === sessionUser.id) { context.req.session.destroy() }
|
||||
}
|
||||
|
||||
const destroySessionIfBeingUsed = (sessID, context) => {
|
||||
|
|
@ -45,7 +44,7 @@ const getUserFromCookie = context => {
|
|||
}
|
||||
|
||||
const getLamassuCookie = context => {
|
||||
return context.req.cookies && context.req.cookies.lid
|
||||
return context.req.cookies && context.req.cookies.lamassu_sid
|
||||
}
|
||||
|
||||
const initializeSession = (context, user, rememberMe) => {
|
||||
|
|
|
|||
|
|
@ -13,7 +13,7 @@ const resolvers = {
|
|||
},
|
||||
Mutation: {
|
||||
setCustomer: (root, { customerId, customerInput }, context, info) => {
|
||||
const token = !!context.req.cookies.lid && context.req.session.user.id
|
||||
const token = !!context.req.cookies.lamassu_sid && context.req.session.user.id
|
||||
if (customerId === anonymous.uuid) return customers.getCustomerById(customerId)
|
||||
return customers.updateCustomer(customerId, customerInput, token)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -18,7 +18,7 @@ const resolvers = {
|
|||
machine: (...[, { deviceId }]) => machineLoader.getMachine(deviceId)
|
||||
},
|
||||
Mutation: {
|
||||
machineAction: (...[, { deviceId, action, cashbox, cassette1, cassette2, newName }]) => machineAction({ deviceId, action, cashbox, cassette1, cassette2, newName })
|
||||
machineAction: (...[, { deviceId, action, cashbox, cassette1, cassette2, newName }, context]) => machineAction({ deviceId, action, cashbox, cassette1, cassette2, newName }, context)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,11 +1,5 @@
|
|||
const got = require('got')
|
||||
|
||||
const logger = require('../../../logger')
|
||||
const settingsLoader = require('../../../new-settings-loader')
|
||||
|
||||
const notify = () => got.post('http://localhost:3030/dbChange')
|
||||
.catch(e => logger.error('lamassu-server not responding'))
|
||||
|
||||
const resolvers = {
|
||||
Query: {
|
||||
accounts: () => settingsLoader.showAccounts(),
|
||||
|
|
@ -14,10 +8,7 @@ const resolvers = {
|
|||
Mutation: {
|
||||
saveAccounts: (...[, { accounts }]) => settingsLoader.saveAccounts(accounts),
|
||||
// resetAccounts: (...[, { schemaVersion }]) => settingsLoader.resetAccounts(schemaVersion),
|
||||
saveConfig: (...[, { config }]) => settingsLoader.saveConfig(config).then(it => {
|
||||
notify()
|
||||
return it
|
||||
}),
|
||||
saveConfig: (...[, { config }]) => settingsLoader.saveConfig(config),
|
||||
// resetConfig: (...[, { schemaVersion }]) => settingsLoader.resetConfig(schemaVersion),
|
||||
// migrateConfigAndAccounts: () => settingsLoader.migrate()
|
||||
}
|
||||
|
|
|
|||
24
lib/new-admin/middlewares/cleanUserSessions.js
Normal file
24
lib/new-admin/middlewares/cleanUserSessions.js
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
const { asyncLocalStorage } = require('../../async-storage')
|
||||
const db = require('../../db')
|
||||
const { USER_SESSIONS_TABLE_NAME } = require('../../constants')
|
||||
const logger = require('../../logger')
|
||||
|
||||
const schemaCache = {}
|
||||
|
||||
const cleanUserSessions = (cleanInterval) => (req, res, next) => {
|
||||
const schema = asyncLocalStorage.getStore() ? asyncLocalStorage.getStore().get('schema') : null
|
||||
const now = Date.now()
|
||||
|
||||
if (!schema) return next()
|
||||
if (schema && schemaCache.schema + cleanInterval > now) return next()
|
||||
|
||||
logger.debug(`Clearing expired sessions for schema ${schema}`)
|
||||
return db.none('DELETE FROM $1^ WHERE expire < to_timestamp($2 / 1000.0)', [USER_SESSIONS_TABLE_NAME, now])
|
||||
.then(() => {
|
||||
schemaCache.schema = now
|
||||
return next()
|
||||
})
|
||||
.catch(next)
|
||||
}
|
||||
|
||||
module.exports = cleanUserSessions
|
||||
29
lib/new-admin/middlewares/context.js
Normal file
29
lib/new-admin/middlewares/context.js
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
const { AuthenticationError } = require('apollo-server-express')
|
||||
const base64 = require('base-64')
|
||||
const users = require('../../users')
|
||||
|
||||
const buildApolloContext = async ({ req, res }) => {
|
||||
if (!req.session.user) return { req, res }
|
||||
|
||||
const user = await users.verifyAndUpdateUser(
|
||||
req.session.user.id,
|
||||
req.headers['user-agent'] || 'Unknown',
|
||||
req.ip
|
||||
)
|
||||
if (!user || !user.enabled) throw new AuthenticationError('Authentication failed')
|
||||
|
||||
req.session.ua = req.headers['user-agent'] || 'Unknown'
|
||||
req.session.ipAddress = req.ip
|
||||
req.session.lastUsed = new Date(Date.now()).toISOString()
|
||||
req.session.user.id = user.id
|
||||
req.session.user.username = user.username
|
||||
req.session.user.role = user.role
|
||||
|
||||
res.set('lamassu_role', user.role)
|
||||
res.cookie('pazuz_operatoridentifier', base64.encode(user.username))
|
||||
res.set('Access-Control-Expose-Headers', 'lamassu_role')
|
||||
|
||||
return { req, res }
|
||||
}
|
||||
|
||||
module.exports = buildApolloContext
|
||||
9
lib/new-admin/middlewares/index.js
Normal file
9
lib/new-admin/middlewares/index.js
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
const cleanUserSessions = require('./cleanUserSessions')
|
||||
const buildApolloContext = require('./context')
|
||||
const session = require('./session')
|
||||
|
||||
module.exports = {
|
||||
cleanUserSessions,
|
||||
buildApolloContext,
|
||||
session
|
||||
}
|
||||
|
|
@ -3,10 +3,11 @@ const express = require('express')
|
|||
const router = express.Router()
|
||||
const hkdf = require('futoin-hkdf')
|
||||
const session = require('express-session')
|
||||
const pgSession = require('connect-pg-simple')(session)
|
||||
const PgSession = require('connect-pg-simple')(session)
|
||||
const mnemonicHelpers = require('../../mnemonic-helpers')
|
||||
const db = require('../../db')
|
||||
const options = require('../../options')
|
||||
const { USER_SESSIONS_TABLE_NAME } = require('../../constants')
|
||||
|
||||
const getSecret = () => {
|
||||
const mnemonic = fs.readFileSync(options.mnemonicPath, 'utf8')
|
||||
|
|
@ -20,11 +21,11 @@ const getSecret = () => {
|
|||
const hostname = options.hostname
|
||||
|
||||
router.use('*', session({
|
||||
store: new pgSession({
|
||||
store: new PgSession({
|
||||
pgPromise: db,
|
||||
tableName: 'user_sessions'
|
||||
tableName: USER_SESSIONS_TABLE_NAME
|
||||
}),
|
||||
name: 'lid',
|
||||
name: 'lamassu_sid',
|
||||
secret: getSecret(),
|
||||
resave: false,
|
||||
saveUninitialized: false,
|
||||
|
|
|
|||
|
|
@ -6,13 +6,14 @@ function getMachine (machineId) {
|
|||
.then(machines => machines.find(({ deviceId }) => deviceId === machineId))
|
||||
}
|
||||
|
||||
function machineAction ({ deviceId, action, cashbox, cassette1, cassette2, newName }) {
|
||||
function machineAction ({ deviceId, action, cashbox, cassette1, cassette2, newName }, context) {
|
||||
const operatorId = context.res.locals.operatorId
|
||||
return getMachine(deviceId)
|
||||
.then(machine => {
|
||||
if (!machine) throw new UserInputError(`machine:${deviceId} not found`, { deviceId })
|
||||
return machine
|
||||
})
|
||||
.then(machineLoader.setMachine({ deviceId, action, cashbox, cassettes: [cassette1, cassette2], newName }))
|
||||
.then(machineLoader.setMachine({ deviceId, action, cashbox, cassettes: [cassette1, cassette2], newName }, operatorId))
|
||||
.then(getMachine(deviceId))
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ const pify = require('pify')
|
|||
const readFile = pify(fs.readFile)
|
||||
const crypto = require('crypto')
|
||||
const baseX = require('base-x')
|
||||
const { NIL } = require('uuid')
|
||||
|
||||
const options = require('../../options')
|
||||
const db = require('../../db')
|
||||
|
|
@ -19,7 +20,7 @@ function totem (name) {
|
|||
return readFile(caPath)
|
||||
.then(data => {
|
||||
const caHash = crypto.createHash('sha256').update(data).digest()
|
||||
const token = crypto.randomBytes(32)
|
||||
const token = Buffer.concat([crypto.randomBytes(32), NIL])
|
||||
const hexToken = token.toString('hex')
|
||||
const caHexToken = crypto.createHash('sha256').update(hexToken).digest('hex')
|
||||
const buf = Buffer.concat([caHash, token, Buffer.from(options.hostname)])
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
const _ = require('lodash/fp')
|
||||
const db = require('./db')
|
||||
const migration = require('./config-migration')
|
||||
const { asyncLocalStorage } = require('./async-storage')
|
||||
|
||||
const OLD_SETTINGS_LOADER_SCHEMA_VERSION = 1
|
||||
const NEW_SETTINGS_LOADER_SCHEMA_VERSION = 2
|
||||
|
|
@ -73,7 +74,10 @@ function saveConfig (config) {
|
|||
return loadLatestConfigOrNone()
|
||||
.then(currentConfig => {
|
||||
const newConfig = _.assign(currentConfig, config)
|
||||
return db.none(configSql, ['config', { config: newConfig }, true, NEW_SETTINGS_LOADER_SCHEMA_VERSION])
|
||||
return db.tx(t => {
|
||||
return t.none(configSql, ['config', { config: newConfig }, true, NEW_SETTINGS_LOADER_SCHEMA_VERSION])
|
||||
.then(() => t.none('NOTIFY $1:name, $2', ['poller', JSON.stringify({ type: 'reload', schema: asyncLocalStorage.getStore().get('schema') })]))
|
||||
}).catch(console.error)
|
||||
})
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -6,6 +6,8 @@ const _ = require('lodash/fp')
|
|||
|
||||
require('dotenv').config()
|
||||
|
||||
const DATABASE = process.env.LAMASSU_DB ?? 'DEV'
|
||||
|
||||
const dbMapping = psqlConf => ({
|
||||
STRESS_TEST: _.replace('lamassu', 'lamassu_stress', psqlConf),
|
||||
RELEASE: _.replace('lamassu', 'lamassu_release', psqlConf),
|
||||
|
|
@ -39,7 +41,7 @@ function load () {
|
|||
opts: JSON.parse(fs.readFileSync(globalConfigPath))
|
||||
}
|
||||
|
||||
config.opts.postgresql = dbMapping(config.opts.postgresql)[process.env.LAMASSU_DB]
|
||||
config.opts.postgresql = dbMapping(config.opts.postgresql)[DATABASE]
|
||||
|
||||
return config
|
||||
} catch (_) {
|
||||
|
|
@ -50,7 +52,7 @@ function load () {
|
|||
opts: JSON.parse(fs.readFileSync(homeConfigPath))
|
||||
}
|
||||
|
||||
config.opts.postgresql = dbMapping(config.opts.postgresql)[process.env.LAMASSU_DB]
|
||||
config.opts.postgresql = dbMapping(config.opts.postgresql)[DATABASE]
|
||||
|
||||
return config
|
||||
} catch (_) {
|
||||
|
|
|
|||
192
lib/poller.js
192
lib/poller.js
|
|
@ -1,5 +1,5 @@
|
|||
const _ = require('lodash/fp')
|
||||
|
||||
const Queue = require('queue-promise')
|
||||
const plugins = require('./plugins')
|
||||
const notifier = require('./notifier')
|
||||
const T = require('./time')
|
||||
|
|
@ -11,6 +11,12 @@ const sanctions = require('./ofac/index')
|
|||
const coinAtmRadar = require('./coinatmradar/coinatmradar')
|
||||
const configManager = require('./new-config-manager')
|
||||
const complianceTriggers = require('./compliance-triggers')
|
||||
const { asyncLocalStorage, defaultStore } = require('./async-storage')
|
||||
const settingsLoader = require('./new-settings-loader')
|
||||
const NodeCache = require('node-cache')
|
||||
const util = require('util')
|
||||
const db = require('./db')
|
||||
const state = require('./middlewares/state')
|
||||
|
||||
const INCOMING_TX_INTERVAL = 30 * T.seconds
|
||||
const LIVE_INCOMING_TX_INTERVAL = 5 * T.seconds
|
||||
|
|
@ -24,25 +30,106 @@ const LOGS_CLEAR_INTERVAL = 1 * T.day
|
|||
const SANCTIONS_INITIAL_DOWNLOAD_INTERVAL = 5 * T.minutes
|
||||
const SANCTIONS_UPDATE_INTERVAL = 1 * T.week
|
||||
const RADAR_UPDATE_INTERVAL = 5 * T.minutes
|
||||
const PRUNE_MACHINES_HEARBEAT = 1 * T.day
|
||||
const PRUNE_MACHINES_HEARTBEAT = 1 * T.day
|
||||
|
||||
const CHECK_NOTIFICATION_INTERVAL = 20 * T.seconds
|
||||
|
||||
const PENDING_INTERVAL = 10 * T.seconds
|
||||
const CACHE_ENTRY_TTL = 3600 // seconds
|
||||
|
||||
const coinFilter = ['ETH']
|
||||
const FAST_QUEUE_WAIT = 1 * T.seconds
|
||||
const SLOW_QUEUE_WAIT = 10 * T.seconds
|
||||
|
||||
let _pi, _settings
|
||||
const FAST_QUEUE = new Queue({
|
||||
concurrent: 600,
|
||||
interval: FAST_QUEUE_WAIT
|
||||
})
|
||||
|
||||
function reload (__settings) {
|
||||
_settings = __settings
|
||||
_pi = plugins(_settings)
|
||||
logger.debug('settings reloaded in poller')
|
||||
updateAndLoadSanctions()
|
||||
const SLOW_QUEUE = new Queue({
|
||||
concurrent: 10,
|
||||
interval: SLOW_QUEUE_WAIT
|
||||
})
|
||||
|
||||
// Fix for asyncLocalStorage store being lost due to callback-based queue
|
||||
FAST_QUEUE.enqueue = util.promisify(FAST_QUEUE.enqueue)
|
||||
SLOW_QUEUE.enqueue = util.promisify(SLOW_QUEUE.enqueue)
|
||||
|
||||
const QUEUE = {
|
||||
FAST: FAST_QUEUE,
|
||||
SLOW: SLOW_QUEUE
|
||||
}
|
||||
|
||||
function pi () { return _pi }
|
||||
function settings () { return _settings }
|
||||
const coinFilter = ['ETH']
|
||||
const schemaCallbacks = new Map()
|
||||
|
||||
const cachedVariables = new NodeCache({
|
||||
stdTTL: CACHE_ENTRY_TTL,
|
||||
checkperiod: CACHE_ENTRY_TTL,
|
||||
deleteOnExpire: false,
|
||||
useClones: false // pass values by reference instead of cloning
|
||||
})
|
||||
|
||||
cachedVariables.on('expired', (key, val) => {
|
||||
if (!val.isReloading) {
|
||||
// since val is passed by reference we don't need to do cachedVariables.set()
|
||||
val.isReloading = true
|
||||
return reload(key)
|
||||
}
|
||||
})
|
||||
|
||||
db.connect({ direct: true }).then(sco => {
|
||||
sco.client.on('notification', data => {
|
||||
const parsedData = JSON.parse(data.payload)
|
||||
switch (parsedData.type) {
|
||||
case 'reload':
|
||||
return reload(parsedData.schema)
|
||||
case 'machineAction':
|
||||
return machineAction(parsedData.action, parsedData.value)
|
||||
default:
|
||||
break
|
||||
}
|
||||
})
|
||||
return sco.none('LISTEN $1:name', 'poller')
|
||||
}).catch(console.error)
|
||||
|
||||
function reload (schema) {
|
||||
const store = defaultStore()
|
||||
store.set('schema', schema)
|
||||
// set asyncLocalStorage so settingsLoader loads settings for the right schema
|
||||
return asyncLocalStorage.run(store, () => {
|
||||
return settingsLoader.loadLatest().then(settings => {
|
||||
const pi = plugins(settings)
|
||||
cachedVariables.set(schema, { settings, pi, isReloading: false })
|
||||
logger.debug(`Settings for schema '${schema}' reloaded in poller`)
|
||||
return updateAndLoadSanctions()
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
function machineAction (type, value) {
|
||||
const deviceId = value.deviceId
|
||||
const operatorId = value.operatorId
|
||||
const pid = state.pids?.[operatorId]?.[deviceId]?.pid
|
||||
|
||||
switch (type) {
|
||||
case 'reboot':
|
||||
logger.debug(`Rebooting machine '${deviceId}' from operator ${operatorId}`)
|
||||
state.reboots[operatorId] = { [deviceId]: pid }
|
||||
break
|
||||
case 'shutdown':
|
||||
logger.debug(`Shutting down machine '${deviceId}' from operator ${operatorId}`)
|
||||
state.shutdowns[operatorId] = { [deviceId]: pid }
|
||||
break
|
||||
case 'restartServices':
|
||||
logger.debug(`Restarting services of machine '${deviceId}' from operator ${operatorId}`)
|
||||
state.restartServicesMap[operatorId] = { [deviceId]: pid }
|
||||
break
|
||||
default:
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
function pi () { return cachedVariables.get(asyncLocalStorage.getStore().get('schema')).pi }
|
||||
function settings () { return cachedVariables.get(asyncLocalStorage.getStore().get('schema')).settings }
|
||||
|
||||
function initialSanctionsDownload () {
|
||||
const structs = sanctions.getStructs()
|
||||
|
|
@ -70,9 +157,40 @@ function updateCoinAtmRadar () {
|
|||
.then(rates => coinAtmRadar.update(rates, settings()))
|
||||
}
|
||||
|
||||
function start (__settings) {
|
||||
reload(__settings)
|
||||
function initializeEachSchema (schemas = ['public']) {
|
||||
// for each schema set "thread variables" and do polling
|
||||
return _.forEach(schema => {
|
||||
const store = defaultStore()
|
||||
store.set('schema', schema)
|
||||
return asyncLocalStorage.run(store, () => {
|
||||
return settingsLoader.loadLatest().then(settings => {
|
||||
// prevent inadvertedly clearing the array without clearing timeouts
|
||||
if (schemaCallbacks.has(schema)) throw new Error(`The schema "${schema}" cannot be initialized twice on poller`)
|
||||
const pi = plugins(settings)
|
||||
cachedVariables.set(schema, { settings, pi, isReloading: false })
|
||||
schemaCallbacks.set(schema, [])
|
||||
return doPolling(schema)
|
||||
})
|
||||
}).catch(console.error)
|
||||
}, schemas)
|
||||
}
|
||||
|
||||
function addToQueue (func, interval, schema, queue, ...vars) {
|
||||
return schemaCallbacks.get(schema).push(setInterval(() => {
|
||||
return queue.enqueue().then(() => {
|
||||
// get plugins or settings from the cache every time func is run
|
||||
const loadVariables = vars.length > 0 && typeof vars[0] === 'function'
|
||||
if (loadVariables) {
|
||||
const funcVars = [...vars]
|
||||
funcVars[0] = vars[0]()
|
||||
return func(...funcVars)
|
||||
}
|
||||
return func(...vars)
|
||||
}).catch(console.error)
|
||||
}, interval))
|
||||
}
|
||||
|
||||
function doPolling (schema) {
|
||||
pi().executeTrades()
|
||||
pi().pong()
|
||||
pi().clearOldLogs()
|
||||
|
|
@ -87,23 +205,37 @@ function start (__settings) {
|
|||
notifier.checkNotification(pi())
|
||||
updateCoinAtmRadar()
|
||||
|
||||
setInterval(() => pi().executeTrades(), TRADE_INTERVAL)
|
||||
setInterval(() => cashOutTx.monitorLiveIncoming(settings(), false, coinFilter), LIVE_INCOMING_TX_INTERVAL)
|
||||
setInterval(() => cashOutTx.monitorStaleIncoming(settings(), false, coinFilter), INCOMING_TX_INTERVAL)
|
||||
addToQueue(pi().executeTrades, TRADE_INTERVAL, schema, QUEUE.FAST)
|
||||
addToQueue(cashOutTx.monitorLiveIncoming, LIVE_INCOMING_TX_INTERVAL, schema, QUEUE.FAST, settings, false, coinFilter)
|
||||
addToQueue(cashOutTx.monitorStaleIncoming, INCOMING_TX_INTERVAL, schema, QUEUE.FAST, settings, false, coinFilter)
|
||||
if (!_.isEmpty(coinFilter)) {
|
||||
setInterval(() => cashOutTx.monitorLiveIncoming(settings(), true, coinFilter), LIVE_INCOMING_TX_INTERVAL_FILTER)
|
||||
setInterval(() => cashOutTx.monitorStaleIncoming(settings(), true, coinFilter), INCOMING_TX_INTERVAL_FILTER)
|
||||
addToQueue(cashOutTx.monitorLiveIncoming, LIVE_INCOMING_TX_INTERVAL_FILTER, schema, QUEUE.FAST, settings, true, coinFilter)
|
||||
addToQueue(cashOutTx.monitorStaleIncoming, INCOMING_TX_INTERVAL_FILTER, schema, QUEUE.FAST, settings, true, coinFilter)
|
||||
}
|
||||
setInterval(() => cashOutTx.monitorUnnotified(settings()), UNNOTIFIED_INTERVAL)
|
||||
setInterval(() => cashInTx.monitorPending(settings()), PENDING_INTERVAL)
|
||||
setInterval(() => pi().sweepHd(), SWEEP_HD_INTERVAL)
|
||||
setInterval(() => pi().pong(), PONG_INTERVAL)
|
||||
setInterval(() => pi().clearOldLogs(), LOGS_CLEAR_INTERVAL)
|
||||
setInterval(() => notifier.checkNotification(pi()), CHECK_NOTIFICATION_INTERVAL)
|
||||
setInterval(initialSanctionsDownload, SANCTIONS_INITIAL_DOWNLOAD_INTERVAL)
|
||||
setInterval(updateAndLoadSanctions, SANCTIONS_UPDATE_INTERVAL)
|
||||
setInterval(updateCoinAtmRadar, RADAR_UPDATE_INTERVAL)
|
||||
setInterval(() => pi().pruneMachinesHeartbeat(), PRUNE_MACHINES_HEARBEAT)
|
||||
addToQueue(cashOutTx.monitorUnnotified, UNNOTIFIED_INTERVAL, schema, QUEUE.FAST, settings)
|
||||
addToQueue(cashInTx.monitorPending, PENDING_INTERVAL, schema, QUEUE.FAST, settings)
|
||||
addToQueue(pi().sweepHd, SWEEP_HD_INTERVAL, schema, QUEUE.FAST, settings)
|
||||
addToQueue(pi().pong, PONG_INTERVAL, schema, QUEUE.FAST)
|
||||
addToQueue(pi().clearOldLogs, LOGS_CLEAR_INTERVAL, schema, QUEUE.SLOW)
|
||||
addToQueue(notifier.checkNotification, CHECK_NOTIFICATION_INTERVAL, schema, QUEUE.FAST, pi)
|
||||
addToQueue(initialSanctionsDownload, SANCTIONS_INITIAL_DOWNLOAD_INTERVAL, schema, QUEUE.SLOW)
|
||||
addToQueue(updateAndLoadSanctions, SANCTIONS_UPDATE_INTERVAL, schema, QUEUE.SLOW)
|
||||
addToQueue(updateCoinAtmRadar, RADAR_UPDATE_INTERVAL, schema, QUEUE.SLOW)
|
||||
addToQueue(pi().pruneMachinesHeartbeat(), PRUNE_MACHINES_HEARTBEAT, schema, QUEUE.SLOW, settings)
|
||||
}
|
||||
|
||||
module.exports = { start, reload }
|
||||
function setup (schemasToAdd = [], schemasToRemove = []) {
|
||||
// clear callback array for each schema in schemasToRemove and clear cached variables
|
||||
_.forEach(schema => {
|
||||
const callbacks = schemaCallbacks.get(schema)
|
||||
_.forEach(clearInterval, callbacks)
|
||||
schemaCallbacks.delete(schema)
|
||||
cachedVariables.del(schema)
|
||||
}, schemasToRemove)
|
||||
|
||||
return initializeEachSchema(schemasToAdd)
|
||||
}
|
||||
|
||||
const getActiveSchemas = () => Array.from(schemaCallbacks.keys())
|
||||
|
||||
module.exports = { setup, reload, getActiveSchemas }
|
||||
|
|
|
|||
|
|
@ -25,15 +25,12 @@ const phoneCodeRoutes = require('./routes/phoneCodeRoutes')
|
|||
const pollingRoutes = require('./routes/pollingRoutes')
|
||||
const stateRoutes = require('./routes/stateRoutes')
|
||||
const termsAndConditionsRoutes = require('./routes/termsAndConditionsRoutes')
|
||||
const txRoutes = require('./routes/txRoutes')
|
||||
const { router: txRoutes } = require('./routes/txRoutes')
|
||||
const verifyUserRoutes = require('./routes/verifyUserRoutes')
|
||||
const verifyTxRoutes = require('./routes/verifyTxRoutes')
|
||||
const verifyPromoCodeRoutes = require('./routes/verifyPromoCodeRoutes')
|
||||
|
||||
const localAppRoutes = require('./routes/localAppRoutes')
|
||||
|
||||
const app = express()
|
||||
const localApp = express()
|
||||
|
||||
const configRequiredRoutes = [
|
||||
'/poll',
|
||||
|
|
@ -87,7 +84,4 @@ app.use((req, res) => {
|
|||
res.status(404).json({ error: 'No such route' })
|
||||
})
|
||||
|
||||
// localapp routes
|
||||
localApp.use('/', localAppRoutes)
|
||||
|
||||
module.exports = { app, localApp }
|
||||
module.exports = { app }
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@ const { getCashInSettings } = require('../new-config-manager')
|
|||
const { AUTOMATIC } = require('../constants.js')
|
||||
|
||||
function notifyCashboxRemoval (req, res, next) {
|
||||
const operatorId = res.locals.operatorId
|
||||
return Promise.all([getMachine(req.deviceId), loadLatestConfig()])
|
||||
.then(([machine, config]) => {
|
||||
const cashInSettings = getCashInSettings(config)
|
||||
|
|
@ -15,7 +16,7 @@ function notifyCashboxRemoval (req, res, next) {
|
|||
return res.status(200).send({ status: 'OK' })
|
||||
}
|
||||
return cashbox.createCashboxBatch(req.deviceId, machine.cashbox)
|
||||
.then(() => setMachine({ deviceId: req.deviceId, action: 'emptyCashInBills' }))
|
||||
.then(() => setMachine({ deviceId: req.deviceId, action: 'emptyCashInBills' }, operatorId))
|
||||
.then(() => res.status(200).send({ status: 'OK' }))
|
||||
})
|
||||
.catch(next)
|
||||
|
|
|
|||
|
|
@ -1,48 +0,0 @@
|
|||
const express = require('express')
|
||||
const router = express.Router()
|
||||
|
||||
const state = require('../middlewares/state')
|
||||
|
||||
router.get('/pid', (req, res) => {
|
||||
const deviceId = req.query.device_id
|
||||
const pidRec = state.pids[deviceId]
|
||||
res.json(pidRec)
|
||||
})
|
||||
|
||||
router.post('/reboot', (req, res) => {
|
||||
const deviceId = req.query.device_id
|
||||
const pid = state.pids[deviceId] && state.pids[deviceId].pid
|
||||
|
||||
if (!deviceId || !pid) {
|
||||
return res.sendStatus(400)
|
||||
}
|
||||
|
||||
state.reboots[deviceId] = pid
|
||||
res.sendStatus(200)
|
||||
})
|
||||
|
||||
router.post('/shutdown', (req, res) => {
|
||||
const deviceId = req.query.device_id
|
||||
const pid = state.pids[deviceId] && state.pids[deviceId].pid
|
||||
|
||||
if (!deviceId || !pid) {
|
||||
return res.sendStatus(400)
|
||||
}
|
||||
|
||||
state.shutdowns[deviceId] = pid
|
||||
res.sendStatus(200)
|
||||
})
|
||||
|
||||
router.post('/restartServices', (req, res) => {
|
||||
const deviceId = req.query.device_id
|
||||
const pid = state.pids[deviceId] && state.pids[deviceId].pid
|
||||
|
||||
if (!deviceId || !pid) {
|
||||
return res.sendStatus(400)
|
||||
}
|
||||
|
||||
state.restartServicesMap[deviceId] = pid
|
||||
res.sendStatus(200)
|
||||
})
|
||||
|
||||
module.exports = router
|
||||
|
|
@ -31,6 +31,7 @@ function poll (req, res, next) {
|
|||
const serialNumber = req.query.sn
|
||||
const pid = req.query.pid
|
||||
const settings = req.settings
|
||||
const operatorId = res.locals.operatorId
|
||||
const localeConfig = configManager.getLocale(deviceId, settings.config)
|
||||
const zeroConfLimits = _.reduce((acc, cryptoCode) => {
|
||||
acc[cryptoCode] = configManager.getWalletSettings(cryptoCode, settings.config).zeroConfLimit
|
||||
|
|
@ -48,15 +49,15 @@ function poll (req, res, next) {
|
|||
const receipt = configManager.getReceipt(settings.config)
|
||||
const terms = configManager.getTermsConditions(settings.config)
|
||||
|
||||
state.pids[deviceId] = { pid, ts: Date.now() }
|
||||
state.pids[operatorId] = { [deviceId]: { pid, ts: Date.now() } }
|
||||
|
||||
return pi.pollQueries(serialNumber, deviceTime, req.query, machineVersion, machineModel)
|
||||
.then(results => {
|
||||
const cassettes = results.cassettes
|
||||
|
||||
const reboot = pid && state.reboots[deviceId] && state.reboots[deviceId] === pid
|
||||
const shutdown = pid && state.shutdowns[deviceId] && state.shutdowns[deviceId] === pid
|
||||
const restartServices = pid && state.restartServicesMap[deviceId] && state.restartServicesMap[deviceId] === pid
|
||||
const reboot = pid && state.reboots?.[operatorId]?.[deviceId] === pid
|
||||
const shutdown = pid && state.shutdowns?.[operatorId]?.[deviceId] === pid
|
||||
const restartServices = pid && state.restartServicesMap?.[operatorId]?.[deviceId] === pid
|
||||
const langs = localeConfig.languages
|
||||
|
||||
const locale = {
|
||||
|
|
|
|||
|
|
@ -66,4 +66,4 @@ router.post('/', postTx)
|
|||
router.get('/:id', getTx)
|
||||
router.get('/', getPhoneTx)
|
||||
|
||||
module.exports = router
|
||||
module.exports = { postTx, getTx, getPhoneTx, router }
|
||||
|
|
|
|||
|
|
@ -59,7 +59,7 @@ function verifyAndUpdateUser (id, ua, ip) {
|
|||
.then(user => {
|
||||
if (!user) return null
|
||||
|
||||
const sql2 = `UPDATE users SET last_accessed=now(), last_accessed_from=$1, last_accessed_address=$2 WHERE id=$3 RETURNING id, role, enabled`
|
||||
const sql2 = `UPDATE users SET last_accessed=now(), last_accessed_from=$1, last_accessed_address=$2 WHERE id=$3 RETURNING id, username, role, enabled`
|
||||
return db.one(sql2, [ua, ip, id])
|
||||
})
|
||||
.then(user => user)
|
||||
|
|
|
|||
5
new-lamassu-admin/package-lock.json
generated
5
new-lamassu-admin/package-lock.json
generated
|
|
@ -8192,6 +8192,11 @@
|
|||
}
|
||||
}
|
||||
},
|
||||
"base-64": {
|
||||
"version": "1.0.0",
|
||||
"resolved": "https://registry.npmjs.org/base-64/-/base-64-1.0.0.tgz",
|
||||
"integrity": "sha512-kwDPIFCGx0NZHog36dj+tHiwP4QMzsZ3AgMViUBKI0+V5n4U0ufTCUMhnQ04diaRI8EX/QcPfql7zlhZ7j4zgg=="
|
||||
},
|
||||
"base-x": {
|
||||
"version": "3.0.8",
|
||||
"resolved": "https://registry.npmjs.org/base-x/-/base-x-3.0.8.tgz",
|
||||
|
|
|
|||
|
|
@ -14,6 +14,7 @@
|
|||
"apollo-link-error": "^1.1.13",
|
||||
"apollo-link-http": "^1.5.17",
|
||||
"axios": "0.21.1",
|
||||
"base-64": "^1.0.0",
|
||||
"bignumber.js": "9.0.0",
|
||||
"classnames": "2.2.6",
|
||||
"countries-and-timezones": "^2.4.0",
|
||||
|
|
@ -96,8 +97,8 @@
|
|||
"storybook": "start-storybook -p 9009 -s public",
|
||||
"postinstall": "patch-package",
|
||||
"build-storybook": "build-storybook -s public",
|
||||
"start-lamassu": "REACT_APP_BUILD_TARGET=LAMASSU react-scripts start",
|
||||
"start-pazuz": "REACT_APP_BUILD_TARGET=PAZUZ react-scripts start"
|
||||
"lamassu": "REACT_APP_BUILD_TARGET=LAMASSU react-scripts start",
|
||||
"pazuz": "REACT_APP_BUILD_TARGET=PAZUZ react-scripts start"
|
||||
},
|
||||
"browserslist": {
|
||||
"production": [
|
||||
|
|
|
|||
|
|
@ -154,7 +154,7 @@ const App = () => {
|
|||
const [userData, setUserData] = useState(null)
|
||||
|
||||
const setRole = role => {
|
||||
if (userData && userData.role !== role) {
|
||||
if (userData && role && userData.role !== role) {
|
||||
setUserData({ ...userData, role })
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,9 +1,12 @@
|
|||
import { useQuery } from '@apollo/react-hooks'
|
||||
import { Paper } from '@material-ui/core'
|
||||
import { makeStyles } from '@material-ui/core/styles'
|
||||
import classnames from 'classnames'
|
||||
import gql from 'graphql-tag'
|
||||
import * as R from 'ramda'
|
||||
import React from 'react'
|
||||
import React, { useContext } from 'react'
|
||||
|
||||
import AppContext from 'src/AppContext'
|
||||
import TitleSection from 'src/components/layout/TitleSection'
|
||||
import { H3, Info2, Label2, Label3, P } from 'src/components/typography'
|
||||
import { ReactComponent as BitcoinLogo } from 'src/styling/logos/icon-bitcoin-colour.svg'
|
||||
|
|
@ -17,6 +20,40 @@ import styles from './ATMWallet.styles'
|
|||
|
||||
const useStyles = makeStyles(styles)
|
||||
|
||||
const GET_OPERATOR_BY_USERNAME = gql`
|
||||
query operatorByUsername($username: String) {
|
||||
operatorByUsername(username: $username) {
|
||||
id
|
||||
entityId
|
||||
name
|
||||
fiatBalances
|
||||
cryptoBalances
|
||||
machines
|
||||
joined
|
||||
assetValue
|
||||
preferredFiatCurrency
|
||||
contactInfo {
|
||||
name
|
||||
email
|
||||
}
|
||||
fundings {
|
||||
id
|
||||
origin
|
||||
destination
|
||||
fiatAmount
|
||||
fiatBalanceAfter
|
||||
fiatCurrency
|
||||
created
|
||||
status
|
||||
description
|
||||
}
|
||||
}
|
||||
}
|
||||
`
|
||||
|
||||
const formatCurrency = amount =>
|
||||
amount.toLocaleString('en-US', { maximumFractionDigits: 2 })
|
||||
|
||||
const CHIPS_PER_ROW = 6
|
||||
|
||||
const Assets = ({ balance, wallets, currency }) => {
|
||||
|
|
@ -32,10 +69,10 @@ const Assets = ({ balance, wallets, currency }) => {
|
|||
<P className={classes.fieldHeader}>Available balance</P>
|
||||
<div className={classes.totalAssetWrapper}>
|
||||
<Info2 noMargin className={classes.fieldValue}>
|
||||
{balance.toLocaleString('en-US', { maximumFractionDigits: 2 })}
|
||||
{formatCurrency(balance)}
|
||||
</Info2>
|
||||
<Info2 noMargin className={classes.fieldCurrency}>
|
||||
{currency}
|
||||
{R.toUpper(currency)}
|
||||
</Info2>
|
||||
</div>
|
||||
</div>
|
||||
|
|
@ -44,12 +81,10 @@ const Assets = ({ balance, wallets, currency }) => {
|
|||
<P className={classes.fieldHeader}>Total balance in wallets</P>
|
||||
<div className={classes.totalAssetWrapper}>
|
||||
<Info2 noMargin className={classes.fieldValue}>
|
||||
{walletFiatSum().toLocaleString('en-US', {
|
||||
maximumFractionDigits: 2
|
||||
})}
|
||||
{formatCurrency(walletFiatSum())}
|
||||
</Info2>
|
||||
<Info2 noMargin className={classes.fieldCurrency}>
|
||||
{currency}
|
||||
{R.toUpper(currency)}
|
||||
</Info2>
|
||||
</div>
|
||||
</div>
|
||||
|
|
@ -58,10 +93,10 @@ const Assets = ({ balance, wallets, currency }) => {
|
|||
<P className={classes.fieldHeader}>Total assets</P>
|
||||
<div className={classes.totalAssetWrapper}>
|
||||
<Info2 noMargin className={classes.fieldValue}>
|
||||
{balance.toLocaleString('en-US', { maximumFractionDigits: 2 })}
|
||||
{formatCurrency(balance)}
|
||||
</Info2>
|
||||
<Info2 noMargin className={classes.fieldCurrency}>
|
||||
{currency}
|
||||
{R.toUpper(currency)}
|
||||
</Info2>
|
||||
</div>
|
||||
</div>
|
||||
|
|
@ -129,56 +164,71 @@ const WalletInfoChip = ({ wallet, currency }) => {
|
|||
|
||||
const ATMWallet = () => {
|
||||
const classes = useStyles({ numberOfChips: CHIPS_PER_ROW })
|
||||
const { userData } = useContext(AppContext)
|
||||
|
||||
const { data, loading } = useQuery(GET_OPERATOR_BY_USERNAME, {
|
||||
context: { clientName: 'pazuz' },
|
||||
variables: { username: userData?.username }
|
||||
})
|
||||
|
||||
const operatorData = R.path(['operatorByUsername'], data)
|
||||
|
||||
const wallets = [
|
||||
{
|
||||
cryptoCode: 'BTC',
|
||||
name: 'Bitcoin',
|
||||
amount: 2.7,
|
||||
fiatValue: 81452,
|
||||
amount: operatorData?.cryptoBalances.xbt ?? 0,
|
||||
fiatValue: 0,
|
||||
isHedged: true
|
||||
},
|
||||
{
|
||||
cryptoCode: 'ETH',
|
||||
name: 'Ethereum',
|
||||
amount: 4.1,
|
||||
fiatValue: 4924,
|
||||
amount: operatorData?.cryptoBalances.eth ?? 0,
|
||||
fiatValue: 0,
|
||||
isHedged: true
|
||||
},
|
||||
{
|
||||
cryptoCode: 'LTC',
|
||||
name: 'Litecoin',
|
||||
amount: 15,
|
||||
fiatValue: 3016,
|
||||
amount: operatorData?.cryptoBalances.ltc ?? 0,
|
||||
fiatValue: 0,
|
||||
isHedged: true
|
||||
},
|
||||
{
|
||||
cryptoCode: 'ZEC',
|
||||
name: 'Z-Cash',
|
||||
amount: 20,
|
||||
fiatValue: 2887,
|
||||
amount: operatorData?.cryptoBalances.zec ?? 0,
|
||||
fiatValue: 0,
|
||||
isHedged: false
|
||||
},
|
||||
{
|
||||
cryptoCode: 'BCH',
|
||||
name: 'Bitcoin Cash',
|
||||
amount: 10.7,
|
||||
fiatValue: 7074,
|
||||
amount: operatorData?.cryptoBalances.bch ?? 0,
|
||||
fiatValue: 0,
|
||||
isHedged: true
|
||||
},
|
||||
{
|
||||
cryptoCode: 'DASH',
|
||||
name: 'Dash',
|
||||
amount: 10.7,
|
||||
fiatValue: 1091,
|
||||
amount: operatorData?.cryptoBalances.dash ?? 0,
|
||||
fiatValue: 0,
|
||||
isHedged: false
|
||||
}
|
||||
]
|
||||
|
||||
return (
|
||||
!loading && (
|
||||
<>
|
||||
<TitleSection title="ATM Wallets" />
|
||||
<Assets balance={8952} wallets={wallets} currency={'USD'} />
|
||||
<Assets
|
||||
balance={
|
||||
operatorData.fiatBalances[operatorData.preferredFiatCurrency]
|
||||
}
|
||||
wallets={wallets}
|
||||
currency={operatorData.preferredFiatCurrency}
|
||||
/>
|
||||
<H3 className={classes.walletChipTitle}>ATM Wallets</H3>
|
||||
<div className={classes.walletChipList}>
|
||||
{R.map(
|
||||
|
|
@ -190,6 +240,7 @@ const ATMWallet = () => {
|
|||
</div>
|
||||
</>
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
export default ATMWallet
|
||||
|
|
|
|||
|
|
@ -1,7 +1,11 @@
|
|||
import { useQuery } from '@apollo/react-hooks'
|
||||
import { makeStyles } from '@material-ui/core/styles'
|
||||
import gql from 'graphql-tag'
|
||||
import moment from 'moment'
|
||||
import React from 'react'
|
||||
import * as R from 'ramda'
|
||||
import React, { useContext } from 'react'
|
||||
|
||||
import AppContext from 'src/AppContext'
|
||||
import { Tooltip } from 'src/components/Tooltip'
|
||||
import TitleSection from 'src/components/layout/TitleSection'
|
||||
import DataTable from 'src/components/tables/DataTable'
|
||||
|
|
@ -9,47 +13,42 @@ import { H4, Info2, P } from 'src/components/typography'
|
|||
|
||||
import styles from './Accounting.styles'
|
||||
|
||||
const mockData = [
|
||||
{
|
||||
operation: 'Hedging summary',
|
||||
direction: 'in',
|
||||
extraInfo: 'This is mocked information',
|
||||
amount: 486,
|
||||
currency: 'USD',
|
||||
balanceAfterTx: 10438,
|
||||
date: '2021-02-22T20:16:12.020Z'
|
||||
},
|
||||
{
|
||||
operation: 'Funding transaction',
|
||||
direction: 'in',
|
||||
amount: 2000,
|
||||
currency: 'USD',
|
||||
balanceAfterTx: 9952,
|
||||
date: '2021-02-22T12:40:32.020Z'
|
||||
},
|
||||
{
|
||||
operation: 'ZEC hot wallet top up',
|
||||
direction: 'out',
|
||||
amount: 1000,
|
||||
currency: 'USD',
|
||||
balanceAfterTx: 7952,
|
||||
date: '2021-02-21T16:30:44.020Z'
|
||||
},
|
||||
{
|
||||
operation: 'Funding transaction',
|
||||
direction: 'in',
|
||||
amount: 8000,
|
||||
currency: 'USD',
|
||||
balanceAfterTx: 8952,
|
||||
date: '2021-02-21T08:16:20.020Z'
|
||||
}
|
||||
]
|
||||
|
||||
const formatCurrency = amount =>
|
||||
amount.toLocaleString('en-US', { maximumFractionDigits: 2 })
|
||||
|
||||
const useStyles = makeStyles(styles)
|
||||
|
||||
const GET_OPERATOR_BY_USERNAME = gql`
|
||||
query operatorByUsername($username: String) {
|
||||
operatorByUsername(username: $username) {
|
||||
id
|
||||
entityId
|
||||
name
|
||||
fiatBalances
|
||||
cryptoBalances
|
||||
machines
|
||||
joined
|
||||
assetValue
|
||||
preferredFiatCurrency
|
||||
contactInfo {
|
||||
name
|
||||
email
|
||||
}
|
||||
fundings {
|
||||
id
|
||||
origin
|
||||
destination
|
||||
fiatAmount
|
||||
fiatBalanceAfter
|
||||
fiatCurrency
|
||||
created
|
||||
status
|
||||
description
|
||||
}
|
||||
}
|
||||
}
|
||||
`
|
||||
|
||||
const Assets = ({ balance, hedgingReserve, currency }) => {
|
||||
const classes = useStyles()
|
||||
|
||||
|
|
@ -62,7 +61,7 @@ const Assets = ({ balance, hedgingReserve, currency }) => {
|
|||
{formatCurrency(balance)}
|
||||
</Info2>
|
||||
<Info2 noMargin className={classes.fieldCurrency}>
|
||||
{currency}
|
||||
{R.toUpper(currency)}
|
||||
</Info2>
|
||||
</div>
|
||||
</div>
|
||||
|
|
@ -74,7 +73,7 @@ const Assets = ({ balance, hedgingReserve, currency }) => {
|
|||
{formatCurrency(hedgingReserve)}
|
||||
</Info2>
|
||||
<Info2 noMargin className={classes.fieldCurrency}>
|
||||
{currency}
|
||||
{R.toUpper(currency)}
|
||||
</Info2>
|
||||
</div>
|
||||
</div>
|
||||
|
|
@ -86,7 +85,7 @@ const Assets = ({ balance, hedgingReserve, currency }) => {
|
|||
{formatCurrency(balance - hedgingReserve)}
|
||||
</Info2>
|
||||
<Info2 noMargin className={classes.fieldCurrency}>
|
||||
{currency}
|
||||
{R.toUpper(currency)}
|
||||
</Info2>
|
||||
</div>
|
||||
</div>
|
||||
|
|
@ -96,6 +95,14 @@ const Assets = ({ balance, hedgingReserve, currency }) => {
|
|||
|
||||
const Accounting = () => {
|
||||
const classes = useStyles()
|
||||
const { userData } = useContext(AppContext)
|
||||
|
||||
const { data, loading } = useQuery(GET_OPERATOR_BY_USERNAME, {
|
||||
context: { clientName: 'pazuz' },
|
||||
variables: { username: userData?.username }
|
||||
})
|
||||
|
||||
const operatorData = R.path(['operatorByUsername'], data)
|
||||
|
||||
const elements = [
|
||||
{
|
||||
|
|
@ -106,7 +113,7 @@ const Accounting = () => {
|
|||
view: it => {
|
||||
return (
|
||||
<span className={classes.operation}>
|
||||
{it.operation}
|
||||
{it.description}
|
||||
{!!it.extraInfo && (
|
||||
<Tooltip width={175}>
|
||||
<P>{it.extraInfo}</P>
|
||||
|
|
@ -122,18 +129,15 @@ const Accounting = () => {
|
|||
size: 'sm',
|
||||
textAlign: 'right',
|
||||
view: it =>
|
||||
`${
|
||||
it.direction === 'in'
|
||||
? formatCurrency(it.amount)
|
||||
: formatCurrency(-it.amount)
|
||||
} ${it.currency}`
|
||||
`${formatCurrency(it.fiatAmount)} ${R.toUpper(it.fiatCurrency)}`
|
||||
},
|
||||
{
|
||||
header: 'Balance after operation',
|
||||
width: 250,
|
||||
size: 'sm',
|
||||
textAlign: 'right',
|
||||
view: it => `${formatCurrency(it.balanceAfterTx)} ${it.currency}`
|
||||
view: it =>
|
||||
`${formatCurrency(it.fiatBalanceAfter)} ${R.toUpper(it.fiatCurrency)}`
|
||||
},
|
||||
{
|
||||
header: 'Date',
|
||||
|
|
@ -152,19 +156,27 @@ const Accounting = () => {
|
|||
]
|
||||
|
||||
return (
|
||||
!loading && (
|
||||
<>
|
||||
<TitleSection title="Accounting" />
|
||||
<Assets balance={10438} hedgingReserve={1486} currency={'USD'} />
|
||||
<Assets
|
||||
balance={
|
||||
operatorData.fiatBalances[operatorData.preferredFiatCurrency]
|
||||
}
|
||||
hedgingReserve={operatorData.hedgingReserve ?? 0}
|
||||
currency={operatorData.preferredFiatCurrency}
|
||||
/>
|
||||
<H4 className={classes.tableTitle}>Fiat balance history</H4>
|
||||
<DataTable
|
||||
loading={false}
|
||||
emptyText="No transactions so far"
|
||||
elements={elements}
|
||||
data={mockData}
|
||||
data={operatorData.fundings ?? []}
|
||||
rowSize="sm"
|
||||
/>
|
||||
</>
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
export default Accounting
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
import { useQuery } from '@apollo/react-hooks'
|
||||
import Grid from '@material-ui/core/Grid'
|
||||
import Table from '@material-ui/core/Table'
|
||||
import TableBody from '@material-ui/core/TableBody'
|
||||
|
|
@ -6,78 +7,47 @@ import TableContainer from '@material-ui/core/TableContainer'
|
|||
import TableHead from '@material-ui/core/TableHead'
|
||||
import TableRow from '@material-ui/core/TableRow'
|
||||
import { makeStyles, withStyles } from '@material-ui/core/styles'
|
||||
import gql from 'graphql-tag'
|
||||
import * as R from 'ramda'
|
||||
import React from 'react'
|
||||
import React, { useContext } from 'react'
|
||||
|
||||
import AppContext from 'src/AppContext'
|
||||
import TitleSection from 'src/components/layout/TitleSection'
|
||||
import { H4, Label2, P, Info2 } from 'src/components/typography'
|
||||
|
||||
import styles from './Assets.styles'
|
||||
const useStyles = makeStyles(styles)
|
||||
|
||||
const mockData = [
|
||||
{
|
||||
id: 'fiatBalance',
|
||||
display: 'Fiat balance',
|
||||
amount: 10438,
|
||||
currency: 'USD',
|
||||
class: 'Available balance'
|
||||
},
|
||||
{
|
||||
id: 'hedgingReserve',
|
||||
display: 'Hedging reserve',
|
||||
amount: -1486,
|
||||
currency: 'USD',
|
||||
class: 'Available balance',
|
||||
direction: 'out'
|
||||
},
|
||||
{
|
||||
id: 'hedgedWalletAssets',
|
||||
display: 'Hedged wallet assets',
|
||||
amount: 96446,
|
||||
currency: 'USD',
|
||||
class: 'Wallet assets',
|
||||
direction: 'in'
|
||||
},
|
||||
{
|
||||
id: 'unhedgedWalletAssets',
|
||||
display: 'Unhedged wallet assets',
|
||||
amount: 3978,
|
||||
currency: 'USD',
|
||||
class: 'Wallet assets',
|
||||
direction: 'in'
|
||||
const GET_OPERATOR_BY_USERNAME = gql`
|
||||
query operatorByUsername($username: String) {
|
||||
operatorByUsername(username: $username) {
|
||||
id
|
||||
entityId
|
||||
name
|
||||
fiatBalances
|
||||
cryptoBalances
|
||||
machines
|
||||
joined
|
||||
assetValue
|
||||
preferredFiatCurrency
|
||||
contactInfo {
|
||||
name
|
||||
email
|
||||
}
|
||||
]
|
||||
|
||||
const mockDataTotal = [
|
||||
{
|
||||
id: 'fiatBalance',
|
||||
display: 'Fiat balance',
|
||||
amount: 10438,
|
||||
currency: 'USD'
|
||||
},
|
||||
{
|
||||
id: 'hedgingReserve',
|
||||
display: 'Hedging reserve',
|
||||
amount: -1486,
|
||||
currency: 'USD',
|
||||
direction: 'out'
|
||||
},
|
||||
{
|
||||
id: 'hedgedWalletAssets',
|
||||
display: 'Market value of hedged wallet assets',
|
||||
amount: 94980,
|
||||
currency: 'USD',
|
||||
direction: 'in'
|
||||
},
|
||||
{
|
||||
id: 'unhedgedWalletAssets',
|
||||
display: 'Unhedged wallet assets',
|
||||
amount: 3978,
|
||||
currency: 'USD',
|
||||
direction: 'in'
|
||||
fundings {
|
||||
id
|
||||
origin
|
||||
destination
|
||||
fiatAmount
|
||||
fiatBalanceAfter
|
||||
fiatCurrency
|
||||
created
|
||||
status
|
||||
description
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
`
|
||||
|
||||
const cellStyling = {
|
||||
borderBottom: '4px solid white',
|
||||
|
|
@ -163,11 +133,87 @@ const formatCurrency = amount =>
|
|||
|
||||
const Assets = () => {
|
||||
const classes = useStyles()
|
||||
const { userData } = useContext(AppContext)
|
||||
|
||||
const filterByClass = x =>
|
||||
R.filter(it => R.path(['class'])(it) === x)(mockData)
|
||||
const { data, loading } = useQuery(GET_OPERATOR_BY_USERNAME, {
|
||||
context: { clientName: 'pazuz' },
|
||||
variables: { username: userData?.username }
|
||||
})
|
||||
|
||||
const operatorData = R.path(['operatorByUsername'], data)
|
||||
|
||||
const balanceData = [
|
||||
{
|
||||
id: 'fiatBalance',
|
||||
display: 'Fiat balance',
|
||||
amount:
|
||||
operatorData?.fiatBalances[operatorData?.preferredFiatCurrency] ?? 0,
|
||||
currency: R.toUpper(operatorData?.preferredFiatCurrency ?? ''),
|
||||
class: 'Available balance'
|
||||
},
|
||||
{
|
||||
id: 'hedgingReserve',
|
||||
display: 'Hedging reserve',
|
||||
amount:
|
||||
operatorData?.fiatBalances[operatorData?.preferredFiatCurrency] ?? 0,
|
||||
currency: R.toUpper(operatorData?.preferredFiatCurrency ?? ''),
|
||||
class: 'Available balance',
|
||||
direction: 'out'
|
||||
}
|
||||
]
|
||||
|
||||
const walletData = [
|
||||
{
|
||||
id: 'hedgedWalletAssets',
|
||||
display: 'Hedged wallet assets',
|
||||
amount: 0,
|
||||
currency: R.toUpper(operatorData?.preferredFiatCurrency ?? ''),
|
||||
class: 'Wallet assets',
|
||||
direction: 'in'
|
||||
},
|
||||
{
|
||||
id: 'unhedgedWalletAssets',
|
||||
display: 'Unhedged wallet assets',
|
||||
amount: 0,
|
||||
currency: R.toUpper(operatorData?.preferredFiatCurrency ?? ''),
|
||||
class: 'Wallet assets',
|
||||
direction: 'in'
|
||||
}
|
||||
]
|
||||
|
||||
const totalData = [
|
||||
{
|
||||
id: 'fiatBalance',
|
||||
display: 'Fiat balance',
|
||||
amount:
|
||||
operatorData?.fiatBalances[operatorData?.preferredFiatCurrency] ?? 0,
|
||||
currency: R.toUpper(operatorData?.preferredFiatCurrency ?? '')
|
||||
},
|
||||
{
|
||||
id: 'hedgingReserve',
|
||||
display: 'Hedging reserve',
|
||||
amount: 0,
|
||||
currency: R.toUpper(operatorData?.preferredFiatCurrency ?? ''),
|
||||
direction: 'out'
|
||||
},
|
||||
{
|
||||
id: 'hedgedWalletAssets',
|
||||
display: 'Market value of hedged wallet assets',
|
||||
amount: 0,
|
||||
currency: R.toUpper(operatorData?.preferredFiatCurrency ?? ''),
|
||||
direction: 'in'
|
||||
},
|
||||
{
|
||||
id: 'unhedgedWalletAssets',
|
||||
display: 'Unhedged wallet assets',
|
||||
amount: 0,
|
||||
currency: R.toUpper(operatorData?.preferredFiatCurrency ?? ''),
|
||||
direction: 'in'
|
||||
}
|
||||
]
|
||||
|
||||
return (
|
||||
!loading && (
|
||||
<>
|
||||
<TitleSection title="Balance sheet" />
|
||||
<div className={classes.root}>
|
||||
|
|
@ -177,15 +223,15 @@ const Assets = () => {
|
|||
<div className={classes.leftSide}>
|
||||
<AssetsAmountTable
|
||||
title="Available balance"
|
||||
data={filterByClass('Available balance')}
|
||||
numToRender={mockData.length}
|
||||
data={balanceData}
|
||||
numToRender={balanceData.length}
|
||||
/>
|
||||
</div>
|
||||
<div className={classes.leftSide}>
|
||||
<AssetsAmountTable
|
||||
title="Wallet assets"
|
||||
data={filterByClass('Wallet assets')}
|
||||
numToRender={mockData.length}
|
||||
data={walletData}
|
||||
numToRender={walletData.length}
|
||||
/>
|
||||
</div>
|
||||
</Grid>
|
||||
|
|
@ -195,8 +241,8 @@ const Assets = () => {
|
|||
<div className={classes.rightSide}>
|
||||
<AssetsAmountTable
|
||||
title="Total assets"
|
||||
data={mockDataTotal}
|
||||
numToRender={mockDataTotal.length}
|
||||
data={totalData}
|
||||
numToRender={totalData.length}
|
||||
/>
|
||||
</div>
|
||||
</Grid>
|
||||
|
|
@ -205,6 +251,7 @@ const Assets = () => {
|
|||
</div>
|
||||
</>
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
export default Assets
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
import { useMutation, useLazyQuery } from '@apollo/react-hooks'
|
||||
import { makeStyles } from '@material-ui/core/styles'
|
||||
import base64 from 'base-64'
|
||||
import gql from 'graphql-tag'
|
||||
import React, { useContext, useState } from 'react'
|
||||
import { useHistory } from 'react-router-dom'
|
||||
|
|
@ -56,7 +57,17 @@ const Input2FAState = ({ state, dispatch }) => {
|
|||
|
||||
const [input2FA, { error: mutationError }] = useMutation(INPUT_2FA, {
|
||||
onCompleted: ({ input2FA: success }) => {
|
||||
success ? getUserData() : setInvalidToken(true)
|
||||
if (success) {
|
||||
const options = {
|
||||
context: {
|
||||
headers: {
|
||||
'Pazuz-Operator-Identifier': base64.encode(state.clientField)
|
||||
}
|
||||
}
|
||||
}
|
||||
return getUserData(options)
|
||||
}
|
||||
return setInvalidToken(true)
|
||||
}
|
||||
})
|
||||
|
||||
|
|
@ -76,14 +87,21 @@ const Input2FAState = ({ state, dispatch }) => {
|
|||
return
|
||||
}
|
||||
|
||||
input2FA({
|
||||
const options = {
|
||||
variables: {
|
||||
username: state.clientField,
|
||||
password: state.passwordField,
|
||||
code: state.twoFAField,
|
||||
rememberMe: state.rememberMeField
|
||||
},
|
||||
context: {
|
||||
headers: {
|
||||
'Pazuz-Operator-Identifier': base64.encode(state.clientField)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
input2FA(options)
|
||||
}
|
||||
|
||||
const getErrorMsg = () => {
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
import { useMutation } from '@apollo/react-hooks'
|
||||
import { makeStyles } from '@material-ui/core/styles'
|
||||
import base64 from 'base-64'
|
||||
import { Field, Form, Formik } from 'formik'
|
||||
import gql from 'graphql-tag'
|
||||
import React from 'react'
|
||||
|
|
@ -49,12 +50,18 @@ const LoginState = ({ state, dispatch }) => {
|
|||
const [login, { error: mutationError }] = useMutation(LOGIN)
|
||||
|
||||
const submitLogin = async (username, password, rememberMe) => {
|
||||
const { data: loginResponse } = await login({
|
||||
const options = {
|
||||
variables: {
|
||||
username,
|
||||
password
|
||||
},
|
||||
context: {
|
||||
headers: {
|
||||
'Pazuz-Operator-Identifier': base64.encode(username)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
const { data: loginResponse } = await login(options)
|
||||
|
||||
if (!loginResponse.login) return
|
||||
|
||||
|
|
|
|||
|
|
@ -91,10 +91,16 @@ const Register = () => {
|
|||
const classes = useStyles()
|
||||
const history = useHistory()
|
||||
const token = QueryParams().get('t')
|
||||
const identifier = QueryParams().get('id') ?? null
|
||||
|
||||
const [state, dispatch] = useReducer(reducer, initialState)
|
||||
|
||||
const { error: queryError, loading } = useQuery(VALIDATE_REGISTER_LINK, {
|
||||
const queryOptions = {
|
||||
context: {
|
||||
headers: {
|
||||
'Pazuz-Operator-Identifier': identifier
|
||||
}
|
||||
},
|
||||
variables: { token: token },
|
||||
onCompleted: ({ validateRegisterLink: info }) => {
|
||||
if (!info) {
|
||||
|
|
@ -114,7 +120,12 @@ const Register = () => {
|
|||
dispatch({
|
||||
type: 'failure'
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
const { error: queryError, loading } = useQuery(
|
||||
VALIDATE_REGISTER_LINK,
|
||||
queryOptions
|
||||
)
|
||||
|
||||
const [register, { error: mutationError }] = useMutation(REGISTER, {
|
||||
onCompleted: ({ register: success }) => {
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
import { useMutation, useQuery, useLazyQuery } from '@apollo/react-hooks'
|
||||
import { makeStyles } from '@material-ui/core/styles'
|
||||
import base64 from 'base-64'
|
||||
import gql from 'graphql-tag'
|
||||
import QRCode from 'qrcode.react'
|
||||
import React, { useContext, useState } from 'react'
|
||||
|
|
@ -67,13 +68,34 @@ const Setup2FAState = ({ state, dispatch }) => {
|
|||
setInvalidToken(false)
|
||||
}
|
||||
|
||||
const { error: queryError } = useQuery(GET_2FA_SECRET, {
|
||||
const queryOptions = {
|
||||
variables: { username: state.clientField, password: state.passwordField },
|
||||
context: {
|
||||
headers: {
|
||||
'Pazuz-Operator-Identifier': base64.encode(state.clientField)
|
||||
}
|
||||
},
|
||||
onCompleted: ({ get2FASecret }) => {
|
||||
setSecret(get2FASecret.secret)
|
||||
setOtpauth(get2FASecret.otpauth)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
const mutationOptions = {
|
||||
variables: {
|
||||
username: state.clientField,
|
||||
password: state.passwordField,
|
||||
rememberMe: state.rememberMeField,
|
||||
codeConfirmation: twoFAConfirmation
|
||||
},
|
||||
context: {
|
||||
headers: {
|
||||
'Pazuz-Operator-Identifier': base64.encode(state.clientField)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const { error: queryError } = useQuery(GET_2FA_SECRET, queryOptions)
|
||||
|
||||
const [getUserData] = useLazyQuery(GET_USER_DATA, {
|
||||
onCompleted: ({ userData }) => {
|
||||
|
|
@ -84,7 +106,14 @@ const Setup2FAState = ({ state, dispatch }) => {
|
|||
|
||||
const [setup2FA, { error: mutationError }] = useMutation(SETUP_2FA, {
|
||||
onCompleted: ({ setup2FA: success }) => {
|
||||
success ? getUserData() : setInvalidToken(true)
|
||||
const options = {
|
||||
context: {
|
||||
headers: {
|
||||
'Pazuz-Operator-Identifier': base64.encode(state.clientField)
|
||||
}
|
||||
}
|
||||
}
|
||||
success ? getUserData(options) : setInvalidToken(true)
|
||||
}
|
||||
})
|
||||
|
||||
|
|
@ -149,14 +178,7 @@ const Setup2FAState = ({ state, dispatch }) => {
|
|||
setInvalidToken(true)
|
||||
return
|
||||
}
|
||||
setup2FA({
|
||||
variables: {
|
||||
username: state.clientField,
|
||||
password: state.passwordField,
|
||||
rememberMe: state.rememberMeField,
|
||||
codeConfirmation: twoFAConfirmation
|
||||
}
|
||||
})
|
||||
setup2FA(mutationOptions)
|
||||
}}
|
||||
buttonClassName={classes.loginButton}>
|
||||
Done
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
import { useMutation } from '@apollo/react-hooks'
|
||||
import { makeStyles } from '@material-ui/core/styles'
|
||||
import base64 from 'base-64'
|
||||
import classnames from 'classnames'
|
||||
import { Field, Form, Formik } from 'formik'
|
||||
import gql from 'graphql-tag'
|
||||
|
|
@ -74,7 +75,12 @@ const CreateUserModal = ({ state, dispatch }) => {
|
|||
|
||||
const [createUser, { error }] = useMutation(CREATE_USER, {
|
||||
onCompleted: ({ createRegisterToken: token }) => {
|
||||
setCreateUserURL(urlResolver(`/register?t=${token.token}`))
|
||||
const queryParams =
|
||||
// Pazuz-created register tokens add a field to identify the creator
|
||||
process.env.REACT_APP_BUILD_TARGET === 'LAMASSU'
|
||||
? `t=${token.token}`
|
||||
: `t=${token.token}&id=${base64.encode(usernameField)}`
|
||||
setCreateUserURL(urlResolver(`/register?${queryParams}`))
|
||||
}
|
||||
})
|
||||
|
||||
|
|
|
|||
|
|
@ -12,7 +12,10 @@ import AppContext from 'src/AppContext'
|
|||
const URI =
|
||||
process.env.NODE_ENV === 'development' ? 'https://localhost:8070' : ''
|
||||
|
||||
const getClient = (history, location, setUserData, setRole) =>
|
||||
const ALT_URI =
|
||||
process.env.NODE_ENV === 'development' ? 'http://localhost:4001' : ''
|
||||
|
||||
const getClient = (history, location, getUserData, setUserData, setRole) =>
|
||||
new ApolloClient({
|
||||
link: ApolloLink.from([
|
||||
onError(({ graphQLErrors, networkError }) => {
|
||||
|
|
@ -36,17 +39,24 @@ const getClient = (history, location, setUserData, setRole) =>
|
|||
} = context
|
||||
|
||||
if (headers) {
|
||||
const role = headers.get('role')
|
||||
const role = headers.get('lamassu_role')
|
||||
setRole(role)
|
||||
}
|
||||
|
||||
return response
|
||||
})
|
||||
}),
|
||||
ApolloLink.split(
|
||||
operation => operation.getContext().clientName === 'pazuz',
|
||||
new HttpLink({
|
||||
credentials: 'include',
|
||||
uri: `${ALT_URI}/graphql`
|
||||
}),
|
||||
new HttpLink({
|
||||
credentials: 'include',
|
||||
uri: `${URI}/graphql`
|
||||
})
|
||||
)
|
||||
]),
|
||||
cache: new InMemoryCache(),
|
||||
defaultOptions: {
|
||||
|
|
@ -67,8 +77,14 @@ const getClient = (history, location, setUserData, setRole) =>
|
|||
const Provider = ({ children }) => {
|
||||
const history = useHistory()
|
||||
const location = useLocation()
|
||||
const { setUserData, setRole } = useContext(AppContext)
|
||||
const client = getClient(history, location, setUserData, setRole)
|
||||
const { userData, setUserData, setRole } = useContext(AppContext)
|
||||
const client = getClient(
|
||||
history,
|
||||
location,
|
||||
() => userData,
|
||||
setUserData,
|
||||
setRole
|
||||
)
|
||||
|
||||
return <ApolloProvider client={client}>{children}</ApolloProvider>
|
||||
}
|
||||
|
|
|
|||
10
package-lock.json
generated
10
package-lock.json
generated
|
|
@ -5979,6 +5979,11 @@
|
|||
}
|
||||
}
|
||||
},
|
||||
"base-64": {
|
||||
"version": "1.0.0",
|
||||
"resolved": "https://registry.npmjs.org/base-64/-/base-64-1.0.0.tgz",
|
||||
"integrity": "sha512-kwDPIFCGx0NZHog36dj+tHiwP4QMzsZ3AgMViUBKI0+V5n4U0ufTCUMhnQ04diaRI8EX/QcPfql7zlhZ7j4zgg=="
|
||||
},
|
||||
"base-x": {
|
||||
"version": "3.0.9",
|
||||
"resolved": "https://registry.npmjs.org/base-x/-/base-x-3.0.9.tgz",
|
||||
|
|
@ -17669,6 +17674,11 @@
|
|||
"resolved": "https://registry.npmjs.org/querystringify/-/querystringify-2.2.0.tgz",
|
||||
"integrity": "sha512-FIqgj2EUvTa7R50u0rGsyTftzjYmv/a3hO345bZNrqabNqjtgiDMgmo4mkUjd+nzU5oF3dClKqFIPUKybUyqoQ=="
|
||||
},
|
||||
"queue-promise": {
|
||||
"version": "2.2.1",
|
||||
"resolved": "https://registry.npmjs.org/queue-promise/-/queue-promise-2.2.1.tgz",
|
||||
"integrity": "sha512-C3eyRwLF9m6dPV4MtqMVFX+Xmc7keZ9Ievm3jJ/wWM5t3uVbFnGsJXwpYzZ4LaIEcX9bss/mdaKzyrO6xheRuA=="
|
||||
},
|
||||
"random-bytes": {
|
||||
"version": "1.0.0",
|
||||
"resolved": "https://registry.npmjs.org/random-bytes/-/random-bytes-1.0.0.tgz",
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@
|
|||
"apollo-server-express": "2.25.1",
|
||||
"argon2": "0.28.2",
|
||||
"axios": "0.21.1",
|
||||
"base-64": "^1.0.0",
|
||||
"base-x": "3.0.9",
|
||||
"bchaddrjs": "^0.3.0",
|
||||
"bignumber.js": "9.0.1",
|
||||
|
|
@ -65,6 +66,7 @@
|
|||
"pretty-ms": "^2.1.0",
|
||||
"promise-sequential": "^1.1.1",
|
||||
"request-promise": "^4.2.6",
|
||||
"queue-promise": "^2.2.1",
|
||||
"semver": "^7.1.3",
|
||||
"serve-static": "^1.12.4",
|
||||
"socket.io": "^2.0.3",
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue