@ordinatio/scheduling-api 0.4.1 → 0.5.0
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/CHANGELOG.md +48 -0
- package/README.md +9 -5
- package/dist/agent/index.js +1401 -142
- package/dist/agent/index.mjs +1401 -142
- package/dist/index.js +71 -43
- package/dist/index.mjs +71 -43
- package/package.json +3 -3
package/dist/index.js
CHANGED
|
@@ -3483,7 +3483,7 @@ async function createCoupon(request, { host }) {
|
|
|
3483
3483
|
async function getOwnedCoupon(db, id, organizationId) {
|
|
3484
3484
|
return db.couponCode.findFirst({
|
|
3485
3485
|
where: { id, organizationId },
|
|
3486
|
-
select: { id: true, code: true, usedCount: true, isActive: true }
|
|
3486
|
+
select: { id: true, code: true, usedCount: true, isActive: true, discountType: true, discountValue: true }
|
|
3487
3487
|
});
|
|
3488
3488
|
}
|
|
3489
3489
|
async function updateCoupon(request, { host, params }) {
|
|
@@ -3503,6 +3503,11 @@ async function updateCoupon(request, { host, params }) {
|
|
|
3503
3503
|
const validation = await parseBody(request, UpdateCouponCodeSchema);
|
|
3504
3504
|
if (!validation.success) return validation.response;
|
|
3505
3505
|
const input = validation.data;
|
|
3506
|
+
const finalType = input.discountType ?? existing.discountType;
|
|
3507
|
+
const finalValue = input.discountValue ?? Number(existing.discountValue);
|
|
3508
|
+
if (finalType === "PERCENTAGE" && finalValue > 100) {
|
|
3509
|
+
return Response.json({ error: "Percentage discount cannot exceed 100%" }, { status: 400 });
|
|
3510
|
+
}
|
|
3506
3511
|
const updated = await db.couponCode.update({
|
|
3507
3512
|
where: { id, organizationId },
|
|
3508
3513
|
data: {
|
|
@@ -3521,6 +3526,9 @@ async function updateCoupon(request, { host, params }) {
|
|
|
3521
3526
|
});
|
|
3522
3527
|
return Response.json({ coupon: updated });
|
|
3523
3528
|
} catch (error) {
|
|
3529
|
+
if (getErrorCode(error) === "P2002") {
|
|
3530
|
+
return Response.json({ error: "A coupon with this code already exists" }, { status: 409 });
|
|
3531
|
+
}
|
|
3524
3532
|
return Response.json({ error: getErrorMessage(error, "Failed to update coupon") }, { status: 500 });
|
|
3525
3533
|
}
|
|
3526
3534
|
}
|
|
@@ -3588,6 +3596,10 @@ async function createPackage(request, { host }) {
|
|
|
3588
3596
|
const validation = await parseBody(request, CreateServicePackageSchema);
|
|
3589
3597
|
if (!validation.success) return validation.response;
|
|
3590
3598
|
const input = validation.data;
|
|
3599
|
+
if (input.typeId) {
|
|
3600
|
+
const type = await db.appointmentType.findFirst({ where: { id: input.typeId, organizationId }, select: { id: true } });
|
|
3601
|
+
if (!type) return Response.json({ error: "Appointment type not found" }, { status: 404 });
|
|
3602
|
+
}
|
|
3591
3603
|
const pkg = await db.servicePackage.create({
|
|
3592
3604
|
data: {
|
|
3593
3605
|
name: input.name,
|
|
@@ -3639,6 +3651,10 @@ async function updatePackage(request, { host, params }) {
|
|
|
3639
3651
|
const validation = await parseBody(request, UpdateServicePackageSchema);
|
|
3640
3652
|
if (!validation.success) return validation.response;
|
|
3641
3653
|
const input = validation.data;
|
|
3654
|
+
if (input.typeId) {
|
|
3655
|
+
const type = await db.appointmentType.findFirst({ where: { id: input.typeId, organizationId }, select: { id: true } });
|
|
3656
|
+
if (!type) return Response.json({ error: "Appointment type not found" }, { status: 404 });
|
|
3657
|
+
}
|
|
3642
3658
|
const updated = await db.servicePackage.update({
|
|
3643
3659
|
where: { id, organizationId },
|
|
3644
3660
|
data: {
|
|
@@ -4140,46 +4156,54 @@ async function createRecurringSeries(request, { host }) {
|
|
|
4140
4156
|
{ status: 422 }
|
|
4141
4157
|
);
|
|
4142
4158
|
}
|
|
4143
|
-
|
|
4144
|
-
|
|
4145
|
-
|
|
4146
|
-
|
|
4147
|
-
|
|
4148
|
-
|
|
4149
|
-
|
|
4150
|
-
|
|
4151
|
-
|
|
4152
|
-
|
|
4153
|
-
|
|
4154
|
-
|
|
4155
|
-
|
|
4156
|
-
|
|
4157
|
-
|
|
4158
|
-
|
|
4159
|
-
|
|
4160
|
-
|
|
4161
|
-
|
|
4162
|
-
|
|
4163
|
-
|
|
4164
|
-
|
|
4165
|
-
|
|
4166
|
-
|
|
4167
|
-
|
|
4168
|
-
|
|
4169
|
-
|
|
4170
|
-
|
|
4171
|
-
|
|
4172
|
-
|
|
4173
|
-
|
|
4174
|
-
|
|
4175
|
-
|
|
4176
|
-
|
|
4177
|
-
|
|
4178
|
-
|
|
4179
|
-
|
|
4159
|
+
let series;
|
|
4160
|
+
try {
|
|
4161
|
+
series = await db.$transaction(async (tx) => {
|
|
4162
|
+
const created = await tx.recurringSeries.create({
|
|
4163
|
+
data: {
|
|
4164
|
+
organizationId,
|
|
4165
|
+
calendarId: input.calendarId,
|
|
4166
|
+
typeId: input.typeId,
|
|
4167
|
+
frequency: input.frequency,
|
|
4168
|
+
occurrences: slots.length,
|
|
4169
|
+
startTimeLocal: input.startTimeLocal,
|
|
4170
|
+
durationMins: apptType.duration,
|
|
4171
|
+
timezone: input.timezone,
|
|
4172
|
+
endsAt: endsAt ?? null,
|
|
4173
|
+
clientEmail: input.clientEmail,
|
|
4174
|
+
clientName: input.clientName,
|
|
4175
|
+
clientPhone: input.clientPhone ?? null,
|
|
4176
|
+
clientId,
|
|
4177
|
+
notes: input.notes ?? null,
|
|
4178
|
+
status: "ACTIVE"
|
|
4179
|
+
}
|
|
4180
|
+
});
|
|
4181
|
+
await tx.appointment.createMany({
|
|
4182
|
+
data: slots.map(({ startTime, endTime }, idx) => ({
|
|
4183
|
+
calendarId: input.calendarId,
|
|
4184
|
+
typeId: input.typeId,
|
|
4185
|
+
clientEmail: input.clientEmail,
|
|
4186
|
+
clientName: input.clientName,
|
|
4187
|
+
clientPhone: input.clientPhone ?? null,
|
|
4188
|
+
clientId,
|
|
4189
|
+
startTime,
|
|
4190
|
+
endTime,
|
|
4191
|
+
duration: apptType.duration,
|
|
4192
|
+
timezone: input.timezone,
|
|
4193
|
+
notes: input.notes ?? null,
|
|
4194
|
+
status: "CONFIRMED",
|
|
4195
|
+
recurringId: created.id,
|
|
4196
|
+
recurringIndex: idx + 1
|
|
4197
|
+
}))
|
|
4198
|
+
});
|
|
4199
|
+
return created;
|
|
4180
4200
|
});
|
|
4181
|
-
|
|
4182
|
-
|
|
4201
|
+
} catch (error) {
|
|
4202
|
+
if (getErrorCode(error) === "P2002") {
|
|
4203
|
+
return Response.json({ error: "One of the visits would start at exactly the same time as a booking already on this calendar. Nothing was saved." }, { status: 409 });
|
|
4204
|
+
}
|
|
4205
|
+
throw error;
|
|
4206
|
+
}
|
|
4183
4207
|
await host.activity.log(db, {
|
|
4184
4208
|
action: "scheduling.recurring_series_created",
|
|
4185
4209
|
description: `created ${slots.length}-occurrence ${input.frequency} series for ${input.clientName} (${input.clientEmail})`,
|
|
@@ -4898,7 +4922,7 @@ async function putCalendar(request, { host, params }) {
|
|
|
4898
4922
|
if (archived) await undoArchive(host, db, id, organizationId, existing.slug);
|
|
4899
4923
|
throw error;
|
|
4900
4924
|
}
|
|
4901
|
-
if (input.slotGranularityMinutes !== void 0) await host.availabilityCache.invalidate(id);
|
|
4925
|
+
if (input.slotGranularityMinutes !== void 0 || input.timezone !== void 0) await host.availabilityCache.invalidate(id);
|
|
4902
4926
|
if (archiving) await releaseCalendarSlugIfStillInactive(host, db, id, organizationId, existing.slug);
|
|
4903
4927
|
await bustCalendarCachesOnUpdate(host, organizationId, existing.slug, input, id);
|
|
4904
4928
|
await host.activity.log(db, {
|
|
@@ -5406,7 +5430,7 @@ async function createTemplate(request, { host }) {
|
|
|
5406
5430
|
if (input.channel === "SMS" && input.subject) {
|
|
5407
5431
|
return Response.json({ error: "SMS templates cannot have a subject" }, { status: 400 });
|
|
5408
5432
|
}
|
|
5409
|
-
if (input.channel === "EMAIL" && !input.subject
|
|
5433
|
+
if (input.channel === "EMAIL" && !stripHtml(input.subject).trim()) {
|
|
5410
5434
|
return Response.json({ error: "EMAIL templates require a subject" }, { status: 400 });
|
|
5411
5435
|
}
|
|
5412
5436
|
const template = await (0, import_scheduling42.createNotificationTemplate)(db, organizationId, {
|
|
@@ -6575,7 +6599,7 @@ async function resolveBookingContext(host, cors, body) {
|
|
|
6575
6599
|
if (body.packagePurchaseId) {
|
|
6576
6600
|
const purchase = await db.packagePurchase.findFirst({
|
|
6577
6601
|
where: { id: body.packagePurchaseId, organizationId: orgId },
|
|
6578
|
-
select: { id: true, status: true, expiresAt: true, sessionsUsed: true, sessionsTotal: true, clientEmail: true }
|
|
6602
|
+
select: { id: true, status: true, expiresAt: true, sessionsUsed: true, sessionsTotal: true, clientEmail: true, package: { select: { typeId: true } } }
|
|
6579
6603
|
});
|
|
6580
6604
|
if (!purchase) return fail(404, { error: "Package purchase not found" });
|
|
6581
6605
|
if (purchase.status !== "ACTIVE") {
|
|
@@ -6586,6 +6610,10 @@ async function resolveBookingContext(host, cors, body) {
|
|
|
6586
6610
|
if (purchase.clientEmail.toLowerCase() !== body.clientEmail.toLowerCase()) {
|
|
6587
6611
|
return fail(403, { error: "Package does not belong to this client" });
|
|
6588
6612
|
}
|
|
6613
|
+
const packageTypeId = purchase.package?.typeId ?? null;
|
|
6614
|
+
if (packageTypeId && packageTypeId !== body.typeId) {
|
|
6615
|
+
return fail(409, { error: "This package cannot be used for this appointment type" });
|
|
6616
|
+
}
|
|
6589
6617
|
}
|
|
6590
6618
|
return { ok: true, orgId, db, calendar, appointmentType, noShowApprovalOverride };
|
|
6591
6619
|
}
|
package/dist/index.mjs
CHANGED
|
@@ -3001,7 +3001,7 @@ async function createCoupon(request, { host }) {
|
|
|
3001
3001
|
async function getOwnedCoupon(db, id, organizationId) {
|
|
3002
3002
|
return db.couponCode.findFirst({
|
|
3003
3003
|
where: { id, organizationId },
|
|
3004
|
-
select: { id: true, code: true, usedCount: true, isActive: true }
|
|
3004
|
+
select: { id: true, code: true, usedCount: true, isActive: true, discountType: true, discountValue: true }
|
|
3005
3005
|
});
|
|
3006
3006
|
}
|
|
3007
3007
|
async function updateCoupon(request, { host, params }) {
|
|
@@ -3021,6 +3021,11 @@ async function updateCoupon(request, { host, params }) {
|
|
|
3021
3021
|
const validation = await parseBody(request, UpdateCouponCodeSchema);
|
|
3022
3022
|
if (!validation.success) return validation.response;
|
|
3023
3023
|
const input = validation.data;
|
|
3024
|
+
const finalType = input.discountType ?? existing.discountType;
|
|
3025
|
+
const finalValue = input.discountValue ?? Number(existing.discountValue);
|
|
3026
|
+
if (finalType === "PERCENTAGE" && finalValue > 100) {
|
|
3027
|
+
return Response.json({ error: "Percentage discount cannot exceed 100%" }, { status: 400 });
|
|
3028
|
+
}
|
|
3024
3029
|
const updated = await db.couponCode.update({
|
|
3025
3030
|
where: { id, organizationId },
|
|
3026
3031
|
data: {
|
|
@@ -3039,6 +3044,9 @@ async function updateCoupon(request, { host, params }) {
|
|
|
3039
3044
|
});
|
|
3040
3045
|
return Response.json({ coupon: updated });
|
|
3041
3046
|
} catch (error) {
|
|
3047
|
+
if (getErrorCode(error) === "P2002") {
|
|
3048
|
+
return Response.json({ error: "A coupon with this code already exists" }, { status: 409 });
|
|
3049
|
+
}
|
|
3042
3050
|
return Response.json({ error: getErrorMessage(error, "Failed to update coupon") }, { status: 500 });
|
|
3043
3051
|
}
|
|
3044
3052
|
}
|
|
@@ -3106,6 +3114,10 @@ async function createPackage(request, { host }) {
|
|
|
3106
3114
|
const validation = await parseBody(request, CreateServicePackageSchema);
|
|
3107
3115
|
if (!validation.success) return validation.response;
|
|
3108
3116
|
const input = validation.data;
|
|
3117
|
+
if (input.typeId) {
|
|
3118
|
+
const type = await db.appointmentType.findFirst({ where: { id: input.typeId, organizationId }, select: { id: true } });
|
|
3119
|
+
if (!type) return Response.json({ error: "Appointment type not found" }, { status: 404 });
|
|
3120
|
+
}
|
|
3109
3121
|
const pkg = await db.servicePackage.create({
|
|
3110
3122
|
data: {
|
|
3111
3123
|
name: input.name,
|
|
@@ -3157,6 +3169,10 @@ async function updatePackage(request, { host, params }) {
|
|
|
3157
3169
|
const validation = await parseBody(request, UpdateServicePackageSchema);
|
|
3158
3170
|
if (!validation.success) return validation.response;
|
|
3159
3171
|
const input = validation.data;
|
|
3172
|
+
if (input.typeId) {
|
|
3173
|
+
const type = await db.appointmentType.findFirst({ where: { id: input.typeId, organizationId }, select: { id: true } });
|
|
3174
|
+
if (!type) return Response.json({ error: "Appointment type not found" }, { status: 404 });
|
|
3175
|
+
}
|
|
3160
3176
|
const updated = await db.servicePackage.update({
|
|
3161
3177
|
where: { id, organizationId },
|
|
3162
3178
|
data: {
|
|
@@ -3663,46 +3679,54 @@ async function createRecurringSeries(request, { host }) {
|
|
|
3663
3679
|
{ status: 422 }
|
|
3664
3680
|
);
|
|
3665
3681
|
}
|
|
3666
|
-
|
|
3667
|
-
|
|
3668
|
-
|
|
3669
|
-
|
|
3670
|
-
|
|
3671
|
-
|
|
3672
|
-
|
|
3673
|
-
|
|
3674
|
-
|
|
3675
|
-
|
|
3676
|
-
|
|
3677
|
-
|
|
3678
|
-
|
|
3679
|
-
|
|
3680
|
-
|
|
3681
|
-
|
|
3682
|
-
|
|
3683
|
-
|
|
3684
|
-
|
|
3685
|
-
|
|
3686
|
-
|
|
3687
|
-
|
|
3688
|
-
|
|
3689
|
-
|
|
3690
|
-
|
|
3691
|
-
|
|
3692
|
-
|
|
3693
|
-
|
|
3694
|
-
|
|
3695
|
-
|
|
3696
|
-
|
|
3697
|
-
|
|
3698
|
-
|
|
3699
|
-
|
|
3700
|
-
|
|
3701
|
-
|
|
3702
|
-
|
|
3682
|
+
let series;
|
|
3683
|
+
try {
|
|
3684
|
+
series = await db.$transaction(async (tx) => {
|
|
3685
|
+
const created = await tx.recurringSeries.create({
|
|
3686
|
+
data: {
|
|
3687
|
+
organizationId,
|
|
3688
|
+
calendarId: input.calendarId,
|
|
3689
|
+
typeId: input.typeId,
|
|
3690
|
+
frequency: input.frequency,
|
|
3691
|
+
occurrences: slots.length,
|
|
3692
|
+
startTimeLocal: input.startTimeLocal,
|
|
3693
|
+
durationMins: apptType.duration,
|
|
3694
|
+
timezone: input.timezone,
|
|
3695
|
+
endsAt: endsAt ?? null,
|
|
3696
|
+
clientEmail: input.clientEmail,
|
|
3697
|
+
clientName: input.clientName,
|
|
3698
|
+
clientPhone: input.clientPhone ?? null,
|
|
3699
|
+
clientId,
|
|
3700
|
+
notes: input.notes ?? null,
|
|
3701
|
+
status: "ACTIVE"
|
|
3702
|
+
}
|
|
3703
|
+
});
|
|
3704
|
+
await tx.appointment.createMany({
|
|
3705
|
+
data: slots.map(({ startTime, endTime }, idx) => ({
|
|
3706
|
+
calendarId: input.calendarId,
|
|
3707
|
+
typeId: input.typeId,
|
|
3708
|
+
clientEmail: input.clientEmail,
|
|
3709
|
+
clientName: input.clientName,
|
|
3710
|
+
clientPhone: input.clientPhone ?? null,
|
|
3711
|
+
clientId,
|
|
3712
|
+
startTime,
|
|
3713
|
+
endTime,
|
|
3714
|
+
duration: apptType.duration,
|
|
3715
|
+
timezone: input.timezone,
|
|
3716
|
+
notes: input.notes ?? null,
|
|
3717
|
+
status: "CONFIRMED",
|
|
3718
|
+
recurringId: created.id,
|
|
3719
|
+
recurringIndex: idx + 1
|
|
3720
|
+
}))
|
|
3721
|
+
});
|
|
3722
|
+
return created;
|
|
3703
3723
|
});
|
|
3704
|
-
|
|
3705
|
-
|
|
3724
|
+
} catch (error) {
|
|
3725
|
+
if (getErrorCode(error) === "P2002") {
|
|
3726
|
+
return Response.json({ error: "One of the visits would start at exactly the same time as a booking already on this calendar. Nothing was saved." }, { status: 409 });
|
|
3727
|
+
}
|
|
3728
|
+
throw error;
|
|
3729
|
+
}
|
|
3706
3730
|
await host.activity.log(db, {
|
|
3707
3731
|
action: "scheduling.recurring_series_created",
|
|
3708
3732
|
description: `created ${slots.length}-occurrence ${input.frequency} series for ${input.clientName} (${input.clientEmail})`,
|
|
@@ -4421,7 +4445,7 @@ async function putCalendar(request, { host, params }) {
|
|
|
4421
4445
|
if (archived) await undoArchive(host, db, id, organizationId, existing.slug);
|
|
4422
4446
|
throw error;
|
|
4423
4447
|
}
|
|
4424
|
-
if (input.slotGranularityMinutes !== void 0) await host.availabilityCache.invalidate(id);
|
|
4448
|
+
if (input.slotGranularityMinutes !== void 0 || input.timezone !== void 0) await host.availabilityCache.invalidate(id);
|
|
4425
4449
|
if (archiving) await releaseCalendarSlugIfStillInactive(host, db, id, organizationId, existing.slug);
|
|
4426
4450
|
await bustCalendarCachesOnUpdate(host, organizationId, existing.slug, input, id);
|
|
4427
4451
|
await host.activity.log(db, {
|
|
@@ -4935,7 +4959,7 @@ async function createTemplate(request, { host }) {
|
|
|
4935
4959
|
if (input.channel === "SMS" && input.subject) {
|
|
4936
4960
|
return Response.json({ error: "SMS templates cannot have a subject" }, { status: 400 });
|
|
4937
4961
|
}
|
|
4938
|
-
if (input.channel === "EMAIL" && !input.subject
|
|
4962
|
+
if (input.channel === "EMAIL" && !stripHtml(input.subject).trim()) {
|
|
4939
4963
|
return Response.json({ error: "EMAIL templates require a subject" }, { status: 400 });
|
|
4940
4964
|
}
|
|
4941
4965
|
const template = await createNotificationTemplate(db, organizationId, {
|
|
@@ -6119,7 +6143,7 @@ async function resolveBookingContext(host, cors, body) {
|
|
|
6119
6143
|
if (body.packagePurchaseId) {
|
|
6120
6144
|
const purchase = await db.packagePurchase.findFirst({
|
|
6121
6145
|
where: { id: body.packagePurchaseId, organizationId: orgId },
|
|
6122
|
-
select: { id: true, status: true, expiresAt: true, sessionsUsed: true, sessionsTotal: true, clientEmail: true }
|
|
6146
|
+
select: { id: true, status: true, expiresAt: true, sessionsUsed: true, sessionsTotal: true, clientEmail: true, package: { select: { typeId: true } } }
|
|
6123
6147
|
});
|
|
6124
6148
|
if (!purchase) return fail(404, { error: "Package purchase not found" });
|
|
6125
6149
|
if (purchase.status !== "ACTIVE") {
|
|
@@ -6130,6 +6154,10 @@ async function resolveBookingContext(host, cors, body) {
|
|
|
6130
6154
|
if (purchase.clientEmail.toLowerCase() !== body.clientEmail.toLowerCase()) {
|
|
6131
6155
|
return fail(403, { error: "Package does not belong to this client" });
|
|
6132
6156
|
}
|
|
6157
|
+
const packageTypeId = purchase.package?.typeId ?? null;
|
|
6158
|
+
if (packageTypeId && packageTypeId !== body.typeId) {
|
|
6159
|
+
return fail(409, { error: "This package cannot be used for this appointment type" });
|
|
6160
|
+
}
|
|
6133
6161
|
}
|
|
6134
6162
|
return { ok: true, orgId, db, calendar, appointmentType, noShowApprovalOverride };
|
|
6135
6163
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ordinatio/scheduling-api",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"description": "HTTP API for @ordinatio/scheduling. Every handler takes a Request and returns a Response.",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -11,14 +11,14 @@
|
|
|
11
11
|
"sanitize-html": "^2.16.0"
|
|
12
12
|
},
|
|
13
13
|
"peerDependencies": {
|
|
14
|
-
"@ordinatio/scheduling": "^0.
|
|
14
|
+
"@ordinatio/scheduling": "^0.6.0"
|
|
15
15
|
},
|
|
16
16
|
"devDependencies": {
|
|
17
17
|
"@types/sanitize-html": "^2.13.0",
|
|
18
18
|
"tsup": "^8.0.0",
|
|
19
19
|
"typescript": "5",
|
|
20
20
|
"vitest": "4.0.18",
|
|
21
|
-
"@ordinatio/scheduling": "0.
|
|
21
|
+
"@ordinatio/scheduling": "0.6.0"
|
|
22
22
|
},
|
|
23
23
|
"engines": {
|
|
24
24
|
"node": ">=22.0.0"
|