2019-05-29 05:21:06 -04:00
|
|
|
/* eslint-disable
|
|
|
|
max-len,
|
|
|
|
no-return-assign,
|
|
|
|
*/
|
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* DS102: Remove unnecessary code created because of implicit returns
|
|
|
|
* DS103: Rewrite code to no longer use __guard__
|
|
|
|
* DS207: Consider shorter variations of null checks
|
|
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
*/
|
|
|
|
let GeoIpLookup
|
|
|
|
const request = require('request')
|
|
|
|
const settings = require('settings-sharelatex')
|
|
|
|
const _ = require('underscore')
|
|
|
|
const logger = require('logger-sharelatex')
|
|
|
|
const URL = require('url')
|
|
|
|
|
|
|
|
const currencyMappings = {
|
|
|
|
GB: 'GBP',
|
|
|
|
US: 'USD',
|
|
|
|
CH: 'CHF',
|
|
|
|
NZ: 'NZD',
|
|
|
|
AU: 'AUD',
|
|
|
|
DK: 'DKK',
|
|
|
|
NO: 'NOK',
|
|
|
|
CA: 'CAD',
|
|
|
|
SE: 'SEK'
|
|
|
|
}
|
|
|
|
|
|
|
|
// Countries which would likely prefer Euro's
|
|
|
|
const EuroCountries = [
|
|
|
|
'AT',
|
|
|
|
'BE',
|
|
|
|
'BG',
|
|
|
|
'HR',
|
|
|
|
'CY',
|
|
|
|
'CZ',
|
|
|
|
'EE',
|
|
|
|
'FI',
|
|
|
|
'FR',
|
|
|
|
'DE',
|
|
|
|
'EL',
|
|
|
|
'HU',
|
|
|
|
'IE',
|
|
|
|
'IT',
|
|
|
|
'LV',
|
|
|
|
'LT',
|
|
|
|
'LU',
|
|
|
|
'MT',
|
|
|
|
'NL',
|
|
|
|
'PL',
|
|
|
|
'PT',
|
|
|
|
'RO',
|
|
|
|
'SK',
|
|
|
|
'SI',
|
|
|
|
'ES'
|
|
|
|
]
|
|
|
|
|
|
|
|
_.each(EuroCountries, country => (currencyMappings[country] = 'EUR'))
|
|
|
|
|
|
|
|
module.exports = GeoIpLookup = {
|
|
|
|
getDetails(ip, callback) {
|
|
|
|
if (ip == null) {
|
|
|
|
const e = new Error('no ip passed')
|
|
|
|
return callback(e)
|
|
|
|
}
|
|
|
|
ip = ip.trim().split(' ')[0]
|
|
|
|
const opts = {
|
|
|
|
url: URL.resolve(settings.apis.geoIpLookup.url, ip),
|
|
|
|
timeout: 1000,
|
|
|
|
json: true
|
|
|
|
}
|
|
|
|
logger.log({ ip, opts }, 'getting geo ip details')
|
2021-04-14 09:17:21 -04:00
|
|
|
return request.get(opts, function (err, res, ipDetails) {
|
2019-05-29 05:21:06 -04:00
|
|
|
if (err != null) {
|
2019-07-01 09:48:09 -04:00
|
|
|
logger.warn({ err, ip }, 'error getting ip details')
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
return callback(err, ipDetails)
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
getCurrencyCode(ip, callback) {
|
2021-04-14 09:17:21 -04:00
|
|
|
return GeoIpLookup.getDetails(ip, function (err, ipDetails) {
|
2019-05-29 05:21:06 -04:00
|
|
|
if (err != null || ipDetails == null) {
|
|
|
|
logger.err(
|
|
|
|
{ err, ip },
|
|
|
|
'problem getting currencyCode for ip, defaulting to USD'
|
|
|
|
)
|
|
|
|
return callback(null, 'USD')
|
|
|
|
}
|
|
|
|
const countryCode = __guard__(
|
|
|
|
ipDetails != null ? ipDetails.country_code : undefined,
|
|
|
|
x => x.toUpperCase()
|
|
|
|
)
|
|
|
|
const currencyCode = currencyMappings[countryCode] || 'USD'
|
|
|
|
logger.log({ ip, currencyCode, ipDetails }, 'got currencyCode for ip')
|
|
|
|
return callback(err, currencyCode, countryCode)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function __guard__(value, transform) {
|
|
|
|
return typeof value !== 'undefined' && value !== null
|
|
|
|
? transform(value)
|
|
|
|
: undefined
|
|
|
|
}
|