-
Notifications
You must be signed in to change notification settings - Fork 1
PS-469 - add more logs #123
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
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 |
|---|---|---|
|
|
@@ -79,34 +79,58 @@ export class AdminService { | |
|
|
||
| let needsReconciliation = false; | ||
| const winningsId = body.winningsId; | ||
| this.logger.log( | ||
| `updateWinnings called by ${userId} for winningsId=${winningsId}`, | ||
| ); | ||
| this.logger.log(`updateWinnings payload: ${JSON.stringify(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. [ |
||
|
|
||
| try { | ||
| const payments = await this.getPaymentsByWinningsId( | ||
| winningsId, | ||
| body.paymentId, | ||
| ); | ||
|
|
||
| this.logger.log( | ||
|
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. [ |
||
| `Found ${payments.length} payment(s) for winningsId=${winningsId}`, | ||
| ); | ||
| if (payments.length === 0) { | ||
| this.logger.warn( | ||
| `No payments found for winningsId=${winningsId}, paymentId=${body.paymentId}`, | ||
| ); | ||
| throw new NotFoundException('failed to get current payments'); | ||
| } | ||
|
|
||
| let releaseDate; | ||
| if (body.paymentStatus) { | ||
| releaseDate = await this.getPaymentReleaseDateByWinningsId(winningsId); | ||
| this.logger.log( | ||
|
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. [ |
||
| `Payment release date for winningsId=${winningsId}: ${releaseDate}`, | ||
| ); | ||
| } | ||
|
|
||
| const transactions: (( | ||
| tx: Prisma.TransactionClient, | ||
| ) => Promise<unknown>)[] = []; | ||
| const now = new Date().getTime(); | ||
|
|
||
| // iterate payments and build transaction list | ||
| payments.forEach((payment) => { | ||
| this.logger.log( | ||
|
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. [ |
||
| `Processing payment ${payment.payment_id} (installment ${payment.installment_number}) with current status=${payment.payment_status}`, | ||
vas3a marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ); | ||
|
|
||
| if ( | ||
| payment.payment_status && | ||
| payment.payment_status === PaymentStatus.CANCELLED | ||
| ) { | ||
| this.logger.warn( | ||
| `Attempt to update cancelled payment ${payment.payment_id} — rejecting`, | ||
| ); | ||
| throw new BadRequestException('cannot update cancelled winnings'); | ||
| } | ||
|
|
||
| let version = payment.version ?? 1; | ||
| const queuedActions: string[] = []; | ||
|
|
||
| if (body.description) { | ||
| transactions.push((tx) => | ||
|
|
@@ -129,6 +153,9 @@ export class AdminService { | |
| }, | ||
| }), | ||
| ); | ||
| queuedActions.push( | ||
| `update description -> "${body.description}" (version ${version})`, | ||
| ); | ||
|
|
||
| if (payment.installment_number === 1) { | ||
| transactions.push((tx) => | ||
|
|
@@ -140,6 +167,7 @@ export class AdminService { | |
| tx, | ||
| ), | ||
| ); | ||
| queuedActions.push('add audit for description change'); | ||
vas3a marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
|
|
||
|
|
@@ -171,6 +199,9 @@ export class AdminService { | |
| payment.payment_status !== PaymentStatus.ON_HOLD_ADMIN && | ||
| payment.payment_status !== PaymentStatus.PAID | ||
| ) { | ||
| this.logger.warn( | ||
vas3a marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| `Invalid attempt to set OWED for payment ${payment.payment_id} when not on hold admin or paid`, | ||
| ); | ||
| throw new BadRequestException( | ||
| "cannot put a payment back to owed unless it is on hold by an admin, or it's been paid", | ||
| ); | ||
|
|
@@ -180,13 +211,19 @@ export class AdminService { | |
| break; | ||
|
|
||
| default: | ||
| this.logger.warn( | ||
| `Invalid payment status provided: ${body.paymentStatus}`, | ||
vas3a marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ); | ||
| throw new BadRequestException('invalid payment status provided'); | ||
| } | ||
|
|
||
| if ( | ||
| errMessage && | ||
| payment.payment_status === PaymentStatus.PROCESSING | ||
| ) { | ||
| this.logger.warn( | ||
| `Rejected status change for ${payment.payment_id}: ${errMessage}`, | ||
vas3a marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ); | ||
| throw new BadRequestException(errMessage); | ||
| } | ||
|
|
||
|
|
@@ -201,11 +238,17 @@ export class AdminService { | |
| tx, | ||
| ), | ||
| ); | ||
| queuedActions.push( | ||
| `update status ${payment.payment_status} -> ${body.paymentStatus}`, | ||
vas3a marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ); | ||
|
|
||
| paymentStatus = body.paymentStatus as PaymentStatus; | ||
|
|
||
| if (body.paymentStatus === PaymentStatus.OWED) { | ||
| needsReconciliation = true; | ||
| this.logger.log( | ||
|
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. [ |
||
| `Payment ${payment.payment_id} marked OWED; will trigger reconciliation later`, | ||
vas3a marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ); | ||
| } | ||
|
|
||
| if (payment.installment_number === 1) { | ||
|
|
@@ -218,6 +261,7 @@ export class AdminService { | |
| tx, | ||
| ), | ||
| ); | ||
| queuedActions.push('add audit for status change'); | ||
vas3a marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
|
|
||
|
|
@@ -232,6 +276,9 @@ export class AdminService { | |
| PaymentStatus.ON_HOLD_ADMIN, | ||
| ].includes(paymentStatus) | ||
| ) { | ||
| this.logger.warn( | ||
| `Cannot update release date for payment ${payment.payment_id} in status ${paymentStatus}`, | ||
| ); | ||
| throw new BadRequestException( | ||
| `Cannot update release date for payment unless it's in one of the states: ${[ | ||
| PaymentStatus.OWED, | ||
|
|
@@ -251,6 +298,9 @@ export class AdminService { | |
| tx, | ||
| ), | ||
| ); | ||
| queuedActions.push( | ||
| `update release_date ${payment.release_date?.toISOString()} -> ${newReleaseDate.toISOString()}`, | ||
vas3a marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ); | ||
|
|
||
| if (payment.installment_number === 1) { | ||
| transactions.push((tx) => | ||
|
|
@@ -262,6 +312,7 @@ export class AdminService { | |
| tx, | ||
| ), | ||
| ); | ||
| queuedActions.push('add audit for release date change'); | ||
vas3a marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
|
|
||
|
|
@@ -297,6 +348,10 @@ export class AdminService { | |
| tx, | ||
| ), | ||
| ); | ||
|
|
||
| queuedActions.push( | ||
| `update amounts -> ${body.paymentAmount.toFixed(2)} (installment 1)`, | ||
vas3a marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ); | ||
| } else { | ||
| transactions.push((tx) => | ||
| this.updatePaymentAmount( | ||
|
|
@@ -310,17 +365,35 @@ export class AdminService { | |
| tx, | ||
| ), | ||
| ); | ||
| queuedActions.push( | ||
| `update amounts -> total ${body.paymentAmount.toFixed(2)} (installment ${payment.installment_number})`, | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| this.logger.log( | ||
|
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. [ |
||
| `Queued ${queuedActions.length} action(s) for payment ${payment.payment_id}: ${queuedActions.join( | ||
| ' ; ', | ||
| )}`, | ||
| ); | ||
| }); | ||
|
|
||
| this.logger.log( | ||
|
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. [ |
||
| `Executing ${transactions.length} transaction step(s) for winningsId=${winningsId}`, | ||
| ); | ||
|
|
||
| // Run all transaction tasks in a single prisma transaction | ||
| await this.prisma.$transaction(async (tx) => { | ||
| for (const transaction of transactions) { | ||
| await transaction(tx); | ||
| for (let i = 0; i < transactions.length; i++) { | ||
| this.logger.log(`Executing transaction ${i + 1}/${transactions.length}`); | ||
|
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. [ |
||
| await transactions[i](tx); | ||
| } | ||
| }); | ||
|
|
||
| this.logger.log( | ||
|
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. [ |
||
| `Successfully executed transactions for winningsId=${winningsId}`, | ||
| ); | ||
|
|
||
| if (needsReconciliation) { | ||
| const winning = await this.prisma.winnings.findFirst({ | ||
| select: { | ||
|
|
@@ -332,16 +405,32 @@ export class AdminService { | |
| }); | ||
|
|
||
| if (winning?.winner_id) { | ||
| this.logger.log( | ||
|
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. [ |
||
| `Triggering payments reconciliation for user ${winning.winner_id}`, | ||
vas3a marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ); | ||
| await this.paymentsService.reconcileUserPayments(winning.winner_id); | ||
| this.logger.log( | ||
|
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. [ |
||
| `Reconciliation triggered for user ${winning.winner_id}`, | ||
vas3a marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ); | ||
| } else { | ||
| this.logger.warn( | ||
| `Needs reconciliation but no winner_id found for winningsId=${winningsId}`, | ||
vas3a marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ); | ||
| } | ||
| } | ||
|
|
||
| result.data = 'Successfully updated winnings'; | ||
| this.logger.log( | ||
|
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. [ |
||
| `updateWinnings completed for winningsId=${winningsId}: ${result.data}`, | ||
| ); | ||
| } catch (error) { | ||
| if ( | ||
| error instanceof NotFoundException || | ||
| error instanceof BadRequestException | ||
| ) { | ||
| this.logger.warn( | ||
| `updateWinnings validation error for winningsId=${winningsId}: ${error.message}`, | ||
vas3a marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ); | ||
| throw error; | ||
| } | ||
| this.logger.error('Updating winnings failed', error); | ||
vas3a marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -63,6 +63,10 @@ export class TaxFormHandler { | |
| recipient: trolley_recipient, | ||
| taxFormData: TaxFormStatusUpdatedEventData, | ||
| ) { | ||
| this.logger.log( | ||
| `Processing tax form '${taxFormId}' for user '${recipient.user_id}' (recipient trolley id: '${recipient.trolley_id}')`, | ||
| ); | ||
|
|
||
| const existingFormAssociation = | ||
| await this.prisma.user_tax_form_associations.findFirst({ | ||
| where: { | ||
|
|
@@ -71,42 +75,72 @@ export class TaxFormHandler { | |
| }, | ||
| }); | ||
|
|
||
| if (existingFormAssociation) { | ||
| this.logger.log( | ||
|
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. [ |
||
| `Found existing association id='${existingFormAssociation.id}' status='${existingFormAssociation.tax_form_status}' date_filed='${existingFormAssociation.date_filed}'`, | ||
| ); | ||
| } else { | ||
| this.logger.log('No existing tax form association found'); | ||
|
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. [ |
||
| } | ||
|
|
||
| const taxFormStatus = this.getTaxFormStatus( | ||
| existingFormAssociation, | ||
| taxFormData, | ||
| ); | ||
| this.logger.log(`Determined tax form status: '${taxFormStatus}'`); | ||
|
|
||
| // voided forms associations are removed from DB | ||
| if ( | ||
| taxFormData.status === TrolleyTaxFormStatus.Voided && | ||
| existingFormAssociation | ||
| ) { | ||
| return this.prisma.user_tax_form_associations.deleteMany({ | ||
| this.logger.log( | ||
| `Tax form '${taxFormId}' marked Voided — removing association(s) for user '${recipient.user_id}'`, | ||
| ); | ||
| const result = await this.prisma.user_tax_form_associations.deleteMany({ | ||
vas3a marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| where: { | ||
| user_id: recipient.user_id, | ||
| tax_form_id: taxFormId, | ||
| }, | ||
| }); | ||
| this.logger.log( | ||
| `Deleted ${result.count ?? 0} association(s) for user '${recipient.user_id}' taxFormId '${taxFormId}'`, | ||
| ); | ||
| return result; | ||
| } | ||
|
|
||
| if (!existingFormAssociation) { | ||
| return this.prisma.user_tax_form_associations.create({ | ||
| this.logger.log( | ||
| `Creating tax form association for user '${recipient.user_id}' taxFormId '${taxFormId}'`, | ||
| ); | ||
| const created = await this.prisma.user_tax_form_associations.create({ | ||
vas3a marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| data: { | ||
| user_id: recipient.user_id, | ||
| tax_form_status: taxFormStatus, | ||
| date_filed: taxFormData.signedAt, | ||
| tax_form_id: taxFormId, | ||
| }, | ||
| }); | ||
| this.logger.log( | ||
| `Created association id='${created.id}' tax_form_status='${created.tax_form_status}'`, | ||
vas3a marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ); | ||
| return created; | ||
| } | ||
|
|
||
| return this.prisma.user_tax_form_associations.update({ | ||
| where: { id: existingFormAssociation?.id }, | ||
| this.logger.log( | ||
| `Updating association id='${existingFormAssociation.id}' for user '${recipient.user_id}'`, | ||
vas3a marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ); | ||
| const updated = await this.prisma.user_tax_form_associations.update({ | ||
| where: { id: existingFormAssociation.id }, | ||
| data: { | ||
| tax_form_status: taxFormStatus, | ||
| date_filed: taxFormData.signedAt, | ||
| }, | ||
| }); | ||
| this.logger.log( | ||
| `Updated association id='${updated.id}' tax_form_status='${updated.tax_form_status}'`, | ||
vas3a marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ); | ||
| return updated; | ||
| } | ||
|
|
||
| /** | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[⚠️
maintainability]Consider using
logger.infofor informational messages instead oflogger.logto maintain consistency and clarity in log levels.