@timum/timum_pdk 2.1.0 → 2.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/timumPdk.js CHANGED
@@ -1,365 +1,372 @@
1
- import { createApi, fetchBaseQuery } from "@reduxjs/toolkit/query/react";
2
-
3
- import failsaveStorage from "./util/failSafeLocalStorage";
4
-
5
- let host = undefined;
6
- export const setTimumApiHost = (url) => {
7
- host = url;
8
- };
9
-
10
- export const getTimumApiHost = () => {
11
- if (host) {
12
- return host;
13
- } else {
14
- return failsaveStorage.get("timumApiHost", "https://www.timum.de");
15
- }
16
- };
17
-
18
- // we do it this way so that the base url can be determined dynamically
19
- export const getBaseUrl = () => {
20
- return getTimumApiHost() + "/rest/1";
21
- };
22
-
23
- export const constructUrl = (url, props, plain) => {
24
- if (props.params) {
25
- for (const [paramName, paramValue] of Object.entries(props.params)) {
26
- if (Array.isArray(paramValue)) {
27
- for (let el of paramValue) {
28
- if (!url.includes("?")) {
29
- url = `${url}?${paramName}=${el}`;
30
- } else {
31
- url = `${url}&${paramName}=${el}`;
32
- }
33
- }
34
-
35
- } else {
36
- if (!url.includes("?")) {
37
- url = `${url}?${paramName}=${paramValue}`;
38
- } else {
39
- url = `${url}&${paramName}=${paramValue}`;
40
- }
41
- }
42
- }
43
- }
44
-
45
- if (!url.includes("?")) {
46
- url = `${url}?X-DISABLE-COOKIES=true`;
47
- } else {
48
- url = `${url}&X-DISABLE-COOKIES=true`;
49
- }
50
-
51
- if (plain) {
52
- return `${getTimumApiHost()}${url}`;
53
- } else {
54
- return `${getBaseUrl()}${url}`;
55
- }
56
- };
57
-
58
- export const timumApiSlice = createApi({
59
- reducerPath: "timumApi",
60
- baseQuery: fetchBaseQuery({
61
- baseUrl: undefined,
62
- credentials: "include",
63
- }),
64
- tagTypes: [
65
- "Timeslot",
66
- "Product",
67
- "Account",
68
- "Provider",
69
- "User",
70
- "IdentifiedCustomer",
71
- ],
72
- endpoints: (builder) => ({
73
- // ##########################################
74
- // # ConsumerAPI v2
75
- // ##########################################
76
-
77
- upcomingBookables: builder.query({
78
- query: (props) => ({
79
- url: constructUrl(
80
- `/resources/${props.channelOrResourceId}/upcoming_bookables`,
81
- props
82
- ),
83
- headers: props.headers,
84
- }),
85
- providesTags: (/* result = [], error, arg */) => ["Timeslot"],
86
- }),
87
-
88
- specificBookables: builder.query({
89
- query: (props) => ({
90
- url: constructUrl(
91
- `/bookables/specific_bookables`,
92
- props
93
- ),
94
- headers: props.headers,
95
- method: "post",
96
- body: props.body,
97
- }),
98
- providesTags: (/* result = [], error, arg */) => ["Timeslot"],
99
- }),
100
-
101
- activeProducts: builder.query({
102
- query: (props) => ({
103
- url: constructUrl(
104
- `/resources/${props.channelOrResourceId}/active_products`,
105
- props
106
- ),
107
- headers: props.headers,
108
- }),
109
- providesTags: (/* result = [], error, arg */) => ["Product"],
110
- }),
111
-
112
-
113
-
114
- createAppointmentWithConsumer: builder.mutation({
115
- query: (props) => ({
116
- url: constructUrl(
117
- `/resources/${props.channelOrResourceId}/create_appointment_with_consumer`,
118
- props
119
- ),
120
- method: "post",
121
- body: props.body,
122
- headers: props.headers,
123
- }),
124
- invalidatesTags: (result, error, arg) => {
125
- const invalidatedTags = [{ type: "Timeslot" }];
126
-
127
- if (arg.shouldInvalidateCustomerIdentification) {
128
- invalidatedTags.push({ type: "IdentifiedCustomer" });
129
- }
130
-
131
- return invalidatedTags;
132
- },
133
- }),
134
-
135
- removeCustomerFromAppointment: builder.mutation({
136
- query: (props) => ({
137
- url: constructUrl(
138
- `/customers/${props.customersId}/appointments/${props.appointmentId}`,
139
- props
140
- ),
141
- method: "delete",
142
- headers: props.headers,
143
- }),
144
- invalidatesTags: (/* result, error, arg */) => [{ type: "Timeslot" }],
145
- }),
146
-
147
- authenticate: builder.query({
148
- query: (props) => {
149
- return {
150
- url: constructUrl(`/auth/cookieless/login`, props, true),
151
- headers: props.headers,
152
- method: "post",
153
- body: props.body,
154
- };
155
- },
156
- }),
157
-
158
- /**
159
- * Returns an object with the following makeup:
160
- * {
161
- * contact: {
162
- * name,
163
- * email,
164
- * mobile,
165
- * phone
166
- * },
167
- * resource: {
168
- * name,
169
- * description,
170
- * msgHelpText
171
- * },
172
- * provider: {
173
- * name
174
- * }
175
- * }
176
- */
177
- publicData: builder.query({
178
- query: (props) => ({
179
- url: constructUrl(
180
- `/resources/${props.channelOrResourceId}/public_data`,
181
- props
182
- ),
183
- headers: props.headers,
184
- }),
185
- }),
186
-
187
- identifyCustomer: builder.query({
188
- query: (props) => ({
189
- url: constructUrl(
190
- `/crms/${props.crmSlug}/resources/${props.channelOrResourceId}/customers/${props.personId}/identify`,
191
- props
192
- ),
193
- headers: props.headers,
194
- }),
195
- providesTags: "IdentifiedCustomer", //
196
- keepUnusedDataFor: 9999999, // once identified it's not going to change
197
- }),
198
-
199
- /**
200
- * body contain:
201
- * resource_id*,
202
- * product_id*,
203
- * provider_id,
204
- * timeslotUuid (if available)
205
- * from* (ISO format),
206
- * to* (ISO format)
207
- * -> * marks those which are required.
208
- */
209
- reserveAppoinment: builder.mutation({
210
- query: (props) => ({
211
- url: constructUrl(
212
- `/resources/${props.channelOrResourceId}/reserve_appointment`,
213
- props
214
- ),
215
- method: "post",
216
- body: props.body,
217
- headers: props.headers,
218
- }),
219
- }),
220
-
221
- /**
222
- * body must contain:
223
- * placeholder_id (id created for ephemeral customer during call to),
224
- * appointment_id
225
- */
226
- revokeAppointmentReservation: builder.mutation({
227
- query: (props) => ({
228
- url: constructUrl(
229
- `/resources/${props.channelOrResourceId}/revoke_reservation`,
230
- props
231
- ),
232
- method: "post",
233
- body: props.body,
234
- headers: props.headers,
235
- }),
236
- }),
237
-
238
- // ##########################################
239
- // # CRM API requests (general api)
240
- // ##########################################
241
-
242
- getAccount: builder.query({
243
- query: (props) => ({
244
- url: constructUrl(
245
- `/crms/${props.platform}/account/${props.accountReference}`,
246
- props
247
- ),
248
- }),
249
- providesTags: (/* result = [], error, arg */) => ["Account"],
250
- }),
251
-
252
- createAccount: builder.mutation({
253
- query: (props) => {
254
- return {
255
- url: constructUrl(`/crms/${props.platform}/account`, props),
256
- method: "post",
257
- body: JSON.stringify(props.accountData),
258
- };
259
- },
260
- invalidatesTags: (/* result, error, arg */) => [{ type: "Account" }],
261
- }),
262
-
263
- getProviders: builder.query({
264
- query: (props) => ({
265
- url: constructUrl(
266
- `/crms/${props.platform}/account/${props.accountReference}/providers`,
267
- props
268
- ),
269
- }),
270
- providesTags: (/* result = [], error, arg */) => ["Product"],
271
- }),
272
-
273
- getProvider: builder.query({
274
- query: (props) => ({
275
- url: constructUrl(
276
- `/crms/${props.platform}/provider/${props.providerReference}`,
277
- props
278
- ),
279
- }),
280
- }),
281
-
282
- createProvider: builder.mutation({
283
- query: (props) => ({
284
- url: constructUrl(`/crms/${props.platform}/provider`, props),
285
- method: "post",
286
- body: JSON.stringify(
287
- (() => ({
288
- user: props.userData ?? {},
289
- provider: props.providerData ?? {},
290
- address: props.addressData ?? null,
291
- account: props.account ?? null,
292
- sendEmail: props.sendEmail ?? false,
293
- }))()
294
- ),
295
- }),
296
- invalidatesTags: (/* result, error, arg */) => [{ type: "Provider" }],
297
- }),
298
-
299
- // loginUserViaApi: builder.query({
300
- // query: (props) => ({
301
- // url: constructUrl(`/crms/${props.platform}/user/loginWithJwt`),
302
- // }),
303
- // async onQueryStarted(props, { dispatch, queryFulfilled }) {
304
- // const { data /* , meta */ } = await queryFulfilled;
305
- // if (data) {
306
- // dispatch(timumClientAuthorised({ auth2: data.auth2 }));
307
- // }
308
- // },
309
- // }),
310
- getUser: builder.query({
311
- query: (props) => ({
312
- url: constructUrl(
313
- `/crms/${props.platform}/user/${props.userReference}`,
314
- props
315
- ),
316
- }),
317
- }),
318
-
319
- createUser: builder.mutation({
320
- query: (props) => ({
321
- url: constructUrl(`/crms/${props.platform}/user`, props),
322
- method: "post",
323
- body: JSON.stringify(props.userData),
324
- }),
325
- invalidatesTags: (/* result, error, arg */) => [{ type: "User" }],
326
- }),
327
- }),
328
- });
329
-
330
- export const {
331
- // ##########################################
332
- // # ConsumerAPI v2
333
- // ##########################################
334
-
335
- useUpcomingBookablesQuery,
336
- useLazyUpcomingBookablesQuery,
337
- useSpecificBookablesQuery,
338
- useLazySpecificBookablesQuery,
339
- useActiveProductsQuery,
340
- useLazyActiveProductsQuery,
341
- useCreateAppointmentWithConsumerMutation,
342
- useIdentifyCustomerQuery,
343
- useLazyIdentifyCustomerQuery,
344
- usePublicDataQuery,
345
- useLazyPublicDataQuery,
346
- useReserveAppoinmentMutation,
347
- useRevokeAppointmentReservationMutation,
348
- useRemoveCustomerFromAppointmentMutation,
349
- useAuthenticateQuery,
350
- useLazyAuthenticateQuery,
351
-
352
- // ##########################################
353
- // # CRM API requests (general api)
354
- // ##########################################
355
- useCreateAccountMutation,
356
- useGetAccountQuery,
357
- useLazyGetAccountQuery,
358
- useGetProvidersQuery,
359
- useLazyGetProvidersQuery,
360
- useCreateProviderMutation,
361
- // useLoginUserViaApiQuery,
362
- useCreateUserMutation,
363
- useGetUserQuery,
364
- useLazyGetUserQuery,
365
- } = timumApiSlice;
1
+ import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react';
2
+
3
+ import failsaveStorage from './util/failSafeLocalStorage';
4
+
5
+ let host = undefined;
6
+ export const setTimumApiHost = (url) => {
7
+ host = url;
8
+ };
9
+
10
+ export const getTimumApiHost = () => {
11
+ if (host) {
12
+ return host;
13
+ } else {
14
+ return failsaveStorage.get('timumApiHost', 'https://www.timum.de');
15
+ }
16
+ };
17
+
18
+ // we do it this way so that the base url can be determined dynamically
19
+ export const getBaseUrl = () => {
20
+ return getTimumApiHost() + '/rest/1';
21
+ };
22
+
23
+ export const constructUrl = (url, props, plain) => {
24
+ if (props.params) {
25
+ for (const [paramName, paramValue] of Object.entries(props.params)) {
26
+ if (Array.isArray(paramValue)) {
27
+ for (let el of paramValue) {
28
+ if (!url.includes('?')) {
29
+ url = `${url}?${paramName}=${el}`;
30
+ } else {
31
+ url = `${url}&${paramName}=${el}`;
32
+ }
33
+ }
34
+ } else {
35
+ if (!url.includes('?')) {
36
+ url = `${url}?${paramName}=${paramValue}`;
37
+ } else {
38
+ url = `${url}&${paramName}=${paramValue}`;
39
+ }
40
+ }
41
+ }
42
+ }
43
+
44
+ if (!url.includes('?')) {
45
+ url = `${url}?X-DISABLE-COOKIES=true`;
46
+ } else {
47
+ url = `${url}&X-DISABLE-COOKIES=true`;
48
+ }
49
+
50
+ if (plain) {
51
+ return `${getTimumApiHost()}${url}`;
52
+ } else {
53
+ return `${getBaseUrl()}${url}`;
54
+ }
55
+ };
56
+
57
+ export const timumApiSlice = createApi({
58
+ reducerPath: 'timumApi',
59
+ baseQuery: fetchBaseQuery({
60
+ baseUrl: undefined,
61
+ credentials: 'include',
62
+ }),
63
+ tagTypes: [
64
+ 'Timeslot',
65
+ 'Product',
66
+ 'Account',
67
+ 'Provider',
68
+ 'User',
69
+ 'IdentifiedCustomer',
70
+ ],
71
+ endpoints: (builder) => ({
72
+ // ##########################################
73
+ // # ConsumerAPI v2
74
+ // ##########################################
75
+
76
+ upcomingBookables: builder.query({
77
+ query: (props) => ({
78
+ url: constructUrl(
79
+ `/resources/${props.channelOrResourceId}/upcoming_bookables`,
80
+ props,
81
+ ),
82
+ method: 'post',
83
+ headers: props.headers,
84
+ }),
85
+ providesTags: (/* result = [], error, arg */) => ['Timeslot'],
86
+ }),
87
+
88
+ specificBookables: builder.query({
89
+ query: (props) => ({
90
+ url: constructUrl(`/bookables/specific_bookables`, props),
91
+ headers: props.headers,
92
+ method: 'post',
93
+ body: props.body,
94
+ }),
95
+ providesTags: (/* result = [], error, arg */) => ['Timeslot'],
96
+ }),
97
+
98
+ activeProducts: builder.query({
99
+ query: (props) => ({
100
+ url: constructUrl(
101
+ `/resources/${props.channelOrResourceId}/active_products`,
102
+ props,
103
+ ),
104
+ headers: props.headers,
105
+ }),
106
+ providesTags: (/* result = [], error, arg */) => ['Product'],
107
+ }),
108
+
109
+ specificProducts: builder.query({
110
+ query: (props) => ({
111
+ url: constructUrl(`/products/specific_products`, props),
112
+ headers: props.headers,
113
+ method: 'post',
114
+ body: props.body,
115
+ }),
116
+ providesTags: (/* result = [], error, arg */) => ['Product'],
117
+ }),
118
+
119
+ createAppointmentWithConsumer: builder.mutation({
120
+ query: (props) => ({
121
+ url: constructUrl(
122
+ `/resources/${props.channelOrResourceId}/create_appointment_with_consumer`,
123
+ props,
124
+ ),
125
+ method: 'post',
126
+ body: props.body,
127
+ headers: props.headers,
128
+ }),
129
+ invalidatesTags: (result, error, arg) => {
130
+ const invalidatedTags = [{ type: 'Timeslot' }];
131
+
132
+ if (arg.shouldInvalidateCustomerIdentification) {
133
+ invalidatedTags.push({ type: 'IdentifiedCustomer' });
134
+ }
135
+
136
+ return invalidatedTags;
137
+ },
138
+ }),
139
+
140
+ removeCustomerFromAppointment: builder.mutation({
141
+ query: (props) => ({
142
+ url: constructUrl(
143
+ `/customers/${props.customersId}/appointments/${props.appointmentId}`,
144
+ props,
145
+ ),
146
+ method: 'delete',
147
+ headers: props.headers,
148
+ }),
149
+ invalidatesTags: (/* result, error, arg */) => [{ type: 'Timeslot' }],
150
+ }),
151
+
152
+ authenticate: builder.query({
153
+ query: (props) => {
154
+ return {
155
+ url: constructUrl(`/auth/cookieless/login`, props, true),
156
+ headers: props.headers,
157
+ method: 'post',
158
+ body: props.body,
159
+ };
160
+ },
161
+ }),
162
+
163
+ /**
164
+ * Returns an object with the following makeup:
165
+ * {
166
+ * contact: {
167
+ * name,
168
+ * email,
169
+ * mobile,
170
+ * phone
171
+ * },
172
+ * resource: {
173
+ * name,
174
+ * description,
175
+ * msgHelpText
176
+ * },
177
+ * provider: {
178
+ * name
179
+ * }
180
+ * }
181
+ */
182
+ publicData: builder.query({
183
+ query: (props) => ({
184
+ url: constructUrl(
185
+ `/resources/${props.channelOrResourceId}/public_data`,
186
+ props,
187
+ ),
188
+ headers: props.headers,
189
+ }),
190
+ }),
191
+
192
+ identifyCustomer: builder.query({
193
+ query: (props) => ({
194
+ url: constructUrl(
195
+ `/crms/${props.crmSlug}/resources/${props.channelOrResourceId}/customers/${props.personId}/identify`,
196
+ props,
197
+ ),
198
+ headers: props.headers,
199
+ }),
200
+ providesTags: 'IdentifiedCustomer', //
201
+ keepUnusedDataFor: 9999999, // once identified it's not going to change
202
+ }),
203
+
204
+ /**
205
+ * body contain:
206
+ * resource_id*,
207
+ * product_id*,
208
+ * provider_id,
209
+ * timeslotUuid (if available)
210
+ * from* (ISO format),
211
+ * to* (ISO format)
212
+ * -> * marks those which are required.
213
+ */
214
+ reserveAppoinment: builder.mutation({
215
+ query: (props) => ({
216
+ url: constructUrl(
217
+ `/resources/${props.channelOrResourceId}/reserve_appointment`,
218
+ props,
219
+ ),
220
+ method: 'post',
221
+ body: props.body,
222
+ headers: props.headers,
223
+ }),
224
+ }),
225
+
226
+ /**
227
+ * body must contain:
228
+ * placeholder_id (id created for ephemeral customer during call to),
229
+ * appointment_id
230
+ */
231
+ revokeAppointmentReservation: builder.mutation({
232
+ query: (props) => ({
233
+ url: constructUrl(
234
+ `/resources/${props.channelOrResourceId}/revoke_reservation`,
235
+ props,
236
+ ),
237
+ method: 'post',
238
+ body: props.body,
239
+ headers: props.headers,
240
+ }),
241
+ }),
242
+
243
+ // ##########################################
244
+ // # CRM API requests (general api)
245
+ // ##########################################
246
+
247
+ getAccount: builder.query({
248
+ query: (props) => ({
249
+ url: constructUrl(
250
+ `/crms/${props.platform}/account/${props.accountReference}`,
251
+ props,
252
+ ),
253
+ }),
254
+ providesTags: (/* result = [], error, arg */) => ['Account'],
255
+ }),
256
+
257
+ createAccount: builder.mutation({
258
+ query: (props) => {
259
+ return {
260
+ url: constructUrl(`/crms/${props.platform}/account`, props),
261
+ method: 'post',
262
+ body: JSON.stringify(props.accountData),
263
+ };
264
+ },
265
+ invalidatesTags: (/* result, error, arg */) => [{ type: 'Account' }],
266
+ }),
267
+
268
+ getProviders: builder.query({
269
+ query: (props) => ({
270
+ url: constructUrl(
271
+ `/crms/${props.platform}/account/${props.accountReference}/providers`,
272
+ props,
273
+ ),
274
+ }),
275
+ providesTags: (/* result = [], error, arg */) => ['Product'],
276
+ }),
277
+
278
+ getProvider: builder.query({
279
+ query: (props) => ({
280
+ url: constructUrl(
281
+ `/crms/${props.platform}/provider/${props.providerReference}`,
282
+ props,
283
+ ),
284
+ }),
285
+ }),
286
+
287
+ createProvider: builder.mutation({
288
+ query: (props) => ({
289
+ url: constructUrl(`/crms/${props.platform}/provider`, props),
290
+ method: 'post',
291
+ body: JSON.stringify(
292
+ (() => ({
293
+ user: props.userData ?? {},
294
+ provider: props.providerData ?? {},
295
+ address: props.addressData ?? null,
296
+ account: props.account ?? null,
297
+ sendEmail: props.sendEmail ?? false,
298
+ }))(),
299
+ ),
300
+ }),
301
+ invalidatesTags: (/* result, error, arg */) => [{ type: 'Provider' }],
302
+ }),
303
+
304
+ // loginUserViaApi: builder.query({
305
+ // query: (props) => ({
306
+ // url: constructUrl(`/crms/${props.platform}/user/loginWithJwt`),
307
+ // }),
308
+ // async onQueryStarted(props, { dispatch, queryFulfilled }) {
309
+ // const { data /* , meta */ } = await queryFulfilled;
310
+ // if (data) {
311
+ // dispatch(timumClientAuthorised({ auth2: data.auth2 }));
312
+ // }
313
+ // },
314
+ // }),
315
+ getUser: builder.query({
316
+ query: (props) => ({
317
+ url: constructUrl(
318
+ `/crms/${props.platform}/user/${props.userReference}`,
319
+ props,
320
+ ),
321
+ }),
322
+ }),
323
+
324
+ createUser: builder.mutation({
325
+ query: (props) => ({
326
+ url: constructUrl(`/crms/${props.platform}/user`, props),
327
+ method: 'post',
328
+ body: JSON.stringify(props.userData),
329
+ }),
330
+ invalidatesTags: (/* result, error, arg */) => [{ type: 'User' }],
331
+ }),
332
+ }),
333
+ });
334
+
335
+ export const {
336
+ // ##########################################
337
+ // # ConsumerAPI v2
338
+ // ##########################################
339
+
340
+ useUpcomingBookablesQuery,
341
+ useLazyUpcomingBookablesQuery,
342
+ useSpecificBookablesQuery,
343
+ useLazySpecificBookablesQuery,
344
+ useActiveProductsQuery,
345
+ useLazyActiveProductsQuery,
346
+ useSpecificProductsQuery,
347
+ useLazySpecificProductsQuery,
348
+ useCreateAppointmentWithConsumerMutation,
349
+ useIdentifyCustomerQuery,
350
+ useLazyIdentifyCustomerQuery,
351
+ usePublicDataQuery,
352
+ useLazyPublicDataQuery,
353
+ useReserveAppoinmentMutation,
354
+ useRevokeAppointmentReservationMutation,
355
+ useRemoveCustomerFromAppointmentMutation,
356
+ useAuthenticateQuery,
357
+ useLazyAuthenticateQuery,
358
+
359
+ // ##########################################
360
+ // # CRM API requests (general api)
361
+ // ##########################################
362
+ useCreateAccountMutation,
363
+ useGetAccountQuery,
364
+ useLazyGetAccountQuery,
365
+ useGetProvidersQuery,
366
+ useLazyGetProvidersQuery,
367
+ useCreateProviderMutation,
368
+ // useLoginUserViaApiQuery,
369
+ useCreateUserMutation,
370
+ useGetUserQuery,
371
+ useLazyGetUserQuery,
372
+ } = timumApiSlice;