@casual-simulation/aux-records 3.2.3 → 3.2.4-alpha.5940763320
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AIChatInterface.d.ts +8 -0
- package/AIChatInterface.js.map +1 -1
- package/AIController.d.ts +10 -4
- package/AIController.js +112 -3
- package/AIController.js.map +1 -1
- package/AuthController.d.ts +4 -8
- package/AuthController.js +39 -45
- package/AuthController.js.map +1 -1
- package/AuthStore.d.ts +236 -4
- package/ConfigurationStore.d.ts +12 -0
- package/ConfigurationStore.js +2 -0
- package/ConfigurationStore.js.map +1 -0
- package/DataRecordsController.d.ts +18 -6
- package/DataRecordsController.js +31 -5
- package/DataRecordsController.js.map +1 -1
- package/Errors.d.ts +4 -0
- package/EventRecordsController.d.ts +12 -3
- package/EventRecordsController.js +27 -5
- package/EventRecordsController.js.map +1 -1
- package/FileRecordsController.d.ts +13 -3
- package/FileRecordsController.js +46 -9
- package/FileRecordsController.js.map +1 -1
- package/MemoryFileRecordsLookup.d.ts +11 -0
- package/MemoryFileRecordsLookup.js +128 -0
- package/MemoryFileRecordsLookup.js.map +1 -0
- package/MemoryStore.d.ts +191 -0
- package/MemoryStore.js +1381 -0
- package/MemoryStore.js.map +1 -0
- package/MetricsStore.d.ts +186 -0
- package/MetricsStore.js +2 -0
- package/MetricsStore.js.map +1 -0
- package/OpenAIChatInterface.js +1 -0
- package/OpenAIChatInterface.js.map +1 -1
- package/PolicyController.d.ts +7 -4
- package/PolicyController.js +33 -8
- package/PolicyController.js.map +1 -1
- package/RecordsController.d.ts +185 -7
- package/RecordsController.js +588 -70
- package/RecordsController.js.map +1 -1
- package/RecordsHttpServer.d.ts +8 -0
- package/RecordsHttpServer.js +450 -8
- package/RecordsHttpServer.js.map +1 -1
- package/RecordsStore.d.ts +263 -5
- package/StripeInterface.d.ts +331 -0
- package/StripeInterface.js +37 -1
- package/StripeInterface.js.map +1 -1
- package/SubscriptionConfiguration.d.ts +2523 -33
- package/SubscriptionConfiguration.js +130 -1
- package/SubscriptionConfiguration.js.map +1 -1
- package/SubscriptionController.d.ts +23 -6
- package/SubscriptionController.js +344 -69
- package/SubscriptionController.js.map +1 -1
- package/TestUtils.d.ts +7 -6
- package/TestUtils.js +28 -14
- package/TestUtils.js.map +1 -1
- package/Utils.js +9 -0
- package/Utils.js.map +1 -1
- package/index.d.ts +5 -4
- package/index.js +5 -4
- package/index.js.map +1 -1
- package/package.json +2 -2
- package/MemoryAuthStore.d.ts +0 -33
- package/MemoryAuthStore.js +0 -186
- package/MemoryAuthStore.js.map +0 -1
- package/MemoryDataRecordsStore.d.ts +0 -10
- package/MemoryDataRecordsStore.js +0 -98
- package/MemoryDataRecordsStore.js.map +0 -1
- package/MemoryEventRecordsStore.d.ts +0 -10
- package/MemoryEventRecordsStore.js +0 -89
- package/MemoryEventRecordsStore.js.map +0 -1
- package/MemoryFileRecordsStore.d.ts +0 -25
- package/MemoryFileRecordsStore.js +0 -310
- package/MemoryFileRecordsStore.js.map +0 -1
- package/MemoryPolicyStore.d.ts +0 -43
- package/MemoryPolicyStore.js +0 -255
- package/MemoryPolicyStore.js.map +0 -1
- package/MemoryRecordsStore.d.ts +0 -13
- package/MemoryRecordsStore.js +0 -67
- package/MemoryRecordsStore.js.map +0 -1
package/MemoryStore.js
ADDED
|
@@ -0,0 +1,1381 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
import { sortBy } from 'lodash';
|
|
11
|
+
import { v4 as uuid } from 'uuid';
|
|
12
|
+
import { getExpireTime, } from './PolicyStore';
|
|
13
|
+
import { DEFAULT_ANY_RESOURCE_POLICY_DOCUMENT, DEFAULT_PUBLIC_READ_POLICY_DOCUMENT, PUBLIC_READ_MARKER, } from './PolicyPermissions';
|
|
14
|
+
import { DateTime } from 'luxon';
|
|
15
|
+
export class MemoryStore {
|
|
16
|
+
get users() {
|
|
17
|
+
return this._users;
|
|
18
|
+
}
|
|
19
|
+
get loginRequests() {
|
|
20
|
+
return this._loginRequests;
|
|
21
|
+
}
|
|
22
|
+
get sessions() {
|
|
23
|
+
return this._sessions;
|
|
24
|
+
}
|
|
25
|
+
get emailRules() {
|
|
26
|
+
return this._emailRules;
|
|
27
|
+
}
|
|
28
|
+
get smsRules() {
|
|
29
|
+
return this._smsRules;
|
|
30
|
+
}
|
|
31
|
+
get recordKeys() {
|
|
32
|
+
return this._recordKeys;
|
|
33
|
+
}
|
|
34
|
+
get dataBuckets() {
|
|
35
|
+
return this._dataBuckets;
|
|
36
|
+
}
|
|
37
|
+
get eventBuckets() {
|
|
38
|
+
return this._eventBuckets;
|
|
39
|
+
}
|
|
40
|
+
get files() {
|
|
41
|
+
return this._files;
|
|
42
|
+
}
|
|
43
|
+
get subscriptionConfiguration() {
|
|
44
|
+
return this._subscriptionConfiguration;
|
|
45
|
+
}
|
|
46
|
+
set subscriptionConfiguration(value) {
|
|
47
|
+
this._subscriptionConfiguration = value;
|
|
48
|
+
}
|
|
49
|
+
constructor(config) {
|
|
50
|
+
this._users = [];
|
|
51
|
+
this._loginRequests = [];
|
|
52
|
+
this._sessions = [];
|
|
53
|
+
this._subscriptions = [];
|
|
54
|
+
this._periods = [];
|
|
55
|
+
this._invoices = [];
|
|
56
|
+
this._records = [];
|
|
57
|
+
this._recordKeys = [];
|
|
58
|
+
this._studios = [];
|
|
59
|
+
this._studioAssignments = [];
|
|
60
|
+
this._aiChatMetrics = [];
|
|
61
|
+
this._aiImageMetrics = [];
|
|
62
|
+
this._aiSkyboxMetrics = [];
|
|
63
|
+
this._dataBuckets = new Map();
|
|
64
|
+
this._eventBuckets = new Map();
|
|
65
|
+
this._files = new Map();
|
|
66
|
+
this._fileUploadUrl = 'http://localhost:9191';
|
|
67
|
+
this._emailRules = [];
|
|
68
|
+
this._smsRules = [];
|
|
69
|
+
this._subscriptionConfiguration = config.subscriptions;
|
|
70
|
+
this.policies = {};
|
|
71
|
+
this.roles = {};
|
|
72
|
+
this.roleAssignments = {};
|
|
73
|
+
}
|
|
74
|
+
getSubscriptionConfiguration() {
|
|
75
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
76
|
+
return this._subscriptionConfiguration;
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
getRecordByName(name) {
|
|
80
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
81
|
+
const record = this._records.find((r) => r.name === name);
|
|
82
|
+
return record;
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
updateRecord(record) {
|
|
86
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
87
|
+
const existingRecordIndex = this._records.findIndex((r) => r.name === record.name);
|
|
88
|
+
if (existingRecordIndex >= 0) {
|
|
89
|
+
this._records[existingRecordIndex] = record;
|
|
90
|
+
}
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
addRecord(record) {
|
|
94
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
95
|
+
const existingRecordIndex = this._records.findIndex((r) => r.name === record.name);
|
|
96
|
+
if (existingRecordIndex < 0) {
|
|
97
|
+
this._records.push(record);
|
|
98
|
+
}
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
addRecordKey(key) {
|
|
102
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
103
|
+
const existingKeyIndex = this._recordKeys.findIndex((k) => k.recordName === key.recordName &&
|
|
104
|
+
k.secretHash === key.secretHash);
|
|
105
|
+
if (existingKeyIndex < 0) {
|
|
106
|
+
this._recordKeys.push(key);
|
|
107
|
+
}
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
getRecordKeyByRecordAndHash(recordName, hash) {
|
|
111
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
112
|
+
const key = this._recordKeys.find((k) => k.recordName === recordName && k.secretHash == hash);
|
|
113
|
+
return key;
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
listRecordsByOwnerId(ownerId) {
|
|
117
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
118
|
+
return sortBy(this._records
|
|
119
|
+
.filter((r) => r.ownerId === ownerId)
|
|
120
|
+
.map((r) => ({
|
|
121
|
+
name: r.name,
|
|
122
|
+
ownerId: r.ownerId,
|
|
123
|
+
studioId: r.studioId,
|
|
124
|
+
})), (r) => r.name);
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
listRecordsByStudioId(studioId) {
|
|
128
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
129
|
+
return sortBy(this._records
|
|
130
|
+
.filter((r) => r.studioId === studioId)
|
|
131
|
+
.map((r) => ({
|
|
132
|
+
name: r.name,
|
|
133
|
+
ownerId: r.ownerId,
|
|
134
|
+
studioId: r.studioId,
|
|
135
|
+
})), (r) => r.name);
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
listRecordsByStudioIdAndUserId(studioId, userId) {
|
|
139
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
140
|
+
return sortBy(this._records
|
|
141
|
+
.filter((s) => {
|
|
142
|
+
if (s.studioId !== studioId) {
|
|
143
|
+
return false;
|
|
144
|
+
}
|
|
145
|
+
const isAssigned = this._studioAssignments.some((a) => a.studioId === studioId && a.userId === userId);
|
|
146
|
+
return isAssigned;
|
|
147
|
+
})
|
|
148
|
+
.map((r) => ({
|
|
149
|
+
name: r.name,
|
|
150
|
+
ownerId: r.ownerId,
|
|
151
|
+
studioId: r.studioId,
|
|
152
|
+
})), (r) => r.name);
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
addStudio(studio) {
|
|
156
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
157
|
+
const existingStudioIndex = this._studios.findIndex((r) => r.id === studio.id);
|
|
158
|
+
if (existingStudioIndex < 0) {
|
|
159
|
+
this._studios.push(studio);
|
|
160
|
+
}
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
createStudioForUser(studio, adminId) {
|
|
164
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
165
|
+
yield this.addStudio(studio);
|
|
166
|
+
const assignment = {
|
|
167
|
+
studioId: studio.id,
|
|
168
|
+
userId: adminId,
|
|
169
|
+
isPrimaryContact: true,
|
|
170
|
+
role: 'admin',
|
|
171
|
+
};
|
|
172
|
+
yield this.addStudioAssignment(assignment);
|
|
173
|
+
return {
|
|
174
|
+
studio,
|
|
175
|
+
assignment,
|
|
176
|
+
};
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
updateStudio(studio) {
|
|
180
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
181
|
+
const existingStudioIndex = this._studios.findIndex((r) => r.id === studio.id);
|
|
182
|
+
if (existingStudioIndex >= 0) {
|
|
183
|
+
this._studios[existingStudioIndex] = studio;
|
|
184
|
+
}
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
getStudioById(id) {
|
|
188
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
189
|
+
return this._studios.find((s) => s.id === id);
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
getStudioByStripeCustomerId(customerId) {
|
|
193
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
194
|
+
return this._studios.find((s) => s.stripeCustomerId === customerId);
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
listStudiosForUser(userId) {
|
|
198
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
199
|
+
const assignments = yield this.listUserAssignments(userId);
|
|
200
|
+
const studios = yield Promise.all(assignments.map((a) => __awaiter(this, void 0, void 0, function* () {
|
|
201
|
+
const s = yield this.getStudioById(a.studioId);
|
|
202
|
+
return Object.assign(Object.assign({}, s), a);
|
|
203
|
+
})));
|
|
204
|
+
return studios.map((s) => ({
|
|
205
|
+
studioId: s.id,
|
|
206
|
+
displayName: s.displayName,
|
|
207
|
+
role: s.role,
|
|
208
|
+
isPrimaryContact: s.isPrimaryContact,
|
|
209
|
+
}));
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
addStudioAssignment(assignment) {
|
|
213
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
214
|
+
const existingAssignmentIndex = this._studioAssignments.findIndex((r) => r.studioId === assignment.studioId &&
|
|
215
|
+
r.userId === assignment.userId);
|
|
216
|
+
if (existingAssignmentIndex < 0) {
|
|
217
|
+
this._studioAssignments.push(assignment);
|
|
218
|
+
}
|
|
219
|
+
});
|
|
220
|
+
}
|
|
221
|
+
removeStudioAssignment(studioId, userId) {
|
|
222
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
223
|
+
this._studioAssignments = this._studioAssignments.filter((s) => s.studioId !== studioId || s.userId !== userId);
|
|
224
|
+
});
|
|
225
|
+
}
|
|
226
|
+
updateStudioAssignment(assignment) {
|
|
227
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
228
|
+
const existingAssignmentIndex = this._studioAssignments.findIndex((r) => r.studioId === assignment.studioId &&
|
|
229
|
+
r.userId === assignment.userId);
|
|
230
|
+
if (existingAssignmentIndex >= 0) {
|
|
231
|
+
this._studioAssignments[existingAssignmentIndex] = assignment;
|
|
232
|
+
}
|
|
233
|
+
});
|
|
234
|
+
}
|
|
235
|
+
listStudioAssignments(studioId, filters) {
|
|
236
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
237
|
+
const assignments = this._studioAssignments.filter((s) => {
|
|
238
|
+
const matchesRole = !(filters === null || filters === void 0 ? void 0 : filters.role) || s.role === filters.role;
|
|
239
|
+
const matchesPrimaryContact = !(filters === null || filters === void 0 ? void 0 : filters.isPrimaryContact) ||
|
|
240
|
+
s.isPrimaryContact === filters.isPrimaryContact;
|
|
241
|
+
const matchesUserId = !(filters === null || filters === void 0 ? void 0 : filters.userId) || s.userId === filters.userId;
|
|
242
|
+
return (s.studioId === studioId &&
|
|
243
|
+
matchesRole &&
|
|
244
|
+
matchesPrimaryContact &&
|
|
245
|
+
matchesUserId);
|
|
246
|
+
});
|
|
247
|
+
let results = [];
|
|
248
|
+
for (let s of assignments) {
|
|
249
|
+
const user = yield this.findUser(s.userId);
|
|
250
|
+
if (!user) {
|
|
251
|
+
continue;
|
|
252
|
+
}
|
|
253
|
+
results.push({
|
|
254
|
+
studioId: s.studioId,
|
|
255
|
+
userId: s.userId,
|
|
256
|
+
isPrimaryContact: s.isPrimaryContact,
|
|
257
|
+
role: s.role,
|
|
258
|
+
user: {
|
|
259
|
+
id: user.id,
|
|
260
|
+
name: user.name,
|
|
261
|
+
email: user.email,
|
|
262
|
+
phoneNumber: user.phoneNumber,
|
|
263
|
+
},
|
|
264
|
+
});
|
|
265
|
+
}
|
|
266
|
+
return results;
|
|
267
|
+
});
|
|
268
|
+
}
|
|
269
|
+
listUserAssignments(userId) {
|
|
270
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
271
|
+
const assignments = this._studioAssignments.filter((s) => s.userId === userId);
|
|
272
|
+
return assignments.map((s) => {
|
|
273
|
+
const studio = this._studios.find((studio) => studio.id === s.studioId);
|
|
274
|
+
return {
|
|
275
|
+
displayName: studio.displayName,
|
|
276
|
+
studioId: s.studioId,
|
|
277
|
+
userId: s.userId,
|
|
278
|
+
isPrimaryContact: s.isPrimaryContact,
|
|
279
|
+
role: s.role,
|
|
280
|
+
};
|
|
281
|
+
});
|
|
282
|
+
});
|
|
283
|
+
}
|
|
284
|
+
countRecords(filter) {
|
|
285
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
286
|
+
let count = 0;
|
|
287
|
+
for (let record of this._records) {
|
|
288
|
+
if (filter.studioId && record.studioId === filter.studioId) {
|
|
289
|
+
count++;
|
|
290
|
+
}
|
|
291
|
+
else if (filter.ownerId && record.ownerId === filter.ownerId) {
|
|
292
|
+
count++;
|
|
293
|
+
}
|
|
294
|
+
else {
|
|
295
|
+
count++;
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
return count;
|
|
299
|
+
});
|
|
300
|
+
}
|
|
301
|
+
saveUser(user) {
|
|
302
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
303
|
+
let index = this._findUserIndex(user.id);
|
|
304
|
+
if (index >= 0) {
|
|
305
|
+
this._users[index] = user;
|
|
306
|
+
}
|
|
307
|
+
else {
|
|
308
|
+
this._users.push(user);
|
|
309
|
+
}
|
|
310
|
+
});
|
|
311
|
+
}
|
|
312
|
+
saveNewUser(user) {
|
|
313
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
314
|
+
let index = this._users.findIndex((u) => (!!user.email && u.email === user.email) ||
|
|
315
|
+
(!!user.phoneNumber && u.phoneNumber === user.phoneNumber));
|
|
316
|
+
if (index >= 0) {
|
|
317
|
+
return {
|
|
318
|
+
success: false,
|
|
319
|
+
errorCode: 'user_already_exists',
|
|
320
|
+
errorMessage: 'The user already exists.',
|
|
321
|
+
};
|
|
322
|
+
}
|
|
323
|
+
else {
|
|
324
|
+
this._users.push(user);
|
|
325
|
+
}
|
|
326
|
+
return {
|
|
327
|
+
success: true,
|
|
328
|
+
};
|
|
329
|
+
});
|
|
330
|
+
}
|
|
331
|
+
findUser(userId) {
|
|
332
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
333
|
+
const user = this._users.find((u) => u.id === userId);
|
|
334
|
+
return user;
|
|
335
|
+
});
|
|
336
|
+
}
|
|
337
|
+
setRevokeAllSessionsTimeForUser(userId, allSessionRevokeTimeMs) {
|
|
338
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
339
|
+
const user = yield this.findUser(userId);
|
|
340
|
+
if (user) {
|
|
341
|
+
yield this.saveUser(Object.assign(Object.assign({}, user), { allSessionRevokeTimeMs: allSessionRevokeTimeMs }));
|
|
342
|
+
}
|
|
343
|
+
});
|
|
344
|
+
}
|
|
345
|
+
findUserByAddress(address, addressType) {
|
|
346
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
347
|
+
const user = this._users.find((u) => addressType === 'email'
|
|
348
|
+
? u.email === address
|
|
349
|
+
: u.phoneNumber === address);
|
|
350
|
+
return user;
|
|
351
|
+
});
|
|
352
|
+
}
|
|
353
|
+
findUserByStripeCustomerId(customerId) {
|
|
354
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
355
|
+
const user = this._users.find((u) => u.stripeCustomerId === customerId);
|
|
356
|
+
return user;
|
|
357
|
+
});
|
|
358
|
+
}
|
|
359
|
+
findLoginRequest(userId, requestId) {
|
|
360
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
361
|
+
return this._loginRequests.find((lr) => lr.userId === userId && lr.requestId === requestId);
|
|
362
|
+
});
|
|
363
|
+
}
|
|
364
|
+
findSession(userId, sessionId) {
|
|
365
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
366
|
+
return this._sessions.find((s) => s.userId === userId && s.sessionId === sessionId);
|
|
367
|
+
});
|
|
368
|
+
}
|
|
369
|
+
saveLoginRequest(request) {
|
|
370
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
371
|
+
const index = this._loginRequests.findIndex((lr) => lr.userId === request.userId &&
|
|
372
|
+
lr.requestId === request.requestId);
|
|
373
|
+
if (index >= 0) {
|
|
374
|
+
this._loginRequests[index] = request;
|
|
375
|
+
}
|
|
376
|
+
else {
|
|
377
|
+
this._loginRequests.push(request);
|
|
378
|
+
}
|
|
379
|
+
return request;
|
|
380
|
+
});
|
|
381
|
+
}
|
|
382
|
+
setCurrentLoginRequest(userId, requestId) {
|
|
383
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
384
|
+
const userIndex = this._users.findIndex((u) => u.id === userId);
|
|
385
|
+
if (userIndex >= 0) {
|
|
386
|
+
const user = this._users[userIndex];
|
|
387
|
+
this._users[userIndex] = Object.assign(Object.assign({}, user), { currentLoginRequestId: requestId });
|
|
388
|
+
}
|
|
389
|
+
});
|
|
390
|
+
}
|
|
391
|
+
markLoginRequestComplete(userId, requestId, completedTimeMs) {
|
|
392
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
393
|
+
const index = this._loginRequests.findIndex((lr) => lr.userId === userId && lr.requestId === requestId);
|
|
394
|
+
if (index >= 0) {
|
|
395
|
+
this._loginRequests[index].completedTimeMs = completedTimeMs;
|
|
396
|
+
}
|
|
397
|
+
else {
|
|
398
|
+
throw new Error('Request not found.');
|
|
399
|
+
}
|
|
400
|
+
});
|
|
401
|
+
}
|
|
402
|
+
incrementLoginRequestAttemptCount(userId, requestId) {
|
|
403
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
404
|
+
const request = this._loginRequests.find((lr) => lr.userId === userId && lr.requestId === requestId);
|
|
405
|
+
if (request) {
|
|
406
|
+
request.attemptCount += 1;
|
|
407
|
+
}
|
|
408
|
+
});
|
|
409
|
+
}
|
|
410
|
+
saveSession(session) {
|
|
411
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
412
|
+
const index = this._sessions.findIndex((s) => s.userId === session.userId && s.sessionId === session.sessionId);
|
|
413
|
+
if (index >= 0) {
|
|
414
|
+
this._sessions[index] = session;
|
|
415
|
+
}
|
|
416
|
+
else {
|
|
417
|
+
this._sessions.push(session);
|
|
418
|
+
}
|
|
419
|
+
});
|
|
420
|
+
}
|
|
421
|
+
replaceSession(session, newSession, revokeTimeMs) {
|
|
422
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
423
|
+
this.saveSession(Object.assign(Object.assign({}, session), { revokeTimeMs: revokeTimeMs, nextSessionId: newSession.sessionId }));
|
|
424
|
+
this.saveSession(Object.assign(Object.assign({}, newSession), { previousSessionId: session.sessionId }));
|
|
425
|
+
});
|
|
426
|
+
}
|
|
427
|
+
listSessions(userId, expireTimeMs) {
|
|
428
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
429
|
+
let orderedSessions = sortBy(this._sessions.filter((s) => s.userId === userId), (s) => -s.expireTimeMs);
|
|
430
|
+
if (expireTimeMs) {
|
|
431
|
+
orderedSessions = orderedSessions.filter((s) => s.expireTimeMs < expireTimeMs);
|
|
432
|
+
}
|
|
433
|
+
return {
|
|
434
|
+
success: true,
|
|
435
|
+
sessions: orderedSessions.slice(0, 10),
|
|
436
|
+
};
|
|
437
|
+
});
|
|
438
|
+
}
|
|
439
|
+
listEmailRules() {
|
|
440
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
441
|
+
return this._emailRules.slice();
|
|
442
|
+
});
|
|
443
|
+
}
|
|
444
|
+
listSmsRules() {
|
|
445
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
446
|
+
return this._smsRules.slice();
|
|
447
|
+
});
|
|
448
|
+
}
|
|
449
|
+
saveSubscription(subscription) {
|
|
450
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
451
|
+
const index = this._subscriptions.findIndex((s) => s.id === subscription.id);
|
|
452
|
+
if (index >= 0) {
|
|
453
|
+
this._subscriptions[index] = subscription;
|
|
454
|
+
}
|
|
455
|
+
else {
|
|
456
|
+
this._subscriptions.push(subscription);
|
|
457
|
+
}
|
|
458
|
+
});
|
|
459
|
+
}
|
|
460
|
+
getSubscriptionById(id) {
|
|
461
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
462
|
+
return this._subscriptions.find((s) => s.id === id);
|
|
463
|
+
});
|
|
464
|
+
}
|
|
465
|
+
getSubscriptionByStripeSubscriptionId(id) {
|
|
466
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
467
|
+
return this._subscriptions.find((s) => s.stripeSubscriptionId === id);
|
|
468
|
+
});
|
|
469
|
+
}
|
|
470
|
+
saveSubscriptionPeriod(period) {
|
|
471
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
472
|
+
const index = this._periods.findIndex((p) => p.id === period.id);
|
|
473
|
+
if (index >= 0) {
|
|
474
|
+
this._periods[index] = period;
|
|
475
|
+
}
|
|
476
|
+
else {
|
|
477
|
+
this._periods.push(period);
|
|
478
|
+
}
|
|
479
|
+
});
|
|
480
|
+
}
|
|
481
|
+
getSubscriptionPeriodById(id) {
|
|
482
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
483
|
+
return this._periods.find((p) => p.id === id);
|
|
484
|
+
});
|
|
485
|
+
}
|
|
486
|
+
listSubscriptionPeriodsBySubscriptionId(subscriptionId) {
|
|
487
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
488
|
+
return this._periods.filter((p) => p.subscriptionId === subscriptionId);
|
|
489
|
+
});
|
|
490
|
+
}
|
|
491
|
+
saveInvoice(invoice) {
|
|
492
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
493
|
+
const index = this._invoices.findIndex((i) => i.id === invoice.id);
|
|
494
|
+
if (index >= 0) {
|
|
495
|
+
this._invoices[index] = invoice;
|
|
496
|
+
}
|
|
497
|
+
else {
|
|
498
|
+
this._invoices.push(invoice);
|
|
499
|
+
}
|
|
500
|
+
});
|
|
501
|
+
}
|
|
502
|
+
getInvoiceById(id) {
|
|
503
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
504
|
+
return this._invoices.find((i) => i.id === id);
|
|
505
|
+
});
|
|
506
|
+
}
|
|
507
|
+
updateSubscriptionInfo(request) {
|
|
508
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
509
|
+
if (request.userId) {
|
|
510
|
+
const user = yield this.findUser(request.userId);
|
|
511
|
+
let subscription = yield this.getSubscriptionByStripeSubscriptionId(request.stripeSubscriptionId);
|
|
512
|
+
if (subscription) {
|
|
513
|
+
yield this.saveSubscription(Object.assign(Object.assign({}, subscription), { stripeCustomerId: request.stripeCustomerId, stripeSubscriptionId: request.stripeSubscriptionId, subscriptionId: request.subscriptionId, subscriptionStatus: request.subscriptionStatus, currentPeriodEndMs: request.currentPeriodEndMs, currentPeriodStartMs: request.currentPeriodStartMs }));
|
|
514
|
+
}
|
|
515
|
+
else {
|
|
516
|
+
subscription = {
|
|
517
|
+
id: uuid(),
|
|
518
|
+
userId: user.id,
|
|
519
|
+
studioId: null,
|
|
520
|
+
stripeCustomerId: request.stripeCustomerId,
|
|
521
|
+
stripeSubscriptionId: request.stripeSubscriptionId,
|
|
522
|
+
subscriptionId: request.subscriptionId,
|
|
523
|
+
subscriptionStatus: request.subscriptionStatus,
|
|
524
|
+
currentPeriodEndMs: request.currentPeriodEndMs,
|
|
525
|
+
currentPeriodStartMs: request.currentPeriodStartMs,
|
|
526
|
+
};
|
|
527
|
+
yield this.saveSubscription(subscription);
|
|
528
|
+
}
|
|
529
|
+
yield this.saveUser(Object.assign(Object.assign({}, user), { subscriptionId: request.subscriptionId, subscriptionStatus: request.subscriptionStatus, stripeCustomerId: request.stripeCustomerId, subscriptionPeriodStartMs: request.currentPeriodStartMs, subscriptionPeriodEndMs: request.currentPeriodEndMs, subscriptionInfoId: subscription.id }));
|
|
530
|
+
}
|
|
531
|
+
else if (request.studioId) {
|
|
532
|
+
const studio = yield this.getStudioById(request.studioId);
|
|
533
|
+
let subscription = yield this.getSubscriptionByStripeSubscriptionId(request.stripeSubscriptionId);
|
|
534
|
+
if (subscription) {
|
|
535
|
+
yield this.saveSubscription(Object.assign(Object.assign({}, subscription), { stripeCustomerId: request.stripeCustomerId, stripeSubscriptionId: request.stripeSubscriptionId, subscriptionId: request.subscriptionId, subscriptionStatus: request.subscriptionStatus, currentPeriodEndMs: request.currentPeriodEndMs, currentPeriodStartMs: request.currentPeriodStartMs }));
|
|
536
|
+
}
|
|
537
|
+
else {
|
|
538
|
+
subscription = {
|
|
539
|
+
id: uuid(),
|
|
540
|
+
userId: null,
|
|
541
|
+
studioId: studio.id,
|
|
542
|
+
stripeCustomerId: request.stripeCustomerId,
|
|
543
|
+
stripeSubscriptionId: request.stripeSubscriptionId,
|
|
544
|
+
subscriptionId: request.subscriptionId,
|
|
545
|
+
subscriptionStatus: request.subscriptionStatus,
|
|
546
|
+
currentPeriodEndMs: request.currentPeriodEndMs,
|
|
547
|
+
currentPeriodStartMs: request.currentPeriodStartMs,
|
|
548
|
+
};
|
|
549
|
+
yield this.saveSubscription(subscription);
|
|
550
|
+
}
|
|
551
|
+
yield this.updateStudio(Object.assign(Object.assign({}, studio), { subscriptionId: request.subscriptionId, subscriptionStatus: request.subscriptionStatus, stripeCustomerId: request.stripeCustomerId, subscriptionPeriodStartMs: request.currentPeriodStartMs, subscriptionPeriodEndMs: request.currentPeriodEndMs, subscriptionInfoId: subscription.id }));
|
|
552
|
+
}
|
|
553
|
+
});
|
|
554
|
+
}
|
|
555
|
+
updateSubscriptionPeriod(request) {
|
|
556
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
557
|
+
if (request.userId) {
|
|
558
|
+
let user = yield this.findUser(request.userId);
|
|
559
|
+
let subscription = yield this.getSubscriptionByStripeSubscriptionId(request.stripeSubscriptionId);
|
|
560
|
+
if (!subscription) {
|
|
561
|
+
subscription = {
|
|
562
|
+
id: uuid(),
|
|
563
|
+
userId: user.id,
|
|
564
|
+
studioId: null,
|
|
565
|
+
stripeCustomerId: request.stripeCustomerId,
|
|
566
|
+
stripeSubscriptionId: request.stripeSubscriptionId,
|
|
567
|
+
subscriptionId: request.subscriptionId,
|
|
568
|
+
subscriptionStatus: request.subscriptionStatus,
|
|
569
|
+
currentPeriodEndMs: request.currentPeriodEndMs,
|
|
570
|
+
currentPeriodStartMs: request.currentPeriodStartMs,
|
|
571
|
+
};
|
|
572
|
+
user.stripeCustomerId = request.stripeCustomerId;
|
|
573
|
+
user.subscriptionStatus = request.subscriptionStatus;
|
|
574
|
+
user.subscriptionId = request.subscriptionId;
|
|
575
|
+
user.subscriptionInfoId = subscription.id;
|
|
576
|
+
yield this.saveSubscription(subscription);
|
|
577
|
+
}
|
|
578
|
+
yield this.saveUser(Object.assign(Object.assign({}, user), { subscriptionPeriodStartMs: request.currentPeriodStartMs, subscriptionPeriodEndMs: request.currentPeriodEndMs }));
|
|
579
|
+
const periodId = uuid();
|
|
580
|
+
const invoiceId = uuid();
|
|
581
|
+
yield this.saveInvoice(Object.assign({ id: invoiceId, periodId: periodId, subscriptionId: subscription.id }, request.invoice));
|
|
582
|
+
yield this.saveSubscriptionPeriod({
|
|
583
|
+
id: periodId,
|
|
584
|
+
invoiceId: invoiceId,
|
|
585
|
+
subscriptionId: subscription.id,
|
|
586
|
+
periodEndMs: request.currentPeriodEndMs,
|
|
587
|
+
periodStartMs: request.currentPeriodStartMs,
|
|
588
|
+
});
|
|
589
|
+
}
|
|
590
|
+
else if (request.studioId) {
|
|
591
|
+
let studio = yield this.getStudioById(request.studioId);
|
|
592
|
+
let subscription = yield this.getSubscriptionByStripeSubscriptionId(request.stripeSubscriptionId);
|
|
593
|
+
if (!subscription) {
|
|
594
|
+
subscription = {
|
|
595
|
+
id: uuid(),
|
|
596
|
+
userId: null,
|
|
597
|
+
studioId: studio.id,
|
|
598
|
+
stripeCustomerId: request.stripeCustomerId,
|
|
599
|
+
stripeSubscriptionId: request.stripeSubscriptionId,
|
|
600
|
+
subscriptionId: request.subscriptionId,
|
|
601
|
+
subscriptionStatus: request.subscriptionStatus,
|
|
602
|
+
currentPeriodEndMs: request.currentPeriodEndMs,
|
|
603
|
+
currentPeriodStartMs: request.currentPeriodStartMs,
|
|
604
|
+
};
|
|
605
|
+
studio.stripeCustomerId = request.stripeCustomerId;
|
|
606
|
+
studio.subscriptionStatus = request.subscriptionStatus;
|
|
607
|
+
studio.subscriptionId = request.subscriptionId;
|
|
608
|
+
studio.subscriptionInfoId = subscription.id;
|
|
609
|
+
yield this.saveSubscription(subscription);
|
|
610
|
+
}
|
|
611
|
+
yield this.updateStudio(Object.assign(Object.assign({}, studio), { subscriptionPeriodStartMs: request.currentPeriodStartMs, subscriptionPeriodEndMs: request.currentPeriodEndMs }));
|
|
612
|
+
const periodId = uuid();
|
|
613
|
+
const invoiceId = uuid();
|
|
614
|
+
yield this.saveInvoice(Object.assign({ id: invoiceId, periodId: periodId, subscriptionId: subscription.id }, request.invoice));
|
|
615
|
+
yield this.saveSubscriptionPeriod({
|
|
616
|
+
id: periodId,
|
|
617
|
+
invoiceId: invoiceId,
|
|
618
|
+
subscriptionId: subscription.id,
|
|
619
|
+
periodEndMs: request.currentPeriodEndMs,
|
|
620
|
+
periodStartMs: request.currentPeriodStartMs,
|
|
621
|
+
});
|
|
622
|
+
}
|
|
623
|
+
});
|
|
624
|
+
}
|
|
625
|
+
_findUserIndex(id) {
|
|
626
|
+
return this._users.findIndex((u) => u.id === id);
|
|
627
|
+
}
|
|
628
|
+
setData(recordName, address, data, publisherId, subjectId, updatePolicy, deletePolicy, markers) {
|
|
629
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
630
|
+
let record = this._getDataRecord(recordName);
|
|
631
|
+
record.set(address, {
|
|
632
|
+
data: data,
|
|
633
|
+
publisherId: publisherId,
|
|
634
|
+
subjectId: subjectId,
|
|
635
|
+
updatePolicy,
|
|
636
|
+
deletePolicy,
|
|
637
|
+
markers,
|
|
638
|
+
});
|
|
639
|
+
return {
|
|
640
|
+
success: true,
|
|
641
|
+
};
|
|
642
|
+
});
|
|
643
|
+
}
|
|
644
|
+
getData(recordName, address) {
|
|
645
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
646
|
+
let record = this._getDataRecord(recordName);
|
|
647
|
+
let data = record.get(address);
|
|
648
|
+
if (!data) {
|
|
649
|
+
return {
|
|
650
|
+
success: false,
|
|
651
|
+
errorCode: 'data_not_found',
|
|
652
|
+
errorMessage: 'The data was not found.',
|
|
653
|
+
};
|
|
654
|
+
}
|
|
655
|
+
return {
|
|
656
|
+
success: true,
|
|
657
|
+
data: data.data,
|
|
658
|
+
publisherId: data.publisherId,
|
|
659
|
+
subjectId: data.subjectId,
|
|
660
|
+
updatePolicy: data.updatePolicy,
|
|
661
|
+
deletePolicy: data.deletePolicy,
|
|
662
|
+
markers: data.markers,
|
|
663
|
+
};
|
|
664
|
+
});
|
|
665
|
+
}
|
|
666
|
+
eraseData(recordName, address) {
|
|
667
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
668
|
+
let record = this._getDataRecord(recordName);
|
|
669
|
+
let deleted = record.delete(address);
|
|
670
|
+
if (!deleted) {
|
|
671
|
+
return {
|
|
672
|
+
success: false,
|
|
673
|
+
errorCode: 'data_not_found',
|
|
674
|
+
errorMessage: 'The data was not found.',
|
|
675
|
+
};
|
|
676
|
+
}
|
|
677
|
+
return {
|
|
678
|
+
success: true,
|
|
679
|
+
};
|
|
680
|
+
});
|
|
681
|
+
}
|
|
682
|
+
listData(recordName, address) {
|
|
683
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
684
|
+
let record = this._getDataRecord(recordName);
|
|
685
|
+
let items = [];
|
|
686
|
+
const count = record.size;
|
|
687
|
+
for (let [key, item] of record.entries()) {
|
|
688
|
+
if (!address || key > address) {
|
|
689
|
+
items.push({
|
|
690
|
+
address: key,
|
|
691
|
+
data: item.data,
|
|
692
|
+
markers: item.markers,
|
|
693
|
+
});
|
|
694
|
+
}
|
|
695
|
+
}
|
|
696
|
+
return {
|
|
697
|
+
success: true,
|
|
698
|
+
items,
|
|
699
|
+
totalCount: count,
|
|
700
|
+
};
|
|
701
|
+
});
|
|
702
|
+
}
|
|
703
|
+
_getDataRecord(recordName) {
|
|
704
|
+
let record = this._dataBuckets.get(recordName);
|
|
705
|
+
if (!record) {
|
|
706
|
+
record = new Map();
|
|
707
|
+
this._dataBuckets.set(recordName, record);
|
|
708
|
+
}
|
|
709
|
+
return record;
|
|
710
|
+
}
|
|
711
|
+
presignFileUpload(request) {
|
|
712
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
713
|
+
return {
|
|
714
|
+
success: true,
|
|
715
|
+
uploadHeaders: Object.assign(Object.assign({}, request.headers), { 'record-name': request.recordName, 'content-type': request.fileMimeType }),
|
|
716
|
+
uploadMethod: 'POST',
|
|
717
|
+
uploadUrl: `${this._fileUploadUrl}/${request.recordName}/${request.fileName}`,
|
|
718
|
+
};
|
|
719
|
+
});
|
|
720
|
+
}
|
|
721
|
+
presignFileRead(request) {
|
|
722
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
723
|
+
return {
|
|
724
|
+
success: true,
|
|
725
|
+
requestHeaders: Object.assign(Object.assign({}, request.headers), { 'record-name': request.recordName }),
|
|
726
|
+
requestMethod: 'GET',
|
|
727
|
+
requestUrl: `${this._fileUploadUrl}/${request.recordName}/${request.fileName}`,
|
|
728
|
+
};
|
|
729
|
+
});
|
|
730
|
+
}
|
|
731
|
+
getFileNameFromUrl(fileUrl) {
|
|
732
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
733
|
+
if (fileUrl.startsWith(this._fileUploadUrl)) {
|
|
734
|
+
let recordNameAndFileName = fileUrl.slice(this._fileUploadUrl.length + 1);
|
|
735
|
+
let nextSlash = recordNameAndFileName.indexOf('/');
|
|
736
|
+
if (nextSlash < 0) {
|
|
737
|
+
return {
|
|
738
|
+
success: false,
|
|
739
|
+
errorCode: 'unacceptable_url',
|
|
740
|
+
errorMessage: 'The URL does not match an expected format.',
|
|
741
|
+
};
|
|
742
|
+
}
|
|
743
|
+
let recordName = recordNameAndFileName.slice(0, nextSlash);
|
|
744
|
+
let fileName = recordNameAndFileName.slice(nextSlash + 1);
|
|
745
|
+
if (recordName && fileName) {
|
|
746
|
+
return {
|
|
747
|
+
success: true,
|
|
748
|
+
recordName,
|
|
749
|
+
fileName,
|
|
750
|
+
};
|
|
751
|
+
}
|
|
752
|
+
return {
|
|
753
|
+
success: false,
|
|
754
|
+
errorCode: 'unacceptable_url',
|
|
755
|
+
errorMessage: 'The URL does not match an expected format.',
|
|
756
|
+
};
|
|
757
|
+
}
|
|
758
|
+
return {
|
|
759
|
+
success: false,
|
|
760
|
+
errorCode: 'unacceptable_url',
|
|
761
|
+
errorMessage: 'The URL does not match an expected format.',
|
|
762
|
+
};
|
|
763
|
+
});
|
|
764
|
+
}
|
|
765
|
+
getFileRecord(recordName, fileName) {
|
|
766
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
767
|
+
let file = this._files.get(fileName);
|
|
768
|
+
if (file) {
|
|
769
|
+
return {
|
|
770
|
+
success: true,
|
|
771
|
+
fileName: file.fileName,
|
|
772
|
+
recordName: file.recordName,
|
|
773
|
+
publisherId: file.publisherId,
|
|
774
|
+
subjectId: file.subjectId,
|
|
775
|
+
sizeInBytes: file.sizeInBytes,
|
|
776
|
+
uploaded: file.uploaded,
|
|
777
|
+
description: file.description,
|
|
778
|
+
url: `${this._fileUploadUrl}/${file.recordName}/${file.fileName}`,
|
|
779
|
+
markers: file.markers,
|
|
780
|
+
};
|
|
781
|
+
}
|
|
782
|
+
else {
|
|
783
|
+
return {
|
|
784
|
+
success: false,
|
|
785
|
+
errorCode: 'file_not_found',
|
|
786
|
+
errorMessage: 'The file was not found in the store.',
|
|
787
|
+
};
|
|
788
|
+
}
|
|
789
|
+
});
|
|
790
|
+
}
|
|
791
|
+
listUploadedFiles(recordName, fileName) {
|
|
792
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
793
|
+
let files = sortBy([...this._files.values()].filter((f) => f.recordName === recordName && f.uploaded), (f) => f.fileName);
|
|
794
|
+
const count = files.length;
|
|
795
|
+
if (fileName) {
|
|
796
|
+
files = files.filter((f) => f.fileName > fileName);
|
|
797
|
+
}
|
|
798
|
+
return {
|
|
799
|
+
success: true,
|
|
800
|
+
files: files.slice(0, 10).map((f) => ({
|
|
801
|
+
fileName: f.fileName,
|
|
802
|
+
uploaded: f.uploaded,
|
|
803
|
+
markers: f.markers,
|
|
804
|
+
description: f.description,
|
|
805
|
+
sizeInBytes: f.sizeInBytes,
|
|
806
|
+
url: `${this._fileUploadUrl}/${f.recordName}/${f.fileName}`,
|
|
807
|
+
})),
|
|
808
|
+
totalCount: count,
|
|
809
|
+
};
|
|
810
|
+
});
|
|
811
|
+
}
|
|
812
|
+
addFileRecord(recordName, fileName, publisherId, subjectId, sizeInBytes, description, markers) {
|
|
813
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
814
|
+
if (this._files.has(fileName)) {
|
|
815
|
+
return {
|
|
816
|
+
success: false,
|
|
817
|
+
errorCode: 'file_already_exists',
|
|
818
|
+
errorMessage: 'The file already exists in the store.',
|
|
819
|
+
};
|
|
820
|
+
}
|
|
821
|
+
let file = {
|
|
822
|
+
fileName: fileName,
|
|
823
|
+
recordName: recordName,
|
|
824
|
+
publisherId,
|
|
825
|
+
subjectId,
|
|
826
|
+
sizeInBytes,
|
|
827
|
+
description,
|
|
828
|
+
markers,
|
|
829
|
+
uploaded: false,
|
|
830
|
+
};
|
|
831
|
+
this._files.set(fileName, file);
|
|
832
|
+
return {
|
|
833
|
+
success: true,
|
|
834
|
+
};
|
|
835
|
+
});
|
|
836
|
+
}
|
|
837
|
+
updateFileRecord(recordName, fileName, markers) {
|
|
838
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
839
|
+
if (!this._files.has(fileName)) {
|
|
840
|
+
return {
|
|
841
|
+
success: false,
|
|
842
|
+
errorCode: 'file_not_found',
|
|
843
|
+
errorMessage: 'The file was not found in the store.',
|
|
844
|
+
};
|
|
845
|
+
}
|
|
846
|
+
let file = this._files.get(fileName);
|
|
847
|
+
this._files.set(fileName, Object.assign(Object.assign({}, file), { markers: markers.slice() }));
|
|
848
|
+
return {
|
|
849
|
+
success: true,
|
|
850
|
+
};
|
|
851
|
+
});
|
|
852
|
+
}
|
|
853
|
+
setFileRecordAsUploaded(recordName, fileName) {
|
|
854
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
855
|
+
let file = this._files.get(fileName);
|
|
856
|
+
if (!file) {
|
|
857
|
+
return {
|
|
858
|
+
success: false,
|
|
859
|
+
errorCode: 'file_not_found',
|
|
860
|
+
errorMessage: 'The file was not found in the store.',
|
|
861
|
+
};
|
|
862
|
+
}
|
|
863
|
+
file.uploaded = true;
|
|
864
|
+
return {
|
|
865
|
+
success: true,
|
|
866
|
+
};
|
|
867
|
+
});
|
|
868
|
+
}
|
|
869
|
+
eraseFileRecord(recordName, fileName) {
|
|
870
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
871
|
+
const deleted = this._files.delete(fileName);
|
|
872
|
+
if (!deleted) {
|
|
873
|
+
return {
|
|
874
|
+
success: false,
|
|
875
|
+
errorCode: 'file_not_found',
|
|
876
|
+
errorMessage: 'The file was not found in the store.',
|
|
877
|
+
};
|
|
878
|
+
}
|
|
879
|
+
return {
|
|
880
|
+
success: true,
|
|
881
|
+
};
|
|
882
|
+
});
|
|
883
|
+
}
|
|
884
|
+
getAllowedUploadHeaders() {
|
|
885
|
+
return ['record-name', 'content-type'];
|
|
886
|
+
}
|
|
887
|
+
addEventCount(recordName, eventName, count) {
|
|
888
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
889
|
+
const record = this._getEventRecord(recordName);
|
|
890
|
+
if (record.has(eventName)) {
|
|
891
|
+
let data = record.get(eventName);
|
|
892
|
+
data.count += count;
|
|
893
|
+
}
|
|
894
|
+
else {
|
|
895
|
+
record.set(eventName, {
|
|
896
|
+
count: count,
|
|
897
|
+
});
|
|
898
|
+
}
|
|
899
|
+
return {
|
|
900
|
+
success: true,
|
|
901
|
+
};
|
|
902
|
+
});
|
|
903
|
+
}
|
|
904
|
+
getEventCount(recordName, eventName) {
|
|
905
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
906
|
+
const record = this._getEventRecord(recordName);
|
|
907
|
+
const e = record.has(eventName)
|
|
908
|
+
? record.get(eventName)
|
|
909
|
+
: {
|
|
910
|
+
count: 0,
|
|
911
|
+
};
|
|
912
|
+
return {
|
|
913
|
+
success: true,
|
|
914
|
+
count: e.count,
|
|
915
|
+
markers: e.markers,
|
|
916
|
+
};
|
|
917
|
+
});
|
|
918
|
+
}
|
|
919
|
+
updateEvent(recordName, eventName, updates) {
|
|
920
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
921
|
+
const record = this._getEventRecord(recordName);
|
|
922
|
+
const e = record.has(eventName)
|
|
923
|
+
? record.get(eventName)
|
|
924
|
+
: {
|
|
925
|
+
count: 0,
|
|
926
|
+
};
|
|
927
|
+
record.set(eventName, Object.assign(Object.assign({}, e), updates));
|
|
928
|
+
return {
|
|
929
|
+
success: true,
|
|
930
|
+
};
|
|
931
|
+
});
|
|
932
|
+
}
|
|
933
|
+
listEvents(recordName, eventName) {
|
|
934
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
935
|
+
const record = this._getEventRecord(recordName);
|
|
936
|
+
const totalCount = record.size;
|
|
937
|
+
let events = sortBy([...record.entries()], ([name, data]) => name);
|
|
938
|
+
if (eventName) {
|
|
939
|
+
events = events.filter(([name, data]) => name > eventName);
|
|
940
|
+
}
|
|
941
|
+
return {
|
|
942
|
+
success: true,
|
|
943
|
+
events: events.slice(0, 10).map(([name, data]) => ({
|
|
944
|
+
eventName: name,
|
|
945
|
+
count: data.count,
|
|
946
|
+
markers: data.markers,
|
|
947
|
+
})),
|
|
948
|
+
totalCount,
|
|
949
|
+
};
|
|
950
|
+
});
|
|
951
|
+
}
|
|
952
|
+
_getEventRecord(recordName) {
|
|
953
|
+
let record = this._eventBuckets.get(recordName);
|
|
954
|
+
if (!record) {
|
|
955
|
+
record = new Map();
|
|
956
|
+
this._eventBuckets.set(recordName, record);
|
|
957
|
+
}
|
|
958
|
+
return record;
|
|
959
|
+
}
|
|
960
|
+
listUserPolicies(recordName, startingMarker) {
|
|
961
|
+
var _a;
|
|
962
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
963
|
+
const recordPolicies = (_a = this.policies[recordName]) !== null && _a !== void 0 ? _a : {};
|
|
964
|
+
const keys = sortBy(Object.keys(recordPolicies));
|
|
965
|
+
let results = [];
|
|
966
|
+
let start = !startingMarker;
|
|
967
|
+
for (let key of keys) {
|
|
968
|
+
if (start) {
|
|
969
|
+
results.push({
|
|
970
|
+
marker: key,
|
|
971
|
+
document: recordPolicies[key].document,
|
|
972
|
+
markers: recordPolicies[key].markers,
|
|
973
|
+
});
|
|
974
|
+
}
|
|
975
|
+
else if (key === startingMarker || key > startingMarker) {
|
|
976
|
+
start = true;
|
|
977
|
+
}
|
|
978
|
+
}
|
|
979
|
+
return {
|
|
980
|
+
success: true,
|
|
981
|
+
policies: results,
|
|
982
|
+
totalCount: results.length,
|
|
983
|
+
};
|
|
984
|
+
});
|
|
985
|
+
}
|
|
986
|
+
getUserPolicy(recordName, marker) {
|
|
987
|
+
var _a;
|
|
988
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
989
|
+
const policy = (_a = this.policies[recordName]) === null || _a === void 0 ? void 0 : _a[marker];
|
|
990
|
+
if (!policy) {
|
|
991
|
+
return {
|
|
992
|
+
success: false,
|
|
993
|
+
errorCode: 'policy_not_found',
|
|
994
|
+
errorMessage: 'The policy was not found.',
|
|
995
|
+
};
|
|
996
|
+
}
|
|
997
|
+
return {
|
|
998
|
+
success: true,
|
|
999
|
+
document: policy.document,
|
|
1000
|
+
markers: policy.markers,
|
|
1001
|
+
};
|
|
1002
|
+
});
|
|
1003
|
+
}
|
|
1004
|
+
updateUserPolicy(recordName, marker, policy) {
|
|
1005
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1006
|
+
if (!this.policies[recordName]) {
|
|
1007
|
+
this.policies[recordName] = {};
|
|
1008
|
+
}
|
|
1009
|
+
this.policies[recordName][marker] = {
|
|
1010
|
+
document: policy.document,
|
|
1011
|
+
markers: policy.markers,
|
|
1012
|
+
};
|
|
1013
|
+
return {
|
|
1014
|
+
success: true,
|
|
1015
|
+
};
|
|
1016
|
+
});
|
|
1017
|
+
}
|
|
1018
|
+
listPoliciesForMarker(recordName, marker) {
|
|
1019
|
+
var _a;
|
|
1020
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1021
|
+
const policies = [DEFAULT_ANY_RESOURCE_POLICY_DOCUMENT];
|
|
1022
|
+
if (marker === PUBLIC_READ_MARKER) {
|
|
1023
|
+
policies.push(DEFAULT_PUBLIC_READ_POLICY_DOCUMENT);
|
|
1024
|
+
}
|
|
1025
|
+
const policy = (_a = this.policies[recordName]) === null || _a === void 0 ? void 0 : _a[marker];
|
|
1026
|
+
if (policy) {
|
|
1027
|
+
policies.push(policy.document);
|
|
1028
|
+
}
|
|
1029
|
+
return policies;
|
|
1030
|
+
});
|
|
1031
|
+
}
|
|
1032
|
+
listRolesForUser(recordName, userId) {
|
|
1033
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1034
|
+
return this._getRolesForEntity(recordName, userId);
|
|
1035
|
+
});
|
|
1036
|
+
}
|
|
1037
|
+
listRolesForInst(recordName, inst) {
|
|
1038
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1039
|
+
return this._getRolesForEntity(recordName, inst);
|
|
1040
|
+
});
|
|
1041
|
+
}
|
|
1042
|
+
listAssignmentsForRole(recordName, role) {
|
|
1043
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1044
|
+
let record = this.roles[recordName];
|
|
1045
|
+
let assignedRoles = this.roleAssignments[recordName];
|
|
1046
|
+
let assignments = [];
|
|
1047
|
+
for (let id in record) {
|
|
1048
|
+
if (record[id].has(role)) {
|
|
1049
|
+
assignments.push({
|
|
1050
|
+
type: 'user',
|
|
1051
|
+
userId: id,
|
|
1052
|
+
role: {
|
|
1053
|
+
role,
|
|
1054
|
+
expireTimeMs: null,
|
|
1055
|
+
},
|
|
1056
|
+
});
|
|
1057
|
+
}
|
|
1058
|
+
}
|
|
1059
|
+
for (let id in assignedRoles) {
|
|
1060
|
+
let roles = assignedRoles[id];
|
|
1061
|
+
let assignment = roles.find((r) => r.role === role);
|
|
1062
|
+
if (assignment) {
|
|
1063
|
+
assignments.push({
|
|
1064
|
+
type: 'user',
|
|
1065
|
+
userId: id,
|
|
1066
|
+
role: assignment,
|
|
1067
|
+
});
|
|
1068
|
+
}
|
|
1069
|
+
}
|
|
1070
|
+
return {
|
|
1071
|
+
assignments,
|
|
1072
|
+
totalCount: assignments.length,
|
|
1073
|
+
};
|
|
1074
|
+
});
|
|
1075
|
+
}
|
|
1076
|
+
listAssignments(recordName, startingRole) {
|
|
1077
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1078
|
+
let record = this.roles[recordName];
|
|
1079
|
+
let assignedRoles = this.roleAssignments[recordName];
|
|
1080
|
+
let totalCount = 0;
|
|
1081
|
+
let assignments = [];
|
|
1082
|
+
for (let id in record) {
|
|
1083
|
+
const roles = record[id];
|
|
1084
|
+
for (let role of roles) {
|
|
1085
|
+
assignments.push({
|
|
1086
|
+
type: 'user',
|
|
1087
|
+
userId: id,
|
|
1088
|
+
role: {
|
|
1089
|
+
role,
|
|
1090
|
+
expireTimeMs: null,
|
|
1091
|
+
},
|
|
1092
|
+
});
|
|
1093
|
+
totalCount += 1;
|
|
1094
|
+
}
|
|
1095
|
+
}
|
|
1096
|
+
for (let id in assignedRoles) {
|
|
1097
|
+
let roles = assignedRoles[id];
|
|
1098
|
+
for (let role of roles) {
|
|
1099
|
+
assignments.push({
|
|
1100
|
+
type: 'user',
|
|
1101
|
+
userId: id,
|
|
1102
|
+
role,
|
|
1103
|
+
});
|
|
1104
|
+
totalCount += 1;
|
|
1105
|
+
}
|
|
1106
|
+
}
|
|
1107
|
+
assignments = sortBy(assignments, (a) => a.role.role, (a) => { var _a; return (_a = a.userId) !== null && _a !== void 0 ? _a : a.inst; });
|
|
1108
|
+
if (startingRole) {
|
|
1109
|
+
assignments = assignments.filter((a) => a.role.role > startingRole);
|
|
1110
|
+
}
|
|
1111
|
+
return {
|
|
1112
|
+
assignments: assignments.slice(0, 10),
|
|
1113
|
+
totalCount: totalCount,
|
|
1114
|
+
};
|
|
1115
|
+
});
|
|
1116
|
+
}
|
|
1117
|
+
assignSubjectRole(recordName, subjectId, type, role) {
|
|
1118
|
+
var _a;
|
|
1119
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1120
|
+
if (!this.roleAssignments[recordName]) {
|
|
1121
|
+
this.roleAssignments[recordName] = {};
|
|
1122
|
+
}
|
|
1123
|
+
const roles = (_a = this.roleAssignments[recordName][subjectId]) !== null && _a !== void 0 ? _a : [];
|
|
1124
|
+
const filtered = roles.filter((r) => r.role !== role.role ||
|
|
1125
|
+
getExpireTime(r.expireTimeMs) <= role.expireTimeMs);
|
|
1126
|
+
this.roleAssignments[recordName][subjectId] = [
|
|
1127
|
+
...filtered,
|
|
1128
|
+
{
|
|
1129
|
+
role: role.role,
|
|
1130
|
+
expireTimeMs: role.expireTimeMs === Infinity ? null : role.expireTimeMs,
|
|
1131
|
+
},
|
|
1132
|
+
];
|
|
1133
|
+
return {
|
|
1134
|
+
success: true,
|
|
1135
|
+
};
|
|
1136
|
+
});
|
|
1137
|
+
}
|
|
1138
|
+
revokeSubjectRole(recordName, subjectId, type, role) {
|
|
1139
|
+
var _a;
|
|
1140
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1141
|
+
if (!this.roleAssignments[recordName]) {
|
|
1142
|
+
this.roleAssignments[recordName] = {};
|
|
1143
|
+
}
|
|
1144
|
+
const roles = (_a = this.roleAssignments[recordName][subjectId]) !== null && _a !== void 0 ? _a : [];
|
|
1145
|
+
const filtered = roles.filter((r) => r.role !== role);
|
|
1146
|
+
this.roleAssignments[recordName][subjectId] = filtered;
|
|
1147
|
+
return {
|
|
1148
|
+
success: true,
|
|
1149
|
+
};
|
|
1150
|
+
});
|
|
1151
|
+
}
|
|
1152
|
+
updateUserRoles(recordName, userId, update) {
|
|
1153
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1154
|
+
if (!this.roleAssignments[recordName]) {
|
|
1155
|
+
this.roleAssignments[recordName] = {};
|
|
1156
|
+
}
|
|
1157
|
+
const assignments = update.roles
|
|
1158
|
+
.filter((r) => getExpireTime(r.expireTimeMs) > Date.now())
|
|
1159
|
+
.map((r) => (Object.assign(Object.assign({}, r), { expireTimeMs: r.expireTimeMs === Infinity ? null : r.expireTimeMs })));
|
|
1160
|
+
this.roleAssignments[recordName][userId] = assignments;
|
|
1161
|
+
return {
|
|
1162
|
+
success: true,
|
|
1163
|
+
};
|
|
1164
|
+
});
|
|
1165
|
+
}
|
|
1166
|
+
updateInstRoles(recordName, inst, update) {
|
|
1167
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1168
|
+
if (!this.roleAssignments[recordName]) {
|
|
1169
|
+
this.roleAssignments[recordName] = {};
|
|
1170
|
+
}
|
|
1171
|
+
const assignments = update.roles
|
|
1172
|
+
.filter((r) => getExpireTime(r.expireTimeMs) > Date.now())
|
|
1173
|
+
.map((r) => (Object.assign(Object.assign({}, r), { expireTimeMs: r.expireTimeMs === Infinity ? null : r.expireTimeMs })));
|
|
1174
|
+
this.roleAssignments[recordName][inst] = assignments;
|
|
1175
|
+
return {
|
|
1176
|
+
success: true,
|
|
1177
|
+
};
|
|
1178
|
+
});
|
|
1179
|
+
}
|
|
1180
|
+
_getRolesForEntity(recordName, id) {
|
|
1181
|
+
var _a, _b, _c, _d;
|
|
1182
|
+
const roles = (_b = (_a = this.roles[recordName]) === null || _a === void 0 ? void 0 : _a[id]) !== null && _b !== void 0 ? _b : new Set();
|
|
1183
|
+
const assignments = (_d = (_c = this.roleAssignments[recordName]) === null || _c === void 0 ? void 0 : _c[id]) !== null && _d !== void 0 ? _d : [];
|
|
1184
|
+
return [
|
|
1185
|
+
...[...roles].map((r) => ({
|
|
1186
|
+
role: r,
|
|
1187
|
+
expireTimeMs: null,
|
|
1188
|
+
})),
|
|
1189
|
+
...assignments.filter((a) => getExpireTime(a.expireTimeMs) > Date.now()),
|
|
1190
|
+
];
|
|
1191
|
+
}
|
|
1192
|
+
getSubscriptionDataMetricsByRecordName(recordName) {
|
|
1193
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1194
|
+
const info = yield this._getSubscriptionInfo(recordName);
|
|
1195
|
+
const records = yield this._listRecordsForSubscription(recordName);
|
|
1196
|
+
let totalItems = 0;
|
|
1197
|
+
for (let record of records) {
|
|
1198
|
+
let bucket = this._dataBuckets.get(record.name);
|
|
1199
|
+
if (!bucket) {
|
|
1200
|
+
continue;
|
|
1201
|
+
}
|
|
1202
|
+
totalItems += bucket.size;
|
|
1203
|
+
}
|
|
1204
|
+
return Object.assign(Object.assign({}, info), { totalItems });
|
|
1205
|
+
});
|
|
1206
|
+
}
|
|
1207
|
+
getSubscriptionFileMetricsByRecordName(recordName) {
|
|
1208
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1209
|
+
const info = yield this._getSubscriptionInfo(recordName);
|
|
1210
|
+
const records = yield this._listRecordsForSubscription(recordName);
|
|
1211
|
+
let totalFiles = 0;
|
|
1212
|
+
let totalBytesStored = 0;
|
|
1213
|
+
let totalBytesReserved = 0;
|
|
1214
|
+
for (let record of records) {
|
|
1215
|
+
let files = [...this.files.values()].filter((f) => f.recordName === record.name);
|
|
1216
|
+
for (let file of files) {
|
|
1217
|
+
totalFiles++;
|
|
1218
|
+
totalBytesReserved += file.sizeInBytes;
|
|
1219
|
+
if (file.uploaded) {
|
|
1220
|
+
totalBytesStored += file.sizeInBytes;
|
|
1221
|
+
}
|
|
1222
|
+
}
|
|
1223
|
+
}
|
|
1224
|
+
return Object.assign(Object.assign({}, info), { totalFiles, totalFileBytesReserved: totalBytesReserved });
|
|
1225
|
+
});
|
|
1226
|
+
}
|
|
1227
|
+
getSubscriptionEventMetricsByRecordName(recordName) {
|
|
1228
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1229
|
+
const info = yield this._getSubscriptionInfo(recordName);
|
|
1230
|
+
const records = yield this._listRecordsForSubscription(recordName);
|
|
1231
|
+
let totalEventNames = 0;
|
|
1232
|
+
for (let record of records) {
|
|
1233
|
+
let bucket = this._eventBuckets.get(record.name);
|
|
1234
|
+
if (!bucket) {
|
|
1235
|
+
continue;
|
|
1236
|
+
}
|
|
1237
|
+
totalEventNames += bucket.size;
|
|
1238
|
+
}
|
|
1239
|
+
return Object.assign(Object.assign({}, info), { totalEventNames });
|
|
1240
|
+
});
|
|
1241
|
+
}
|
|
1242
|
+
getSubscriptionRecordMetrics(filter) {
|
|
1243
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1244
|
+
const metrics = yield this._getSubscriptionMetrics(filter);
|
|
1245
|
+
const totalRecords = yield this.countRecords(filter);
|
|
1246
|
+
return Object.assign(Object.assign({}, metrics), { totalRecords });
|
|
1247
|
+
});
|
|
1248
|
+
}
|
|
1249
|
+
getSubscriptionAiChatMetrics(filter) {
|
|
1250
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1251
|
+
const info = yield this._getSubscriptionMetrics(filter);
|
|
1252
|
+
const metrics = filter.ownerId
|
|
1253
|
+
? this._aiChatMetrics.filter((m) => m.userId === filter.ownerId &&
|
|
1254
|
+
(!info.currentPeriodStartMs ||
|
|
1255
|
+
(m.createdAtMs >= info.currentPeriodStartMs &&
|
|
1256
|
+
m.createdAtMs < info.currentPeriodEndMs)))
|
|
1257
|
+
: this._aiChatMetrics.filter((m) => m.studioId === filter.studioId &&
|
|
1258
|
+
(!info.currentPeriodStartMs ||
|
|
1259
|
+
(m.createdAtMs >= info.currentPeriodStartMs &&
|
|
1260
|
+
m.createdAtMs < info.currentPeriodEndMs)));
|
|
1261
|
+
let totalTokens = 0;
|
|
1262
|
+
for (let m of metrics) {
|
|
1263
|
+
totalTokens += m.tokens;
|
|
1264
|
+
}
|
|
1265
|
+
return Object.assign(Object.assign({}, info), { totalTokensInCurrentPeriod: totalTokens });
|
|
1266
|
+
});
|
|
1267
|
+
}
|
|
1268
|
+
recordChatMetrics(metrics) {
|
|
1269
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1270
|
+
this._aiChatMetrics.push(metrics);
|
|
1271
|
+
});
|
|
1272
|
+
}
|
|
1273
|
+
getSubscriptionAiImageMetrics(filter) {
|
|
1274
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1275
|
+
const info = yield this._getSubscriptionMetrics(filter);
|
|
1276
|
+
const metrics = filter.ownerId
|
|
1277
|
+
? this._aiImageMetrics.filter((m) => m.userId === filter.ownerId &&
|
|
1278
|
+
(!info.currentPeriodStartMs ||
|
|
1279
|
+
(m.createdAtMs >= info.currentPeriodStartMs &&
|
|
1280
|
+
m.createdAtMs < info.currentPeriodEndMs)))
|
|
1281
|
+
: this._aiImageMetrics.filter((m) => m.studioId === filter.studioId &&
|
|
1282
|
+
(!info.currentPeriodStartMs ||
|
|
1283
|
+
(m.createdAtMs >= info.currentPeriodStartMs &&
|
|
1284
|
+
m.createdAtMs < info.currentPeriodEndMs)));
|
|
1285
|
+
let totalPixels = 0;
|
|
1286
|
+
for (let m of metrics) {
|
|
1287
|
+
totalPixels += m.squarePixels;
|
|
1288
|
+
}
|
|
1289
|
+
return Object.assign(Object.assign({}, info), { totalSquarePixelsInCurrentPeriod: totalPixels });
|
|
1290
|
+
});
|
|
1291
|
+
}
|
|
1292
|
+
recordImageMetrics(metrics) {
|
|
1293
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1294
|
+
this._aiImageMetrics.push(metrics);
|
|
1295
|
+
});
|
|
1296
|
+
}
|
|
1297
|
+
getSubscriptionAiSkyboxMetrics(filter) {
|
|
1298
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1299
|
+
const info = yield this._getSubscriptionMetrics(filter);
|
|
1300
|
+
const metrics = filter.ownerId
|
|
1301
|
+
? this._aiSkyboxMetrics.filter((m) => m.userId === filter.ownerId &&
|
|
1302
|
+
(!info.currentPeriodStartMs ||
|
|
1303
|
+
(m.createdAtMs >= info.currentPeriodStartMs &&
|
|
1304
|
+
m.createdAtMs < info.currentPeriodEndMs)))
|
|
1305
|
+
: this._aiSkyboxMetrics.filter((m) => m.studioId === filter.studioId &&
|
|
1306
|
+
(!info.currentPeriodStartMs ||
|
|
1307
|
+
(m.createdAtMs >= info.currentPeriodStartMs &&
|
|
1308
|
+
m.createdAtMs < info.currentPeriodEndMs)));
|
|
1309
|
+
let totalSkyboxes = 0;
|
|
1310
|
+
for (let m of metrics) {
|
|
1311
|
+
totalSkyboxes += m.skyboxes;
|
|
1312
|
+
}
|
|
1313
|
+
return Object.assign(Object.assign({}, info), { totalSkyboxesInCurrentPeriod: totalSkyboxes });
|
|
1314
|
+
});
|
|
1315
|
+
}
|
|
1316
|
+
recordSkyboxMetrics(metrics) {
|
|
1317
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1318
|
+
this._aiSkyboxMetrics.push(metrics);
|
|
1319
|
+
});
|
|
1320
|
+
}
|
|
1321
|
+
_getSubscriptionInfo(recordName) {
|
|
1322
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1323
|
+
const record = yield this.getRecordByName(recordName);
|
|
1324
|
+
const metrics = yield this._getSubscriptionMetrics(record);
|
|
1325
|
+
return Object.assign(Object.assign({}, metrics), { recordName: record.name });
|
|
1326
|
+
});
|
|
1327
|
+
}
|
|
1328
|
+
_getSubscriptionMetrics(filter) {
|
|
1329
|
+
var _a;
|
|
1330
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1331
|
+
const config = yield this.getSubscriptionConfiguration();
|
|
1332
|
+
let currentPeriodStart = null;
|
|
1333
|
+
let currentPeriodEnd = null;
|
|
1334
|
+
if ((_a = config === null || config === void 0 ? void 0 : config.defaultFeatures) === null || _a === void 0 ? void 0 : _a.defaultPeriodLength) {
|
|
1335
|
+
const now = DateTime.utc();
|
|
1336
|
+
const periodStart = now.minus(config.defaultFeatures.defaultPeriodLength);
|
|
1337
|
+
currentPeriodStart = periodStart.toMillis();
|
|
1338
|
+
currentPeriodEnd = now.toMillis();
|
|
1339
|
+
}
|
|
1340
|
+
let metrics = {
|
|
1341
|
+
ownerId: filter.ownerId,
|
|
1342
|
+
studioId: filter.studioId,
|
|
1343
|
+
subscriptionId: null,
|
|
1344
|
+
subscriptionStatus: null,
|
|
1345
|
+
currentPeriodStartMs: currentPeriodStart,
|
|
1346
|
+
currentPeriodEndMs: currentPeriodEnd,
|
|
1347
|
+
};
|
|
1348
|
+
if (filter.ownerId) {
|
|
1349
|
+
const user = yield this.findUser(filter.ownerId);
|
|
1350
|
+
if (user) {
|
|
1351
|
+
metrics.subscriptionStatus = user.subscriptionStatus;
|
|
1352
|
+
metrics.subscriptionId = user.subscriptionId;
|
|
1353
|
+
metrics.currentPeriodEndMs = user.subscriptionPeriodEndMs;
|
|
1354
|
+
metrics.currentPeriodStartMs = user.subscriptionPeriodStartMs;
|
|
1355
|
+
}
|
|
1356
|
+
}
|
|
1357
|
+
else if (filter.studioId) {
|
|
1358
|
+
const studio = yield this.getStudioById(filter.studioId);
|
|
1359
|
+
if (studio) {
|
|
1360
|
+
metrics.subscriptionId = studio.subscriptionId;
|
|
1361
|
+
metrics.subscriptionStatus = studio.subscriptionStatus;
|
|
1362
|
+
metrics.currentPeriodEndMs = studio.subscriptionPeriodEndMs;
|
|
1363
|
+
metrics.currentPeriodStartMs = studio.subscriptionPeriodStartMs;
|
|
1364
|
+
}
|
|
1365
|
+
}
|
|
1366
|
+
return metrics;
|
|
1367
|
+
});
|
|
1368
|
+
}
|
|
1369
|
+
_listRecordsForSubscription(recordName) {
|
|
1370
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1371
|
+
const record = yield this.getRecordByName(recordName);
|
|
1372
|
+
if (record.ownerId) {
|
|
1373
|
+
return this.listRecordsByOwnerId(record.ownerId);
|
|
1374
|
+
}
|
|
1375
|
+
else {
|
|
1376
|
+
return this.listRecordsByStudioId(record.studioId);
|
|
1377
|
+
}
|
|
1378
|
+
});
|
|
1379
|
+
}
|
|
1380
|
+
}
|
|
1381
|
+
//# sourceMappingURL=MemoryStore.js.map
|