@ossy/booking 3.19.0 → 3.20.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/README.md +1 -1
- package/package.json +7 -7
- package/src/availability.schema.js +20 -0
- package/src/booking-resources.js +22 -5
- package/src/booking.schema.js +15 -0
- package/src/cancel-booking.task.js +19 -13
- package/src/client-booking.flow.js +10 -2
- package/src/confirm-booking.api.js +3 -0
- package/src/confirm-booking.task.js +19 -13
- package/src/create-booking.task.js +32 -27
- package/src/create-booking.task.spec.js +108 -0
- package/src/create-service.task.js +10 -14
- package/src/decline-booking.api.js +3 -0
- package/src/decline-booking.task.js +19 -13
- package/src/delete-service.task.js +5 -8
- package/src/get-availability.task.js +2 -1
- package/src/get-available-slots.task.js +10 -4
- package/src/get-services.task.js +4 -2
- package/src/list-bookings.task.js +12 -1
- package/src/provider-catalog.flow.js +12 -2
- package/src/public-booking.page.jsx +12 -1
- package/src/save-availability.task.js +15 -21
- package/src/save-availability.task.spec.js +101 -0
- package/src/sdk-for-workspace.js +16 -0
- package/src/send-booking-reminder.task.js +5 -6
- package/src/service-duration.js +18 -0
- package/src/service-duration.spec.js +25 -0
- package/src/service.schema.js +4 -1
- package/src/services.page.jsx +31 -5
- package/src/update-service.flow.js +1 -1
- package/src/update-service.task.js +10 -10
package/README.md
CHANGED
|
@@ -50,7 +50,7 @@ Resource schemas live in `src/*.schema.js` and are auto-discovered by `@ossy/app
|
|
|
50
50
|
|
|
51
51
|
Documents are stored under canonical locations. Import paths from `@ossy/booking` (`services`, `bookings`, `availability`).
|
|
52
52
|
|
|
53
|
-
Tasks write resources via `
|
|
53
|
+
Tasks write resources via `sdk.invoke` (`CreateResource` / `PatchResourceContent` / …), using `schemaId` on resource events (ADR 0008).
|
|
54
54
|
|
|
55
55
|
## Projections
|
|
56
56
|
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ossy/booking",
|
|
3
3
|
"description": "Booking feature package — services, availability, and appointment management for Ossy providers",
|
|
4
|
-
"version": "3.
|
|
4
|
+
"version": "3.20.1",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./src/index.js",
|
|
7
7
|
"module": "./src/index.js",
|
|
@@ -16,12 +16,12 @@
|
|
|
16
16
|
"dependencies": {
|
|
17
17
|
"@ossy/config": "^3.0.9",
|
|
18
18
|
"@ossy/email": "^3.11.0",
|
|
19
|
-
"@ossy/event-store": "^3.
|
|
19
|
+
"@ossy/event-store": "^3.20.1",
|
|
20
20
|
"@ossy/observability": "^3.0.9",
|
|
21
|
-
"@ossy/platform": "^3.
|
|
22
|
-
"@ossy/resources": "^3.
|
|
23
|
-
"@ossy/users": "^3.
|
|
24
|
-
"@ossy/workspaces": "^3.
|
|
21
|
+
"@ossy/platform": "^3.20.1",
|
|
22
|
+
"@ossy/resources": "^3.20.1",
|
|
23
|
+
"@ossy/users": "^3.20.1",
|
|
24
|
+
"@ossy/workspaces": "^3.20.1"
|
|
25
25
|
},
|
|
26
26
|
"peerDependencies": {
|
|
27
27
|
"@ossy/app": ">=1.0.0",
|
|
@@ -38,5 +38,5 @@
|
|
|
38
38
|
"/src",
|
|
39
39
|
"README.md"
|
|
40
40
|
],
|
|
41
|
-
"gitHead": "
|
|
41
|
+
"gitHead": "3e8bb118b3ef163792a004663ccb803ab51a8601"
|
|
42
42
|
}
|
|
@@ -25,5 +25,25 @@ export default {
|
|
|
25
25
|
type: 'text',
|
|
26
26
|
default: 'Europe/Stockholm',
|
|
27
27
|
},
|
|
28
|
+
{
|
|
29
|
+
name: 'weeklyWindows',
|
|
30
|
+
type: 'json',
|
|
31
|
+
description: 'Weekly open windows [{ dayOfWeek, startTime, endTime }]',
|
|
32
|
+
},
|
|
33
|
+
{
|
|
34
|
+
name: 'sessionDurations',
|
|
35
|
+
type: 'json',
|
|
36
|
+
description: 'Allowed session lengths in minutes',
|
|
37
|
+
},
|
|
38
|
+
{
|
|
39
|
+
name: 'bufferMinutes',
|
|
40
|
+
type: 'number',
|
|
41
|
+
description: 'Gap between bookings in minutes',
|
|
42
|
+
},
|
|
43
|
+
{
|
|
44
|
+
name: 'blackoutDates',
|
|
45
|
+
type: 'json',
|
|
46
|
+
description: 'Dates when no bookings are accepted',
|
|
47
|
+
},
|
|
28
48
|
],
|
|
29
49
|
}
|
package/src/booking-resources.js
CHANGED
|
@@ -1,10 +1,27 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { ListResources } from '@ossy/resources'
|
|
2
|
+
|
|
3
|
+
function requireSdk (sdk) {
|
|
4
|
+
if (!sdk?.invoke) {
|
|
5
|
+
throw Object.assign(new Error('sdk is required'), { status: 500 })
|
|
6
|
+
}
|
|
7
|
+
return sdk
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
function asResourceList (result) {
|
|
11
|
+
if (Array.isArray(result)) return result
|
|
12
|
+
if (Array.isArray(result?.items)) return result.items
|
|
13
|
+
return []
|
|
14
|
+
}
|
|
2
15
|
|
|
3
16
|
/**
|
|
4
|
-
* List booking resources by schema id (and optional workspace).
|
|
17
|
+
* List booking resources by schema id (and optional workspace) via sdk.invoke.
|
|
5
18
|
*/
|
|
6
|
-
export function getBookingResources ({ schemaId, belongsTo }) {
|
|
19
|
+
export async function getBookingResources ({ schemaId, belongsTo, sdk } = {}) {
|
|
20
|
+
requireSdk(sdk)
|
|
7
21
|
const query = { type: schemaId }
|
|
8
|
-
|
|
9
|
-
|
|
22
|
+
const resources = await sdk.invoke(ListResources, {
|
|
23
|
+
...(belongsTo ? { workspaceId: belongsTo } : {}),
|
|
24
|
+
query,
|
|
25
|
+
})
|
|
26
|
+
return asResourceList(resources)
|
|
10
27
|
}
|
package/src/booking.schema.js
CHANGED
|
@@ -51,5 +51,20 @@ export default {
|
|
|
51
51
|
name: 'notes',
|
|
52
52
|
type: 'textarea',
|
|
53
53
|
},
|
|
54
|
+
{
|
|
55
|
+
name: 'clientMessage',
|
|
56
|
+
type: 'textarea',
|
|
57
|
+
description: 'Optional message from the client with the booking request',
|
|
58
|
+
},
|
|
59
|
+
{
|
|
60
|
+
name: 'attachments',
|
|
61
|
+
type: 'json',
|
|
62
|
+
description: 'Optional attachment descriptors sent with the booking request',
|
|
63
|
+
},
|
|
64
|
+
{
|
|
65
|
+
name: 'duration',
|
|
66
|
+
type: 'number',
|
|
67
|
+
description: 'Requested session length in minutes',
|
|
68
|
+
},
|
|
54
69
|
],
|
|
55
70
|
}
|
|
@@ -1,18 +1,29 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { GetResource, PatchResourceContent } from '@ossy/resources'
|
|
2
2
|
import BookingCancellationEmail from './booking-cancellation.email.jsx'
|
|
3
3
|
|
|
4
4
|
export const metadata = { id: '@ossy/booking/tasks/cancel' }
|
|
5
5
|
|
|
6
|
-
|
|
6
|
+
async function loadBooking (sdk, bookingId) {
|
|
7
|
+
try {
|
|
8
|
+
const booking = await sdk.invoke(GetResource, { resourceId: bookingId })
|
|
9
|
+
if (!booking?.id) throw Object.assign(new Error('Booking not found'), { status: 404 })
|
|
10
|
+
return booking
|
|
11
|
+
} catch (err) {
|
|
12
|
+
if (err?.status === 404) throw Object.assign(new Error('Booking not found'), { status: 404 })
|
|
13
|
+
throw err
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export async function run ({ payload, req, log, integrations, sdk }) {
|
|
7
18
|
const bookingId = payload?.bookingId ?? req?.params?.bookingId
|
|
8
19
|
const workspaceId = payload?.workspaceId ?? req?.workspaceId
|
|
9
20
|
|
|
10
21
|
if (!bookingId) throw Object.assign(new Error('bookingId is required'), { status: 400 })
|
|
22
|
+
if (!sdk?.invoke) throw Object.assign(new Error('sdk is required'), { status: 500 })
|
|
11
23
|
|
|
12
24
|
log?.info(`[booking/tasks/cancel] Cancelling booking ${bookingId}`)
|
|
13
25
|
|
|
14
|
-
const booking = await
|
|
15
|
-
if (!booking?.id) throw Object.assign(new Error('Booking not found'), { status: 404 })
|
|
26
|
+
const booking = await loadBooking(sdk, bookingId)
|
|
16
27
|
|
|
17
28
|
if (booking.belongsTo !== workspaceId) {
|
|
18
29
|
throw Object.assign(new Error('You do not have permission to cancel this booking'), { status: 403 })
|
|
@@ -22,15 +33,10 @@ export async function run ({ payload, req, log, integrations }) {
|
|
|
22
33
|
throw Object.assign(new Error('Booking is already cancelled'), { status: 409 })
|
|
23
34
|
}
|
|
24
35
|
|
|
25
|
-
await
|
|
26
|
-
bookingId,
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
// Required for BookingListProjection.scopeFromEvent (live projection updates).
|
|
30
|
-
belongsTo: booking.belongsTo,
|
|
31
|
-
content: { ...booking.content, status: 'cancelled' },
|
|
32
|
-
}),
|
|
33
|
-
)
|
|
36
|
+
await sdk.invoke(PatchResourceContent, {
|
|
37
|
+
resourceId: bookingId,
|
|
38
|
+
content: { status: 'cancelled' },
|
|
39
|
+
})
|
|
34
40
|
|
|
35
41
|
log?.info(`[booking/tasks/cancel] Booking ${bookingId} cancelled`)
|
|
36
42
|
|
|
@@ -39,11 +39,19 @@ export default {
|
|
|
39
39
|
|
|
40
40
|
// Catalog service
|
|
41
41
|
{ action: OpenServices },
|
|
42
|
-
{
|
|
42
|
+
{
|
|
43
|
+
result: {
|
|
44
|
+
selector: '[data-services-status="empty"], [data-services-status="populated"]',
|
|
45
|
+
timeout: 20000,
|
|
46
|
+
},
|
|
47
|
+
},
|
|
48
|
+
{ press: 'Escape' },
|
|
43
49
|
{ result: { action: OpenCreateService, timeout: 10000 } },
|
|
44
50
|
{ action: OpenCreateService },
|
|
51
|
+
{ result: { selector: '[data-services-form="open"]', timeout: 10000 } },
|
|
45
52
|
{ result: { action: CreateService, timeout: 15000 } },
|
|
46
|
-
|
|
53
|
+
// Pin duration to a catalog select option ('30'|'60'|'90').
|
|
54
|
+
{ form: ServiceForm, fields: { duration: '60' } },
|
|
47
55
|
{ action: CreateService },
|
|
48
56
|
{ result: { action: OpenDeleteService, timeout: 15000 } },
|
|
49
57
|
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { ActionService } from '@ossy/platform'
|
|
2
|
+
import { createInProcessSdk } from '@ossy/platform/tasks'
|
|
2
3
|
import { verifyBookingEmailToken } from './booking-email-token.js'
|
|
3
4
|
|
|
4
5
|
export const metadata = {
|
|
@@ -22,6 +23,8 @@ export default async function handle (req, res) {
|
|
|
22
23
|
await ActionService.invoke('@ossy/booking/actions/confirm', {
|
|
23
24
|
payload: { bookingId, workspaceId },
|
|
24
25
|
req: { ...req, workspaceId },
|
|
26
|
+
// Email-token confirm runs as a workspace-scoped bot, not the visitor.
|
|
27
|
+
sdk: createInProcessSdk().forWorkspace(workspaceId),
|
|
25
28
|
})
|
|
26
29
|
res.status(200).send(htmlPage('Bokning bekräftad', 'Bokningen är bekräftad. Klienten har fått ett bekräftelsemejl.'))
|
|
27
30
|
} catch (err) {
|
|
@@ -1,22 +1,33 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { GetResource, PatchResourceContent } from '@ossy/resources'
|
|
2
2
|
import BookingConfirmationEmail, { id as bookingConfirmationEmailId } from './booking-confirmation.email.jsx'
|
|
3
3
|
import { resolveProviderName, workspaceIdFromContent } from './provider-fields.js'
|
|
4
4
|
import { resolveWorkspaceContact } from './resolve-workspace-contact.js'
|
|
5
5
|
|
|
6
6
|
export const metadata = { id: '@ossy/booking/tasks/confirm' }
|
|
7
7
|
|
|
8
|
-
|
|
8
|
+
async function loadBooking (sdk, bookingId) {
|
|
9
|
+
try {
|
|
10
|
+
const booking = await sdk.invoke(GetResource, { resourceId: bookingId })
|
|
11
|
+
if (!booking?.id) throw Object.assign(new Error('Booking not found'), { status: 404 })
|
|
12
|
+
return booking
|
|
13
|
+
} catch (err) {
|
|
14
|
+
if (err?.status === 404) throw Object.assign(new Error('Booking not found'), { status: 404 })
|
|
15
|
+
throw err
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export async function run ({ payload, req, log, integrations, sdk }) {
|
|
9
20
|
const bookingId = payload?.bookingId ?? req?.params?.bookingId
|
|
10
21
|
const workspaceId = payload?.workspaceId ?? req?.workspaceId
|
|
11
22
|
const providerName = resolveProviderName(payload ?? {})
|
|
12
23
|
const { baseUrl } = payload ?? {}
|
|
13
24
|
|
|
14
25
|
if (!bookingId) throw Object.assign(new Error('bookingId is required'), { status: 400 })
|
|
26
|
+
if (!sdk?.invoke) throw Object.assign(new Error('sdk is required'), { status: 500 })
|
|
15
27
|
|
|
16
28
|
log?.info(`[booking/tasks/confirm] Confirming booking ${bookingId}`)
|
|
17
29
|
|
|
18
|
-
const booking = await
|
|
19
|
-
if (!booking?.id) throw Object.assign(new Error('Booking not found'), { status: 404 })
|
|
30
|
+
const booking = await loadBooking(sdk, bookingId)
|
|
20
31
|
|
|
21
32
|
if (booking.belongsTo !== workspaceId) {
|
|
22
33
|
throw Object.assign(new Error('You do not have permission to confirm this booking'), { status: 403 })
|
|
@@ -29,15 +40,10 @@ export async function run ({ payload, req, log, integrations }) {
|
|
|
29
40
|
)
|
|
30
41
|
}
|
|
31
42
|
|
|
32
|
-
await
|
|
33
|
-
bookingId,
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
// Required for BookingListProjection.scopeFromEvent (live projection updates).
|
|
37
|
-
belongsTo: booking.belongsTo,
|
|
38
|
-
content: { ...booking.content, status: 'confirmed' },
|
|
39
|
-
}),
|
|
40
|
-
)
|
|
43
|
+
await sdk.invoke(PatchResourceContent, {
|
|
44
|
+
resourceId: bookingId,
|
|
45
|
+
content: { status: 'confirmed' },
|
|
46
|
+
})
|
|
41
47
|
|
|
42
48
|
log?.info(`[booking/tasks/confirm] Booking ${bookingId} confirmed`)
|
|
43
49
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { randomUUID } from 'node:crypto'
|
|
2
2
|
import { ConfigService } from '@ossy/config'
|
|
3
|
-
import {
|
|
3
|
+
import { CreateResource } from '@ossy/resources'
|
|
4
4
|
import { BookingSchema } from './schema-ids.js'
|
|
5
5
|
import { getAvailableSlots } from './availability-engine.js'
|
|
6
6
|
import { toMs } from './time.js'
|
|
@@ -13,10 +13,11 @@ import {
|
|
|
13
13
|
resolveProviderName,
|
|
14
14
|
} from './provider-fields.js'
|
|
15
15
|
import { resolveWorkspaceContact, resolveWorkspaceId } from './resolve-workspace-contact.js'
|
|
16
|
+
import { sdkForWorkspace } from './sdk-for-workspace.js'
|
|
16
17
|
|
|
17
18
|
export const metadata = { id: '@ossy/booking/tasks/create' }
|
|
18
19
|
|
|
19
|
-
export async function run({ payload, req, log, integrations }) {
|
|
20
|
+
export async function run({ payload, req, log, integrations, sdk }) {
|
|
20
21
|
const {
|
|
21
22
|
startAt,
|
|
22
23
|
duration,
|
|
@@ -30,12 +31,16 @@ export async function run({ payload, req, log, integrations }) {
|
|
|
30
31
|
declineUrl,
|
|
31
32
|
} = payload ?? {}
|
|
32
33
|
|
|
34
|
+
if (!sdk?.invoke) throw Object.assign(new Error('sdk is required'), { status: 500 })
|
|
35
|
+
|
|
33
36
|
const workspaceId = await resolveWorkspaceId(payload)
|
|
34
37
|
let providerName = resolveProviderName(payload)
|
|
35
38
|
let providerEmail = resolveProviderEmail(payload)
|
|
36
39
|
|
|
37
40
|
if (!workspaceId) throw Object.assign(new Error('workspaceId or providerSlug is required'), { status: 400 })
|
|
38
41
|
|
|
42
|
+
const workspaceSdk = sdkForWorkspace(sdk, workspaceId)
|
|
43
|
+
|
|
39
44
|
if (!providerEmail || !providerName) {
|
|
40
45
|
const contact = await resolveWorkspaceContact(workspaceId)
|
|
41
46
|
providerEmail = providerEmail ?? contact?.providerEmail
|
|
@@ -48,10 +53,10 @@ export async function run({ payload, req, log, integrations }) {
|
|
|
48
53
|
|
|
49
54
|
log?.info(`[booking/tasks/create] Creating booking for ${clientEmail} with workspace ${workspaceId}`)
|
|
50
55
|
|
|
51
|
-
// Re-validate slot availability
|
|
52
56
|
const availabilityResources = await getBookingResources({
|
|
53
57
|
schemaId: BookingSchema.availability,
|
|
54
58
|
belongsTo: workspaceId,
|
|
59
|
+
sdk: workspaceSdk,
|
|
55
60
|
})
|
|
56
61
|
|
|
57
62
|
// New format: single resource with weeklyWindows array in content
|
|
@@ -83,6 +88,7 @@ export async function run({ payload, req, log, integrations }) {
|
|
|
83
88
|
const allBookingResources = await getBookingResources({
|
|
84
89
|
schemaId: BookingSchema.booking,
|
|
85
90
|
belongsTo: workspaceId,
|
|
91
|
+
sdk: workspaceSdk,
|
|
86
92
|
})
|
|
87
93
|
|
|
88
94
|
const confirmedBookings = allBookingResources
|
|
@@ -114,32 +120,31 @@ export async function run({ payload, req, log, integrations }) {
|
|
|
114
120
|
throw Object.assign(new Error('The requested slot is no longer available'), { status: 409 })
|
|
115
121
|
}
|
|
116
122
|
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
123
|
+
const content = {
|
|
124
|
+
...(serviceId ? { serviceId } : {}),
|
|
125
|
+
clientName,
|
|
126
|
+
clientEmail,
|
|
127
|
+
workspaceId,
|
|
128
|
+
providerId: workspaceId,
|
|
129
|
+
startsAt: startMs,
|
|
130
|
+
endsAt,
|
|
131
|
+
status: 'pending',
|
|
132
|
+
paymentStatus: 'unpaid',
|
|
133
|
+
clientMessage: clientMessage ?? '',
|
|
134
|
+
attachments: Array.isArray(attachments) ? attachments : [],
|
|
135
|
+
duration,
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
const bookingNameId = randomUUID()
|
|
139
|
+
const booking = await workspaceSdk.invoke(CreateResource, {
|
|
140
|
+
workspaceId,
|
|
141
|
+
type: BookingSchema.booking,
|
|
124
142
|
location: bookings,
|
|
125
|
-
name: `booking-${
|
|
126
|
-
content
|
|
127
|
-
serviceId: serviceId ?? null,
|
|
128
|
-
clientName,
|
|
129
|
-
clientEmail,
|
|
130
|
-
clientMessage: clientMessage ?? '',
|
|
131
|
-
attachments: Array.isArray(attachments) ? attachments : [],
|
|
132
|
-
workspaceId,
|
|
133
|
-
providerId: workspaceId,
|
|
134
|
-
startsAt: startMs,
|
|
135
|
-
endsAt,
|
|
136
|
-
duration,
|
|
137
|
-
status: 'pending',
|
|
138
|
-
paymentStatus: 'unpaid',
|
|
139
|
-
},
|
|
143
|
+
name: `booking-${bookingNameId}.json`,
|
|
144
|
+
content,
|
|
140
145
|
})
|
|
141
146
|
|
|
142
|
-
const
|
|
147
|
+
const bookingId = booking.id
|
|
143
148
|
|
|
144
149
|
log?.info(`[booking/tasks/create] Booking ${bookingId} created`)
|
|
145
150
|
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { beforeEach, describe, expect, it, jest } from '@jest/globals'
|
|
2
|
+
|
|
3
|
+
const CreateResource = { id: '@ossy/resources/actions/create' }
|
|
4
|
+
|
|
5
|
+
await jest.unstable_mockModule('@ossy/resources', () => ({
|
|
6
|
+
CreateResource,
|
|
7
|
+
}))
|
|
8
|
+
|
|
9
|
+
await jest.unstable_mockModule('./availability-engine.js', () => ({
|
|
10
|
+
getAvailableSlots: () => [{ startAt: 1_700_000_000_000 }],
|
|
11
|
+
}))
|
|
12
|
+
|
|
13
|
+
await jest.unstable_mockModule('./booking-resources.js', () => ({
|
|
14
|
+
getBookingResources: jest.fn(async ({ schemaId }) => {
|
|
15
|
+
if (schemaId.includes('availability')) {
|
|
16
|
+
return [{
|
|
17
|
+
id: 'avail-1',
|
|
18
|
+
content: {
|
|
19
|
+
weeklyWindows: [{ dayOfWeek: 1, startTime: '09:00', endTime: '17:00' }],
|
|
20
|
+
bufferMinutes: 0,
|
|
21
|
+
timezone: 'Europe/Stockholm',
|
|
22
|
+
},
|
|
23
|
+
}]
|
|
24
|
+
}
|
|
25
|
+
return []
|
|
26
|
+
}),
|
|
27
|
+
}))
|
|
28
|
+
|
|
29
|
+
await jest.unstable_mockModule('./resolve-workspace-contact.js', () => ({
|
|
30
|
+
resolveWorkspaceId: async () => 'ws-1',
|
|
31
|
+
resolveWorkspaceContact: async () => ({
|
|
32
|
+
providerEmail: 'provider@example.com',
|
|
33
|
+
providerName: 'Provider',
|
|
34
|
+
}),
|
|
35
|
+
}))
|
|
36
|
+
|
|
37
|
+
await jest.unstable_mockModule('./provider-fields.js', () => ({
|
|
38
|
+
resolveProviderEmail: () => 'provider@example.com',
|
|
39
|
+
resolveProviderName: () => 'Provider',
|
|
40
|
+
}))
|
|
41
|
+
|
|
42
|
+
await jest.unstable_mockModule('./booking-email-token.js', () => ({
|
|
43
|
+
createBookingEmailToken: () => 'token',
|
|
44
|
+
}))
|
|
45
|
+
|
|
46
|
+
await jest.unstable_mockModule('./booking-request.email.jsx', () => ({
|
|
47
|
+
default: () => null,
|
|
48
|
+
id: '@ossy/booking/email/booking-request',
|
|
49
|
+
}))
|
|
50
|
+
|
|
51
|
+
const { run } = await import('./create-booking.task.js')
|
|
52
|
+
const { BookingSchema } = await import('./schema-ids.js')
|
|
53
|
+
|
|
54
|
+
describe('create-booking task', () => {
|
|
55
|
+
beforeEach(() => {
|
|
56
|
+
jest.clearAllMocks()
|
|
57
|
+
})
|
|
58
|
+
|
|
59
|
+
it('creates the booking with duration and message in one CreateResource', async () => {
|
|
60
|
+
const invoke = jest.fn(async (action, payload) => {
|
|
61
|
+
if (action === CreateResource) {
|
|
62
|
+
return { id: 'booking-1', content: payload.content }
|
|
63
|
+
}
|
|
64
|
+
throw new Error(`unexpected action ${action?.id ?? action}`)
|
|
65
|
+
})
|
|
66
|
+
|
|
67
|
+
const result = await run({
|
|
68
|
+
payload: {
|
|
69
|
+
startAt: 1_700_000_000_000,
|
|
70
|
+
duration: 60,
|
|
71
|
+
clientName: 'Ada',
|
|
72
|
+
clientEmail: 'ada@example.com',
|
|
73
|
+
clientMessage: 'Hello',
|
|
74
|
+
attachments: [{ name: 'brief.pdf' }],
|
|
75
|
+
serviceId: 'svc-1',
|
|
76
|
+
},
|
|
77
|
+
sdk: { invoke, forWorkspace: () => ({ invoke }) },
|
|
78
|
+
log: { info: () => {} },
|
|
79
|
+
})
|
|
80
|
+
|
|
81
|
+
expect(invoke).toHaveBeenCalledTimes(1)
|
|
82
|
+
expect(invoke.mock.calls[0][0]).toBe(CreateResource)
|
|
83
|
+
expect(invoke.mock.calls[0][1]).toMatchObject({
|
|
84
|
+
workspaceId: 'ws-1',
|
|
85
|
+
type: BookingSchema.booking,
|
|
86
|
+
content: {
|
|
87
|
+
serviceId: 'svc-1',
|
|
88
|
+
clientName: 'Ada',
|
|
89
|
+
clientEmail: 'ada@example.com',
|
|
90
|
+
clientMessage: 'Hello',
|
|
91
|
+
attachments: [{ name: 'brief.pdf' }],
|
|
92
|
+
duration: 60,
|
|
93
|
+
status: 'pending',
|
|
94
|
+
paymentStatus: 'unpaid',
|
|
95
|
+
startsAt: 1_700_000_000_000,
|
|
96
|
+
endsAt: 1_700_000_000_000 + 60 * 60 * 1000,
|
|
97
|
+
},
|
|
98
|
+
})
|
|
99
|
+
expect(invoke.mock.calls[0][1].name).toMatch(/^booking-.+\.json$/)
|
|
100
|
+
expect(result).toMatchObject({
|
|
101
|
+
id: 'booking-1',
|
|
102
|
+
content: {
|
|
103
|
+
duration: 60,
|
|
104
|
+
clientMessage: 'Hello',
|
|
105
|
+
},
|
|
106
|
+
})
|
|
107
|
+
})
|
|
108
|
+
})
|
|
@@ -1,16 +1,17 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { ResourcesEvents, commitResource } from '@ossy/resources/server'
|
|
1
|
+
import { CreateResource } from '@ossy/resources'
|
|
3
2
|
import { BookingSchema } from './schema-ids.js'
|
|
4
3
|
import { services } from './locations.js'
|
|
4
|
+
import { serviceDurationMinutes } from './service-duration.js'
|
|
5
5
|
|
|
6
6
|
export const metadata = { id: '@ossy/booking/tasks/create-service' }
|
|
7
7
|
|
|
8
|
-
export async function run ({ payload, req, log }) {
|
|
8
|
+
export async function run ({ payload, req, log, sdk }) {
|
|
9
9
|
const workspaceId = req?.workspaceId
|
|
10
10
|
|
|
11
11
|
if (!workspaceId) {
|
|
12
12
|
throw Object.assign(new Error('workspaceId is required'), { status: 400 })
|
|
13
13
|
}
|
|
14
|
+
if (!sdk?.invoke) throw Object.assign(new Error('sdk is required'), { status: 500 })
|
|
14
15
|
|
|
15
16
|
const { name, duration, price, currency, description, active } = payload ?? {}
|
|
16
17
|
|
|
@@ -18,29 +19,24 @@ export async function run ({ payload, req, log }) {
|
|
|
18
19
|
throw Object.assign(new Error('name is required'), { status: 400 })
|
|
19
20
|
}
|
|
20
21
|
|
|
21
|
-
const resourceId = nanoid()
|
|
22
22
|
const content = {
|
|
23
23
|
name: name.trim(),
|
|
24
|
-
duration:
|
|
24
|
+
duration: serviceDurationMinutes(duration),
|
|
25
25
|
price: Number(price) || 0,
|
|
26
26
|
currency: currency ?? 'SEK',
|
|
27
27
|
description: description?.trim() ?? '',
|
|
28
28
|
active: active !== false,
|
|
29
29
|
}
|
|
30
30
|
|
|
31
|
-
log?.info(`[booking/tasks/create-service] Creating service
|
|
31
|
+
log?.info(`[booking/tasks/create-service] Creating service for workspace ${workspaceId}`)
|
|
32
32
|
|
|
33
|
-
const
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
createdBy: req?.userId ?? 'system',
|
|
37
|
-
belongsTo: workspaceId,
|
|
33
|
+
const service = await sdk.invoke(CreateResource, {
|
|
34
|
+
workspaceId,
|
|
35
|
+
type: BookingSchema.service,
|
|
38
36
|
location: services,
|
|
39
37
|
name: `${content.name.replace(/\s+/g, '-').toLowerCase()}.json`,
|
|
40
38
|
content,
|
|
41
39
|
})
|
|
42
40
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
return { id: resourceId, ...content, ...service }
|
|
41
|
+
return { id: service.id, ...content, ...service }
|
|
46
42
|
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { ActionService } from '@ossy/platform'
|
|
2
|
+
import { createInProcessSdk } from '@ossy/platform/tasks'
|
|
2
3
|
import { verifyBookingEmailToken } from './booking-email-token.js'
|
|
3
4
|
|
|
4
5
|
export const metadata = {
|
|
@@ -22,6 +23,8 @@ export default async function handle (req, res) {
|
|
|
22
23
|
await ActionService.invoke('@ossy/booking/actions/decline', {
|
|
23
24
|
payload: { bookingId, workspaceId },
|
|
24
25
|
req: { ...req, workspaceId },
|
|
26
|
+
// Email-token decline runs as a workspace-scoped bot, not the visitor.
|
|
27
|
+
sdk: createInProcessSdk().forWorkspace(workspaceId),
|
|
25
28
|
})
|
|
26
29
|
res.status(200).send(htmlPage('Bokning avböjd', 'Bokningen har avböjts. Klienten har fått ett meddelande.'))
|
|
27
30
|
} catch (err) {
|
|
@@ -1,19 +1,30 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { GetResource, PatchResourceContent } from '@ossy/resources'
|
|
2
2
|
import BookingCancellationEmail from './booking-cancellation.email.jsx'
|
|
3
3
|
|
|
4
4
|
export const metadata = { id: '@ossy/booking/tasks/decline' }
|
|
5
5
|
|
|
6
|
-
|
|
6
|
+
async function loadBooking (sdk, bookingId) {
|
|
7
|
+
try {
|
|
8
|
+
const booking = await sdk.invoke(GetResource, { resourceId: bookingId })
|
|
9
|
+
if (!booking?.id) throw Object.assign(new Error('Booking not found'), { status: 404 })
|
|
10
|
+
return booking
|
|
11
|
+
} catch (err) {
|
|
12
|
+
if (err?.status === 404) throw Object.assign(new Error('Booking not found'), { status: 404 })
|
|
13
|
+
throw err
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export async function run ({ payload, req, log, integrations, sdk }) {
|
|
7
18
|
const bookingId = payload?.bookingId ?? req?.params?.bookingId
|
|
8
19
|
const workspaceId = payload?.workspaceId ?? req?.workspaceId
|
|
9
20
|
const reason = payload?.reason ?? null
|
|
10
21
|
|
|
11
22
|
if (!bookingId) throw Object.assign(new Error('bookingId is required'), { status: 400 })
|
|
23
|
+
if (!sdk?.invoke) throw Object.assign(new Error('sdk is required'), { status: 500 })
|
|
12
24
|
|
|
13
25
|
log?.info(`[booking/tasks/decline] Declining booking ${bookingId}`)
|
|
14
26
|
|
|
15
|
-
const booking = await
|
|
16
|
-
if (!booking?.id) throw Object.assign(new Error('Booking not found'), { status: 404 })
|
|
27
|
+
const booking = await loadBooking(sdk, bookingId)
|
|
17
28
|
|
|
18
29
|
if (booking.belongsTo !== workspaceId) {
|
|
19
30
|
throw Object.assign(new Error('You do not have permission to decline this booking'), { status: 403 })
|
|
@@ -27,15 +38,10 @@ export async function run ({ payload, req, log, integrations }) {
|
|
|
27
38
|
throw Object.assign(new Error('Use cancel-booking to cancel an already confirmed booking'), { status: 409 })
|
|
28
39
|
}
|
|
29
40
|
|
|
30
|
-
await
|
|
31
|
-
bookingId,
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
// Required for BookingListProjection.scopeFromEvent (live projection updates).
|
|
35
|
-
belongsTo: booking.belongsTo,
|
|
36
|
-
content: { ...booking.content, status: 'cancelled', declineReason: reason ?? '' },
|
|
37
|
-
}),
|
|
38
|
-
)
|
|
41
|
+
await sdk.invoke(PatchResourceContent, {
|
|
42
|
+
resourceId: bookingId,
|
|
43
|
+
content: { status: 'cancelled', declineReason: reason ?? '' },
|
|
44
|
+
})
|
|
39
45
|
|
|
40
46
|
log?.info(`[booking/tasks/decline] Booking ${bookingId} declined`)
|
|
41
47
|
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { DeleteResource } from '@ossy/resources'
|
|
2
2
|
import { BookingSchema } from './schema-ids.js'
|
|
3
3
|
import { getBookingResources } from './booking-resources.js'
|
|
4
4
|
|
|
5
5
|
export const metadata = { id: '@ossy/booking/tasks/delete-service' }
|
|
6
6
|
|
|
7
|
-
export async function run ({ payload, req, log }) {
|
|
7
|
+
export async function run ({ payload, req, log, sdk }) {
|
|
8
8
|
const workspaceId = req?.workspaceId
|
|
9
9
|
const { serviceId } = payload ?? {}
|
|
10
10
|
|
|
@@ -14,10 +14,12 @@ export async function run ({ payload, req, log }) {
|
|
|
14
14
|
if (!serviceId) {
|
|
15
15
|
throw Object.assign(new Error('serviceId is required'), { status: 400 })
|
|
16
16
|
}
|
|
17
|
+
if (!sdk?.invoke) throw Object.assign(new Error('sdk is required'), { status: 500 })
|
|
17
18
|
|
|
18
19
|
const existing = await getBookingResources({
|
|
19
20
|
schemaId: BookingSchema.service,
|
|
20
21
|
belongsTo: workspaceId,
|
|
22
|
+
sdk,
|
|
21
23
|
})
|
|
22
24
|
|
|
23
25
|
const resource = existing.find(r => r.id === serviceId)
|
|
@@ -27,12 +29,7 @@ export async function run ({ payload, req, log }) {
|
|
|
27
29
|
|
|
28
30
|
log?.info(`[booking/tasks/delete-service] Deleting service ${serviceId}`)
|
|
29
31
|
|
|
30
|
-
await
|
|
31
|
-
serviceId,
|
|
32
|
-
ResourcesEvents.Deleted({
|
|
33
|
-
createdBy: req?.userId ?? 'system',
|
|
34
|
-
}),
|
|
35
|
-
)
|
|
32
|
+
await sdk.invoke(DeleteResource, { resourceId: serviceId })
|
|
36
33
|
|
|
37
34
|
return { id: serviceId, deleted: true }
|
|
38
35
|
}
|
|
@@ -3,7 +3,7 @@ import { BookingSchema } from './schema-ids.js'
|
|
|
3
3
|
|
|
4
4
|
export const metadata = { id: '@ossy/booking/tasks/get-availability' }
|
|
5
5
|
|
|
6
|
-
export async function run({ payload, req, log }) {
|
|
6
|
+
export async function run({ payload, req, log, sdk }) {
|
|
7
7
|
const workspaceId = req?.workspaceId
|
|
8
8
|
|
|
9
9
|
if (!workspaceId) {
|
|
@@ -15,6 +15,7 @@ export async function run({ payload, req, log }) {
|
|
|
15
15
|
const resources = await getBookingResources({
|
|
16
16
|
schemaId: BookingSchema.availability,
|
|
17
17
|
belongsTo: workspaceId,
|
|
18
|
+
sdk,
|
|
18
19
|
})
|
|
19
20
|
|
|
20
21
|
if (!resources.length) {
|
|
@@ -2,13 +2,14 @@ import { getAvailableSlots } from './availability-engine.js'
|
|
|
2
2
|
import { getBookingResources } from './booking-resources.js'
|
|
3
3
|
import { BookingSchema } from './schema-ids.js'
|
|
4
4
|
import { resolveWorkspaceId } from './resolve-workspace-contact.js'
|
|
5
|
+
import { sdkForWorkspace } from './sdk-for-workspace.js'
|
|
5
6
|
|
|
6
7
|
export const metadata = { id: '@ossy/booking/tasks/get-available-slots' }
|
|
7
8
|
|
|
8
9
|
const DEFAULT_DURATION = 60 // minutes
|
|
9
10
|
const DEFAULT_RANGE_DAYS = 30
|
|
10
11
|
|
|
11
|
-
export async function run ({ payload, log }) {
|
|
12
|
+
export async function run ({ payload, log, sdk }) {
|
|
12
13
|
const { duration, serviceId, from, to } = payload ?? {}
|
|
13
14
|
const workspaceId = await resolveWorkspaceId(payload)
|
|
14
15
|
|
|
@@ -18,22 +19,26 @@ export async function run ({ payload, log }) {
|
|
|
18
19
|
|
|
19
20
|
log?.info(`[booking/tasks/get-available-slots] Loading slots for workspace ${workspaceId}`)
|
|
20
21
|
|
|
22
|
+
const workspaceSdk = sdkForWorkspace(sdk, workspaceId)
|
|
21
23
|
let slotDuration = duration
|
|
22
24
|
|
|
23
25
|
if (serviceId) {
|
|
24
26
|
const serviceResources = await getBookingResources({
|
|
25
27
|
schemaId: BookingSchema.service,
|
|
26
28
|
belongsTo: workspaceId,
|
|
29
|
+
sdk: workspaceSdk,
|
|
27
30
|
})
|
|
28
31
|
const service = serviceResources.find(r => r.id === serviceId)
|
|
29
|
-
if (service?.content?.duration) {
|
|
30
|
-
|
|
32
|
+
if (service?.content?.duration != null && service.content.duration !== '') {
|
|
33
|
+
// Coerce legacy string select values ('30'|'60'|'90') to minutes.
|
|
34
|
+
slotDuration = Number(service.content.duration)
|
|
31
35
|
}
|
|
32
36
|
}
|
|
33
37
|
|
|
34
38
|
const availabilityResources = await getBookingResources({
|
|
35
39
|
schemaId: BookingSchema.availability,
|
|
36
40
|
belongsTo: workspaceId,
|
|
41
|
+
sdk: workspaceSdk,
|
|
37
42
|
})
|
|
38
43
|
|
|
39
44
|
if (!availabilityResources.length) {
|
|
@@ -73,6 +78,7 @@ export async function run ({ payload, log }) {
|
|
|
73
78
|
const allBookingResources = await getBookingResources({
|
|
74
79
|
schemaId: BookingSchema.booking,
|
|
75
80
|
belongsTo: workspaceId,
|
|
81
|
+
sdk: workspaceSdk,
|
|
76
82
|
})
|
|
77
83
|
|
|
78
84
|
const confirmedBookings = allBookingResources
|
|
@@ -87,7 +93,7 @@ export async function run ({ payload, log }) {
|
|
|
87
93
|
? (configResource.content.sessionDurations ?? [DEFAULT_DURATION])
|
|
88
94
|
: [firstResource?.content?.sessionDuration ?? DEFAULT_DURATION]
|
|
89
95
|
|
|
90
|
-
const resolvedDuration = slotDuration ?? sessionDurations[0] ?? DEFAULT_DURATION
|
|
96
|
+
const resolvedDuration = Number(slotDuration ?? sessionDurations[0] ?? DEFAULT_DURATION) || DEFAULT_DURATION
|
|
91
97
|
|
|
92
98
|
const slots = getAvailableSlots({
|
|
93
99
|
availability,
|
package/src/get-services.task.js
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import { getBookingResources } from './booking-resources.js'
|
|
2
2
|
import { BookingSchema } from './schema-ids.js'
|
|
3
3
|
import { resolveWorkspaceContact, resolveWorkspaceId } from './resolve-workspace-contact.js'
|
|
4
|
+
import { sdkForWorkspace } from './sdk-for-workspace.js'
|
|
4
5
|
|
|
5
6
|
export const metadata = { id: '@ossy/booking/tasks/get-services' }
|
|
6
7
|
|
|
7
|
-
export async function run ({ payload, req, log }) {
|
|
8
|
+
export async function run ({ payload, req, log, sdk }) {
|
|
8
9
|
const workspaceId = await resolveWorkspaceId(payload) ?? payload?.workspaceId ?? req?.workspaceId
|
|
9
10
|
|
|
10
11
|
if (!workspaceId) {
|
|
@@ -16,6 +17,7 @@ export async function run ({ payload, req, log }) {
|
|
|
16
17
|
const resources = await getBookingResources({
|
|
17
18
|
schemaId: BookingSchema.service,
|
|
18
19
|
belongsTo: workspaceId,
|
|
20
|
+
sdk: sdkForWorkspace(sdk, workspaceId),
|
|
19
21
|
})
|
|
20
22
|
|
|
21
23
|
const includeInactive = payload?.includeInactive === true
|
|
@@ -24,7 +26,7 @@ export async function run ({ payload, req, log }) {
|
|
|
24
26
|
.map(r => ({
|
|
25
27
|
id: r.id,
|
|
26
28
|
name: r.content?.name ?? r.name,
|
|
27
|
-
duration: r.content?.duration ?? 60,
|
|
29
|
+
duration: Number(r.content?.duration ?? 60) || 60,
|
|
28
30
|
price: r.content?.price ?? 0,
|
|
29
31
|
currency: r.content?.currency ?? 'SEK',
|
|
30
32
|
description: r.content?.description ?? '',
|
|
@@ -6,12 +6,22 @@ import { toMs } from './time.js'
|
|
|
6
6
|
|
|
7
7
|
export const metadata = { id: '@ossy/booking/tasks/list' }
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
function isWorkspaceMember (req, payload, workspaceId) {
|
|
10
|
+
const user = payload?.user ?? req?.user
|
|
11
|
+
if (user) return Boolean(user.workspaces?.includes(workspaceId))
|
|
12
|
+
const userId = payload?.userId ?? req?.userId
|
|
13
|
+
return userId === 'system'
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export async function run({ payload, req, log, sdk }) {
|
|
10
17
|
const workspaceId = req?.workspaceId
|
|
11
18
|
const status = payload?.status ?? null
|
|
12
19
|
|
|
20
|
+
const member = isWorkspaceMember(req, payload, workspaceId)
|
|
13
21
|
log?.info(`[booking/tasks/list] Loading bookings for workspace ${workspaceId}`, { status })
|
|
14
22
|
|
|
23
|
+
if (!member) return []
|
|
24
|
+
|
|
15
25
|
let bookings = await loadFromProjection(workspaceId)
|
|
16
26
|
|
|
17
27
|
if (bookings == null) {
|
|
@@ -19,6 +29,7 @@ export async function run({ payload, req, log }) {
|
|
|
19
29
|
const resources = await getBookingResources({
|
|
20
30
|
schemaId: BookingSchema.booking,
|
|
21
31
|
belongsTo: workspaceId,
|
|
32
|
+
sdk,
|
|
22
33
|
})
|
|
23
34
|
bookings = resources.map(r => ({
|
|
24
35
|
id: r.id,
|
|
@@ -24,11 +24,21 @@ export default {
|
|
|
24
24
|
steps: [
|
|
25
25
|
...providerOnboard.steps,
|
|
26
26
|
{ action: OpenServices },
|
|
27
|
-
|
|
27
|
+
// Wait until the list finished loading — clicking Add during `loading` can
|
|
28
|
+
// lose the open-form state when the first GetServices resolve remounts.
|
|
29
|
+
{
|
|
30
|
+
result: {
|
|
31
|
+
selector: '[data-services-status="empty"], [data-services-status="populated"]',
|
|
32
|
+
timeout: 20000,
|
|
33
|
+
},
|
|
34
|
+
},
|
|
35
|
+
{ press: 'Escape' },
|
|
28
36
|
{ result: { action: OpenCreateService, timeout: 10000 } },
|
|
29
37
|
{ action: OpenCreateService },
|
|
38
|
+
{ result: { selector: '[data-services-form="open"]', timeout: 10000 } },
|
|
30
39
|
{ result: { action: CreateService, timeout: 15000 } },
|
|
31
|
-
|
|
40
|
+
// Pin duration to a catalog select option ('30'|'60'|'90').
|
|
41
|
+
{ form: ServiceForm, fields: { duration: '60' } },
|
|
32
42
|
{ action: CreateService },
|
|
33
43
|
{ result: { action: OpenDeleteService, timeout: 15000 } },
|
|
34
44
|
],
|
|
@@ -250,7 +250,18 @@ export default function PublicBookingPage ({ providerSlug }) {
|
|
|
250
250
|
from,
|
|
251
251
|
to,
|
|
252
252
|
})
|
|
253
|
-
.then(data => {
|
|
253
|
+
.then(data => {
|
|
254
|
+
setSlots(data)
|
|
255
|
+
setLoadingSlots(false)
|
|
256
|
+
// Auto-advance the calendar to the first month that has available slots
|
|
257
|
+
// so the date picker is immediately usable (avoids end-of-month flakes
|
|
258
|
+
// where the current month has no future slots but next month does).
|
|
259
|
+
if (Array.isArray(data) && data.length > 0) {
|
|
260
|
+
const first = new Date(data[0].startAt)
|
|
261
|
+
setCalYear(first.getFullYear())
|
|
262
|
+
setCalMonth(first.getMonth())
|
|
263
|
+
}
|
|
264
|
+
})
|
|
254
265
|
.catch(() => { setSlotsError(t('publicBooking.slotsError')); setLoadingSlots(false) })
|
|
255
266
|
}, [slug, selectedService, step, sdk, t])
|
|
256
267
|
|
|
@@ -1,17 +1,17 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { ResourcesEvents, commitResource, mutateResource } from '@ossy/resources/server'
|
|
1
|
+
import { CreateResource, PatchResourceContent } from '@ossy/resources'
|
|
3
2
|
import { BookingSchema } from './schema-ids.js'
|
|
4
3
|
import { getBookingResources } from './booking-resources.js'
|
|
5
4
|
import { availability } from './locations.js'
|
|
6
5
|
|
|
7
6
|
export const metadata = { id: '@ossy/booking/tasks/save-availability' }
|
|
8
7
|
|
|
9
|
-
export async function run ({ payload, req, log }) {
|
|
8
|
+
export async function run ({ payload, req, log, sdk }) {
|
|
10
9
|
const workspaceId = req?.workspaceId
|
|
11
10
|
|
|
12
11
|
if (!workspaceId) {
|
|
13
12
|
throw Object.assign(new Error('workspaceId is required'), { status: 400 })
|
|
14
13
|
}
|
|
14
|
+
if (!sdk?.invoke) throw Object.assign(new Error('sdk is required'), { status: 500 })
|
|
15
15
|
|
|
16
16
|
const { weeklyWindows, sessionDurations, bufferMinutes, timezone } = payload ?? {}
|
|
17
17
|
|
|
@@ -33,6 +33,7 @@ export async function run ({ payload, req, log }) {
|
|
|
33
33
|
const existing = await getBookingResources({
|
|
34
34
|
schemaId: BookingSchema.availability,
|
|
35
35
|
belongsTo: workspaceId,
|
|
36
|
+
sdk,
|
|
36
37
|
})
|
|
37
38
|
|
|
38
39
|
const configResource = existing.find(r => Array.isArray(r.content?.weeklyWindows))
|
|
@@ -40,33 +41,26 @@ export async function run ({ payload, req, log }) {
|
|
|
40
41
|
if (configResource) {
|
|
41
42
|
log?.info(`[booking/tasks/save-availability] Updating existing resource ${configResource.id}`)
|
|
42
43
|
|
|
43
|
-
await
|
|
44
|
-
configResource.id,
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
content,
|
|
48
|
-
}),
|
|
49
|
-
)
|
|
44
|
+
await sdk.invoke(PatchResourceContent, {
|
|
45
|
+
resourceId: configResource.id,
|
|
46
|
+
content,
|
|
47
|
+
})
|
|
50
48
|
|
|
51
49
|
return { id: configResource.id, content }
|
|
52
50
|
}
|
|
53
51
|
|
|
54
|
-
|
|
55
|
-
log?.info(`[booking/tasks/save-availability] Creating new
|
|
52
|
+
// Schema includes weeklyWindows / sessionDurations / bufferMinutes — create atomically.
|
|
53
|
+
log?.info(`[booking/tasks/save-availability] Creating new availability config`)
|
|
56
54
|
|
|
57
|
-
const
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
createdBy: req?.userId ?? 'system',
|
|
61
|
-
belongsTo: workspaceId,
|
|
55
|
+
const resource = await sdk.invoke(CreateResource, {
|
|
56
|
+
workspaceId,
|
|
57
|
+
type: BookingSchema.availability,
|
|
62
58
|
location: availability,
|
|
63
59
|
name: 'availability-config.json',
|
|
64
60
|
content,
|
|
65
61
|
})
|
|
66
62
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
log?.info(`[booking/tasks/save-availability] Resource ${resourceId} created`)
|
|
63
|
+
log?.info(`[booking/tasks/save-availability] Resource ${resource.id} created`)
|
|
70
64
|
|
|
71
|
-
return { id:
|
|
65
|
+
return { id: resource.id, content }
|
|
72
66
|
}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { beforeEach, describe, expect, it, jest } from '@jest/globals'
|
|
2
|
+
|
|
3
|
+
const CreateResource = { id: '@ossy/resources/actions/create' }
|
|
4
|
+
const PatchResourceContent = { id: '@ossy/resources/actions/patch-content' }
|
|
5
|
+
|
|
6
|
+
await jest.unstable_mockModule('@ossy/resources', () => ({
|
|
7
|
+
CreateResource,
|
|
8
|
+
PatchResourceContent,
|
|
9
|
+
}))
|
|
10
|
+
|
|
11
|
+
await jest.unstable_mockModule('./booking-resources.js', () => ({
|
|
12
|
+
getBookingResources: jest.fn(),
|
|
13
|
+
}))
|
|
14
|
+
|
|
15
|
+
const { run } = await import('./save-availability.task.js')
|
|
16
|
+
const { getBookingResources } = await import('./booking-resources.js')
|
|
17
|
+
const { BookingSchema } = await import('./schema-ids.js')
|
|
18
|
+
|
|
19
|
+
describe('save-availability task', () => {
|
|
20
|
+
beforeEach(() => {
|
|
21
|
+
jest.clearAllMocks()
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
it('creates availability with full content in one CreateResource', async () => {
|
|
25
|
+
getBookingResources.mockResolvedValue([])
|
|
26
|
+
const invoke = jest.fn(async (action, payload) => {
|
|
27
|
+
if (action === CreateResource) {
|
|
28
|
+
return { id: 'avail-1', content: payload.content }
|
|
29
|
+
}
|
|
30
|
+
throw new Error(`unexpected action ${action?.id ?? action}`)
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
const result = await run({
|
|
34
|
+
payload: {
|
|
35
|
+
weeklyWindows: [{ dayOfWeek: 1, startTime: '09:00', endTime: '17:00' }],
|
|
36
|
+
sessionDurations: [30, 60],
|
|
37
|
+
bufferMinutes: 15,
|
|
38
|
+
timezone: 'Europe/Stockholm',
|
|
39
|
+
},
|
|
40
|
+
req: { workspaceId: 'ws-1' },
|
|
41
|
+
sdk: { invoke },
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
expect(invoke).toHaveBeenCalledTimes(1)
|
|
45
|
+
expect(invoke.mock.calls[0][0]).toBe(CreateResource)
|
|
46
|
+
expect(invoke.mock.calls[0][1]).toMatchObject({
|
|
47
|
+
workspaceId: 'ws-1',
|
|
48
|
+
type: BookingSchema.availability,
|
|
49
|
+
name: 'availability-config.json',
|
|
50
|
+
content: {
|
|
51
|
+
weeklyWindows: [{ dayOfWeek: 1, startTime: '09:00', endTime: '17:00' }],
|
|
52
|
+
sessionDurations: [30, 60],
|
|
53
|
+
bufferMinutes: 15,
|
|
54
|
+
timezone: 'Europe/Stockholm',
|
|
55
|
+
},
|
|
56
|
+
})
|
|
57
|
+
expect(result).toEqual({
|
|
58
|
+
id: 'avail-1',
|
|
59
|
+
content: {
|
|
60
|
+
weeklyWindows: [{ dayOfWeek: 1, startTime: '09:00', endTime: '17:00' }],
|
|
61
|
+
sessionDurations: [30, 60],
|
|
62
|
+
bufferMinutes: 15,
|
|
63
|
+
timezone: 'Europe/Stockholm',
|
|
64
|
+
},
|
|
65
|
+
})
|
|
66
|
+
})
|
|
67
|
+
|
|
68
|
+
it('patches an existing weeklyWindows config resource', async () => {
|
|
69
|
+
getBookingResources.mockResolvedValue([
|
|
70
|
+
{ id: 'avail-existing', content: { weeklyWindows: [] } },
|
|
71
|
+
])
|
|
72
|
+
const invoke = jest.fn(async (action, payload) => {
|
|
73
|
+
if (action === PatchResourceContent) {
|
|
74
|
+
return { id: payload.resourceId, content: payload.content }
|
|
75
|
+
}
|
|
76
|
+
throw new Error(`unexpected action ${action?.id ?? action}`)
|
|
77
|
+
})
|
|
78
|
+
|
|
79
|
+
const result = await run({
|
|
80
|
+
payload: {
|
|
81
|
+
weeklyWindows: [{ dayOfWeek: 2, startTime: '10:00', endTime: '16:00' }],
|
|
82
|
+
bufferMinutes: 0,
|
|
83
|
+
},
|
|
84
|
+
req: { workspaceId: 'ws-1' },
|
|
85
|
+
sdk: { invoke },
|
|
86
|
+
})
|
|
87
|
+
|
|
88
|
+
expect(invoke).toHaveBeenCalledTimes(1)
|
|
89
|
+
expect(invoke.mock.calls[0][0]).toBe(PatchResourceContent)
|
|
90
|
+
expect(invoke.mock.calls[0][1]).toMatchObject({
|
|
91
|
+
resourceId: 'avail-existing',
|
|
92
|
+
content: {
|
|
93
|
+
weeklyWindows: [{ dayOfWeek: 2, startTime: '10:00', endTime: '16:00' }],
|
|
94
|
+
sessionDurations: [60],
|
|
95
|
+
bufferMinutes: 0,
|
|
96
|
+
timezone: 'Europe/Stockholm',
|
|
97
|
+
},
|
|
98
|
+
})
|
|
99
|
+
expect(result.id).toBe('avail-existing')
|
|
100
|
+
})
|
|
101
|
+
})
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Bot limited to one workspace. Public booking and email-token flows use this
|
|
3
|
+
* so the visitor is not the resource actor.
|
|
4
|
+
*
|
|
5
|
+
* @param {{ forWorkspace?: Function }} sdk
|
|
6
|
+
* @param {string} workspaceId
|
|
7
|
+
*/
|
|
8
|
+
export function sdkForWorkspace (sdk, workspaceId) {
|
|
9
|
+
if (!workspaceId) {
|
|
10
|
+
throw Object.assign(new Error('workspaceId is required'), { status: 400 })
|
|
11
|
+
}
|
|
12
|
+
if (typeof sdk?.forWorkspace !== 'function') {
|
|
13
|
+
throw Object.assign(new Error('sdk.forWorkspace is required'), { status: 500 })
|
|
14
|
+
}
|
|
15
|
+
return sdk.forWorkspace(workspaceId)
|
|
16
|
+
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { PatchResourceContent } from '@ossy/resources'
|
|
1
2
|
import { getBookingResources } from './booking-resources.js'
|
|
2
3
|
import { BookingSchema } from './schema-ids.js'
|
|
3
4
|
import BookingReminderEmail from './booking-reminder.email.jsx'
|
|
@@ -19,6 +20,7 @@ export async function run({ sdk, log, integrations }) {
|
|
|
19
20
|
|
|
20
21
|
const allBookings = await getBookingResources({
|
|
21
22
|
schemaId: BookingSchema.booking,
|
|
23
|
+
sdk,
|
|
22
24
|
})
|
|
23
25
|
|
|
24
26
|
const upcoming = allBookings.filter(b => {
|
|
@@ -80,13 +82,10 @@ export async function run({ sdk, log, integrations }) {
|
|
|
80
82
|
)
|
|
81
83
|
}
|
|
82
84
|
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
if (sdk) {
|
|
86
|
-
await sdk.actions.run('@ossy/resources/actions/update-content', {
|
|
85
|
+
if (sdk?.invoke) {
|
|
86
|
+
await sdk.invoke(PatchResourceContent, {
|
|
87
87
|
resourceId: booking.id,
|
|
88
|
-
content: {
|
|
89
|
-
workspaceId,
|
|
88
|
+
content: { reminderSentAt: new Date().toISOString() },
|
|
90
89
|
})
|
|
91
90
|
}
|
|
92
91
|
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/** Allowed catalog durations (minutes). UI may still present these as select strings. */
|
|
2
|
+
export const SERVICE_DURATION_MINUTES = [30, 60, 90]
|
|
3
|
+
|
|
4
|
+
const ALLOWED = new Set(SERVICE_DURATION_MINUTES)
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Normalize create/update payloads to a number of minutes.
|
|
8
|
+
* Accepts number or numeric string; unknown values fall back to 60.
|
|
9
|
+
*
|
|
10
|
+
* @param {unknown} duration
|
|
11
|
+
* @returns {number}
|
|
12
|
+
*/
|
|
13
|
+
export function serviceDurationMinutes (duration) {
|
|
14
|
+
const minutes = Number(duration)
|
|
15
|
+
if (!Number.isFinite(minutes)) return 60
|
|
16
|
+
const rounded = Math.round(minutes)
|
|
17
|
+
return ALLOWED.has(rounded) ? rounded : 60
|
|
18
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { describe, it, expect } from '@jest/globals'
|
|
2
|
+
import { serviceDurationMinutes } from './service-duration.js'
|
|
3
|
+
|
|
4
|
+
describe('serviceDurationMinutes', () => {
|
|
5
|
+
it('stores create and update paths as minutes (number)', () => {
|
|
6
|
+
expect(serviceDurationMinutes(30)).toBe(30)
|
|
7
|
+
expect(serviceDurationMinutes(60)).toBe(60)
|
|
8
|
+
expect(serviceDurationMinutes(90)).toBe(90)
|
|
9
|
+
})
|
|
10
|
+
|
|
11
|
+
it('accepts numeric strings from the select UI', () => {
|
|
12
|
+
expect(serviceDurationMinutes('30')).toBe(30)
|
|
13
|
+
expect(serviceDurationMinutes('60')).toBe(60)
|
|
14
|
+
expect(serviceDurationMinutes('90')).toBe(90)
|
|
15
|
+
})
|
|
16
|
+
|
|
17
|
+
it('falls back to 60 for empty or unknown values', () => {
|
|
18
|
+
expect(serviceDurationMinutes(undefined)).toBe(60)
|
|
19
|
+
expect(serviceDurationMinutes(null)).toBe(60)
|
|
20
|
+
expect(serviceDurationMinutes('')).toBe(60)
|
|
21
|
+
expect(serviceDurationMinutes('45')).toBe(60)
|
|
22
|
+
expect(serviceDurationMinutes(45)).toBe(60)
|
|
23
|
+
expect(serviceDurationMinutes(NaN)).toBe(60)
|
|
24
|
+
})
|
|
25
|
+
})
|
package/src/service.schema.js
CHANGED
|
@@ -5,7 +5,10 @@ export default {
|
|
|
5
5
|
icon: 'calendar',
|
|
6
6
|
fields: [
|
|
7
7
|
{ name: 'name', type: 'text', required: true },
|
|
8
|
-
|
|
8
|
+
// Minutes as a number on create and update. `options` constrain schema
|
|
9
|
+
// mock / flows to catalog durations (without them faker invents 1–9999).
|
|
10
|
+
// The services form still presents these as a 30/60/90 select.
|
|
11
|
+
{ name: 'duration', type: 'number', required: true, options: [30, 60, 90] },
|
|
9
12
|
{ name: 'price', type: 'number' },
|
|
10
13
|
{ name: 'currency', type: 'select', options: ['SEK', 'EUR', 'USD', 'NOK', 'DKK'] },
|
|
11
14
|
{ name: 'description', type: 'textarea' },
|
package/src/services.page.jsx
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import React, { useState, useEffect, useCallback } from 'react'
|
|
1
|
+
import React, { useState, useEffect, useCallback, useMemo } from 'react'
|
|
2
2
|
import { View, Text, Button, Alert, Form, FieldFactory, FormStatus, ContentHeader, Page, Overlay, Guide, useLocale } from '@ossy/design-system'
|
|
3
3
|
import { useSdk } from '@ossy/sdk-react'
|
|
4
4
|
import { useDocumentValidator } from '@ossy/resources'
|
|
@@ -13,6 +13,7 @@ import { metadata as GetServices } from './get-services.action.js'
|
|
|
13
13
|
import { metadata as OpenCreateService } from './open-create-service.action.js'
|
|
14
14
|
import { metadata as ServiceForm } from './service.form.js'
|
|
15
15
|
import serviceTemplate from './service.schema.js'
|
|
16
|
+
import { SERVICE_DURATION_MINUTES, serviceDurationMinutes } from './service-duration.js'
|
|
16
17
|
import { invokeErrorMessage } from './invoke-error-message.js'
|
|
17
18
|
|
|
18
19
|
export const metadata = {
|
|
@@ -32,6 +33,25 @@ const EMPTY_FORM = {
|
|
|
32
33
|
active: true,
|
|
33
34
|
}
|
|
34
35
|
|
|
36
|
+
// Schema stores minutes as a number; the form still presents the catalog as a select.
|
|
37
|
+
const SERVICE_FORM_FIELDS = serviceTemplate.fields.map((field) => (
|
|
38
|
+
field.name === 'duration'
|
|
39
|
+
? {
|
|
40
|
+
name: 'duration',
|
|
41
|
+
type: 'select',
|
|
42
|
+
required: true,
|
|
43
|
+
options: SERVICE_DURATION_MINUTES.map(String),
|
|
44
|
+
}
|
|
45
|
+
: field
|
|
46
|
+
))
|
|
47
|
+
|
|
48
|
+
function withDurationMinutes (data) {
|
|
49
|
+
return {
|
|
50
|
+
...data,
|
|
51
|
+
duration: serviceDurationMinutes(data?.duration),
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
35
55
|
export default function ServicesPage () {
|
|
36
56
|
const { t } = useLocale()
|
|
37
57
|
const sdk = useSdk()
|
|
@@ -44,7 +64,11 @@ export default function ServicesPage () {
|
|
|
44
64
|
const [saving, setSaving] = useState(false)
|
|
45
65
|
const [serviceToDelete, setServiceToDelete] = useState(null)
|
|
46
66
|
const [deleting, setDeleting] = useState(false)
|
|
47
|
-
const
|
|
67
|
+
const validateDocument = useDocumentValidator(serviceTemplate)
|
|
68
|
+
const validate = useMemo(
|
|
69
|
+
() => (data) => validateDocument(withDurationMinutes(data)),
|
|
70
|
+
[validateDocument],
|
|
71
|
+
)
|
|
48
72
|
|
|
49
73
|
const loadServices = useCallback(async () => {
|
|
50
74
|
setLoading(true)
|
|
@@ -73,7 +97,7 @@ export default function ServicesPage () {
|
|
|
73
97
|
setEditingId(service.id)
|
|
74
98
|
setFormDefaults({
|
|
75
99
|
name: service.name ?? '',
|
|
76
|
-
duration: String(service.duration
|
|
100
|
+
duration: String(serviceDurationMinutes(service.duration)),
|
|
77
101
|
price: service.price ?? 0,
|
|
78
102
|
currency: service.currency ?? 'SEK',
|
|
79
103
|
description: service.description ?? '',
|
|
@@ -87,7 +111,8 @@ export default function ServicesPage () {
|
|
|
87
111
|
setError(null)
|
|
88
112
|
try {
|
|
89
113
|
const bookingAction = editingId ? UpdateService : CreateService
|
|
90
|
-
const
|
|
114
|
+
const normalized = withDurationMinutes(data)
|
|
115
|
+
const body = editingId ? { serviceId: editingId, ...normalized } : normalized
|
|
91
116
|
await sdk.invoke(bookingAction, body)
|
|
92
117
|
setFormOpen(false)
|
|
93
118
|
await loadServices()
|
|
@@ -115,6 +140,7 @@ export default function ServicesPage () {
|
|
|
115
140
|
|
|
116
141
|
return (
|
|
117
142
|
<Page
|
|
143
|
+
data-services-form={formOpen ? 'open' : 'closed'}
|
|
118
144
|
header={(
|
|
119
145
|
<ContentHeader
|
|
120
146
|
inset="none"
|
|
@@ -138,7 +164,7 @@ export default function ServicesPage () {
|
|
|
138
164
|
key={editingId ?? 'new'}
|
|
139
165
|
id={ServiceForm.id}
|
|
140
166
|
schemaId={ServiceForm.schemaId}
|
|
141
|
-
fields={
|
|
167
|
+
fields={SERVICE_FORM_FIELDS}
|
|
142
168
|
defaultData={formDefaults}
|
|
143
169
|
onSubmit={handleSave}
|
|
144
170
|
validate={validate}
|
|
@@ -23,7 +23,7 @@ export default {
|
|
|
23
23
|
{ result: { action: OpenEditService, timeout: 10000 } },
|
|
24
24
|
{ action: OpenEditService },
|
|
25
25
|
{ result: { action: UpdateService, timeout: 10000 } },
|
|
26
|
-
{ form: ServiceForm, fields: { name: 'e2e-updated-service' } },
|
|
26
|
+
{ form: ServiceForm, fields: { name: 'e2e-updated-service', duration: '60' } },
|
|
27
27
|
{ action: UpdateService },
|
|
28
28
|
{
|
|
29
29
|
result: {
|
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { PatchResourceContent } from '@ossy/resources'
|
|
2
2
|
import { BookingSchema } from './schema-ids.js'
|
|
3
3
|
import { getBookingResources } from './booking-resources.js'
|
|
4
|
+
import { serviceDurationMinutes } from './service-duration.js'
|
|
4
5
|
|
|
5
6
|
export const metadata = { id: '@ossy/booking/tasks/update-service' }
|
|
6
7
|
|
|
7
|
-
export async function run ({ payload, req, log }) {
|
|
8
|
+
export async function run ({ payload, req, log, sdk }) {
|
|
8
9
|
const workspaceId = req?.workspaceId
|
|
9
10
|
const { serviceId, name, duration, price, currency, description, active } = payload ?? {}
|
|
10
11
|
|
|
@@ -14,10 +15,12 @@ export async function run ({ payload, req, log }) {
|
|
|
14
15
|
if (!serviceId) {
|
|
15
16
|
throw Object.assign(new Error('serviceId is required'), { status: 400 })
|
|
16
17
|
}
|
|
18
|
+
if (!sdk?.invoke) throw Object.assign(new Error('sdk is required'), { status: 500 })
|
|
17
19
|
|
|
18
20
|
const existing = await getBookingResources({
|
|
19
21
|
schemaId: BookingSchema.service,
|
|
20
22
|
belongsTo: workspaceId,
|
|
23
|
+
sdk,
|
|
21
24
|
})
|
|
22
25
|
|
|
23
26
|
const resource = existing.find(r => r.id === serviceId)
|
|
@@ -28,7 +31,7 @@ export async function run ({ payload, req, log }) {
|
|
|
28
31
|
const content = {
|
|
29
32
|
...resource.content,
|
|
30
33
|
...(name != null ? { name: String(name).trim() } : {}),
|
|
31
|
-
...(duration != null ? { duration:
|
|
34
|
+
...(duration != null ? { duration: serviceDurationMinutes(duration) } : {}),
|
|
32
35
|
...(price != null ? { price: Number(price) || 0 } : {}),
|
|
33
36
|
...(currency != null ? { currency } : {}),
|
|
34
37
|
...(description != null ? { description: String(description).trim() } : {}),
|
|
@@ -37,13 +40,10 @@ export async function run ({ payload, req, log }) {
|
|
|
37
40
|
|
|
38
41
|
log?.info(`[booking/tasks/update-service] Updating service ${serviceId}`)
|
|
39
42
|
|
|
40
|
-
await
|
|
41
|
-
serviceId,
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
content,
|
|
45
|
-
}),
|
|
46
|
-
)
|
|
43
|
+
await sdk.invoke(PatchResourceContent, {
|
|
44
|
+
resourceId: serviceId,
|
|
45
|
+
content,
|
|
46
|
+
})
|
|
47
47
|
|
|
48
48
|
return { id: serviceId, ...content }
|
|
49
49
|
}
|