-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathdisbursement.controller.ts
112 lines (109 loc) · 3.5 KB
/
disbursement.controller.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import { Controller } from "@nestjs/common";
import { ZeebeWorker } from "../../zeebe";
import {
ZeebeJob,
ICustomHeaders,
MustReturnJobActionAcknowledgement,
IOutputVariables,
} from "zeebe-node";
// TODO: In the upcoming tasks, either DisbursementScheduleService will be renamed at shared library
// or MSFAA related methods will move to shared library.
import { DisbursementScheduleSharedService } from "@sims/services";
import { DisbursementScheduleService } from "../../services";
import {
SaveDisbursementSchedulesJobInDTO,
AssignMSFAAJobInDTO,
} from "./disbursement.dto";
import { CustomNamedError } from "@sims/utilities";
import {
ASSESSMENT_INVALID_OPERATION_IN_THE_CURRENT_STATE,
ASSESSMENT_NOT_FOUND,
DISBURSEMENT_SCHEDULES_ALREADY_CREATED,
Workers,
} from "@sims/services/constants";
import {
ASSESSMENT_ID,
DISBURSEMENT_SCHEDULES,
} from "@sims/services/workflow/variables/assessment-gateway";
import { MaxJobsToActivate } from "../../types";
import {
DISBURSEMENT_MSFAA_ALREADY_ASSOCIATED,
DISBURSEMENT_NOT_FOUND,
} from "../../constants";
@Controller()
export class DisbursementController {
constructor(
private readonly disbursementScheduleSharedService: DisbursementScheduleSharedService,
private readonly disbursementScheduleService: DisbursementScheduleService,
) {}
/**
* Saves the disbursements schedules (1 or 2) and its values for grants and loans.
*/
@ZeebeWorker(Workers.SaveDisbursementSchedules, {
fetchVariable: [ASSESSMENT_ID, DISBURSEMENT_SCHEDULES],
maxJobsToActivate: MaxJobsToActivate.Normal,
})
async saveDisbursementSchedules(
job: Readonly<
ZeebeJob<
SaveDisbursementSchedulesJobInDTO,
ICustomHeaders,
IOutputVariables
>
>,
): Promise<MustReturnJobActionAcknowledgement> {
try {
await this.disbursementScheduleSharedService.createDisbursementSchedules(
job.variables.assessmentId,
job.variables.disbursementSchedules,
);
return job.complete();
} catch (error: unknown) {
if (error instanceof CustomNamedError) {
switch (error.name) {
case DISBURSEMENT_SCHEDULES_ALREADY_CREATED:
return job.complete();
case ASSESSMENT_NOT_FOUND:
case ASSESSMENT_INVALID_OPERATION_IN_THE_CURRENT_STATE:
return job.error(error.name, error.message);
}
}
return job.fail(
`Unexpected error while creating disbursement schedules. ${error}`,
);
}
}
/**
* Associates an MSFAA number to the disbursement(s) checking
* whatever is needed to create a new MSFAA or use an
* existing one instead.
*/
@ZeebeWorker(Workers.AssociateMSFAA, {
fetchVariable: [ASSESSMENT_ID],
maxJobsToActivate: MaxJobsToActivate.Low,
})
async associateMSFAA(
job: Readonly<
ZeebeJob<AssignMSFAAJobInDTO, ICustomHeaders, IOutputVariables>
>,
): Promise<MustReturnJobActionAcknowledgement> {
try {
await this.disbursementScheduleService.associateMSFAANumber(
job.variables.assessmentId,
);
return job.complete();
} catch (error: unknown) {
if (error instanceof CustomNamedError) {
switch (error.name) {
case DISBURSEMENT_NOT_FOUND:
return job.error(error.name, error.message);
case DISBURSEMENT_MSFAA_ALREADY_ASSOCIATED:
return job.complete();
}
}
return job.fail(
`Unexpected error while associating the MSFAA number to the disbursements. ${error}`,
);
}
}
}