This repository has been archived by the owner on May 14, 2021. It is now read-only.
/
users.ts
318 lines (276 loc) · 10.3 KB
/
users.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
import { Brackets } from "typeorm";
import * as metrics from "../../metrics";
import { Order } from "../../models/orders";
import { isNothing, normalizeError, readUTCDate } from "../../utils/utils";
import { Application } from "../../models/applications";
import { getDefaultLogger as logger } from "../../logging";
import { User, AuthToken as DbAuthToken, WalletApplication } from "../../models/users";
import { MaxWalletsExceeded, NoSuchUser, NoSuchApp, CrossAppWallet } from "../../errors";
import { create as createUserRegistrationFailed } from "../../analytics/events/user_registration_failed";
import { create as createUserLoginServerRequested } from "../../analytics/events/user_login_server_requested";
import { create as createUserLoginServerSucceeded } from "../../analytics/events/user_login_server_succeeded";
import { create as createUserRegistrationRequested } from "../../analytics/events/user_registration_requested";
import { create as createUserRegistrationSucceeded } from "../../analytics/events/user_registration_succeeded";
import { create as createUserLogoutServerRequested } from "../../analytics/events/user_logout_server_requested";
import { create as createWalletAddressUpdateSucceeded } from "../../analytics/events/wallet_address_update_succeeded";
import { create as createRestoreRequestFailed } from "../../analytics/events/restore_request_failed";
import * as payment from "./payment";
import { assertRateLimitRegistration } from "../../utils/rate_limit";
import { setHttpContext } from "../auth";
import { getRedisClient } from "../../redis";
import { BlockchainVersion } from "../../models/offers";
const notExistsTTL = 15 * 60; // cache the fact that a user doesn't exist for 15 minutes
export type V1AuthToken = {
token: string;
activated: boolean;
expiration_date: string;
app_id: string;
user_id: string;
ecosystem_user_id: string;
};
function V1AuthTokenDbToApi(authToken: DbAuthToken, user: User): V1AuthToken {
return {
token: authToken.id,
activated: true, // always true - activation not needed
app_id: user.appId,
user_id: user.appUserId,
ecosystem_user_id: user.id,
expiration_date: authToken.expireDate.toISOString()
};
}
export async function v1GetOrCreateUserCredentials(
app: Application,
appUserId: string,
walletAddress: string,
deviceId: string): Promise<V1AuthToken> {
const data = await register(app, appUserId, app.id, deviceId);
await updateUser(data.user, { walletAddress, deviceId });
return V1AuthTokenDbToApi(data.token, data.user);
}
export type AuthToken = {
token: string;
app_id: string;
user_id: string;
activated: boolean;
expiration_date: string;
ecosystem_user_id: string;
};
function AuthTokenDbToApi(authToken: DbAuthToken, user: User): AuthToken {
return {
activated: true, // always true - activation not needed
app_id: user.appId,
token: authToken.id,
user_id: user.appUserId,
ecosystem_user_id: user.id,
expiration_date: authToken.expireDate.toISOString()
};
}
export async function getOrCreateUserCredentials(
app: Application,
appUserId: string,
deviceId: string): Promise<{ auth: AuthToken, user: UserProfile; }> {
const data = await register(app, appUserId, app.id, deviceId);
return {
user: data.profile,
auth: AuthTokenDbToApi(data.token, data.user)
};
}
export type UpdateUserProps = {
deviceId: string;
walletAddress: string;
};
export async function isRestoreAllowed(walletAddress: string, appId: string, addOnNonExisting: boolean = true): Promise<boolean> {
const appWallet = await WalletApplication.get(walletAddress);
if (appWallet) {
logger().info(`Wallet ${ walletAddress } is associated with app ${ appWallet.appId }, current app is ${ appId }`);
if (appWallet.appId !== appId) {
return false;
}
} else if (addOnNonExisting) {
logger().info(`Wallet ${ walletAddress } does not exist in wallet_application table, add`);
try {
const newWallet = WalletApplication.create({ walletAddress, appId });
await newWallet.save();
} catch (e) {
// might catch a "violates unique constraint" error, check by finding the wallet again
if (!await WalletApplication.get(walletAddress)) {
throw e;
}
}
}
return true;
}
async function createWallet(walletAddress: string, user: User, app: Application) {
let blockchainVersion: BlockchainVersion;
if (app.config.blockchain_version === "3" || app.shouldApplyGradualMigration(user.createdDate)) {
// when gradual migration is on, I don't need to create a KIN2 account
await WalletApplication.updateCreatedDate(walletAddress, "3");
await payment.createWallet(walletAddress, user.appId, user.id, "3");
blockchainVersion = "3";
} else { // kin2
await WalletApplication.updateCreatedDate(walletAddress, "2");
await Promise.all([
payment.createWallet(walletAddress, user.appId, user.id, "2"),
// optimization: create wallets on kin3 to reduce time when migrating
payment.createWallet(walletAddress, user.appId, user.id, "3"),
]);
blockchainVersion = "2";
}
logger().info(`creating wallet for user ${ user.appId }: ${ walletAddress } on KIN${ blockchainVersion }`);
}
export async function updateUser(user: User, props: UpdateUserProps) {
if (props.walletAddress) {
const wallets = await user.getWallets();
const appId = user.appId;
const app = await Application.get(appId);
const totalWalletCount = wallets.count + user.walletCount;
const walletAddress = props.walletAddress;
/* Start of Cross-app restore check (when removing, remove test too) */
if (!await isRestoreAllowed(walletAddress, appId)) {
createRestoreRequestFailed(user.id, props.deviceId, "blocking cross-app restore request").report();
throw CrossAppWallet(walletAddress, appId);
}
/* End of Cross-app restore check */
if (!app) {
throw NoSuchApp(appId);
}
if (!wallets.has(walletAddress) && !app.allowsNewWallet(totalWalletCount)) {
metrics.maxWalletsExceeded(appId);
throw MaxWalletsExceeded();
}
const isNewWallet = await user.updateWallet(props.deviceId, walletAddress);
if (isNewWallet) {
await createWallet(walletAddress, user, app);
}
metrics.walletAddressUpdate(appId, isNewWallet);
createWalletAddressUpdateSucceeded(user.id, props.deviceId).report();
}
}
export async function activateUser(authToken: DbAuthToken, user: User): Promise<AuthToken> {
// no activation needed anymore
return AuthTokenDbToApi(authToken, user);
}
export async function userExists(appId: string, appUserId: string): Promise<boolean> {
if (isNothing(appUserId) || appUserId === "") {
return false; // Some apps are sending empty appUserIds
}
// get from cache
const redis = getRedisClient();
const key = `app:${ appId }:user:${ appUserId }:exists`;
const value = await redis.async.get(key);
if (value) {
return JSON.parse(value);
}
// get from DB
const user = await User.findOne({ appId, appUserId });
// cache
if (user !== undefined) {
await redis.async.set(key, "true");
} else {
await redis.async.setex(key, notExistsTTL, "false");
}
return user !== undefined;
}
export type UserStats = {
earn_count: number;
spend_count: number;
last_earn_date?: string;
last_spend_date?: string;
};
export type V1UserProfile = {
stats: UserStats
};
export type UserProfile = {
stats: UserStats
created_date: string;
current_wallet: string | null;
};
export async function getUserProfile(userId: string, deviceId: string): Promise<UserProfile> {
const user = await User.findOneById(userId);
if (!user) {
throw NoSuchUser(userId);
}
return createUserProfileObject(user, deviceId);
}
export async function logout(user: User, token: DbAuthToken) {
token.valid = false;
createUserLogoutServerRequested(user.id, token.deviceId).report();
await token.save();
}
export async function register(
app: Application,
appUserId: string,
appId: string,
deviceId: string) {
let user = await User.findOne({ appId, appUserId });
if (!user) {
createUserRegistrationRequested(null as any, deviceId).report();
await assertRateLimitRegistration(app);
try {
logger().info("creating a new user", { appId, appUserId });
user = User.new({ appUserId, appId, isNew: true });
await user.save();
metrics.userRegister(true, appId);
createUserRegistrationSucceeded(user.id, deviceId).report();
} catch (e) {
// might catch a "violates unique constraint" error, check by finding the user again
user = await User.findOne({ appId, appUserId });
if (user) {
logger().warn("solved user registration race condition");
metrics.userRegister(false, appId);
} else {
createUserRegistrationFailed(null as any, deviceId, normalizeError(e)).report();
throw e; // some other error
}
}
} else {
metrics.userRegister(false, appId);
}
createUserLoginServerRequested(user.id, deviceId).report();
createUserLoginServerSucceeded(user.id, deviceId).report();
const authToken = await (DbAuthToken.new({ userId: user.id, deviceId }).save());
setHttpContext(authToken, user);
return {
user,
token: authToken,
profile: await createUserProfileObject(user, deviceId)
};
}
async function createUserProfileObject(user: User, deviceId: string): Promise<UserProfile> {
const data: Array<{ type: string; last_date: string; cnt: number; }> = await Order.queryBuilder("ordr_createUserProfileObject")
.select("context.type", "type")
.addSelect("MAX(ordr_createUserProfileObject.createdDate)", "last_date")
.addSelect("COUNT(*)", "cnt")
.leftJoin("ordr_createUserProfileObject.contexts", "context")
.where("context.userId = :userId", { userId: user.id })
.andWhere(new Brackets(qb => {
qb.where("ordr_createUserProfileObject.status = :status", { status: "completed" })
.orWhere(
new Brackets(qb2 => {
qb2.where("ordr_createUserProfileObject.status IN (:statuses)", { statuses: ["pending", "opened"] })
.andWhere("ordr_createUserProfileObject.expirationDate > :date", { date: new Date() });
})
);
}))
.groupBy("context.type")
.getRawMany();
const stats: UserStats = {
earn_count: 0,
spend_count: 0
};
for (const row of data) {
if (row.type === "earn") {
stats.earn_count = Number(row.cnt);
stats.last_earn_date = readUTCDate(row.last_date).toISOString();
} else if (row.type === "spend") {
stats.spend_count = Number(row.cnt);
stats.last_spend_date = readUTCDate(row.last_date).toISOString();
}
}
const wallet = (await user.getWallets(deviceId)).lastUsed();
return {
stats,
created_date: user.createdDate.toISOString(),
current_wallet: wallet ? wallet.address : null
};
}