|
| 1 | +import '../../src/env-test'; |
| 2 | +import { ObjectId } from 'mongodb'; |
| 3 | +import { PlanDBScheme, WorkspaceDBScheme, UserDBScheme } from '@hawk.so/types'; |
| 4 | +import billingNewResolver from '../../src/resolvers/billingNew'; |
| 5 | +import { ResolverContextWithUser } from '../../src/types/graphql'; |
| 6 | + |
| 7 | +// Мокаем telegram модуль |
| 8 | +// jest.mock('../../src/utils/telegram', () => ({ |
| 9 | +// sendMessage: jest.fn().mockResolvedValue(undefined), |
| 10 | +// TelegramBotURLs: { |
| 11 | +// Base: 'base', |
| 12 | +// Money: 'money', |
| 13 | +// }, |
| 14 | +// })); |
| 15 | + |
| 16 | +// Устанавливаем переменные окружения для теста |
| 17 | +process.env.JWT_SECRET_BILLING_CHECKSUM = 'checksum_secret'; |
| 18 | +process.env.JWT_SECRET_ACCESS_TOKEN = 'belarus'; |
| 19 | +process.env.JWT_SECRET_REFRESH_TOKEN = 'abacaba'; |
| 20 | +process.env.JWT_SECRET_PROJECT_TOKEN = 'qwerty'; |
| 21 | + |
| 22 | +describe('GraphQLBillingNew', () => { |
| 23 | + describe('composePayment', () => { |
| 24 | + it('should return isCardLinkOperation = false in case of expired tariff plan', async () => { |
| 25 | + const userId = new ObjectId().toString(); |
| 26 | + const workspaceId = new ObjectId().toString(); |
| 27 | + const planId = new ObjectId().toString(); |
| 28 | + |
| 29 | + const plan: PlanDBScheme = { |
| 30 | + _id: new ObjectId(planId), |
| 31 | + name: 'Test Plan', |
| 32 | + monthlyCharge: 1000, |
| 33 | + monthlyChargeCurrency: 'RUB', |
| 34 | + eventsLimit: 1000, |
| 35 | + isDefault: false, |
| 36 | + isHidden: false, |
| 37 | + }; |
| 38 | + |
| 39 | + // Workspace with expired tariff plan |
| 40 | + const expiredDate = new Date(); |
| 41 | + expiredDate.setMonth(expiredDate.getMonth() - 2); |
| 42 | + |
| 43 | + const workspace: WorkspaceDBScheme = { |
| 44 | + _id: new ObjectId(workspaceId), |
| 45 | + name: 'Test Workspace', |
| 46 | + accountId: 'test-account-id', |
| 47 | + balance: 0, |
| 48 | + billingPeriodEventsCount: 0, |
| 49 | + isBlocked: false, |
| 50 | + lastChargeDate: expiredDate, |
| 51 | + tariffPlanId: new ObjectId(planId), |
| 52 | + inviteHash: 'test-invite-hash', |
| 53 | + subscriptionId: undefined, |
| 54 | + }; |
| 55 | + |
| 56 | + // Mock workspaces factory |
| 57 | + const mockWorkspacesFactory = { |
| 58 | + findById: jest.fn().mockResolvedValue({ |
| 59 | + ...workspace, |
| 60 | + getMemberInfo: jest.fn().mockResolvedValue({ isAdmin: true }), |
| 61 | + isTariffPlanExpired: jest.fn().mockReturnValue(true), // План истек |
| 62 | + isBlocked: false, |
| 63 | + }), |
| 64 | + }; |
| 65 | + |
| 66 | + // Mock plans factory |
| 67 | + const mockPlansFactory = { |
| 68 | + findById: jest.fn().mockResolvedValue(plan), |
| 69 | + }; |
| 70 | + |
| 71 | + const mockContext: ResolverContextWithUser = { |
| 72 | + user: { |
| 73 | + id: userId, |
| 74 | + accessTokenExpired: false, |
| 75 | + }, |
| 76 | + factories: { |
| 77 | + workspacesFactory: mockWorkspacesFactory as any, |
| 78 | + plansFactory: mockPlansFactory as any, |
| 79 | + usersFactory: {} as any, |
| 80 | + projectsFactory: {} as any, |
| 81 | + businessOperationsFactory: {} as any, |
| 82 | + }, |
| 83 | + }; |
| 84 | + |
| 85 | + // Call composePayment resolver |
| 86 | + const result = await billingNewResolver.Query.composePayment( |
| 87 | + undefined, |
| 88 | + { |
| 89 | + input: { |
| 90 | + workspaceId, |
| 91 | + tariffPlanId: planId, |
| 92 | + shouldSaveCard: false, |
| 93 | + }, |
| 94 | + }, |
| 95 | + mockContext |
| 96 | + ); |
| 97 | + |
| 98 | + expect(result.isCardLinkOperation).toBe(false); |
| 99 | + expect(result.plan.monthlyCharge).toBe(1000); |
| 100 | + expect(result.currency).toBe('RUB'); |
| 101 | + |
| 102 | + // Check that nextPaymentDate is one month from now |
| 103 | + const oneMonthFromNow = new Date(); |
| 104 | + |
| 105 | + oneMonthFromNow.setMonth(oneMonthFromNow.getMonth() + 1); |
| 106 | + |
| 107 | + const oneMonthFromNowStr = oneMonthFromNow.toISOString().split('T')[0]; |
| 108 | + const nextPaymentDateStr = result.nextPaymentDate.toISOString().split('T')[0]; |
| 109 | + |
| 110 | + expect(nextPaymentDateStr).toBe(oneMonthFromNowStr); |
| 111 | + }); |
| 112 | + |
| 113 | + it('should return isCardLinkOperation = true in case of active tariff plan', async () => { |
| 114 | + const userId = new ObjectId().toString(); |
| 115 | + const workspaceId = new ObjectId().toString(); |
| 116 | + const planId = new ObjectId().toString(); |
| 117 | + |
| 118 | + |
| 119 | + const plan: PlanDBScheme = { |
| 120 | + _id: new ObjectId(planId), |
| 121 | + name: 'Test Plan', |
| 122 | + monthlyCharge: 1000, |
| 123 | + monthlyChargeCurrency: 'RUB', |
| 124 | + eventsLimit: 1000, |
| 125 | + isDefault: false, |
| 126 | + isHidden: false, |
| 127 | + }; |
| 128 | + |
| 129 | + const lastChargeDate = new Date(Date.now() - 2 * 24 * 60 * 60 * 1000); // Last charge date is 2 days ago |
| 130 | + |
| 131 | + const workspace: WorkspaceDBScheme = { |
| 132 | + _id: new ObjectId(workspaceId), |
| 133 | + name: 'Test Workspace', |
| 134 | + accountId: 'test-account-id', |
| 135 | + balance: 0, |
| 136 | + billingPeriodEventsCount: 0, |
| 137 | + isBlocked: false, |
| 138 | + lastChargeDate, |
| 139 | + tariffPlanId: new ObjectId(planId), |
| 140 | + inviteHash: 'test-invite-hash', |
| 141 | + subscriptionId: undefined, |
| 142 | + }; |
| 143 | + |
| 144 | + const mockWorkspacesFactory = { |
| 145 | + findById: jest.fn().mockResolvedValue({ |
| 146 | + ...workspace, |
| 147 | + getMemberInfo: jest.fn().mockResolvedValue({ isAdmin: true }), |
| 148 | + isTariffPlanExpired: jest.fn().mockReturnValue(false), |
| 149 | + }), |
| 150 | + }; |
| 151 | + |
| 152 | + const mockPlansFactory = { |
| 153 | + findById: jest.fn().mockResolvedValue(plan), |
| 154 | + }; |
| 155 | + |
| 156 | + const mockContext: ResolverContextWithUser = { |
| 157 | + user: { |
| 158 | + id: userId, |
| 159 | + accessTokenExpired: false, |
| 160 | + }, |
| 161 | + factories: { |
| 162 | + workspacesFactory: mockWorkspacesFactory as any, |
| 163 | + plansFactory: mockPlansFactory as any, |
| 164 | + usersFactory: {} as any, |
| 165 | + projectsFactory: {} as any, |
| 166 | + businessOperationsFactory: {} as any, |
| 167 | + }, |
| 168 | + }; |
| 169 | + |
| 170 | + const result = await billingNewResolver.Query.composePayment( |
| 171 | + undefined, |
| 172 | + { |
| 173 | + input: { |
| 174 | + workspaceId, |
| 175 | + tariffPlanId: planId, |
| 176 | + shouldSaveCard: false, |
| 177 | + }, |
| 178 | + }, |
| 179 | + mockContext |
| 180 | + ); |
| 181 | + |
| 182 | + expect(result.isCardLinkOperation).toBe(true); |
| 183 | + expect(result.plan.monthlyCharge).toBe(1000); |
| 184 | + expect(result.currency).toBe('RUB'); |
| 185 | + |
| 186 | + const oneMonthFromLastChargeDate = new Date(lastChargeDate); |
| 187 | + oneMonthFromLastChargeDate.setMonth(oneMonthFromLastChargeDate.getMonth() + 1); |
| 188 | + |
| 189 | + const oneMonthFromLastChargeDateStr = oneMonthFromLastChargeDate.toISOString().split('T')[0]; |
| 190 | + const nextPaymentDateStr = result.nextPaymentDate.toISOString().split('T')[0]; |
| 191 | + expect(nextPaymentDateStr).toBe(oneMonthFromLastChargeDateStr); |
| 192 | + }); |
| 193 | + |
| 194 | + it('should return isCardLinkOperation = false in case of blocked workspace', async () => { |
| 195 | + const userId = new ObjectId().toString(); |
| 196 | + const workspaceId = new ObjectId().toString(); |
| 197 | + const planId = new ObjectId().toString(); |
| 198 | + |
| 199 | + const plan: PlanDBScheme = { |
| 200 | + _id: new ObjectId(planId), |
| 201 | + name: 'Test Plan', |
| 202 | + monthlyCharge: 1000, |
| 203 | + monthlyChargeCurrency: 'RUB', |
| 204 | + eventsLimit: 1000, |
| 205 | + isDefault: false, |
| 206 | + isHidden: false, |
| 207 | + }; |
| 208 | + |
| 209 | + const workspace: WorkspaceDBScheme = { |
| 210 | + _id: new ObjectId(workspaceId), |
| 211 | + name: 'Test Workspace', |
| 212 | + accountId: 'test-account-id', |
| 213 | + balance: 0, |
| 214 | + billingPeriodEventsCount: 0, |
| 215 | + isBlocked: true, |
| 216 | + lastChargeDate: new Date(), |
| 217 | + tariffPlanId: new ObjectId(planId), |
| 218 | + inviteHash: 'test-invite-hash', |
| 219 | + subscriptionId: undefined, |
| 220 | + }; |
| 221 | + |
| 222 | + const mockWorkspacesFactory = { |
| 223 | + findById: jest.fn().mockResolvedValue({ |
| 224 | + ...workspace, |
| 225 | + getMemberInfo: jest.fn().mockResolvedValue({ isAdmin: true }), |
| 226 | + isTariffPlanExpired: jest.fn().mockReturnValue(false), |
| 227 | + }), |
| 228 | + }; |
| 229 | + |
| 230 | + |
| 231 | + const mockPlansFactory = { |
| 232 | + findById: jest.fn().mockResolvedValue(plan), |
| 233 | + }; |
| 234 | + |
| 235 | + const mockContext: ResolverContextWithUser = { |
| 236 | + user: { |
| 237 | + id: userId, |
| 238 | + accessTokenExpired: false, |
| 239 | + }, |
| 240 | + factories: { |
| 241 | + workspacesFactory: mockWorkspacesFactory as any, |
| 242 | + plansFactory: mockPlansFactory as any, |
| 243 | + usersFactory: {} as any, |
| 244 | + projectsFactory: {} as any, |
| 245 | + businessOperationsFactory: {} as any, |
| 246 | + }, |
| 247 | + }; |
| 248 | + |
| 249 | + const result = await billingNewResolver.Query.composePayment( |
| 250 | + undefined, |
| 251 | + { |
| 252 | + input: { |
| 253 | + workspaceId, |
| 254 | + tariffPlanId: planId, |
| 255 | + shouldSaveCard: false, |
| 256 | + }, |
| 257 | + }, |
| 258 | + mockContext |
| 259 | + ); |
| 260 | + |
| 261 | + expect(result.isCardLinkOperation).toBe(false); |
| 262 | + expect(result.plan.monthlyCharge).toBe(1000); |
| 263 | + expect(result.currency).toBe('RUB'); |
| 264 | + |
| 265 | + // Check that nextPaymentDate is one month from now |
| 266 | + const oneMonthFromNow = new Date(); |
| 267 | + |
| 268 | + oneMonthFromNow.setMonth(oneMonthFromNow.getMonth() + 1); |
| 269 | + |
| 270 | + const oneMonthFromNowStr = oneMonthFromNow.toISOString().split('T')[0]; |
| 271 | + const nextPaymentDateStr = result.nextPaymentDate.toISOString().split('T')[0]; |
| 272 | + |
| 273 | + expect(nextPaymentDateStr).toBe(oneMonthFromNowStr); |
| 274 | + }); |
| 275 | + }); |
| 276 | +}) |
0 commit comments