Skip to content

Commit cd50987

Browse files
committed
Add comment and simplify test
1 parent 4456afe commit cd50987

File tree

2 files changed

+113
-161
lines changed

2 files changed

+113
-161
lines changed

src/resolvers/billingNew.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,18 @@ export default {
111111

112112
let isCardLinkOperation = false;
113113

114-
if (workspace.tariffPlanId.toString() === tariffPlanId && !workspace.isTariffPlanExpired() && !workspace.isBlocked) {
114+
115+
/**
116+
* We need to only link card and not pay for the whole plan in case
117+
* 1. We are paying for the same plan and
118+
* 2. Plan is not expired and
119+
* 3. Workspace is not blocked
120+
*/
121+
if (
122+
workspace.tariffPlanId.toString() === tariffPlanId && // 1
123+
!workspace.isTariffPlanExpired() && // 2
124+
!workspace.isBlocked // 3
125+
) {
115126
isCardLinkOperation = true;
116127
}
117128

test/resolvers/billingNew.test.ts

Lines changed: 101 additions & 160 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,113 @@
11
import '../../src/env-test';
22
import { ObjectId } from 'mongodb';
3-
import { PlanDBScheme, WorkspaceDBScheme, UserDBScheme } from '@hawk.so/types';
3+
import { PlanDBScheme, WorkspaceDBScheme } from '@hawk.so/types';
44
import billingNewResolver from '../../src/resolvers/billingNew';
55
import { ResolverContextWithUser } from '../../src/types/graphql';
66

7-
// Устанавливаем переменные окружения для теста
7+
// Set environment variables for test
88
process.env.JWT_SECRET_BILLING_CHECKSUM = 'checksum_secret';
99
process.env.JWT_SECRET_ACCESS_TOKEN = 'belarus';
1010
process.env.JWT_SECRET_REFRESH_TOKEN = 'abacaba';
1111
process.env.JWT_SECRET_PROJECT_TOKEN = 'qwerty';
1212

13+
/**
14+
* Creates test data and mocks for composePayment tests
15+
*/
16+
function createComposePaymentTestSetup(options: {
17+
isTariffPlanExpired?: boolean;
18+
isBlocked?: boolean;
19+
lastChargeDate?: Date;
20+
planMonthlyCharge?: number;
21+
planCurrency?: string;
22+
}) {
23+
const {
24+
isTariffPlanExpired = false,
25+
isBlocked = false,
26+
lastChargeDate = new Date(),
27+
planMonthlyCharge = 1000,
28+
planCurrency = 'RUB'
29+
} = options;
30+
31+
const userId = new ObjectId().toString();
32+
const workspaceId = new ObjectId().toString();
33+
const planId = new ObjectId().toString();
34+
35+
const plan: PlanDBScheme = {
36+
_id: new ObjectId(planId),
37+
name: 'Test Plan',
38+
monthlyCharge: planMonthlyCharge,
39+
monthlyChargeCurrency: planCurrency,
40+
eventsLimit: 1000,
41+
isDefault: false,
42+
isHidden: false,
43+
};
44+
45+
const workspace: WorkspaceDBScheme = {
46+
_id: new ObjectId(workspaceId),
47+
name: 'Test Workspace',
48+
accountId: 'test-account-id',
49+
balance: 0,
50+
billingPeriodEventsCount: 0,
51+
isBlocked,
52+
lastChargeDate,
53+
tariffPlanId: new ObjectId(planId),
54+
inviteHash: 'test-invite-hash',
55+
subscriptionId: undefined,
56+
};
57+
58+
// Mock workspaces factory
59+
const mockWorkspacesFactory = {
60+
findById: jest.fn().mockResolvedValue({
61+
...workspace,
62+
getMemberInfo: jest.fn().mockResolvedValue({ isAdmin: true }),
63+
isTariffPlanExpired: jest.fn().mockReturnValue(isTariffPlanExpired),
64+
isBlocked,
65+
}),
66+
};
67+
68+
// Mock plans factory
69+
const mockPlansFactory = {
70+
findById: jest.fn().mockResolvedValue(plan),
71+
};
72+
73+
const mockContext: ResolverContextWithUser = {
74+
user: {
75+
id: userId,
76+
accessTokenExpired: false,
77+
},
78+
factories: {
79+
workspacesFactory: mockWorkspacesFactory as any,
80+
plansFactory: mockPlansFactory as any,
81+
usersFactory: {} as any,
82+
projectsFactory: {} as any,
83+
businessOperationsFactory: {} as any,
84+
},
85+
};
86+
87+
return {
88+
userId,
89+
workspaceId,
90+
planId,
91+
plan,
92+
workspace,
93+
mockContext,
94+
mockWorkspacesFactory,
95+
mockPlansFactory,
96+
};
97+
}
98+
1399
describe('GraphQLBillingNew', () => {
14100
describe('composePayment', () => {
15101
it('should return isCardLinkOperation = false in case of expired tariff plan', async () => {
16-
const userId = new ObjectId().toString();
17-
const workspaceId = new ObjectId().toString();
18-
const planId = new ObjectId().toString();
19-
20-
const plan: PlanDBScheme = {
21-
_id: new ObjectId(planId),
22-
name: 'Test Plan',
23-
monthlyCharge: 1000,
24-
monthlyChargeCurrency: 'RUB',
25-
eventsLimit: 1000,
26-
isDefault: false,
27-
isHidden: false,
28-
};
29-
30-
// Workspace with expired tariff plan
102+
// Create 2 months ago date
31103
const expiredDate = new Date();
32104
expiredDate.setMonth(expiredDate.getMonth() - 2);
33105

34-
const workspace: WorkspaceDBScheme = {
35-
_id: new ObjectId(workspaceId),
36-
name: 'Test Workspace',
37-
accountId: 'test-account-id',
38-
balance: 0,
39-
billingPeriodEventsCount: 0,
106+
const { mockContext, planId, workspaceId } = createComposePaymentTestSetup({
107+
isTariffPlanExpired: true,
40108
isBlocked: false,
41109
lastChargeDate: expiredDate,
42-
tariffPlanId: new ObjectId(planId),
43-
inviteHash: 'test-invite-hash',
44-
subscriptionId: undefined,
45-
};
46-
47-
// Mock workspaces factory
48-
const mockWorkspacesFactory = {
49-
findById: jest.fn().mockResolvedValue({
50-
...workspace,
51-
getMemberInfo: jest.fn().mockResolvedValue({ isAdmin: true }),
52-
isTariffPlanExpired: jest.fn().mockReturnValue(true), // План истек
53-
isBlocked: false,
54-
}),
55-
};
56-
57-
// Mock plans factory
58-
const mockPlansFactory = {
59-
findById: jest.fn().mockResolvedValue(plan),
60-
};
61-
62-
const mockContext: ResolverContextWithUser = {
63-
user: {
64-
id: userId,
65-
accessTokenExpired: false,
66-
},
67-
factories: {
68-
workspacesFactory: mockWorkspacesFactory as any,
69-
plansFactory: mockPlansFactory as any,
70-
usersFactory: {} as any,
71-
projectsFactory: {} as any,
72-
businessOperationsFactory: {} as any,
73-
},
74-
};
110+
});
75111

76112
// Call composePayment resolver
77113
const result = await billingNewResolver.Query.composePayment(
@@ -102,61 +138,14 @@ describe('GraphQLBillingNew', () => {
102138
});
103139

104140
it('should return isCardLinkOperation = true in case of active tariff plan', async () => {
105-
const userId = new ObjectId().toString();
106-
const workspaceId = new ObjectId().toString();
107-
const planId = new ObjectId().toString();
108-
141+
// Create 2 days ago date
142+
const lastChargeDate = new Date(Date.now() - 2 * 24 * 60 * 60 * 1000);
109143

110-
const plan: PlanDBScheme = {
111-
_id: new ObjectId(planId),
112-
name: 'Test Plan',
113-
monthlyCharge: 1000,
114-
monthlyChargeCurrency: 'RUB',
115-
eventsLimit: 1000,
116-
isDefault: false,
117-
isHidden: false,
118-
};
119-
120-
const lastChargeDate = new Date(Date.now() - 2 * 24 * 60 * 60 * 1000); // Last charge date is 2 days ago
121-
122-
const workspace: WorkspaceDBScheme = {
123-
_id: new ObjectId(workspaceId),
124-
name: 'Test Workspace',
125-
accountId: 'test-account-id',
126-
balance: 0,
127-
billingPeriodEventsCount: 0,
144+
const { mockContext, planId, workspaceId, workspace } = createComposePaymentTestSetup({
145+
isTariffPlanExpired: false,
128146
isBlocked: false,
129147
lastChargeDate,
130-
tariffPlanId: new ObjectId(planId),
131-
inviteHash: 'test-invite-hash',
132-
subscriptionId: undefined,
133-
};
134-
135-
const mockWorkspacesFactory = {
136-
findById: jest.fn().mockResolvedValue({
137-
...workspace,
138-
getMemberInfo: jest.fn().mockResolvedValue({ isAdmin: true }),
139-
isTariffPlanExpired: jest.fn().mockReturnValue(false),
140-
}),
141-
};
142-
143-
const mockPlansFactory = {
144-
findById: jest.fn().mockResolvedValue(plan),
145-
};
146-
147-
const mockContext: ResolverContextWithUser = {
148-
user: {
149-
id: userId,
150-
accessTokenExpired: false,
151-
},
152-
factories: {
153-
workspacesFactory: mockWorkspacesFactory as any,
154-
plansFactory: mockPlansFactory as any,
155-
usersFactory: {} as any,
156-
projectsFactory: {} as any,
157-
businessOperationsFactory: {} as any,
158-
},
159-
};
148+
});
160149

161150
const result = await billingNewResolver.Query.composePayment(
162151
undefined,
@@ -174,7 +163,7 @@ describe('GraphQLBillingNew', () => {
174163
expect(result.plan.monthlyCharge).toBe(1000);
175164
expect(result.currency).toBe('RUB');
176165

177-
const oneMonthFromLastChargeDate = new Date(lastChargeDate);
166+
const oneMonthFromLastChargeDate = new Date(workspace.lastChargeDate);
178167
oneMonthFromLastChargeDate.setMonth(oneMonthFromLastChargeDate.getMonth() + 1);
179168

180169
const oneMonthFromLastChargeDateStr = oneMonthFromLastChargeDate.toISOString().split('T')[0];
@@ -183,59 +172,11 @@ describe('GraphQLBillingNew', () => {
183172
});
184173

185174
it('should return isCardLinkOperation = false in case of blocked workspace', async () => {
186-
const userId = new ObjectId().toString();
187-
const workspaceId = new ObjectId().toString();
188-
const planId = new ObjectId().toString();
189-
190-
const plan: PlanDBScheme = {
191-
_id: new ObjectId(planId),
192-
name: 'Test Plan',
193-
monthlyCharge: 1000,
194-
monthlyChargeCurrency: 'RUB',
195-
eventsLimit: 1000,
196-
isDefault: false,
197-
isHidden: false,
198-
};
199-
200-
const workspace: WorkspaceDBScheme = {
201-
_id: new ObjectId(workspaceId),
202-
name: 'Test Workspace',
203-
accountId: 'test-account-id',
204-
balance: 0,
205-
billingPeriodEventsCount: 0,
175+
const { mockContext, planId, workspaceId } = createComposePaymentTestSetup({
176+
isTariffPlanExpired: false,
206177
isBlocked: true,
207178
lastChargeDate: new Date(),
208-
tariffPlanId: new ObjectId(planId),
209-
inviteHash: 'test-invite-hash',
210-
subscriptionId: undefined,
211-
};
212-
213-
const mockWorkspacesFactory = {
214-
findById: jest.fn().mockResolvedValue({
215-
...workspace,
216-
getMemberInfo: jest.fn().mockResolvedValue({ isAdmin: true }),
217-
isTariffPlanExpired: jest.fn().mockReturnValue(false),
218-
}),
219-
};
220-
221-
222-
const mockPlansFactory = {
223-
findById: jest.fn().mockResolvedValue(plan),
224-
};
225-
226-
const mockContext: ResolverContextWithUser = {
227-
user: {
228-
id: userId,
229-
accessTokenExpired: false,
230-
},
231-
factories: {
232-
workspacesFactory: mockWorkspacesFactory as any,
233-
plansFactory: mockPlansFactory as any,
234-
usersFactory: {} as any,
235-
projectsFactory: {} as any,
236-
businessOperationsFactory: {} as any,
237-
},
238-
};
179+
});
239180

240181
const result = await billingNewResolver.Query.composePayment(
241182
undefined,

0 commit comments

Comments
 (0)