-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Expand file tree
/
Copy pathcreate-usage-records.e2e-ee.ts
More file actions
281 lines (249 loc) · 8.43 KB
/
create-usage-records.e2e-ee.ts
File metadata and controls
281 lines (249 loc) · 8.43 KB
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
// biome-ignore lint/style/noRestrictedImports: <explanation>
import { Logger } from '@nestjs/common';
import { CreateUsageRecords, CreateUsageRecordsCommand } from '@novu/ee-billing';
import { StripeUsageTypeEnum } from '@novu/ee-billing/src/stripe/types';
import { ApiServiceLevelEnum, StripeBillingIntervalEnum } from '@novu/shared';
import { expect } from 'chai';
import sinon from 'sinon';
const mockMonthlyBusinessSubscription = {
id: 'subscription_id',
items: {
data: [
{
id: 'item_id_usage_notifications',
price: { lookup_key: 'business_usage_notifications', recurring: { usage_type: StripeUsageTypeEnum.METERED } },
},
{
id: 'item_id_flat',
price: { lookup_key: 'business_flat_monthly', recurring: { usage_type: StripeUsageTypeEnum.LICENSED } },
},
],
},
};
describe('CreateUsageRecords #novu-v2', () => {
const stripeStub = {
subscriptionItems: {
createUsageRecord: () => {},
},
};
const analyticsServiceStub = {
track: sinon.stub(),
};
const createSubscriptionUsecase = { execute: () => Promise.resolve() };
const getOrCreateCustomerUsecase = { execute: () => Promise.resolve() };
const getPlatformNotificationUsageUsecase = { execute: () => Promise.resolve() };
let createUsageRecordStub: sinon.SinonStub;
let getPlatformNotificationUsageStub: sinon.SinonStub;
let createSubscriptionStub: sinon.SinonStub;
let getOrCreateCustomerStub: sinon.SinonStub;
beforeEach(() => {
createUsageRecordStub = sinon.stub(stripeStub.subscriptionItems, 'createUsageRecord').resolves({
id: 'usage_record_id',
});
getPlatformNotificationUsageStub = sinon.stub(getPlatformNotificationUsageUsecase, 'execute').resolves([
{
_id: 'organization_id',
apiServiceLevel: ApiServiceLevelEnum.BUSINESS,
notificationsCount: 100,
},
] as any);
createSubscriptionStub = sinon.stub(createSubscriptionUsecase, 'execute').resolves({
id: 'subscription_id',
} as any);
getOrCreateCustomerStub = sinon.stub(getOrCreateCustomerUsecase, 'execute').resolves({
id: 'customer_id',
deleted: false,
metadata: {
organizationId: 'organization_id',
},
subscriptions: {
data: [mockMonthlyBusinessSubscription],
},
} as any);
});
afterEach(() => {
createUsageRecordStub.reset();
getOrCreateCustomerStub.reset();
createSubscriptionStub.reset();
getPlatformNotificationUsageStub.reset();
analyticsServiceStub.track.reset();
});
const createUseCase = () => {
const useCase = new CreateUsageRecords(
stripeStub,
getOrCreateCustomerUsecase,
createSubscriptionUsecase,
getPlatformNotificationUsageUsecase,
analyticsServiceStub
);
return useCase;
};
it('should fetch the platform usage records with usage dates between the start and end date of the previous day', async () => {
const mockDate = new Date('2021-01-15T00:01:00Z');
const useCase = createUseCase();
await useCase.execute(
CreateUsageRecordsCommand.create({
startDate: mockDate,
})
);
const expectedStartDate = new Date('2021-01-14T00:00:00Z');
const expectedEndDate = new Date('2021-01-14T23:59:59.999Z');
expect(getPlatformNotificationUsageStub.lastCall.args).to.deep.equal([
{
startDate: expectedStartDate,
endDate: expectedEndDate,
},
]);
});
it('should create a free-tier subscription if the customer has no subscriptions', async () => {
const mockNoSubscriptionsCustomer = {
id: 'customer_id',
subscriptions: { data: [] },
};
getOrCreateCustomerStub.resolves(mockNoSubscriptionsCustomer);
const useCase = createUseCase();
await useCase.execute(
CreateUsageRecordsCommand.create({
startDate: new Date(),
})
);
expect(createSubscriptionStub.callCount).to.equal(1); // this is failing without the promise above
expect(createSubscriptionStub.lastCall.args).to.deep.equal([
{
customer: mockNoSubscriptionsCustomer,
apiServiceLevel: ApiServiceLevelEnum.FREE,
billingInterval: StripeBillingIntervalEnum.MONTH,
},
]);
});
it('should set the usage timestamp to the subscription current period start if the subscription is new', async () => {
const mockSubscriptionStartDate = new Date('2021-02-01T00:00:00Z');
const mockSubscriptionCurrentPeriodStart = mockSubscriptionStartDate.getTime() / 1000;
const mockUsageStartDate = new Date('2021-01-15T00:00:00Z');
getOrCreateCustomerStub.resolves({
subscriptions: {
data: [
{
...mockMonthlyBusinessSubscription,
current_period_start: mockSubscriptionCurrentPeriodStart,
},
],
},
});
const useCase = createUseCase();
await useCase.execute(
CreateUsageRecordsCommand.create({
startDate: mockUsageStartDate,
})
);
expect(createUsageRecordStub.lastCall.args[1].timestamp).to.equal(mockSubscriptionCurrentPeriodStart);
});
it('should set the usage timestamp to the usage start date if the subscription is not new', async () => {
const mockSubscriptionStartDate = new Date('2021-01-01T00:00:00Z');
const mockSubscriptionCreated = mockSubscriptionStartDate.getTime() / 1000;
const mockCurrentDate = new Date('2021-01-15T12:00:00Z');
getOrCreateCustomerStub.resolves({
subscriptions: {
data: [
{
...mockMonthlyBusinessSubscription,
current_period_start: mockSubscriptionCreated,
},
],
},
});
const useCase = createUseCase();
const expectedTimestamp = new Date('2021-01-15T00:00:00Z').getTime() / 1000;
await useCase.execute(
CreateUsageRecordsCommand.create({
startDate: mockCurrentDate,
})
);
expect(createUsageRecordStub.lastCall.args[1].timestamp).to.equal(expectedTimestamp);
});
it('should use the usage subscription item to create the usage record', async () => {
const useCase = createUseCase();
await useCase.execute(
CreateUsageRecordsCommand.create({
startDate: new Date(),
})
);
expect(createUsageRecordStub.lastCall.args[0]).to.equal('item_id_usage_notifications');
});
it('should log an error if the usage subscription item is not found on the subscription', async () => {
const logStub = sinon.spy(Logger, 'error');
getPlatformNotificationUsageStub.resolves([
{
_id: 'organization_id_1',
apiServiceLevel: ApiServiceLevelEnum.FREE,
notificationsCount: 100,
},
]);
const mockNoMeteredSubscription = {
id: 'subscription_id',
items: {
data: [
{
id: 'item_id_flat',
price: { lookup_key: 'business_flat_monthly', recurring: { usage_type: StripeUsageTypeEnum.LICENSED } },
},
],
},
};
getOrCreateCustomerStub.resolves({
subscriptions: {
data: [mockNoMeteredSubscription],
},
});
const useCase = createUseCase();
await useCase.execute(
CreateUsageRecordsCommand.create({
startDate: new Date(),
})
);
expect(logStub.lastCall.args[0].message).to.equal(
"No metered subscription found for organizationId: 'organization_id_1'"
);
logStub.restore();
});
it('should create a usage record for each organization', async () => {
const mockUsageStartDate = new Date('2021-01-15T12:00:00Z');
getPlatformNotificationUsageStub.resolves([
{
_id: 'organization_id_1',
apiServiceLevel: ApiServiceLevelEnum.BUSINESS,
notificationsCount: 100,
},
{
_id: 'organization_id_2',
apiServiceLevel: ApiServiceLevelEnum.BUSINESS,
notificationsCount: 200,
},
]);
const useCase = createUseCase();
await useCase.execute(
CreateUsageRecordsCommand.create({
startDate: mockUsageStartDate,
})
);
const expectedUsageTimestamp = new Date('2021-01-15T00:00:00Z').getTime() / 1000;
expect(createUsageRecordStub.getCalls().map(({ args }) => args)).to.deep.equal([
[
'item_id_usage_notifications',
{
quantity: 100,
timestamp: expectedUsageTimestamp,
action: 'set',
},
],
[
'item_id_usage_notifications',
{
quantity: 200,
timestamp: expectedUsageTimestamp,
action: 'set',
},
],
]);
});
});