diff --git a/lib/logs.js b/lib/logs.js index 0983bf94..60df7704 100644 --- a/lib/logs.js +++ b/lib/logs.js @@ -1,4 +1,5 @@ const _ = require('lodash/fp') +const moment = require('moment') const db = require('./db') const pgp = require('pg-promise')() @@ -109,4 +110,18 @@ function simpleGetMachineLogs (deviceId, from = new Date(0).toISOString(), until .then(_.map(_.mapKeys(_.camelCase))) } -module.exports = { getUnlimitedMachineLogs, getMachineLogs, simpleGetMachineLogs, update, getLastSeen, clearOldLogs } +function logDateFormat (timezone, logs, fields) { + const offset = timezone.split(':')[1] + + return _.map(log => { + const values = _.map(field => moment.utc(log[field]).utcOffset(parseInt(offset)).format('YYYY-MM-DDTHH:mm:ss.SSS'), fields) + const fieldsToOverride = _.zipObject(fields, values) + + return { + ...log, + ...fieldsToOverride + } + }, logs) +} + +module.exports = { getUnlimitedMachineLogs, getMachineLogs, simpleGetMachineLogs, update, getLastSeen, clearOldLogs, logDateFormat } diff --git a/lib/new-admin/graphql/resolvers/log.resolver.js b/lib/new-admin/graphql/resolvers/log.resolver.js index b8a90ecc..8e3bc3fd 100644 --- a/lib/new-admin/graphql/resolvers/log.resolver.js +++ b/lib/new-admin/graphql/resolvers/log.resolver.js @@ -1,18 +1,9 @@ const { parseAsync } = require('json2csv') -const moment = require('moment') const _ = require('lodash/fp') const logs = require('../../../logs') const serverLogs = require('../../services/server-logs') -const dateFormat = (timezone, logs) => _.map(log => { - const offset = timezone.split(':')[1] - return { - ...log, - timestamp: moment.utc(log.timestamp).utcOffset(parseInt(offset)).format('YYYY-MM-DDTHH:mm:ss.SSS') - } -}, logs) - const resolvers = { Query: { machineLogs: (...[, { deviceId, from, until, limit, offset }]) => @@ -24,7 +15,7 @@ const resolvers = { serverLogs.getServerLogs(from, until, limit, offset), serverLogsCsv: (...[, { from, until, limit, offset, timezone }]) => serverLogs.getServerLogs(from, until, limit, offset) - .then(res => parseAsync(dateFormat(timezone, res))) + .then(res => parseAsync(logs.logDateFormat(timezone, res, ['timestamp']))) } } diff --git a/lib/new-admin/graphql/resolvers/transaction.resolver.js b/lib/new-admin/graphql/resolvers/transaction.resolver.js index 15457f98..e3d40ff2 100644 --- a/lib/new-admin/graphql/resolvers/transaction.resolver.js +++ b/lib/new-admin/graphql/resolvers/transaction.resolver.js @@ -1,11 +1,11 @@ const DataLoader = require('dataloader') const { parseAsync } = require('json2csv') -const moment = require('moment') const _ = require('lodash/fp') const filters = require('../../filters') const transactions = require('../../services/transactions') const anonymous = require('../../../constants').anonymousCustomer +const logDateFormat = require('../../../logs').logDateFormat const transactionsLoader = new DataLoader(ids => transactions.getCustomerTransactionsBatch(ids)) const txLogFields = ['txClass', 'id', 'deviceId', 'toAddress', 'cryptoAtoms', @@ -20,15 +20,6 @@ const txLogFields = ['txClass', 'id', 'deviceId', 'toAddress', 'cryptoAtoms', 'customerIdCardDataExpiration', 'customerIdCardData', 'customerName', 'customerFrontCameraPath', 'customerIdCardPhotoPath', 'expired', 'machineName'] -const dateFormat = (timezone, logs) => _.map(log => { - const offset = timezone.split(':')[1] - return { - ...log, - created: moment.utc(log.created).utcOffset(parseInt(offset)).format('YYYY-MM-DDTHH:mm:ss.SSS'), - sendTime: moment.utc(log.sendTime).utcOffset(parseInt(offset)).format('YYYY-MM-DDTHH:mm:ss.SSS') - } -}, logs) - const resolvers = { Customer: { transactions: parent => transactionsLoader.load(parent.id) @@ -39,9 +30,9 @@ const resolvers = { Query: { transactions: (...[, { from, until, limit, offset, deviceId, txClass, machineName, customerName, fiatCode, cryptoCode, toAddress, status }]) => transactions.batch(from, until, limit, offset, deviceId, txClass, machineName, customerName, fiatCode, cryptoCode, toAddress, status), - transactionsCsv: (...[, { from, until, limit, offset, txClass, machineName, customerName, fiatCode, cryptoCode, toAddress, status }]) => + transactionsCsv: (...[, { from, until, limit, offset, txClass, machineName, customerName, fiatCode, cryptoCode, toAddress, status, timezone }]) => transactions.batch(from, until, limit, offset, txClass, machineName, customerName, fiatCode, cryptoCode, toAddress, status) - .then(data => parseAsync(data, { fields: tx_logFields, transforms: dateFormat(timezone, data) })), + .then(data => parseAsync(data, { fields: tx_logFields, transforms: logDateFormat(timezone, data, ['created', 'sendTime']) })), transactionCsv: (...[, { id, txClass }]) => transactions.getTx(id, txClass).then(data => parseAsync(data)), txAssociatedDataCsv: (...[, { id, txClass }]) =>