Skip to content

Commit aec5eec

Browse files
authored
Merge pull request #51 from topcoder-platform/PM-1200_make-sure-taxform-is-active
PM-1200 - make sure taxform stays active
2 parents 78d4141 + 88277f6 commit aec5eec

File tree

1 file changed

+37
-5
lines changed

1 file changed

+37
-5
lines changed

src/api/webhooks/trolley/handlers/tax-form.handler.ts

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import { Injectable, Logger } from '@nestjs/common';
22
import { WebhookEvent } from '../../webhooks.decorators';
33
import { PrismaService } from 'src/shared/global/prisma.service';
4-
import { tax_form_status, trolley_recipient } from '@prisma/client';
4+
import {
5+
tax_form_status,
6+
trolley_recipient,
7+
user_tax_form_associations,
8+
} from '@prisma/client';
59
import { PaymentsService } from 'src/shared/payments';
610
import {
711
TrolleyTaxFormStatus,
@@ -25,16 +29,39 @@ export class TaxFormHandler {
2529
});
2630
}
2731

28-
async createOrUpdateTaxFormAssociation(
29-
taxFormId: string,
30-
recipient: trolley_recipient,
32+
/**
33+
* Determines the tax form status based on the provided existing form association
34+
* and the tax form data from the event.
35+
*
36+
* @param existingFormAssociation - The existing user tax form association, or `null` if none exists.
37+
* @param taxFormData - The data from the tax form status updated event.
38+
* @returns The determined tax form status, which can be either `ACTIVE` or `INACTIVE`.
39+
*/
40+
getTaxFormStatus(
41+
existingFormAssociation: user_tax_form_associations | null,
3142
taxFormData: TaxFormStatusUpdatedEventData,
3243
) {
33-
const taxFormStatus =
44+
const eventTaxFormStatus =
3445
taxFormData.status === TrolleyTaxFormStatus.Reviewed
3546
? tax_form_status.ACTIVE
3647
: tax_form_status.INACTIVE;
3748

49+
if (!existingFormAssociation) {
50+
return eventTaxFormStatus;
51+
}
52+
53+
// Prevent downgrading an active tax form association to inactive when the event status is "submitted"
54+
return existingFormAssociation.tax_form_status === tax_form_status.ACTIVE &&
55+
taxFormData.status === TrolleyTaxFormStatus.Submitted
56+
? tax_form_status.ACTIVE
57+
: eventTaxFormStatus;
58+
}
59+
60+
async createOrUpdateTaxFormAssociation(
61+
taxFormId: string,
62+
recipient: trolley_recipient,
63+
taxFormData: TaxFormStatusUpdatedEventData,
64+
) {
3865
const existingFormAssociation =
3966
await this.prisma.user_tax_form_associations.findFirst({
4067
where: {
@@ -43,6 +70,11 @@ export class TaxFormHandler {
4370
},
4471
});
4572

73+
const taxFormStatus = this.getTaxFormStatus(
74+
existingFormAssociation,
75+
taxFormData,
76+
);
77+
4678
// voided forms associations are removed from DB
4779
if (
4880
taxFormData.status === TrolleyTaxFormStatus.Voided &&

0 commit comments

Comments
 (0)