Skip to content

Commit 79d260b

Browse files
committed
PM-1099 - imporve logging
1 parent 2b1170b commit 79d260b

File tree

3 files changed

+64
-33
lines changed

3 files changed

+64
-33
lines changed
Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Injectable } from '@nestjs/common';
1+
import { Injectable, Logger } from '@nestjs/common';
22
import {
33
PaymentProcessedEventData,
44
PaymentWebhookEvent,
@@ -10,6 +10,8 @@ import { PrismaService } from 'src/shared/global/prisma.service';
1010

1111
@Injectable()
1212
export class PaymentHandler {
13+
private readonly logger = new Logger(PaymentHandler.name);
14+
1315
constructor(
1416
private readonly prisma: PrismaService,
1517
private readonly paymentsService: PaymentsService,
@@ -27,37 +29,54 @@ export class PaymentHandler {
2729
const winningIds = (payload.externalId ?? '').split(',').slice(0, -1);
2830

2931
if (!winningIds.length) {
30-
console.error(
31-
`No matching payments in our db to process for incoming payment.processed with memo '${payload.memo}'.`,
32+
this.logger.error(
33+
`No valid winning IDs found in the externalId: ${payload.externalId}`,
3234
);
3335
}
3436

3537
if (payload.status !== 'processed') {
36-
await this.prisma.$transaction(async (tx) => {
38+
await this.updatePaymentStates(
39+
winningIds,
40+
payload.id,
41+
payment_status.PROCESSING,
42+
'FAILED',
43+
);
44+
45+
return;
46+
}
47+
48+
await this.updatePaymentStates(
49+
winningIds,
50+
payload.id,
51+
payment_status.PAID,
52+
'PROCESSED',
53+
);
54+
}
55+
56+
private async updatePaymentStates(
57+
winningIds: string[],
58+
paymentId: string,
59+
processingState: payment_status,
60+
releaseState: string,
61+
): Promise<void> {
62+
await this.prisma.$transaction(async (tx) => {
63+
try {
3764
await this.paymentsService.updatePaymentProcessingState(
3865
winningIds,
39-
payment_status.PROCESSING,
66+
processingState,
4067
tx,
4168
);
4269

4370
await this.paymentsService.updatePaymentReleaseState(
44-
payload.id,
45-
'FAILED',
71+
paymentId,
72+
releaseState,
4673
);
47-
});
48-
}
49-
50-
await this.prisma.$transaction(async (tx) => {
51-
await this.paymentsService.updatePaymentProcessingState(
52-
winningIds,
53-
payment_status.PAID,
54-
tx,
55-
);
56-
57-
await this.paymentsService.updatePaymentReleaseState(
58-
payload.id,
59-
'PROCESSED',
60-
);
74+
} catch (error) {
75+
this.logger.error(
76+
`Failed to update payment statuses: ${error.message}`,
77+
);
78+
throw error;
79+
}
6180
});
6281
}
6382
}

src/api/withdrawal/withdrawal.service.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Injectable } from '@nestjs/common';
1+
import { Injectable, Logger } from '@nestjs/common';
22
import { ENV_CONFIG } from 'src/config';
33
import { PrismaService } from 'src/shared/global/prisma.service';
44
import { TaxFormRepository } from '../repository/taxForm.repo';
@@ -23,6 +23,8 @@ interface ReleasableWinningRow {
2323

2424
@Injectable()
2525
export class WithdrawalService {
26+
private readonly logger = new Logger(WithdrawalService.name);
27+
2628
constructor(
2729
private readonly prisma: PrismaService,
2830
private readonly taxFormRepo: TaxFormRepository,
@@ -89,7 +91,7 @@ export class WithdrawalService {
8991
}
9092

9193
async withdraw(userId: string, userHandle: string, winningsIds: string[]) {
92-
console.info('Processing withdrawal request');
94+
this.logger.log('Processing withdrawal request');
9395
const hasActiveTaxForm = await this.taxFormRepo.hasActiveTaxForm(userId);
9496

9597
if (!hasActiveTaxForm) {
@@ -114,7 +116,7 @@ export class WithdrawalService {
114116

115117
const totalAmount = this.checkTotalAmount(winnings);
116118

117-
console.info('Begin processing payments', winnings);
119+
this.logger.log('Begin processing payments', winnings);
118120
await this.prisma.$transaction(async (tx) => {
119121
const recipient = await this.getTrolleyRecipientByUserId(userId);
120122

@@ -133,15 +135,17 @@ export class WithdrawalService {
133135
return;
134136
}
135137

136-
const updatedPaymentToProcessingError =
138+
try {
137139
await this.paymentsService.updatePaymentProcessingState(
138140
winningsIds,
139141
payment_status.PROCESSING,
140142
tx,
141143
);
142-
143-
if (updatedPaymentToProcessingError) {
144-
throw new Error('Failed to update payment processing state');
144+
} catch (e) {
145+
this.logger.error(
146+
`Failed to update payment processing state: ${e.message}`,
147+
);
148+
throw new Error('Failed to update payment processing state!');
145149
}
146150

147151
const paymentRelease = await tx.payment_releases.create({
@@ -160,7 +164,7 @@ export class WithdrawalService {
160164
},
161165
},
162166
});
163-
console.info(
167+
this.logger.log(
164168
`Payment release created successfully. ID: ${paymentRelease.payment_release_id}`,
165169
);
166170

src/shared/payments/payments.service.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,14 @@ export class PaymentsService {
119119
});
120120

121121
if (r.count === 0 || r.count !== winningsIds.length) {
122-
return 'Failed to set payment processing state! Not all rows were updated! Please check the provided winnings IDs and status.';
122+
throw new Error(
123+
'Failed to set payment processing state! Not all rows were updated! Please check the provided winnings IDs and status.',
124+
);
123125
}
124126
} catch (error) {
125-
return `Error updating payment processing state: '${error.message}'`;
127+
throw new Error(
128+
`Error updating payment processing state: '${error.message}'`,
129+
);
126130
}
127131
}
128132

@@ -143,10 +147,14 @@ export class PaymentsService {
143147
});
144148

145149
if (r.count === 0) {
146-
return 'Failed to update payment release status! No rows were updated. Please check the provided externalTransaction ID and status.';
150+
throw new Error(
151+
'Failed to update payment release status! No rows were updated. Please check the provided externalTransaction ID and status.',
152+
);
147153
}
148154
} catch (error) {
149-
return `Error updating payment release processing state: '${error.message}'`;
155+
throw new Error(
156+
`Error updating payment release processing state: '${error.message}'`,
157+
);
150158
}
151159
}
152160
}

0 commit comments

Comments
 (0)