-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontroller.ts
More file actions
190 lines (153 loc) · 7.55 KB
/
controller.ts
File metadata and controls
190 lines (153 loc) · 7.55 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import { Context, TypedResponse } from "hono";
import { validate } from "uuid";
import {
CreateClaimDTOSchema,
CreateClaimResponse,
DeleteClaimResponse,
DeletePurchaseLineItemResponse,
GetClaimByIdResponse,
GetClaimsByCompanyIdResponse,
GetPurchaseLineItemsForClaimResponse,
LinkClaimToLineItemDTOSchema,
LinkClaimToLineItemResponse,
LinkClaimToPurchaseDTOSchema,
LinkClaimToPurchaseResponse,
UpdateClaimStatusDTOSchema,
UpdateClaimStatusResponse,
} from "../../types/Claim";
import { withControllerErrorHandling } from "../../utilities/error";
import { ControllerResponse } from "../../utilities/response";
import { IClaimService } from "./service";
import { ClaimPDFGenerationResponse } from "./types";
export interface IClaimController {
getClaimByCompanyId(_ctx: Context): ControllerResponse<TypedResponse<GetClaimsByCompanyIdResponse, 200>>;
createClaim(_ctx: Context): ControllerResponse<TypedResponse<CreateClaimResponse, 201>>;
deleteClaim(_ctx: Context): ControllerResponse<TypedResponse<DeleteClaimResponse, 200>>;
linkClaimToLineItem(_ctx: Context): ControllerResponse<TypedResponse<LinkClaimToLineItemResponse, 201>>;
linkClaimToPurchaseItems(_ctx: Context): ControllerResponse<TypedResponse<LinkClaimToPurchaseResponse, 201>>;
getLinkedPurchaseLineItems(
_ctx: Context
): ControllerResponse<TypedResponse<GetPurchaseLineItemsForClaimResponse, 200>>;
deletePurchaseLineItem(_ctx: Context): ControllerResponse<TypedResponse<DeletePurchaseLineItemResponse, 200>>;
createClaimPDF(_ctx: Context): ControllerResponse<TypedResponse<ClaimPDFGenerationResponse, 200>>;
getClaimById(_ctx: Context): ControllerResponse<TypedResponse<GetClaimByIdResponse, 200>>;
updateClaimStatus(_ctx: Context): ControllerResponse<TypedResponse<UpdateClaimStatusResponse, 200>>;
}
export class ClaimController {
private claimService: IClaimService;
constructor(service: IClaimService) {
this.claimService = service;
}
getClaimByCompanyId = withControllerErrorHandling(
async (ctx: Context): ControllerResponse<TypedResponse<GetClaimsByCompanyIdResponse, 200>> => {
const id = ctx.get("companyId");
if (!validate(id)) {
return ctx.json({ error: "Invalid company ID format" }, 400);
}
const claimResponse = await this.claimService.getClaimsByCompanyId(id);
return ctx.json(claimResponse, 200);
}
);
createClaim = withControllerErrorHandling(
async (ctx: Context): ControllerResponse<TypedResponse<CreateClaimResponse, 201>> => {
const json = await ctx.req.json();
const payload = CreateClaimDTOSchema.parse(json);
const id = ctx.get("companyId");
if (!validate(id)) {
return ctx.json({ error: "Invalid company ID format" }, 400);
}
const claim = await this.claimService.createClaim(payload, id);
return ctx.json(claim, 201);
}
);
deleteClaim = withControllerErrorHandling(
async (ctx: Context): ControllerResponse<TypedResponse<DeleteClaimResponse, 200>> => {
const id = ctx.req.param("id");
const companyId = ctx.get("companyId");
if (!validate(id)) {
return ctx.json({ error: "Invalid claim ID format" }, 400);
}
const claim = await this.claimService.deleteClaim({ id: id }, companyId);
return ctx.json(claim, 200);
}
);
linkClaimToLineItem = withControllerErrorHandling(
async (ctx: Context): ControllerResponse<TypedResponse<LinkClaimToLineItemResponse, 201>> => {
const json = await ctx.req.json();
const payload = LinkClaimToLineItemDTOSchema.parse(json);
if (!validate(payload.claimId) || !validate(payload.purchaseLineItemId)) {
return ctx.json({ error: "Invalid claim or purchase line item Id format" }, 400);
}
const linkedSuccess = await this.claimService.linkClaimToLineItem(payload);
return ctx.json(linkedSuccess, 201);
}
);
linkClaimToPurchaseItems = withControllerErrorHandling(
async (ctx: Context): ControllerResponse<TypedResponse<LinkClaimToPurchaseResponse, 201>> => {
const json = await ctx.req.json();
const payload = LinkClaimToPurchaseDTOSchema.parse(json);
if (!validate(payload.claimId) || !validate(payload.purchaseId)) {
return ctx.json({ error: "Invalid claim or purchase Id format" }, 400);
}
const linkedSuccess = await this.claimService.linkClaimToPurchaseItems(payload);
return ctx.json(linkedSuccess, 201);
}
);
getLinkedPurchaseLineItems = withControllerErrorHandling(
async (ctx: Context): ControllerResponse<TypedResponse<GetPurchaseLineItemsForClaimResponse, 200>> => {
const claimId = ctx.req.param("id");
if (!validate(claimId)) {
return ctx.json({ error: "Invalid claim Id format" }, 400);
}
const purchaseLineItems = await this.claimService.getLinkedPurchaseLineItems(claimId);
return ctx.json(purchaseLineItems, 200);
}
);
deletePurchaseLineItem = withControllerErrorHandling(
async (ctx: Context): ControllerResponse<TypedResponse<DeletePurchaseLineItemResponse, 200>> => {
const claimId = ctx.req.param("claimId");
const lineItemId = ctx.req.param("lineItemId");
if (!validate(claimId) || !validate(lineItemId)) {
return ctx.json({ error: "Invalid claim or line item Id format" }, 400);
}
const deletedLinks = await this.claimService.deletePurchaseLineItem(claimId, lineItemId);
return ctx.json(deletedLinks, 200);
}
);
createClaimPDF = withControllerErrorHandling(
async (ctx: Context): ControllerResponse<TypedResponse<ClaimPDFGenerationResponse, 200>> => {
const userId = ctx.get("userId");
const claimId = ctx.req.param("id");
const companyId = ctx.get("companyId");
if (!validate(claimId)) {
return ctx.json({ error: "Invalid claim Id format" }, 400);
}
const pdfUrl = await this.claimService.createClaimPDF(claimId, userId, companyId);
return ctx.json(pdfUrl, 200);
}
);
getClaimById = withControllerErrorHandling(
async (ctx: Context): ControllerResponse<TypedResponse<GetClaimByIdResponse, 200>> => {
const claimId = ctx.req.param("id");
const companyId = ctx.get("companyId");
if (!validate(claimId)) {
return ctx.json({ error: "Invalid claim ID format" }, 400);
}
const claim = await this.claimService.getClaimById(claimId, companyId);
return ctx.json(claim, 200);
}
);
updateClaimStatus = withControllerErrorHandling(
async (ctx: Context): ControllerResponse<TypedResponse<UpdateClaimStatusResponse, 200>> => {
const claimId = ctx.req.param("id");
const companyId = ctx.get("companyId");
const json = await ctx.req.json();
const payload = UpdateClaimStatusDTOSchema.parse(json);
if (!validate(claimId)) {
return ctx.json({ error: "Invalid claim ID format" }, 400);
}
const claim = await this.claimService.updateClaimStatus(claimId, payload, companyId);
return ctx.json(claim, 200);
}
);
}