Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/modules/transaction/application/output.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { TransactionStatusEnum } from "../domain/validation/status";

export interface TransactionGatewayOutput {
transactionId: number;
transactionStatus: TransactionStatusEnum;
}

export interface GatewayOutput {
status: number;
statusText: string;
data: TransactionGatewayOutput;
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { AxiosResponse } from "axios";
import { inject, injectable } from "tsyringe";
import { RegisteredServicesEnum } from "../../../../shared/DIcontainer/registeredServicesEnum";
import { BaseClass } from "../../../../shared/utils/log-prefix.class";
import { Logger } from "../../../../shared/utils/logger";
import { TransactionService } from "../../domain/services/transaction.service";
import { WebhookService } from "../../domain/services/webhook.service";
import { TransactionInput } from "../input";
import { GatewayOutput } from "../output";

@injectable()
export class ProcessTransactionUseCase extends BaseClass {
Expand All @@ -23,7 +23,7 @@ export class ProcessTransactionUseCase extends BaseClass {
public async run(
input: TransactionInput,
webhookUrl: string
): Promise<AxiosResponse> {
): Promise<GatewayOutput> {
this.appLogger.info(
`${this.logPrefix} processing ${JSON.stringify(input)}`
);
Expand All @@ -35,6 +35,13 @@ export class ProcessTransactionUseCase extends BaseClass {
this.appLogger.info(
`${this.logPrefix} received the ${resp.status}: ${resp.statusText}`
);
return resp;
return {
status: resp.status,
statusText: resp.statusText,
data: {
transactionId: transaction.id,
transactionStatus: transaction.status,
},
};
}
}
2 changes: 1 addition & 1 deletion src/modules/transaction/domain/services/webhook.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export class WebhookService extends BaseClass {

private buildPayload(transaction: Transaction): WebhookPayload {
return {
transactionId: transaction.id,
id: transaction.id,
amount: transaction.amount,
currency: transaction.currency,
status: transaction.status,
Expand Down
26 changes: 21 additions & 5 deletions src/modules/transaction/interfaces/transaction.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import { ValidationError } from "../../../shared/utils/error";
import { BaseClass } from "../../../shared/utils/log-prefix.class";
import { Logger } from "../../../shared/utils/logger";
import { GatewayOutput } from "../application/output";
import { ProcessTransactionUseCase } from "../application/process-transaction-use-case/process-transaction.use-case";
import { CURRENCY_TYPE } from "../domain/validation/currency";
import { TRANSACTION_STATUS_TYPE } from "../domain/validation/status";
Expand All @@ -29,22 +30,36 @@
): Promise<void> => {
try {
this.appLogger.info(
`${this.logPrefix} - ${req.url} - Processing transaction: ${req.body}`
`${this.logPrefix} - ${
req.webhookUrl
} - Processing transaction: ${JSON.stringify(req.safeFields, null, 2)}`
);
const input = req.safeFields!;
const webhookUrl = req.webhookUrl!;
this.appLogger.info(
`${this.logPrefix} - ${req.url} - Using webhook URL: ${webhookUrl}`
);
const result = await this.processTransactionUseCase.run(
const result: GatewayOutput = await this.processTransactionUseCase.run(
input,
webhookUrl
);
resp.status(HttpStatusCode.Ok).send({ result });
this.appLogger.info(
`${this.logPrefix} - ${req.url} - Sending result: ${JSON.stringify(
result
)}`
);
resp.status(HttpStatusCode.Ok).send({
result,
});
} catch (error) {
this.appLogger.error(
`[${this.constructor.name}] Error processing health check request: ${error}`
`[${this.logPrefix}] Error processing health check request: ${
error instanceof Error ? error.message : String(error)

Check warning on line 57 in src/modules/transaction/interfaces/transaction.controller.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch
}`
);
if (error instanceof Error && error.stack) {
this.appLogger.error(error.stack);
}
next(error);
}
};
Expand Down Expand Up @@ -96,8 +111,9 @@
} catch (err) {
if (err instanceof Error) {
next(new ValidationError(err));
} else {
next(err);

Check warning on line 115 in src/modules/transaction/interfaces/transaction.controller.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
}

Check warning on line 116 in src/modules/transaction/interfaces/transaction.controller.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch
next(err);
}
};

Expand Down
40 changes: 31 additions & 9 deletions src/shared/clients/webhook/webhook.client.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import axios, { AxiosResponse, HttpStatusCode } from "axios";
import axios, { AxiosResponse } from "axios";
import { inject, injectable } from "tsyringe";
import { RegisteredServicesEnum } from "../../DIcontainer/registeredServicesEnum";
import { BaseClass } from "../../utils/log-prefix.class";
Expand All @@ -19,17 +19,39 @@
`${this.logPrefix} Sending request to Token Wallet Service at ${meta.webhookUrl}`
);

const resp = await axios.post(meta.webhookUrl, meta.payload);
try {
const resp = await axios.post(meta.webhookUrl, meta.payload);

Check warning on line 23 in src/shared/clients/webhook/webhook.client.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement

this.appLogger.info(
`${this.logPrefix} Received response with status: ${resp.status}`
);
this.appLogger.info(
`${this.logPrefix} Received response with status: ${resp.status}`
);

Check warning on line 27 in src/shared/clients/webhook/webhook.client.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement

if (resp.status !== HttpStatusCode.Ok) {
throw new Error(
`${this.logPrefix} Failed to send webhook: ${resp.statusText}`
// Log the response body for debugging
this.appLogger.info(
`${this.logPrefix} Response body: ${JSON.stringify(resp.data)}`
);

Check warning on line 32 in src/shared/clients/webhook/webhook.client.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement

return resp;

Check warning on line 34 in src/shared/clients/webhook/webhook.client.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
} catch (error: any) {
this.appLogger.error(
`${this.logPrefix} Error sending webhook request: ${error.message}`
);

Check warning on line 38 in src/shared/clients/webhook/webhook.client.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement

// If it's an axios error with response, return the response
if (error.response) {
this.appLogger.info(
`${this.logPrefix} Received error response with status: ${error.response.status}`
);

Check warning on line 44 in src/shared/clients/webhook/webhook.client.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
this.appLogger.info(
`${this.logPrefix} Error response body: ${JSON.stringify(
error.response.data
)}`
);

Check warning on line 49 in src/shared/clients/webhook/webhook.client.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
return error.response;

Check warning on line 50 in src/shared/clients/webhook/webhook.client.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
}

Check warning on line 51 in src/shared/clients/webhook/webhook.client.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement

Check warning on line 51 in src/shared/clients/webhook/webhook.client.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch

// If it's a network error or other issue, re-throw
throw error;

Check warning on line 54 in src/shared/clients/webhook/webhook.client.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
}

Check warning on line 55 in src/shared/clients/webhook/webhook.client.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
return resp;
}
}
2 changes: 1 addition & 1 deletion src/shared/clients/webhook/webhook.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { CurrencyEnum } from "../../../modules/transaction/domain/validation/cur
import { TransactionStatusEnum } from "../../../modules/transaction/domain/validation/status";

export interface WebhookPayload {
transactionId: number;
id: number;
status: TransactionStatusEnum;
amount: number;
currency: CurrencyEnum;
Expand Down
2 changes: 1 addition & 1 deletion tests/modules/transaction/webhook.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ describe("WebhookService", () => {
new Date()
);
const payload = (service as any).buildPayload(tx);
expect(payload.transactionId).toBe(1);
expect(payload.id).toBe(1);
expect(payload.amount).toBe(100);
});
});