@@ -3,27 +3,39 @@ import { ValueAdapter } from './value.adapter';
3
3
import { Form } from '../form-factory' ;
4
4
import { GroupNode , LeafNode } from '../form-factory/form-node' ;
5
5
import moment from 'moment' ;
6
- import { AppointmentResponsePayload } from './appointment-helper' ;
7
-
8
- interface AppointmentPayload {
9
- [ key : string ] : string | { uuid : string } [ ] ;
10
- providers : Array < { uuid : string } > ;
11
- locationUuid : string ;
12
- serviceUuid : string ;
13
- endDateTime : string ;
14
- }
6
+ import {
7
+ AppointmentPayload ,
8
+ AppointmentResponsePayload
9
+ } from './appointment-helper' ;
15
10
16
11
@Injectable ( {
17
12
providedIn : 'root'
18
13
} )
19
14
export class AppointmentAdapter implements ValueAdapter {
15
+ /**
16
+ * Generates the form payload for an appointment.
17
+ * @param {Form } form - The form object.
18
+ * @returns {AppointmentPayload } The generated appointment payload.
19
+ */
20
20
public generateFormPayload ( form : Form ) : AppointmentPayload {
21
21
const uuid = form ?. valueProcessingInfo ?. appointmentUuid ;
22
+ const dateAppointmentIssued =
23
+ form ?. valueProcessingInfo ?. dateAppointmentIssued ;
22
24
23
25
const questionNodes = this . findAppointmentQuestionNodes ( form . rootNode ) ;
24
26
const payload = this . generateFormPayloadForQuestion ( questionNodes ) ;
25
- const appointmentPayload = uuid ? { ...payload , uuid } : payload ;
26
- return appointmentPayload ;
27
+
28
+ // If in edit mode, add the uuid to the payload
29
+ if ( uuid ) {
30
+ payload . uuid = uuid ;
31
+ }
32
+
33
+ // Add dateAppointmentIssued to the payload if it exists
34
+ if ( dateAppointmentIssued ) {
35
+ payload . dateAppointmentIssued = dateAppointmentIssued ;
36
+ }
37
+
38
+ return payload ;
27
39
}
28
40
29
41
public populateForm ( form : Form , payload : AppointmentResponsePayload ) : void {
@@ -86,13 +98,17 @@ export class AppointmentAdapter implements ValueAdapter {
86
98
return appointmentNodes ;
87
99
}
88
100
101
+ /**
102
+ * Generates the form payload for appointment questions.
103
+ * @param {LeafNode[] } appointmentQuestionNodes - An array of leaf nodes representing appointment questions.
104
+ * @returns {AppointmentPayload } The generated appointment payload.
105
+ */
89
106
private generateFormPayloadForQuestion (
90
107
appointmentQuestionNodes : LeafNode [ ]
91
108
) : AppointmentPayload {
92
109
const formPayload = appointmentQuestionNodes . reduce < Record < string , string > > (
93
110
( payload , node ) => {
94
- const appointmentKey =
95
- node . question . extras . questionOptions . appointmentKey ;
111
+ const { appointmentKey } = node . question . extras . questionOptions ;
96
112
payload [ appointmentKey ] = node . control . value ;
97
113
return payload ;
98
114
} ,
@@ -105,26 +121,37 @@ export class AppointmentAdapter implements ValueAdapter {
105
121
duration,
106
122
service,
107
123
location,
108
- ...restPayload
124
+ status = 'Scheduled' ,
125
+ appointmentKind = 'Scheduled' ,
126
+ ...restOfPayload
109
127
} = formPayload ;
110
128
129
+ const endDateTime = duration
130
+ ? this . calculateEndDateTime ( startDateTime , parseInt ( duration , 10 ) )
131
+ : moment ( startDateTime ) . endOf ( 'day' ) . toISOString ( ) ;
132
+
111
133
return {
112
- ...restPayload ,
134
+ ...restOfPayload ,
135
+ status,
136
+ appointmentKind,
113
137
locationUuid : location ,
114
138
serviceUuid : service ,
115
139
providers : [ { uuid : providers } ] ,
116
140
startDateTime : moment ( startDateTime ) . toISOString ( ) ,
117
- endDateTime : this . calculateEndDateTime (
118
- startDateTime ,
119
- parseInt ( duration , 10 )
120
- )
141
+ endDateTime
121
142
} ;
122
143
}
123
144
145
+ /**
146
+ * Calculates the end date and time based on the start date and time and duration.
147
+ * @param {string } startDateTime - The start date and time in ISO format.
148
+ * @param {number } durationMinutes - The duration in minutes.
149
+ * @returns {string } The calculated end date and time in ISO format.
150
+ */
124
151
private calculateEndDateTime (
125
- startDatetime : string ,
152
+ startDateTime : string ,
126
153
durationMinutes : number
127
154
) : string {
128
- return moment ( startDatetime ) . add ( durationMinutes , 'minutes' ) . toISOString ( ) ;
155
+ return moment ( startDateTime ) . add ( durationMinutes , 'minutes' ) . toISOString ( ) ;
129
156
}
130
157
}
0 commit comments