-
Notifications
You must be signed in to change notification settings - Fork 1
Fix ba consume #126
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
Fix ba consume #126
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,12 +8,11 @@ const { TOPCODER_API_V6_BASE_URL, TGBillingAccounts } = ENV_CONFIG; | |
|
|
||
| interface LockAmountDTO { | ||
| challengeId: string; | ||
| lockAmount: number; | ||
| amount: number; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [ |
||
| } | ||
| interface ConsumeAmountDTO { | ||
| challengeId: string; | ||
| consumeAmount: number; | ||
| markup?: number; | ||
| amount: number; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [💡 |
||
| } | ||
|
|
||
| export interface BAValidation { | ||
|
|
@@ -40,7 +39,7 @@ export class BillingAccountsService { | |
| `${TOPCODER_API_V6_BASE_URL}/billing-accounts/${billingAccountId}/lock-amount`, | ||
| { | ||
| method: 'PATCH', | ||
| body: JSON.stringify({ param: dto }), | ||
| body: JSON.stringify(dto), | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [❗❗ |
||
| }, | ||
| ); | ||
| } catch (err: any) { | ||
|
|
@@ -49,7 +48,7 @@ export class BillingAccountsService { | |
| 'Failed to lock challenge amount', | ||
| ); | ||
| throw new Error( | ||
| `Budget Error: Requested amount $${dto.lockAmount} exceeds available budget for Billing Account #${billingAccountId}. | ||
| `Budget Error: Requested amount $${dto.amount} exceeds available budget for Billing Account #${billingAccountId}. | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [❗❗ |
||
| Please contact the Topcoder Project Manager for further assistance.`, | ||
| ); | ||
| } | ||
|
|
@@ -63,7 +62,7 @@ export class BillingAccountsService { | |
| `${TOPCODER_API_V6_BASE_URL}/billing-accounts/${billingAccountId}/consume-amount`, | ||
| { | ||
| method: 'PATCH', | ||
| body: JSON.stringify({ param: dto }), | ||
| body: JSON.stringify(dto), | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [❗❗ |
||
| }, | ||
| ); | ||
| } catch (err: any) { | ||
|
|
@@ -111,7 +110,7 @@ export class BillingAccountsService { | |
|
|
||
| await this.lockAmount(billingAccountId, { | ||
| challengeId: baValidation.challengeId!, | ||
| lockAmount: | ||
| amount: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [❗❗ |
||
| (rollback ? prevAmount : currAmount) * (1 + baValidation.markup!), | ||
| }); | ||
| } else if (status === ChallengeStatuses.Completed.toLowerCase()) { | ||
|
|
@@ -125,9 +124,8 @@ export class BillingAccountsService { | |
| if (currAmount !== prevAmount) { | ||
| await this.consumeAmount(billingAccountId, { | ||
| challengeId: baValidation.challengeId!, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [❗❗ |
||
| consumeAmount: | ||
| amount: | ||
| (rollback ? prevAmount : currAmount) * (1 + baValidation.markup!), | ||
| markup: baValidation.markup, | ||
| }); | ||
| } | ||
| } else if ( | ||
|
|
@@ -155,7 +153,7 @@ export class BillingAccountsService { | |
| if (currAmount !== prevAmount) { | ||
| await this.lockAmount(billingAccountId, { | ||
| challengeId: baValidation.challengeId!, | ||
| lockAmount: rollback ? prevAmount : 0, | ||
| amount: rollback ? prevAmount : 0, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [❗❗ |
||
| }); | ||
| } | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -96,6 +96,29 @@ export class TopcoderM2MService { | |
| const response = await fetch(url, finalOptions); | ||
|
|
||
| if (!response.ok) { | ||
| let responseBody: unknown; | ||
| try { | ||
| const text = await response.text(); | ||
| try { | ||
| responseBody = JSON.parse(text); | ||
| } catch { | ||
| responseBody = text; | ||
| } | ||
| } catch (e) { | ||
| responseBody = `Failed to read response body: ${e?.message ?? e}`; | ||
| } | ||
|
|
||
| this.logger.error( | ||
| 'M2M fetch failed', | ||
| { | ||
| url: String(url), | ||
| method: (finalOptions.method ?? 'GET'), | ||
| status: response.status, | ||
| statusText: response.statusText, | ||
| requestBody: (finalOptions as any).body, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [ |
||
| responseBody, | ||
| }, | ||
| ); | ||
| // Optional: You could throw a custom error here | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [ |
||
| throw new Error(`HTTP error! Status: ${response.status}`); | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.