@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/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
- const series = await db.$transaction(async (tx) => {
4144
- const created = await tx.recurringSeries.create({
4145
- data: {
4146
- organizationId,
4147
- calendarId: input.calendarId,
4148
- typeId: input.typeId,
4149
- frequency: input.frequency,
4150
- occurrences: slots.length,
4151
- startTimeLocal: input.startTimeLocal,
4152
- durationMins: apptType.duration,
4153
- timezone: input.timezone,
4154
- endsAt: endsAt ?? null,
4155
- clientEmail: input.clientEmail,
4156
- clientName: input.clientName,
4157
- clientPhone: input.clientPhone ?? null,
4158
- clientId,
4159
- notes: input.notes ?? null,
4160
- status: "ACTIVE"
4161
- }
4162
- });
4163
- await tx.appointment.createMany({
4164
- data: slots.map(({ startTime, endTime }, idx) => ({
4165
- calendarId: input.calendarId,
4166
- typeId: input.typeId,
4167
- clientEmail: input.clientEmail,
4168
- clientName: input.clientName,
4169
- clientPhone: input.clientPhone ?? null,
4170
- clientId,
4171
- startTime,
4172
- endTime,
4173
- duration: apptType.duration,
4174
- timezone: input.timezone,
4175
- notes: input.notes ?? null,
4176
- status: "CONFIRMED",
4177
- recurringId: created.id,
4178
- recurringIndex: idx + 1
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
- return created;
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?.trim()) {
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
- const series = await db.$transaction(async (tx) => {
3667
- const created = await tx.recurringSeries.create({
3668
- data: {
3669
- organizationId,
3670
- calendarId: input.calendarId,
3671
- typeId: input.typeId,
3672
- frequency: input.frequency,
3673
- occurrences: slots.length,
3674
- startTimeLocal: input.startTimeLocal,
3675
- durationMins: apptType.duration,
3676
- timezone: input.timezone,
3677
- endsAt: endsAt ?? null,
3678
- clientEmail: input.clientEmail,
3679
- clientName: input.clientName,
3680
- clientPhone: input.clientPhone ?? null,
3681
- clientId,
3682
- notes: input.notes ?? null,
3683
- status: "ACTIVE"
3684
- }
3685
- });
3686
- await tx.appointment.createMany({
3687
- data: slots.map(({ startTime, endTime }, idx) => ({
3688
- calendarId: input.calendarId,
3689
- typeId: input.typeId,
3690
- clientEmail: input.clientEmail,
3691
- clientName: input.clientName,
3692
- clientPhone: input.clientPhone ?? null,
3693
- clientId,
3694
- startTime,
3695
- endTime,
3696
- duration: apptType.duration,
3697
- timezone: input.timezone,
3698
- notes: input.notes ?? null,
3699
- status: "CONFIRMED",
3700
- recurringId: created.id,
3701
- recurringIndex: idx + 1
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
- return created;
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?.trim()) {
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.4.1",
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.5.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.5.1"
21
+ "@ordinatio/scheduling": "0.6.0"
22
22
  },
23
23
  "engines": {
24
24
  "node": ">=22.0.0"