1
1
import { Injectable , Logger } from '@nestjs/common' ;
2
2
import { WebhookEvent } from '../../webhooks.decorators' ;
3
3
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' ;
5
9
import { PaymentsService } from 'src/shared/payments' ;
6
10
import {
7
11
TrolleyTaxFormStatus ,
@@ -25,16 +29,39 @@ export class TaxFormHandler {
25
29
} ) ;
26
30
}
27
31
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 ,
31
42
taxFormData : TaxFormStatusUpdatedEventData ,
32
43
) {
33
- const taxFormStatus =
44
+ const eventTaxFormStatus =
34
45
taxFormData . status === TrolleyTaxFormStatus . Reviewed
35
46
? tax_form_status . ACTIVE
36
47
: tax_form_status . INACTIVE ;
37
48
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
+ ) {
38
65
const existingFormAssociation =
39
66
await this . prisma . user_tax_form_associations . findFirst ( {
40
67
where : {
@@ -43,6 +70,11 @@ export class TaxFormHandler {
43
70
} ,
44
71
} ) ;
45
72
73
+ const taxFormStatus = this . getTaxFormStatus (
74
+ existingFormAssociation ,
75
+ taxFormData ,
76
+ ) ;
77
+
46
78
// voided forms associations are removed from DB
47
79
if (
48
80
taxFormData . status === TrolleyTaxFormStatus . Voided &&
0 commit comments