-
Notifications
You must be signed in to change notification settings - Fork 134
/
update-account-ip.ts
120 lines (98 loc) · 3.19 KB
/
update-account-ip.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import { getIpConfig } from "@config"
import { RepositoryError } from "@domain/errors"
import { IpFetcherServiceError } from "@domain/ipfetcher"
import { ErrorLevel } from "@domain/shared"
import { IpFetcher } from "@services/ipfetcher"
import { AccountsIpRepository } from "@services/mongoose/accounts-ips"
import {
addAttributesToCurrentSpan,
recordExceptionInCurrentSpan,
} from "@services/tracing"
const accountsIp = AccountsIpRepository()
export const updateAccountIPsInfo = async ({
accountId,
ip,
logger,
}: {
accountId: AccountId
ip?: IpAddress
logger: Logger
}): Promise<void | RepositoryError> => {
const ipConfig = getIpConfig()
const lastConnection = new Date()
const accountIP = await accountsIp.findById(accountId)
if (accountIP instanceof RepositoryError) return accountIP
if (!ip || !ipConfig.ipRecordingEnabled) {
const result = await accountsIp.update(accountIP)
if (result instanceof Error) {
logger.error(
{ result, accountId, ip },
"impossible to update account last connection",
)
return result
}
return
}
let ipInfo: IPType
const ipFromDb = accountIP.lastIPs.find((ipObject) => ipObject.ip === ip)
if (ipFromDb) {
ipInfo = ipFromDb
ipInfo.lastConnection = lastConnection
} else {
ipInfo = {
ip,
firstConnection: lastConnection,
lastConnection: lastConnection,
}
}
if (
ipConfig.proxyCheckingEnabled &&
(!ipInfo.isoCode || !ipInfo.proxy || !ipInfo.asn)
) {
console.log({ ipInfo }, "ipInfo")
const ipFetcher = IpFetcher()
const ipFetcherInfo = await ipFetcher.fetchIPInfo(ip)
if (ipFetcherInfo instanceof IpFetcherServiceError) {
recordExceptionInCurrentSpan({
error: ipFetcherInfo.message,
level: ErrorLevel.Warn,
attributes: {
ip,
accountId,
},
})
logger.error({ accountId, ip }, "impossible to get ip detail")
return ipFetcherInfo
}
// deep copy
const ipFetcherInfoForOtel = JSON.parse(JSON.stringify(ipFetcherInfo))
for (const key in ipFetcherInfoForOtel) {
ipFetcherInfoForOtel["proxycheck." + key] = ipFetcherInfoForOtel[key]
delete ipFetcherInfoForOtel[key]
}
addAttributesToCurrentSpan(ipFetcherInfoForOtel)
if (!ipFetcherInfo.isoCode || !ipFetcherInfo.proxy || !ipFetcherInfo.asn) {
const error = `missing mandatory fields. isoCode: ${ipFetcherInfo.isoCode}, proxy: ${ipFetcherInfo.proxy}, asn: ${ipFetcherInfo.asn}`
recordExceptionInCurrentSpan({
error,
level: ErrorLevel.Warn,
attributes: {
ip,
accountId,
},
})
} else {
// using Object.assign instead of ... because of conflict with mongoose hidden properties
ipInfo = Object.assign(ipInfo, ipFetcherInfo)
// removing current ip from lastIPs - if it exists
accountIP.lastIPs = accountIP.lastIPs.filter((ipDb) => ipDb.ip !== ip)
// adding it back with the correct info
accountIP.lastIPs.push(ipInfo)
}
}
const result = await accountsIp.update(accountIP)
if (result instanceof Error) {
logger.error({ result, accountId, ip }, "impossible to update ip")
return result
}
}