refactor: move up dateFormat method

This commit is contained in:
Sérgio Salgado 2021-06-07 02:34:12 +01:00 committed by Josh Harvey
parent a0c77b4939
commit b5e35b82c2
3 changed files with 20 additions and 23 deletions

View file

@ -1,4 +1,5 @@
const _ = require('lodash/fp') const _ = require('lodash/fp')
const moment = require('moment')
const db = require('./db') const db = require('./db')
const pgp = require('pg-promise')() const pgp = require('pg-promise')()
@ -109,4 +110,18 @@ function simpleGetMachineLogs (deviceId, from = new Date(0).toISOString(), until
.then(_.map(_.mapKeys(_.camelCase))) .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 }

View file

@ -1,18 +1,9 @@
const { parseAsync } = require('json2csv') const { parseAsync } = require('json2csv')
const moment = require('moment')
const _ = require('lodash/fp') const _ = require('lodash/fp')
const logs = require('../../../logs') const logs = require('../../../logs')
const serverLogs = require('../../services/server-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 = { const resolvers = {
Query: { Query: {
machineLogs: (...[, { deviceId, from, until, limit, offset }]) => machineLogs: (...[, { deviceId, from, until, limit, offset }]) =>
@ -24,7 +15,7 @@ const resolvers = {
serverLogs.getServerLogs(from, until, limit, offset), serverLogs.getServerLogs(from, until, limit, offset),
serverLogsCsv: (...[, { from, until, limit, offset, timezone }]) => serverLogsCsv: (...[, { from, until, limit, offset, timezone }]) =>
serverLogs.getServerLogs(from, until, limit, offset) serverLogs.getServerLogs(from, until, limit, offset)
.then(res => parseAsync(dateFormat(timezone, res))) .then(res => parseAsync(logs.logDateFormat(timezone, res, ['timestamp'])))
} }
} }

View file

@ -1,11 +1,11 @@
const DataLoader = require('dataloader') const DataLoader = require('dataloader')
const { parseAsync } = require('json2csv') const { parseAsync } = require('json2csv')
const moment = require('moment')
const _ = require('lodash/fp') const _ = require('lodash/fp')
const filters = require('../../filters') const filters = require('../../filters')
const transactions = require('../../services/transactions') const transactions = require('../../services/transactions')
const anonymous = require('../../../constants').anonymousCustomer const anonymous = require('../../../constants').anonymousCustomer
const logDateFormat = require('../../../logs').logDateFormat
const transactionsLoader = new DataLoader(ids => transactions.getCustomerTransactionsBatch(ids)) const transactionsLoader = new DataLoader(ids => transactions.getCustomerTransactionsBatch(ids))
const txLogFields = ['txClass', 'id', 'deviceId', 'toAddress', 'cryptoAtoms', const txLogFields = ['txClass', 'id', 'deviceId', 'toAddress', 'cryptoAtoms',
@ -20,15 +20,6 @@ const txLogFields = ['txClass', 'id', 'deviceId', 'toAddress', 'cryptoAtoms',
'customerIdCardDataExpiration', 'customerIdCardData', 'customerName', 'customerIdCardDataExpiration', 'customerIdCardData', 'customerName',
'customerFrontCameraPath', 'customerIdCardPhotoPath', 'expired', 'machineName'] '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 = { const resolvers = {
Customer: { Customer: {
transactions: parent => transactionsLoader.load(parent.id) transactions: parent => transactionsLoader.load(parent.id)
@ -39,9 +30,9 @@ const resolvers = {
Query: { Query: {
transactions: (...[, { from, until, limit, offset, deviceId, txClass, machineName, customerName, fiatCode, cryptoCode, toAddress, status }]) => 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), 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) 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 }]) => transactionCsv: (...[, { id, txClass }]) =>
transactions.getTx(id, txClass).then(data => parseAsync(data)), transactions.getTx(id, txClass).then(data => parseAsync(data)),
txAssociatedDataCsv: (...[, { id, txClass }]) => txAssociatedDataCsv: (...[, { id, txClass }]) =>