-
Notifications
You must be signed in to change notification settings - Fork 2
fix(payments): Do not unblock workspace in case of card linking payment #545
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 9 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
674f7a1
Add condition
TatianaFomina 9fb1d66
Bump version up to 1.1.43
github-actions[bot] 5c89c0c
Merge branch 'master' into fix/card-linking
TatianaFomina 57795b8
Add test and condition
TatianaFomina bf7bd38
Add isBlocked check
TatianaFomina 05fa6ee
Lint
TatianaFomina 7237efa
Add tests
TatianaFomina 4456afe
Cleanup
TatianaFomina cd50987
Add comment and simplify test
TatianaFomina 4c1e6f5
Lint
TatianaFomina ac04e0d
Update test
TatianaFomina File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -88,7 +88,7 @@ | |
| return router; | ||
| } | ||
|
|
||
| /** | ||
|
Check warning on line 91 in src/billing/cloudpayments.ts
|
||
| * Generates invoice id for payment | ||
| * | ||
| * @param tariffPlan - tariff plan to generate invoice id | ||
|
|
@@ -205,7 +205,7 @@ | |
| telegram.sendMessage(`✅ [Billing / Check] All checks passed successfully «${workspace.name}»`, TelegramBotURLs.Money) | ||
| .catch(e => console.error('Error while sending message to Telegram: ' + e)); | ||
|
|
||
| HawkCatcher.send(new Error('[Billing / Check] All checks passed successfully'), body as any); | ||
|
|
||
| res.json({ | ||
| code: CheckCodes.SUCCESS, | ||
|
|
@@ -272,7 +272,10 @@ | |
|
|
||
| try { | ||
| await businessOperation.setStatus(BusinessOperationStatus.Confirmed); | ||
| await workspace.changePlan(tariffPlan._id); | ||
|
|
||
| if (!data.isCardLinkOperation) { | ||
| await workspace.changePlan(tariffPlan._id); | ||
| } | ||
|
|
||
| const subscriptionId = body.SubscriptionId; | ||
|
|
||
|
|
@@ -339,17 +342,22 @@ | |
| * } | ||
| */ | ||
|
|
||
| try { | ||
| await publish('cron-tasks', 'cron-tasks/limiter', JSON.stringify({ | ||
| type: 'unblock-workspace', | ||
| workspaceId: data.workspaceId, | ||
| })); | ||
| } catch (e) { | ||
| const error = e as Error; | ||
| /** | ||
| * If it is not a card linking operation then unblock workspace | ||
| */ | ||
| if (!data.isCardLinkOperation) { | ||
| try { | ||
| await publish('cron-tasks', 'cron-tasks/limiter', JSON.stringify({ | ||
| type: 'unblock-workspace', | ||
| workspaceId: data.workspaceId, | ||
| })); | ||
| } catch (e) { | ||
| const error = e as Error; | ||
|
|
||
| this.sendError(res, PayCodes.SUCCESS, `[Billing / Pay] Error while sending task to limiter worker ${error.toString()}`, body); | ||
| this.sendError(res, PayCodes.SUCCESS, `[Billing / Pay] Error while sending task to limiter worker ${error.toString()}`, body); | ||
|
|
||
| return; | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| try { | ||
|
|
@@ -547,7 +555,7 @@ | |
|
|
||
| this.handleSendingToTelegramError(telegram.sendMessage(`❌ [Billing / Fail] Transaction failed for «${workspace.name}»`, TelegramBotURLs.Money)); | ||
|
|
||
| HawkCatcher.send(new Error('[Billing / Fail] Transaction failed'), body as any); | ||
|
|
||
| res.json({ | ||
| code: FailCodes.SUCCESS, | ||
|
|
@@ -729,7 +737,7 @@ | |
| * @param errorText - error description | ||
| * @param backtrace - request data and error data | ||
| */ | ||
| private sendError(res: express.Response, errorCode: CheckCodes | PayCodes | FailCodes | RecurrentCodes, errorText: string, backtrace: { [key: string]: any }): void { | ||
| res.json({ | ||
| code: errorCode, | ||
| }); | ||
|
|
@@ -794,7 +802,7 @@ | |
| promise.catch(e => console.error('Error while sending message to Telegram: ' + e)); | ||
| } | ||
|
|
||
| /** | ||
|
Check warning on line 805 in src/billing/cloudpayments.ts
|
||
| * Parses body and returns card data | ||
| * @param request - request body to parse | ||
| */ | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,208 @@ | ||
| import '../../src/env-test'; | ||
| import { ObjectId } from 'mongodb'; | ||
| import { PlanDBScheme, WorkspaceDBScheme } from '@hawk.so/types'; | ||
| import billingNewResolver from '../../src/resolvers/billingNew'; | ||
| import { ResolverContextWithUser } from '../../src/types/graphql'; | ||
|
|
||
| // Set environment variables for test | ||
| process.env.JWT_SECRET_BILLING_CHECKSUM = 'checksum_secret'; | ||
| process.env.JWT_SECRET_ACCESS_TOKEN = 'belarus'; | ||
| process.env.JWT_SECRET_REFRESH_TOKEN = 'abacaba'; | ||
| process.env.JWT_SECRET_PROJECT_TOKEN = 'qwerty'; | ||
|
|
||
| /** | ||
| * Creates test data and mocks for composePayment tests | ||
| */ | ||
| function createComposePaymentTestSetup(options: { | ||
| isTariffPlanExpired?: boolean; | ||
| isBlocked?: boolean; | ||
| lastChargeDate?: Date; | ||
| planMonthlyCharge?: number; | ||
| planCurrency?: string; | ||
| }) { | ||
| const { | ||
| isTariffPlanExpired = false, | ||
| isBlocked = false, | ||
| lastChargeDate = new Date(), | ||
| planMonthlyCharge = 1000, | ||
| planCurrency = 'RUB' | ||
| } = options; | ||
|
|
||
| const userId = new ObjectId().toString(); | ||
| const workspaceId = new ObjectId().toString(); | ||
| const planId = new ObjectId().toString(); | ||
|
|
||
| const plan: PlanDBScheme = { | ||
| _id: new ObjectId(planId), | ||
| name: 'Test Plan', | ||
| monthlyCharge: planMonthlyCharge, | ||
| monthlyChargeCurrency: planCurrency, | ||
| eventsLimit: 1000, | ||
| isDefault: false, | ||
| isHidden: false, | ||
| }; | ||
|
|
||
| const workspace: WorkspaceDBScheme = { | ||
| _id: new ObjectId(workspaceId), | ||
| name: 'Test Workspace', | ||
| accountId: 'test-account-id', | ||
| balance: 0, | ||
| billingPeriodEventsCount: 0, | ||
| isBlocked, | ||
| lastChargeDate, | ||
| tariffPlanId: new ObjectId(planId), | ||
| inviteHash: 'test-invite-hash', | ||
| subscriptionId: undefined, | ||
| }; | ||
|
|
||
| // Mock workspaces factory | ||
| const mockWorkspacesFactory = { | ||
| findById: jest.fn().mockResolvedValue({ | ||
| ...workspace, | ||
| getMemberInfo: jest.fn().mockResolvedValue({ isAdmin: true }), | ||
| isTariffPlanExpired: jest.fn().mockReturnValue(isTariffPlanExpired), | ||
| isBlocked, | ||
| }), | ||
| }; | ||
|
|
||
| // Mock plans factory | ||
| const mockPlansFactory = { | ||
| findById: jest.fn().mockResolvedValue(plan), | ||
| }; | ||
|
|
||
| const mockContext: ResolverContextWithUser = { | ||
| user: { | ||
| id: userId, | ||
| accessTokenExpired: false, | ||
| }, | ||
| factories: { | ||
| workspacesFactory: mockWorkspacesFactory as any, | ||
| plansFactory: mockPlansFactory as any, | ||
| usersFactory: {} as any, | ||
| projectsFactory: {} as any, | ||
| businessOperationsFactory: {} as any, | ||
| }, | ||
| }; | ||
|
|
||
| return { | ||
| userId, | ||
| workspaceId, | ||
| planId, | ||
| plan, | ||
| workspace, | ||
| mockContext, | ||
| mockWorkspacesFactory, | ||
| mockPlansFactory, | ||
| }; | ||
| } | ||
|
|
||
| describe('GraphQLBillingNew', () => { | ||
| describe('composePayment', () => { | ||
| it('should return isCardLinkOperation = false in case of expired tariff plan', async () => { | ||
| // Create 2 months ago date | ||
| const expiredDate = new Date(); | ||
| expiredDate.setMonth(expiredDate.getMonth() - 2); | ||
|
|
||
| const { mockContext, planId, workspaceId } = createComposePaymentTestSetup({ | ||
| isTariffPlanExpired: true, | ||
| isBlocked: false, | ||
| lastChargeDate: expiredDate, | ||
| }); | ||
|
|
||
| // Call composePayment resolver | ||
| const result = await billingNewResolver.Query.composePayment( | ||
| undefined, | ||
| { | ||
| input: { | ||
| workspaceId, | ||
| tariffPlanId: planId, | ||
| shouldSaveCard: false, | ||
| }, | ||
| }, | ||
| mockContext | ||
| ); | ||
|
|
||
| expect(result.isCardLinkOperation).toBe(false); | ||
| expect(result.plan.monthlyCharge).toBe(1000); | ||
| expect(result.currency).toBe('RUB'); | ||
|
|
||
| // Check that nextPaymentDate is one month from now | ||
| const oneMonthFromNow = new Date(); | ||
|
|
||
| oneMonthFromNow.setMonth(oneMonthFromNow.getMonth() + 1); | ||
|
|
||
| const oneMonthFromNowStr = oneMonthFromNow.toISOString().split('T')[0]; | ||
| const nextPaymentDateStr = result.nextPaymentDate.toISOString().split('T')[0]; | ||
|
|
||
| expect(nextPaymentDateStr).toBe(oneMonthFromNowStr); | ||
| }); | ||
|
|
||
| it('should return isCardLinkOperation = true in case of active tariff plan', async () => { | ||
| // Create 2 days ago date | ||
| const lastChargeDate = new Date(Date.now() - 2 * 24 * 60 * 60 * 1000); | ||
|
|
||
| const { mockContext, planId, workspaceId, workspace } = createComposePaymentTestSetup({ | ||
| isTariffPlanExpired: false, | ||
| isBlocked: false, | ||
| lastChargeDate, | ||
| }); | ||
|
|
||
| const result = await billingNewResolver.Query.composePayment( | ||
| undefined, | ||
| { | ||
| input: { | ||
| workspaceId, | ||
| tariffPlanId: planId, | ||
| shouldSaveCard: false, | ||
| }, | ||
| }, | ||
| mockContext | ||
| ); | ||
|
|
||
| expect(result.isCardLinkOperation).toBe(true); | ||
| expect(result.plan.monthlyCharge).toBe(1000); | ||
| expect(result.currency).toBe('RUB'); | ||
|
|
||
| const oneMonthFromLastChargeDate = new Date(workspace.lastChargeDate); | ||
| oneMonthFromLastChargeDate.setMonth(oneMonthFromLastChargeDate.getMonth() + 1); | ||
|
|
||
| const oneMonthFromLastChargeDateStr = oneMonthFromLastChargeDate.toISOString().split('T')[0]; | ||
| const nextPaymentDateStr = result.nextPaymentDate.toISOString().split('T')[0]; | ||
| expect(nextPaymentDateStr).toBe(oneMonthFromLastChargeDateStr); | ||
| }); | ||
|
|
||
| it('should return isCardLinkOperation = false in case of blocked workspace', async () => { | ||
| const { mockContext, planId, workspaceId } = createComposePaymentTestSetup({ | ||
| isTariffPlanExpired: false, | ||
| isBlocked: true, | ||
| lastChargeDate: new Date(), | ||
| }); | ||
|
|
||
| const result = await billingNewResolver.Query.composePayment( | ||
| undefined, | ||
| { | ||
| input: { | ||
| workspaceId, | ||
| tariffPlanId: planId, | ||
| shouldSaveCard: false, | ||
| }, | ||
| }, | ||
| mockContext | ||
| ); | ||
|
|
||
| expect(result.isCardLinkOperation).toBe(false); | ||
| expect(result.plan.monthlyCharge).toBe(1000); | ||
| expect(result.currency).toBe('RUB'); | ||
|
|
||
| // Check that nextPaymentDate is one month from now | ||
| const oneMonthFromNow = new Date(); | ||
|
|
||
| oneMonthFromNow.setMonth(oneMonthFromNow.getMonth() + 1); | ||
|
|
||
| const oneMonthFromNowStr = oneMonthFromNow.toISOString().split('T')[0]; | ||
| const nextPaymentDateStr = result.nextPaymentDate.toISOString().split('T')[0]; | ||
|
|
||
| expect(nextPaymentDateStr).toBe(oneMonthFromNowStr); | ||
| }); | ||
| }); | ||
| }) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.