-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathnode.slice.ts
More file actions
340 lines (300 loc) · 9.46 KB
/
Copy pathnode.slice.ts
File metadata and controls
340 lines (300 loc) · 9.46 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/*
* Copyright (c) 2025 Ping Identity Corporation. All rights reserved.
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
/**
* Import the required utilities from Redux Toolkit
*/
import { createSlice } from '@reduxjs/toolkit';
/**
* Import the needed reducers
*/
import { nodeCollectorReducer, updateCollectorValues } from './node.reducer.js';
import { getCollectorErrors } from './node.utils.js';
/**
* Import the types
*/
import type { Draft, PayloadAction } from '@reduxjs/toolkit';
import type { SubmitCollector } from './collector.types.js';
import type { GenericError } from './error.types.js';
import type {
DavinciErrorResponse,
DaVinciFailureResponse,
DaVinciNextResponse,
DaVinciSuccessResponse,
} from './davinci.types.js';
import type { ContinueNode, SuccessNode, ErrorNode, StartNode, FailureNode } from './node.types.js';
/**
* The possible statuses for the four types of nodes
*/
const CONTINUE_STATUS = 'continue';
const ERROR_STATUS = 'error';
const FAILURE_STATUS = 'failure';
const SUCCESS_STATUS = 'success';
const START_STATUS = 'start';
/**
* @const initialNodeState - Initial state for the node slice
*/
export const initialNodeState = {
cache: null,
client: {
status: START_STATUS,
},
error: null,
server: {
status: START_STATUS,
},
status: START_STATUS,
} satisfies StartNode;
type NodeStates = ErrorNode | FailureNode | ContinueNode | SuccessNode | StartNode;
/**
* @const nodeSlice - Slice for handling the node state
* @see https://redux-toolkit.js.org/api/createSlice
*/
export const nodeSlice = createSlice({
name: 'node',
initialState: initialNodeState as NodeStates,
reducers: {
/**
* @method error - Method for creating an error node
* @param {Object} state - The current state of the slice
* @param {PayloadAction<DaVinciErrorCacheEntry<DavinciErrorResponse>>} action - The action to be dispatched
* @returns {ErrorNode} - The error node
*/
error(
state,
action: PayloadAction<{ data: DavinciErrorResponse; requestId: string; httpStatus: number }>,
) {
const newState = state as Draft<ErrorNode>;
// Reference to the original response from DaVinci in the cache
newState.cache = {
key: action.payload.requestId,
};
// Data for the client to consume
newState.client = {
...(state.client as ContinueNode['client']),
status: ERROR_STATUS,
};
newState.error = {
code: action.payload.data.code,
collectors: getCollectorErrors(action.payload.data),
message: action.payload.data.message,
internalHttpStatus: action.payload.data.httpResponseCode,
status: 'error',
type: 'davinci_error',
};
newState.httpStatus = action.payload.httpStatus;
// Data that the server users
newState.server = {
...state.server,
status: ERROR_STATUS,
};
// Used to help detect the node type
newState.status = ERROR_STATUS;
return newState;
},
/**
* @method failure - Method for creating an error node
* @param {Object} state - The current state of the slice
* @param {PayloadAction<DaVinciFailureResponse>} action - The action to be dispatched
* @returns {FailureNode} - The error node
*/
failure(
state,
action: PayloadAction<{
data: DaVinciFailureResponse | unknown;
requestId: string;
httpStatus: number;
}>,
) {
const newState = state as Draft<FailureNode>;
newState.cache = {
key: action.payload.requestId,
};
newState.client = {
status: FAILURE_STATUS,
};
if (action.payload.data && typeof action.payload.data === 'object') {
const data = action.payload.data as Record<string, string>;
newState.error = {
code: data['code'] || 'unknown',
message: data['message'] || data['errorMessage'] || '',
internalHttpStatus:
typeof data['httpResponseCode'] === 'number' ? data['httpResponseCode'] : 0,
status: FAILURE_STATUS,
type: 'davinci_error',
};
} else {
newState.error = {
code: 'unknown',
message: 'An unknown error occurred',
status: FAILURE_STATUS,
type: 'network_error',
};
}
newState.httpStatus = action.payload.httpStatus;
newState.server = {
status: FAILURE_STATUS,
};
newState.status = FAILURE_STATUS;
return newState;
},
/**
* @method next - Method for creating a next node
* @param {Object} state - The current state of the slice
* @param {PayloadAction<DaVinciNextResponse>} action - The action to be dispatched
* @returns {ContinueNode} - The next node
*/
next(
state,
action: PayloadAction<{
data: DaVinciNextResponse;
requestId: string;
httpStatus: number;
}>,
) {
const newState = state as Draft<ContinueNode>;
const collectors = nodeCollectorReducer([], {
type: action.type,
payload: {
fields: action.payload.data?.form?.components?.fields,
formData: action.payload.data?.formData,
},
});
const submitCollector = collectors.filter(
(collector): collector is SubmitCollector => collector.type === 'SubmitCollector',
)[0];
newState.cache = {
key: action.payload.requestId,
};
newState.client = {
action: submitCollector?.output.key,
description: action.payload.data?.form?.description,
collectors,
name: action.payload.data.form?.name,
status: CONTINUE_STATUS,
};
newState.error = null;
newState.httpStatus = action.payload.httpStatus;
newState.server = {
_links: action.payload.data._links,
id: action.payload.data.id,
interactionId: action.payload.data.interactionId,
interactionToken: action.payload.data.interactionToken,
eventName: action.payload.data.eventName || 'continue',
status: CONTINUE_STATUS,
};
// Used to help detect the node type
newState.status = CONTINUE_STATUS;
return newState;
},
/**
* @method start - Method for creating a start node
* @param {Object} state - The current state of the slice
* @returns {StartNode} - The start node
*/
success(
state,
action: PayloadAction<{
data: DaVinciSuccessResponse;
requestId: string;
httpStatus: number;
}>,
) {
const newState = state as Draft<SuccessNode>;
newState.cache = {
key: action.payload.requestId,
};
newState.client = {
authorization: {
code: action.payload.data.authorizeResponse?.code,
state: action.payload.data.authorizeResponse?.state,
},
status: SUCCESS_STATUS,
};
newState.httpStatus = action.payload.httpStatus;
newState.server = {
id: action.payload.data.id,
interactionId: action.payload.data.interactionId,
interactionToken: action.payload.data.interactionToken,
session: action.payload.data.session?.id,
status: SUCCESS_STATUS,
};
newState.error = null;
newState.status = SUCCESS_STATUS;
return newState;
},
/**
* @method update - Method for updating collector values with the node
* @param {Object} state - The current state of the slice
* @param {PayloadAction<unknown>} action - The action to be dispatched
* @returns {ContinueNode} - The next node
*/
update(state, action: ReturnType<typeof updateCollectorValues>) {
const newState = state as Draft<ContinueNode>;
newState.client.collectors = nodeCollectorReducer(newState.client.collectors, action);
return newState;
},
},
selectors: {
selectClient: (state) => {
return state.client;
},
selectCollectors: (state) => {
if (state.client && 'collectors' in state.client) {
return {
error: null,
state: state?.client.collectors,
};
}
return {
error: {
code: 'unknown',
type: 'state_error',
message: `\`collectors\` are only available on nodes with \`status\` of ${CONTINUE_STATUS} or ${ERROR_STATUS}`,
} as GenericError,
state: [],
};
},
selectCollector: (state, id: string) => {
if (state.client && 'collectors' in state.client) {
return {
error: null,
state: state.client.collectors?.find((collector) => collector.id === id),
};
}
return {
error: {
code: 'unknown',
type: 'state_error',
message: `\`collectors\` are only available on nodes with \`status\` of ${CONTINUE_STATUS} or ${ERROR_STATUS}`,
} as GenericError,
state: null,
};
},
selectError: (state) => {
return state.error;
},
selectErrorCollectors: (state) => {
if (state.status === ERROR_STATUS) {
return {
error: null,
state: state.error?.collectors || [],
};
}
return {
error: {
code: 'unknown',
type: 'state_error',
message: `\`errorCollectors\` are only available on nodes with \`status\` of ${ERROR_STATUS}`,
} as GenericError,
state: [],
};
},
selectServer: (state) => {
return state.server;
},
},
});