-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathletter-mapper.ts
More file actions
111 lines (100 loc) · 2.95 KB
/
Copy pathletter-mapper.ts
File metadata and controls
111 lines (100 loc) · 2.95 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
import { LetterBase, LetterStatus, UpdateLetter } from "@internal/datastore";
import {
GetLetterResponse,
GetLetterResponseSchema,
GetLettersResponse,
GetLettersResponseSchema,
PatchLetterRequest,
PatchLetterResponse,
PatchLetterResponseSchema,
PostLettersRequest,
PostLettersRequestResource,
UpdateLetterCommand,
} from "../contracts/letters";
function letterToResourceResponse(letter: LetterBase) {
return {
id: letter.id,
type: "Letter",
attributes: {
status: letter.status,
specificationId: letter.specificationId,
groupId: letter.groupId,
...(letter.reasonCode != null && { reasonCode: letter.reasonCode }),
...(letter.reasonText != null && { reasonText: letter.reasonText }),
},
};
}
function letterToGetLettersResourceResponse(letter: LetterBase) {
return {
id: letter.id,
type: "Letter",
attributes: {
status: letter.status,
specificationId: letter.specificationId,
groupId: letter.groupId,
},
};
}
// --------------------------
// Map request to command
// --------------------------
export function mapToUpdateCommand(
request: PatchLetterRequest,
supplierId: string,
): UpdateLetterCommand {
return {
id: request.data.id,
supplierId,
status: LetterStatus.parse(request.data.attributes.status),
reasonCode: request.data.attributes.reasonCode,
reasonText: request.data.attributes.reasonText,
};
}
export function mapToUpdateCommands(
request: PostLettersRequest,
supplierId: string,
): UpdateLetterCommand[] {
return request.data.map((letterToUpdate: PostLettersRequestResource) => ({
id: letterToUpdate.id,
supplierId,
status: LetterStatus.parse(letterToUpdate.attributes.status),
reasonCode: letterToUpdate.attributes.reasonCode,
reasonText: letterToUpdate.attributes.reasonText,
}));
}
// ---------------------------------------------
// Map letter command to repository type
// ---------------------------------------------
export function mapToUpdateLetter(
updateLetter: UpdateLetterCommand,
): UpdateLetter {
return {
id: updateLetter.id,
supplierId: updateLetter.supplierId,
status: updateLetter.status,
reasonCode: updateLetter.reasonCode,
reasonText: updateLetter.reasonText,
};
}
// ---------------------------------------------
// Map internal datastore letter to response
// ---------------------------------------------
export function mapToPatchLetterResponse(
letter: LetterBase,
): PatchLetterResponse {
return PatchLetterResponseSchema.parse({
data: letterToResourceResponse(letter),
});
}
export function mapToGetLettersResponse(
letters: LetterBase[],
): GetLettersResponse {
return GetLettersResponseSchema.parse({
data: letters.map((letter) => letterToGetLettersResourceResponse(letter)),
});
}
export function mapToGetLetterResponse(letter: LetterBase): GetLetterResponse {
return GetLetterResponseSchema.parse({
data: letterToResourceResponse(letter),
});
}