@frigear-nu/shifts 0.0.0-stage → 0.0.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/README.md +53 -2
- package/dist/module.d.mts +709 -0
- package/dist/module.json +9 -0
- package/dist/module.mjs +253 -0
- package/dist/runtime/access.d.ts +2 -0
- package/dist/runtime/access.js +84 -0
- package/dist/runtime/app/components/ShiftBroadcastSlideover.d.vue.ts +19 -0
- package/dist/runtime/app/components/ShiftBroadcastSlideover.vue +142 -0
- package/dist/runtime/app/components/ShiftBroadcastSlideover.vue.d.ts +19 -0
- package/dist/runtime/app/components/ShiftBroadcastsCard.d.vue.ts +9 -0
- package/dist/runtime/app/components/ShiftBroadcastsCard.vue +74 -0
- package/dist/runtime/app/components/ShiftBroadcastsCard.vue.d.ts +9 -0
- package/dist/runtime/app/components/ShiftBulkSlideover.d.vue.ts +15 -0
- package/dist/runtime/app/components/ShiftBulkSlideover.vue +185 -0
- package/dist/runtime/app/components/ShiftBulkSlideover.vue.d.ts +15 -0
- package/dist/runtime/app/components/ShiftCheckInCard.d.vue.ts +13 -0
- package/dist/runtime/app/components/ShiftCheckInCard.vue +238 -0
- package/dist/runtime/app/components/ShiftCheckInCard.vue.d.ts +13 -0
- package/dist/runtime/app/components/ShiftCopySlideover.d.vue.ts +11 -0
- package/dist/runtime/app/components/ShiftCopySlideover.vue +71 -0
- package/dist/runtime/app/components/ShiftCopySlideover.vue.d.ts +11 -0
- package/dist/runtime/app/components/ShiftDayView.d.vue.ts +16 -0
- package/dist/runtime/app/components/ShiftDayView.vue +124 -0
- package/dist/runtime/app/components/ShiftDayView.vue.d.ts +16 -0
- package/dist/runtime/app/components/ShiftFeedSlideover.d.vue.ts +7 -0
- package/dist/runtime/app/components/ShiftFeedSlideover.vue +172 -0
- package/dist/runtime/app/components/ShiftFeedSlideover.vue.d.ts +7 -0
- package/dist/runtime/app/components/ShiftNowCard.d.vue.ts +6 -0
- package/dist/runtime/app/components/ShiftNowCard.vue +83 -0
- package/dist/runtime/app/components/ShiftNowCard.vue.d.ts +6 -0
- package/dist/runtime/app/components/ShiftPassFullscreen.d.vue.ts +13 -0
- package/dist/runtime/app/components/ShiftPassFullscreen.vue +79 -0
- package/dist/runtime/app/components/ShiftPassFullscreen.vue.d.ts +13 -0
- package/dist/runtime/app/components/ShiftPerkSlideover.d.vue.ts +14 -0
- package/dist/runtime/app/components/ShiftPerkSlideover.vue +103 -0
- package/dist/runtime/app/components/ShiftPerkSlideover.vue.d.ts +14 -0
- package/dist/runtime/app/components/ShiftPerksCard.d.vue.ts +8 -0
- package/dist/runtime/app/components/ShiftPerksCard.vue +66 -0
- package/dist/runtime/app/components/ShiftPerksCard.vue.d.ts +8 -0
- package/dist/runtime/app/components/ShiftQrScanner.d.vue.ts +10 -0
- package/dist/runtime/app/components/ShiftQrScanner.vue +44 -0
- package/dist/runtime/app/components/ShiftQrScanner.vue.d.ts +10 -0
- package/dist/runtime/app/components/ShiftRoleSlideover.d.vue.ts +15 -0
- package/dist/runtime/app/components/ShiftRoleSlideover.vue +88 -0
- package/dist/runtime/app/components/ShiftRoleSlideover.vue.d.ts +15 -0
- package/dist/runtime/app/components/ShiftScheduleSettingsFields.d.vue.ts +11 -0
- package/dist/runtime/app/components/ShiftScheduleSettingsFields.vue +61 -0
- package/dist/runtime/app/components/ShiftScheduleSettingsFields.vue.d.ts +11 -0
- package/dist/runtime/app/components/ShiftScheduleSlideover.d.vue.ts +12 -0
- package/dist/runtime/app/components/ShiftScheduleSlideover.vue +129 -0
- package/dist/runtime/app/components/ShiftScheduleSlideover.vue.d.ts +12 -0
- package/dist/runtime/app/components/ShiftSlideover.d.vue.ts +13 -0
- package/dist/runtime/app/components/ShiftSlideover.vue +353 -0
- package/dist/runtime/app/components/ShiftSlideover.vue.d.ts +13 -0
- package/dist/runtime/app/components/ShiftStationSlideover.d.vue.ts +15 -0
- package/dist/runtime/app/components/ShiftStationSlideover.vue +92 -0
- package/dist/runtime/app/components/ShiftStationSlideover.vue.d.ts +15 -0
- package/dist/runtime/app/composables/index.d.ts +6 -0
- package/dist/runtime/app/composables/index.js +5 -0
- package/dist/runtime/app/composables/useShiftsActions.d.ts +61 -0
- package/dist/runtime/app/composables/useShiftsActions.js +224 -0
- package/dist/runtime/app/composables/useShiftsFormat.d.ts +12 -0
- package/dist/runtime/app/composables/useShiftsFormat.js +14 -0
- package/dist/runtime/app/composables/useShiftsLinks.d.ts +12 -0
- package/dist/runtime/app/composables/useShiftsLinks.js +6 -0
- package/dist/runtime/app/composables/useShiftsTranslate.d.ts +5 -0
- package/dist/runtime/app/composables/useShiftsTranslate.js +12 -0
- package/dist/runtime/app/composables/useShiftsVenueFeatures.d.ts +17 -0
- package/dist/runtime/app/composables/useShiftsVenueFeatures.js +18 -0
- package/dist/runtime/app/dashboard/NextShift.d.vue.ts +3 -0
- package/dist/runtime/app/dashboard/NextShift.vue +69 -0
- package/dist/runtime/app/dashboard/NextShift.vue.d.ts +3 -0
- package/dist/runtime/app/dashboard/OpenSwaps.d.vue.ts +3 -0
- package/dist/runtime/app/dashboard/OpenSwaps.vue +69 -0
- package/dist/runtime/app/dashboard/OpenSwaps.vue.d.ts +3 -0
- package/dist/runtime/app/dashboard/TeamOnNow.d.vue.ts +3 -0
- package/dist/runtime/app/dashboard/TeamOnNow.vue +72 -0
- package/dist/runtime/app/dashboard/TeamOnNow.vue.d.ts +3 -0
- package/dist/runtime/app/pages/check-in.d.vue.ts +3 -0
- package/dist/runtime/app/pages/check-in.vue +93 -0
- package/dist/runtime/app/pages/check-in.vue.d.ts +3 -0
- package/dist/runtime/app/pages/lead.d.vue.ts +3 -0
- package/dist/runtime/app/pages/lead.vue +300 -0
- package/dist/runtime/app/pages/lead.vue.d.ts +3 -0
- package/dist/runtime/app/pages/shift.d.vue.ts +3 -0
- package/dist/runtime/app/pages/shift.vue +482 -0
- package/dist/runtime/app/pages/shift.vue.d.ts +3 -0
- package/dist/runtime/app/pages/shifts.d.vue.ts +3 -0
- package/dist/runtime/app/pages/shifts.vue +169 -0
- package/dist/runtime/app/pages/shifts.vue.d.ts +3 -0
- package/dist/runtime/app/pages/station.d.vue.ts +3 -0
- package/dist/runtime/app/pages/station.vue +104 -0
- package/dist/runtime/app/pages/station.vue.d.ts +3 -0
- package/dist/runtime/app/pages/swaps.d.vue.ts +3 -0
- package/dist/runtime/app/pages/swaps.vue +192 -0
- package/dist/runtime/app/pages/swaps.vue.d.ts +3 -0
- package/dist/runtime/app/tabs/Shifts.d.vue.ts +9 -0
- package/dist/runtime/app/tabs/Shifts.vue +487 -0
- package/dist/runtime/app/tabs/Shifts.vue.d.ts +9 -0
- package/dist/runtime/app/utils/admin-shifts.d.ts +42 -0
- package/dist/runtime/app/utils/admin-shifts.js +36 -0
- package/dist/runtime/dashboard.d.ts +8 -0
- package/dist/runtime/dashboard.js +30 -0
- package/dist/runtime/emails/shifts-broadcast.d.vue.ts +8 -0
- package/dist/runtime/emails/shifts-broadcast.vue +24 -0
- package/dist/runtime/emails/shifts-broadcast.vue.d.ts +8 -0
- package/dist/runtime/emails/shifts-reminder.d.vue.ts +12 -0
- package/dist/runtime/emails/shifts-reminder.vue +34 -0
- package/dist/runtime/emails/shifts-reminder.vue.d.ts +12 -0
- package/dist/runtime/emails/shifts-swap-accepted.d.vue.ts +11 -0
- package/dist/runtime/emails/shifts-swap-accepted.vue +32 -0
- package/dist/runtime/emails/shifts-swap-accepted.vue.d.ts +11 -0
- package/dist/runtime/emails/shifts-swap-completed.d.vue.ts +11 -0
- package/dist/runtime/emails/shifts-swap-completed.vue +32 -0
- package/dist/runtime/emails/shifts-swap-completed.vue.d.ts +11 -0
- package/dist/runtime/emails/shifts-swap-offered.d.vue.ts +11 -0
- package/dist/runtime/emails/shifts-swap-offered.vue +32 -0
- package/dist/runtime/emails/shifts-swap-offered.vue.d.ts +11 -0
- package/dist/runtime/emails/shifts-swap-pending.d.vue.ts +11 -0
- package/dist/runtime/emails/shifts-swap-pending.vue +32 -0
- package/dist/runtime/emails/shifts-swap-pending.vue.d.ts +11 -0
- package/dist/runtime/emails/shifts-swap-proposed.d.vue.ts +11 -0
- package/dist/runtime/emails/shifts-swap-proposed.vue +32 -0
- package/dist/runtime/emails/shifts-swap-proposed.vue.d.ts +11 -0
- package/dist/runtime/emails/shifts-swap-rejected.d.vue.ts +11 -0
- package/dist/runtime/emails/shifts-swap-rejected.vue +32 -0
- package/dist/runtime/emails/shifts-swap-rejected.vue.d.ts +11 -0
- package/dist/runtime/i18n/lang/da.json +626 -0
- package/dist/runtime/i18n/lang/en.json +626 -0
- package/dist/runtime/menus.d.ts +2 -0
- package/dist/runtime/menus.js +17 -0
- package/dist/runtime/notifications.d.ts +9 -0
- package/dist/runtime/notifications.js +36 -0
- package/dist/runtime/server/db/schema.postgresql.d.ts +2151 -0
- package/dist/runtime/server/db/schema.postgresql.js +228 -0
- package/dist/runtime/server/db/schema.sqlite.d.ts +2263 -0
- package/dist/runtime/server/db/schema.sqlite.js +228 -0
- package/dist/runtime/server/digest-email.d.ts +17 -0
- package/dist/runtime/server/digest-email.js +42 -0
- package/dist/runtime/server/plugins/station-key.d.ts +2 -0
- package/dist/runtime/server/plugins/station-key.js +5 -0
- package/dist/runtime/server/plugins/teams.d.ts +2 -0
- package/dist/runtime/server/plugins/teams.js +19 -0
- package/dist/runtime/server/policies.d.ts +2 -0
- package/dist/runtime/server/policies.js +41 -0
- package/dist/runtime/server/reminder-email.d.ts +67 -0
- package/dist/runtime/server/reminder-email.js +109 -0
- package/dist/runtime/server/routes/admin/assignment.delete.d.ts +2 -0
- package/dist/runtime/server/routes/admin/assignment.delete.js +10 -0
- package/dist/runtime/server/routes/admin/assignments.post.d.ts +2 -0
- package/dist/runtime/server/routes/admin/assignments.post.js +11 -0
- package/dist/runtime/server/routes/admin/broadcasts.get.d.ts +2 -0
- package/dist/runtime/server/routes/admin/broadcasts.get.js +8 -0
- package/dist/runtime/server/routes/admin/broadcasts.post.d.ts +2 -0
- package/dist/runtime/server/routes/admin/broadcasts.post.js +9 -0
- package/dist/runtime/server/routes/admin/candidates.get.d.ts +2 -0
- package/dist/runtime/server/routes/admin/candidates.get.js +10 -0
- package/dist/runtime/server/routes/admin/operation-teams.get.d.ts +2 -0
- package/dist/runtime/server/routes/admin/operation-teams.get.js +9 -0
- package/dist/runtime/server/routes/admin/perk.delete.d.ts +2 -0
- package/dist/runtime/server/routes/admin/perk.delete.js +10 -0
- package/dist/runtime/server/routes/admin/perk.patch.d.ts +2 -0
- package/dist/runtime/server/routes/admin/perk.patch.js +10 -0
- package/dist/runtime/server/routes/admin/perks.get.d.ts +2 -0
- package/dist/runtime/server/routes/admin/perks.get.js +9 -0
- package/dist/runtime/server/routes/admin/perks.post.d.ts +2 -0
- package/dist/runtime/server/routes/admin/perks.post.js +10 -0
- package/dist/runtime/server/routes/admin/role.delete.d.ts +2 -0
- package/dist/runtime/server/routes/admin/role.delete.js +11 -0
- package/dist/runtime/server/routes/admin/role.patch.d.ts +2 -0
- package/dist/runtime/server/routes/admin/role.patch.js +11 -0
- package/dist/runtime/server/routes/admin/roles.get.d.ts +2 -0
- package/dist/runtime/server/routes/admin/roles.get.js +9 -0
- package/dist/runtime/server/routes/admin/roles.post.d.ts +2 -0
- package/dist/runtime/server/routes/admin/roles.post.js +10 -0
- package/dist/runtime/server/routes/admin/schedule-copy.post.d.ts +2 -0
- package/dist/runtime/server/routes/admin/schedule-copy.post.js +12 -0
- package/dist/runtime/server/routes/admin/schedule-publish.post.d.ts +2 -0
- package/dist/runtime/server/routes/admin/schedule-publish.post.js +9 -0
- package/dist/runtime/server/routes/admin/schedule.delete.d.ts +2 -0
- package/dist/runtime/server/routes/admin/schedule.delete.js +10 -0
- package/dist/runtime/server/routes/admin/schedule.get.d.ts +2 -0
- package/dist/runtime/server/routes/admin/schedule.get.js +9 -0
- package/dist/runtime/server/routes/admin/schedule.patch.d.ts +2 -0
- package/dist/runtime/server/routes/admin/schedule.patch.js +10 -0
- package/dist/runtime/server/routes/admin/schedules.get.d.ts +2 -0
- package/dist/runtime/server/routes/admin/schedules.get.js +10 -0
- package/dist/runtime/server/routes/admin/schedules.post.d.ts +2 -0
- package/dist/runtime/server/routes/admin/schedules.post.js +9 -0
- package/dist/runtime/server/routes/admin/shift.delete.d.ts +2 -0
- package/dist/runtime/server/routes/admin/shift.delete.js +10 -0
- package/dist/runtime/server/routes/admin/shift.patch.d.ts +2 -0
- package/dist/runtime/server/routes/admin/shift.patch.js +10 -0
- package/dist/runtime/server/routes/admin/shifts-bulk.post.d.ts +2 -0
- package/dist/runtime/server/routes/admin/shifts-bulk.post.js +10 -0
- package/dist/runtime/server/routes/admin/shifts.get.d.ts +2 -0
- package/dist/runtime/server/routes/admin/shifts.get.js +10 -0
- package/dist/runtime/server/routes/admin/station.delete.d.ts +2 -0
- package/dist/runtime/server/routes/admin/station.delete.js +11 -0
- package/dist/runtime/server/routes/admin/station.patch.d.ts +2 -0
- package/dist/runtime/server/routes/admin/station.patch.js +11 -0
- package/dist/runtime/server/routes/admin/stations.get.d.ts +2 -0
- package/dist/runtime/server/routes/admin/stations.get.js +9 -0
- package/dist/runtime/server/routes/admin/stations.post.d.ts +2 -0
- package/dist/runtime/server/routes/admin/stations.post.js +10 -0
- package/dist/runtime/server/routes/admin/swap-cancel.post.d.ts +2 -0
- package/dist/runtime/server/routes/admin/swap-cancel.post.js +10 -0
- package/dist/runtime/server/routes/admin/swaps.get.d.ts +2 -0
- package/dist/runtime/server/routes/admin/swaps.get.js +10 -0
- package/dist/runtime/server/routes/feed/feed.get.d.ts +2 -0
- package/dist/runtime/server/routes/feed/feed.get.js +9 -0
- package/dist/runtime/server/routes/lead/attended.post.d.ts +2 -0
- package/dist/runtime/server/routes/lead/attended.post.js +11 -0
- package/dist/runtime/server/routes/lead/broadcast.post.d.ts +2 -0
- package/dist/runtime/server/routes/lead/broadcast.post.js +12 -0
- package/dist/runtime/server/routes/lead/check-in.post.d.ts +2 -0
- package/dist/runtime/server/routes/lead/check-in.post.js +9 -0
- package/dist/runtime/server/routes/lead/exceptions.get.d.ts +2 -0
- package/dist/runtime/server/routes/lead/exceptions.get.js +8 -0
- package/dist/runtime/server/routes/lead/no-show.post.d.ts +2 -0
- package/dist/runtime/server/routes/lead/no-show.post.js +11 -0
- package/dist/runtime/server/routes/lead/now.get.d.ts +2 -0
- package/dist/runtime/server/routes/lead/now.get.js +9 -0
- package/dist/runtime/server/routes/lead/on-duty.get.d.ts +2 -0
- package/dist/runtime/server/routes/lead/on-duty.get.js +10 -0
- package/dist/runtime/server/routes/lead/stations.get.d.ts +2 -0
- package/dist/runtime/server/routes/lead/stations.get.js +8 -0
- package/dist/runtime/server/routes/lead/team-shifts.get.d.ts +2 -0
- package/dist/runtime/server/routes/lead/team-shifts.get.js +9 -0
- package/dist/runtime/server/routes/lead/team-swaps.get.d.ts +2 -0
- package/dist/runtime/server/routes/lead/team-swaps.get.js +9 -0
- package/dist/runtime/server/routes/me/check-in.post.d.ts +2 -0
- package/dist/runtime/server/routes/me/check-in.post.js +11 -0
- package/dist/runtime/server/routes/me/earned-perk-claim.post.d.ts +2 -0
- package/dist/runtime/server/routes/me/earned-perk-claim.post.js +10 -0
- package/dist/runtime/server/routes/me/earned-perks.get.d.ts +2 -0
- package/dist/runtime/server/routes/me/earned-perks.get.js +9 -0
- package/dist/runtime/server/routes/me/feed-token.delete.d.ts +2 -0
- package/dist/runtime/server/routes/me/feed-token.delete.js +7 -0
- package/dist/runtime/server/routes/me/feed-token.get.d.ts +2 -0
- package/dist/runtime/server/routes/me/feed-token.get.js +6 -0
- package/dist/runtime/server/routes/me/feed-token.post.d.ts +2 -0
- package/dist/runtime/server/routes/me/feed-token.post.js +7 -0
- package/dist/runtime/server/routes/me/leads.get.d.ts +2 -0
- package/dist/runtime/server/routes/me/leads.get.js +5 -0
- package/dist/runtime/server/routes/me/now.get.d.ts +2 -0
- package/dist/runtime/server/routes/me/now.get.js +5 -0
- package/dist/runtime/server/routes/me/pass.get.d.ts +2 -0
- package/dist/runtime/server/routes/me/pass.get.js +11 -0
- package/dist/runtime/server/routes/me/settle.post.d.ts +2 -0
- package/dist/runtime/server/routes/me/settle.post.js +14 -0
- package/dist/runtime/server/routes/me/shift.get.d.ts +2 -0
- package/dist/runtime/server/routes/me/shift.get.js +10 -0
- package/dist/runtime/server/routes/me/shifts.get.d.ts +2 -0
- package/dist/runtime/server/routes/me/shifts.get.js +6 -0
- package/dist/runtime/server/routes/me/swap.delete.d.ts +2 -0
- package/dist/runtime/server/routes/me/swap.delete.js +11 -0
- package/dist/runtime/server/routes/me/swap.post.d.ts +2 -0
- package/dist/runtime/server/routes/me/swap.post.js +11 -0
- package/dist/runtime/server/routes/stations/token.get.d.ts +2 -0
- package/dist/runtime/server/routes/stations/token.get.js +12 -0
- package/dist/runtime/server/routes/swaps/approve.post.d.ts +2 -0
- package/dist/runtime/server/routes/swaps/approve.post.js +9 -0
- package/dist/runtime/server/routes/swaps/list.get.d.ts +2 -0
- package/dist/runtime/server/routes/swaps/list.get.js +5 -0
- package/dist/runtime/server/routes/swaps/offer-accept.post.d.ts +2 -0
- package/dist/runtime/server/routes/swaps/offer-accept.post.js +9 -0
- package/dist/runtime/server/routes/swaps/offer-reject.post.d.ts +2 -0
- package/dist/runtime/server/routes/swaps/offer-reject.post.js +9 -0
- package/dist/runtime/server/routes/swaps/offer.delete.d.ts +2 -0
- package/dist/runtime/server/routes/swaps/offer.delete.js +11 -0
- package/dist/runtime/server/routes/swaps/offer.post.d.ts +2 -0
- package/dist/runtime/server/routes/swaps/offer.post.js +11 -0
- package/dist/runtime/server/routes/swaps/reject.post.d.ts +2 -0
- package/dist/runtime/server/routes/swaps/reject.post.js +9 -0
- package/dist/runtime/server/swap-email.d.ts +28 -0
- package/dist/runtime/server/swap-email.js +49 -0
- package/dist/runtime/server/tasks/close-shifts.d.ts +2 -0
- package/dist/runtime/server/tasks/close-shifts.js +9 -0
- package/dist/runtime/server/tasks/send-reminders.d.ts +2 -0
- package/dist/runtime/server/tasks/send-reminders.js +9 -0
- package/dist/runtime/server/utils/shifts-admin.d.ts +67 -0
- package/dist/runtime/server/utils/shifts-admin.js +312 -0
- package/dist/runtime/server/utils/shifts-assign.d.ts +89 -0
- package/dist/runtime/server/utils/shifts-assign.js +264 -0
- package/dist/runtime/server/utils/shifts-automation.d.ts +37 -0
- package/dist/runtime/server/utils/shifts-automation.js +171 -0
- package/dist/runtime/server/utils/shifts-broadcasts.d.ts +38 -0
- package/dist/runtime/server/utils/shifts-broadcasts.js +113 -0
- package/dist/runtime/server/utils/shifts-core.d.ts +78 -0
- package/dist/runtime/server/utils/shifts-core.js +142 -0
- package/dist/runtime/server/utils/shifts-dto.d.ts +32 -0
- package/dist/runtime/server/utils/shifts-dto.js +127 -0
- package/dist/runtime/server/utils/shifts-emails.d.ts +16 -0
- package/dist/runtime/server/utils/shifts-emails.js +44 -0
- package/dist/runtime/server/utils/shifts-exceptions.d.ts +7 -0
- package/dist/runtime/server/utils/shifts-exceptions.js +32 -0
- package/dist/runtime/server/utils/shifts-feed.d.ts +16 -0
- package/dist/runtime/server/utils/shifts-feed.js +73 -0
- package/dist/runtime/server/utils/shifts-notify.d.ts +40 -0
- package/dist/runtime/server/utils/shifts-notify.js +22 -0
- package/dist/runtime/server/utils/shifts-on-duty.d.ts +12 -0
- package/dist/runtime/server/utils/shifts-on-duty.js +37 -0
- package/dist/runtime/server/utils/shifts-perks.d.ts +46 -0
- package/dist/runtime/server/utils/shifts-perks.js +183 -0
- package/dist/runtime/server/utils/shifts-reminders.d.ts +30 -0
- package/dist/runtime/server/utils/shifts-reminders.js +102 -0
- package/dist/runtime/server/utils/shifts-schemas.d.ts +318 -0
- package/dist/runtime/server/utils/shifts-schemas.js +177 -0
- package/dist/runtime/server/utils/shifts-station-key.d.ts +7 -0
- package/dist/runtime/server/utils/shifts-station-key.js +22 -0
- package/dist/runtime/server/utils/shifts-stations.d.ts +28 -0
- package/dist/runtime/server/utils/shifts-stations.js +109 -0
- package/dist/runtime/server/utils/shifts-swaps.d.ts +114 -0
- package/dist/runtime/server/utils/shifts-swaps.js +559 -0
- package/dist/runtime/server/utils/shifts-views.d.ts +62 -0
- package/dist/runtime/server/utils/shifts-views.js +212 -0
- package/dist/runtime/utils/api.d.ts +108 -0
- package/dist/runtime/utils/api.js +111 -0
- package/dist/runtime/utils/checkin.d.ts +59 -0
- package/dist/runtime/utils/checkin.js +57 -0
- package/dist/runtime/utils/guards.d.ts +21 -0
- package/dist/runtime/utils/guards.js +36 -0
- package/dist/runtime/utils/ics.d.ts +32 -0
- package/dist/runtime/utils/ics.js +56 -0
- package/dist/runtime/utils/index.d.ts +16 -0
- package/dist/runtime/utils/index.js +15 -0
- package/dist/runtime/utils/links.d.ts +30 -0
- package/dist/runtime/utils/links.js +18 -0
- package/dist/runtime/utils/reminders.d.ts +27 -0
- package/dist/runtime/utils/reminders.js +28 -0
- package/dist/runtime/utils/settings.d.ts +28 -0
- package/dist/runtime/utils/settings.js +18 -0
- package/dist/runtime/utils/station-key.d.ts +9 -0
- package/dist/runtime/utils/station-key.js +12 -0
- package/dist/runtime/utils/status.d.ts +23 -0
- package/dist/runtime/utils/status.js +46 -0
- package/dist/runtime/utils/time.d.ts +22 -0
- package/dist/runtime/utils/time.js +14 -0
- package/dist/types.d.mts +5 -0
- package/package.json +131 -3
|
@@ -0,0 +1,559 @@
|
|
|
1
|
+
import { db, schema } from "@nuxthub/db";
|
|
2
|
+
import { and, asc, desc, eq, gt, inArray, is, ne } from "drizzle-orm";
|
|
3
|
+
import { PgTable } from "drizzle-orm/pg-core";
|
|
4
|
+
import { apiError, chunkParams, notFound, validationError } from "@frigear-nu/module-kit/server";
|
|
5
|
+
import { newCheckInCode, newId } from "../../utils/index.js";
|
|
6
|
+
import { effectiveCapacity } from "../../utils/guards.js";
|
|
7
|
+
import { allowsSwapKind, defaultSwapKind, offerKind, scheduleSettingsOf } from "../../utils/settings.js";
|
|
8
|
+
import { LIVE_SWAP_STATUSES, isLiveSwap } from "../../utils/status.js";
|
|
9
|
+
import { toShiftAssignmentDto, toShiftDto, toShiftPersonDto, toShiftSwapOfferDto } from "./shifts-dto.js";
|
|
10
|
+
import {
|
|
11
|
+
findShiftAssignmentEntry,
|
|
12
|
+
getShiftAssignmentEntryOr404,
|
|
13
|
+
shiftsActiveAssignmentOf,
|
|
14
|
+
shiftsEligibility,
|
|
15
|
+
shiftsFilledCount,
|
|
16
|
+
shiftsFire,
|
|
17
|
+
shiftsGuardWarningsFor,
|
|
18
|
+
shiftsIsFuture,
|
|
19
|
+
shiftsRoleById,
|
|
20
|
+
shiftsTeamsById,
|
|
21
|
+
shiftsUsersById
|
|
22
|
+
} from "./shifts-core.js";
|
|
23
|
+
import { shiftsSendSwapEmail } from "./shifts-emails.js";
|
|
24
|
+
const LIVE_OFFER_STATUSES = ["offered", "accepted"];
|
|
25
|
+
export const SHIFTS_STUCK_SWAP_MS = 5 * 6e4;
|
|
26
|
+
const SWAP_GUARD_CODES = {
|
|
27
|
+
overlap: "guard_overlap",
|
|
28
|
+
min_rest: "guard_rest",
|
|
29
|
+
max_shifts_per_day: "guard_max_per_day"
|
|
30
|
+
};
|
|
31
|
+
export async function findShiftSwap(id) {
|
|
32
|
+
const [row] = await db.select().from(schema.shiftSwaps).where(eq(schema.shiftSwaps.id, id)).limit(1);
|
|
33
|
+
return row;
|
|
34
|
+
}
|
|
35
|
+
export async function getShiftSwapOr404(id) {
|
|
36
|
+
const row = await findShiftSwap(id);
|
|
37
|
+
if (!row) throw notFound("Swap not found.");
|
|
38
|
+
return row;
|
|
39
|
+
}
|
|
40
|
+
export async function getShiftSwapOfferOr404(swapId, offerId) {
|
|
41
|
+
const [row] = await db.select().from(schema.shiftSwapOffers).where(eq(schema.shiftSwapOffers.id, offerId)).limit(1);
|
|
42
|
+
if (!row || row.swapId !== swapId) throw notFound("Offer not found.");
|
|
43
|
+
return row;
|
|
44
|
+
}
|
|
45
|
+
export async function findLiveShiftSwapOf(assignmentId) {
|
|
46
|
+
const [row] = await db.select().from(schema.shiftSwaps).where(and(eq(schema.shiftSwaps.assignmentId, assignmentId), inArray(schema.shiftSwaps.status, [...LIVE_SWAP_STATUSES]))).orderBy(asc(schema.shiftSwaps.createdAt), asc(schema.shiftSwaps.id)).limit(1);
|
|
47
|
+
return row;
|
|
48
|
+
}
|
|
49
|
+
async function shiftsMoveSwap(swap, from, to, extra = {}) {
|
|
50
|
+
const froms = Array.isArray(from) ? from : [from];
|
|
51
|
+
const [row] = await db.update(schema.shiftSwaps).set({ status: to, ...extra }).where(and(eq(schema.shiftSwaps.id, swap.id), inArray(schema.shiftSwaps.status, froms))).returning();
|
|
52
|
+
return row;
|
|
53
|
+
}
|
|
54
|
+
async function shiftsMoveOffer(offer, from, to, extra = {}) {
|
|
55
|
+
const froms = Array.isArray(from) ? from : [from];
|
|
56
|
+
const [row] = await db.update(schema.shiftSwapOffers).set({ status: to, decidedAt: /* @__PURE__ */ new Date(), ...extra }).where(and(eq(schema.shiftSwapOffers.id, offer.id), inArray(schema.shiftSwapOffers.status, froms))).returning();
|
|
57
|
+
return row;
|
|
58
|
+
}
|
|
59
|
+
async function shiftsSyncSwapStatus(swapId) {
|
|
60
|
+
const [waiting] = await db.select({ id: schema.shiftSwapOffers.id }).from(schema.shiftSwapOffers).where(and(eq(schema.shiftSwapOffers.swapId, swapId), eq(schema.shiftSwapOffers.status, "offered"))).limit(1);
|
|
61
|
+
if (!waiting) await shiftsMoveSwap({ id: swapId }, "offered", "open");
|
|
62
|
+
}
|
|
63
|
+
export async function shiftsSwapDtos(rows, options = {}) {
|
|
64
|
+
if (!rows.length) return [];
|
|
65
|
+
const entries = /* @__PURE__ */ new Map();
|
|
66
|
+
for (const row of rows) {
|
|
67
|
+
const entry = await findShiftAssignmentEntry(row.assignmentId);
|
|
68
|
+
if (entry) entries.set(row.assignmentId, entry);
|
|
69
|
+
}
|
|
70
|
+
const offers = (await Promise.all(chunkParams(rows.map((row) => row.id)).map((part) => db.select().from(schema.shiftSwapOffers).where(inArray(schema.shiftSwapOffers.swapId, part))))).flat().sort((a, b) => a.createdAt.getTime() - b.createdAt.getTime() || (a.id < b.id ? -1 : a.id > b.id ? 1 : 0));
|
|
71
|
+
const offered = /* @__PURE__ */ new Map();
|
|
72
|
+
for (const id of [...offers.map((offer) => offer.offeredAssignmentId), ...rows.map((row) => row.wantedAssignmentId)]) {
|
|
73
|
+
if (id && !offered.has(id)) {
|
|
74
|
+
const entry = await findShiftAssignmentEntry(id);
|
|
75
|
+
if (entry) offered.set(id, entry);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
const allEntries = [...entries.values(), ...offered.values()];
|
|
79
|
+
const [users, teams] = await Promise.all([
|
|
80
|
+
shiftsUsersById([...rows.map((row) => row.userId), ...allEntries.map((entry) => entry.assignment.userId), ...offers.map((offer) => offer.userId)]),
|
|
81
|
+
shiftsTeamsById(allEntries.map((entry) => entry.shift.teamId))
|
|
82
|
+
]);
|
|
83
|
+
const teamName = (id) => teams.get(id)?.name ?? id;
|
|
84
|
+
const offerDto = (offer) => {
|
|
85
|
+
const back = offer.offeredAssignmentId ? offered.get(offer.offeredAssignmentId) : void 0;
|
|
86
|
+
return toShiftSwapOfferDto(offer, {
|
|
87
|
+
person: toShiftPersonDto(offer.userId, users.get(offer.userId), false),
|
|
88
|
+
offeredShift: back ? { ...toShiftDto(back.shift, back.schedule), teamName: teamName(back.shift.teamId) } : null
|
|
89
|
+
});
|
|
90
|
+
};
|
|
91
|
+
return rows.flatMap((row) => {
|
|
92
|
+
const entry = entries.get(row.assignmentId);
|
|
93
|
+
if (!entry) return [];
|
|
94
|
+
const own = offers.filter((offer) => offer.swapId === row.id);
|
|
95
|
+
const mine = options.viewerId === void 0 ? void 0 : own.find((offer) => offer.userId === options.viewerId && LIVE_OFFER_STATUSES.includes(offer.status));
|
|
96
|
+
const wanted = row.wantedAssignmentId ? offered.get(row.wantedAssignmentId) : void 0;
|
|
97
|
+
return [{
|
|
98
|
+
id: row.id,
|
|
99
|
+
assignmentId: row.assignmentId,
|
|
100
|
+
kind: row.kind,
|
|
101
|
+
status: row.status,
|
|
102
|
+
openedAt: row.openedAt.toISOString(),
|
|
103
|
+
decidedAt: row.decidedAt?.toISOString() ?? null,
|
|
104
|
+
shift: { ...toShiftDto(entry.shift, entry.schedule), teamName: teamName(entry.shift.teamId), scheduleName: entry.schedule.name },
|
|
105
|
+
owner: toShiftPersonDto(row.userId, users.get(row.userId), false),
|
|
106
|
+
wantedAssignmentId: row.wantedAssignmentId ?? null,
|
|
107
|
+
wantedShift: wanted ? { ...toShiftDto(wanted.shift, wanted.schedule), teamName: teamName(wanted.shift.teamId) } : null,
|
|
108
|
+
settings: scheduleSettingsOf(entry.schedule),
|
|
109
|
+
...options.offers ? { offers: own.map(offerDto) } : {},
|
|
110
|
+
...options.viewerId === void 0 ? {} : { myOffer: mine ? offerDto(mine) : null }
|
|
111
|
+
}];
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
async function shiftsSwapDtoOf(row, options = { offers: true }) {
|
|
115
|
+
const [dto] = await shiftsSwapDtos([row], options);
|
|
116
|
+
if (!dto) throw notFound("Swap not found.");
|
|
117
|
+
return dto;
|
|
118
|
+
}
|
|
119
|
+
async function shiftsOfferDtoOf(swap, offer) {
|
|
120
|
+
return swap.offers?.find((item) => item.id === offer.id) ?? toShiftSwapOfferDto(offer);
|
|
121
|
+
}
|
|
122
|
+
function shiftsAssertSwappable(entry, field) {
|
|
123
|
+
if (entry.assignment.status !== "assigned") throw apiError(409, "not_swappable", `Only an assigned shift can be swapped (${field}).`, { field });
|
|
124
|
+
if (entry.schedule.status !== "published") throw apiError(409, "not_swappable", `The schedule is not published (${field}).`, { field });
|
|
125
|
+
if (!shiftsIsFuture(entry.shift)) throw apiError(409, "not_swappable", `The shift has started already (${field}).`, { field });
|
|
126
|
+
}
|
|
127
|
+
function shiftsAssertHandover(settings, kind) {
|
|
128
|
+
if (allowsSwapKind(settings, kind)) return;
|
|
129
|
+
const message = kind === "trade" ? "This schedule does not let volunteers trade shifts." : settings.coverage === "all" ? "Volunteers must work every shift they hold in this schedule: trade the shift for one of theirs instead." : "This schedule does not let volunteers give shifts away or take them over: trade it instead.";
|
|
130
|
+
throw apiError(409, "handover_not_allowed", message, { kind, coverage: settings.coverage, handover: settings.handover });
|
|
131
|
+
}
|
|
132
|
+
async function shiftsAssertCanTake(event, userId, entry, giving) {
|
|
133
|
+
const role = await shiftsRoleById(entry.assignment.shiftRoleId);
|
|
134
|
+
const eligibility = await shiftsEligibility(event, userId, entry.shift.teamId, role);
|
|
135
|
+
if (!eligibility.eligible) throw apiError(409, "guard_ineligible", "This shift needs a role on its team that the person does not hold.", { reason: eligibility.reason, userId });
|
|
136
|
+
if (await shiftsActiveAssignmentOf(entry.shift.id, userId)) throw apiError(409, "already_assigned", "The person is on that shift already.", { userId });
|
|
137
|
+
const capacity = effectiveCapacity(entry.shift, entry.schedule);
|
|
138
|
+
if (capacity !== null && await shiftsFilledCount(entry.shift.id) > capacity) {
|
|
139
|
+
throw apiError(409, "guard_capacity", `The shift holds more people than its capacity of ${capacity}: staff sort it out first.`, { capacity, userId });
|
|
140
|
+
}
|
|
141
|
+
const warnings = (await shiftsGuardWarningsFor(event, userId, entry, giving ? [giving] : [])).filter((warning) => SWAP_GUARD_CODES[warning.code]);
|
|
142
|
+
if (warnings.length) throw apiError(409, SWAP_GUARD_CODES[warnings[0].code], warnings[0].message, { warnings, userId });
|
|
143
|
+
}
|
|
144
|
+
function shiftsAssertTradeSchedules(owner, back) {
|
|
145
|
+
shiftsAssertHandover(scheduleSettingsOf(owner.schedule), "trade");
|
|
146
|
+
shiftsAssertHandover(scheduleSettingsOf(back.schedule), "trade");
|
|
147
|
+
if (owner.schedule.id === back.schedule.id) return;
|
|
148
|
+
if (owner.schedule.coverage === "all" || back.schedule.coverage === "all") {
|
|
149
|
+
throw apiError(409, "handover_cross_schedule", "A schedule where every shift must be worked only trades within itself: offer a shift of the same schedule.", { field: "assignmentId", scheduleId: owner.schedule.id });
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
const shiftsNeedsLead = (owner, back) => owner.schedule.approval === "lead" || back?.schedule.approval === "lead";
|
|
153
|
+
async function shiftsAssertExchange(event, owner, offererId, back) {
|
|
154
|
+
shiftsAssertSwappable(owner, "swap");
|
|
155
|
+
if (back) {
|
|
156
|
+
if (back.assignment.userId !== offererId) throw apiError(409, "not_swappable", "The shift offered in return is not the offerer's.", { field: "assignmentId" });
|
|
157
|
+
shiftsAssertTradeSchedules(owner, back);
|
|
158
|
+
shiftsAssertSwappable(back, "assignmentId");
|
|
159
|
+
if (back.shift.id === owner.shift.id) throw apiError(409, "not_swappable", "Offer another shift than the one on offer.", { field: "assignmentId" });
|
|
160
|
+
const [busy] = await db.select({ id: schema.shiftSwaps.id }).from(schema.shiftSwaps).where(and(
|
|
161
|
+
eq(schema.shiftSwaps.assignmentId, back.assignment.id),
|
|
162
|
+
inArray(schema.shiftSwaps.status, ["accepted", "pending_approval"])
|
|
163
|
+
)).limit(1);
|
|
164
|
+
if (busy) throw apiError(409, "not_swappable", "The shift offered in return is in another swap that is being carried out.", { field: "assignmentId" });
|
|
165
|
+
}
|
|
166
|
+
await shiftsAssertCanTake(event, offererId, owner, back?.assignment.id ?? null);
|
|
167
|
+
if (back) await shiftsAssertCanTake(event, owner.assignment.userId, back, owner.assignment.id);
|
|
168
|
+
}
|
|
169
|
+
export async function shiftsOpenSwap(event, entry, input = {}) {
|
|
170
|
+
shiftsAssertSwappable(entry, "swap");
|
|
171
|
+
const settings = scheduleSettingsOf(entry.schedule);
|
|
172
|
+
const existing = await findLiveShiftSwapOf(entry.assignment.id);
|
|
173
|
+
const existingAllowed = !!existing && allowsSwapKind(settings, existing.kind);
|
|
174
|
+
const kind = input.kind ?? (existing && existingAllowed ? existing.kind : defaultSwapKind(settings));
|
|
175
|
+
shiftsAssertHandover(settings, kind);
|
|
176
|
+
if (existing) {
|
|
177
|
+
if (existingAllowed && existing.kind === kind) return shiftsSwapDtoOf(existing);
|
|
178
|
+
const cancelled = existing.status === "open" ? await shiftsMoveSwap(existing, "open", "cancelled", { decidedAt: /* @__PURE__ */ new Date() }) : void 0;
|
|
179
|
+
if (!cancelled) throw apiError(409, "swap_changed", "This shift is in a swap of another kind with offers: withdraw it first.", { swapId: existing.id, kind: existing.kind });
|
|
180
|
+
}
|
|
181
|
+
let wanted = null;
|
|
182
|
+
if (input.wantedAssignmentId) {
|
|
183
|
+
if (kind !== "trade") throw validationError([{ path: ["wantedAssignmentId"], message: "Only a trade asks for a shift in return." }]);
|
|
184
|
+
wanted = await findShiftAssignmentEntry(input.wantedAssignmentId) ?? null;
|
|
185
|
+
if (!wanted || wanted.assignment.userId === entry.assignment.userId) {
|
|
186
|
+
throw validationError([{ path: ["wantedAssignmentId"], message: "Pick a shift somebody else holds." }], "Unknown assignment");
|
|
187
|
+
}
|
|
188
|
+
await shiftsAssertExchange(event, entry, wanted.assignment.userId, wanted);
|
|
189
|
+
}
|
|
190
|
+
const [row] = await db.insert(schema.shiftSwaps).values({
|
|
191
|
+
id: newId("shw"),
|
|
192
|
+
assignmentId: entry.assignment.id,
|
|
193
|
+
userId: entry.assignment.userId,
|
|
194
|
+
kind,
|
|
195
|
+
wantedAssignmentId: wanted?.assignment.id ?? null,
|
|
196
|
+
status: "open"
|
|
197
|
+
}).returning();
|
|
198
|
+
const first = await findLiveShiftSwapOf(entry.assignment.id);
|
|
199
|
+
if (first && first.id !== row.id) {
|
|
200
|
+
await db.delete(schema.shiftSwaps).where(eq(schema.shiftSwaps.id, row.id));
|
|
201
|
+
return shiftsSwapDtoOf(first);
|
|
202
|
+
}
|
|
203
|
+
const dto = await shiftsSwapDtoOf(row);
|
|
204
|
+
if (wanted) await shiftsSendSwapEmail(event, "proposed", { swap: dto, to: wanted.assignment.userId });
|
|
205
|
+
return dto;
|
|
206
|
+
}
|
|
207
|
+
export async function shiftsWithdrawSwap(entry) {
|
|
208
|
+
const swap = await findLiveShiftSwapOf(entry.assignment.id);
|
|
209
|
+
if (!swap) throw notFound("This shift is not offered for a swap.");
|
|
210
|
+
if (swap.status === "accepted") {
|
|
211
|
+
if (Date.now() - swap.updatedAt.getTime() < SHIFTS_STUCK_SWAP_MS) throw apiError(409, "swap_changed", "The swap is being completed. Try again.");
|
|
212
|
+
await shiftsRecoverSwap(swap);
|
|
213
|
+
return;
|
|
214
|
+
}
|
|
215
|
+
const cancelled = await shiftsMoveSwap(swap, ["open", "offered", "pending_approval"], "cancelled", { decidedAt: /* @__PURE__ */ new Date() });
|
|
216
|
+
if (!cancelled) throw apiError(409, "swap_changed", "The swap is being completed. Try again.");
|
|
217
|
+
await db.update(schema.shiftSwapOffers).set({ status: "superseded", decidedAt: /* @__PURE__ */ new Date() }).where(and(eq(schema.shiftSwapOffers.swapId, swap.id), inArray(schema.shiftSwapOffers.status, LIVE_OFFER_STATUSES)));
|
|
218
|
+
}
|
|
219
|
+
export async function shiftsCancelSwapsOf(_event, assignmentId) {
|
|
220
|
+
const swaps = await db.select().from(schema.shiftSwaps).where(and(eq(schema.shiftSwaps.assignmentId, assignmentId), inArray(schema.shiftSwaps.status, ["open", "offered", "pending_approval"])));
|
|
221
|
+
for (const swap of swaps) {
|
|
222
|
+
if (!await shiftsMoveSwap(swap, swap.status, "cancelled", { decidedAt: /* @__PURE__ */ new Date() })) continue;
|
|
223
|
+
await db.update(schema.shiftSwapOffers).set({ status: "superseded", decidedAt: /* @__PURE__ */ new Date() }).where(and(eq(schema.shiftSwapOffers.swapId, swap.id), inArray(schema.shiftSwapOffers.status, LIVE_OFFER_STATUSES)));
|
|
224
|
+
}
|
|
225
|
+
const giving = await db.select().from(schema.shiftSwapOffers).where(and(eq(schema.shiftSwapOffers.offeredAssignmentId, assignmentId), inArray(schema.shiftSwapOffers.status, LIVE_OFFER_STATUSES)));
|
|
226
|
+
for (const offer of giving) {
|
|
227
|
+
const moved = await shiftsMoveOffer(offer, offer.status, "withdrawn");
|
|
228
|
+
if (!moved) continue;
|
|
229
|
+
if (offer.status === "accepted") await shiftsMoveSwap({ id: offer.swapId }, "pending_approval", "open");
|
|
230
|
+
else await shiftsSyncSwapStatus(offer.swapId);
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
export async function shiftsAvailableSwaps(event, userId) {
|
|
234
|
+
const rows = await db.select({ swap: schema.shiftSwaps, assignment: schema.shiftAssignments, shift: schema.shifts, schedule: schema.shiftSchedules }).from(schema.shiftSwaps).innerJoin(schema.shiftAssignments, eq(schema.shiftAssignments.id, schema.shiftSwaps.assignmentId)).innerJoin(schema.shifts, eq(schema.shifts.id, schema.shiftAssignments.shiftId)).innerJoin(schema.shiftSchedules, eq(schema.shiftSchedules.id, schema.shifts.scheduleId)).where(and(
|
|
235
|
+
inArray(schema.shiftSwaps.status, ["open", "offered"]),
|
|
236
|
+
eq(schema.shiftSchedules.status, "published"),
|
|
237
|
+
eq(schema.shiftAssignments.status, "assigned"),
|
|
238
|
+
ne(schema.shiftAssignments.userId, userId),
|
|
239
|
+
gt(schema.shifts.startsAt, /* @__PURE__ */ new Date())
|
|
240
|
+
)).orderBy(asc(schema.shifts.startsAt), asc(schema.shiftSwaps.id)).limit(200);
|
|
241
|
+
const takeable = [];
|
|
242
|
+
for (const row of rows) {
|
|
243
|
+
if (!allowsSwapKind(scheduleSettingsOf(row.schedule), row.swap.kind)) continue;
|
|
244
|
+
if (row.swap.wantedAssignmentId) {
|
|
245
|
+
const wanted = await findShiftAssignmentEntry(row.swap.wantedAssignmentId);
|
|
246
|
+
if (wanted?.assignment.userId !== userId) continue;
|
|
247
|
+
}
|
|
248
|
+
const role = await shiftsRoleById(row.assignment.shiftRoleId);
|
|
249
|
+
if (!(await shiftsEligibility(event, userId, row.shift.teamId, role)).eligible) continue;
|
|
250
|
+
if (await shiftsActiveAssignmentOf(row.shift.id, userId)) continue;
|
|
251
|
+
takeable.push(row.swap);
|
|
252
|
+
}
|
|
253
|
+
return shiftsSwapDtos(takeable, { viewerId: userId });
|
|
254
|
+
}
|
|
255
|
+
export async function shiftsAdminSwaps(query) {
|
|
256
|
+
const rows = await db.select({ swap: schema.shiftSwaps }).from(schema.shiftSwaps).innerJoin(schema.shiftAssignments, eq(schema.shiftAssignments.id, schema.shiftSwaps.assignmentId)).innerJoin(schema.shifts, eq(schema.shifts.id, schema.shiftAssignments.shiftId)).innerJoin(schema.shiftSchedules, eq(schema.shiftSchedules.id, schema.shifts.scheduleId)).where(and(
|
|
257
|
+
query.operationId ? eq(schema.shiftSchedules.operationId, query.operationId) : void 0,
|
|
258
|
+
query.status ? eq(schema.shiftSwaps.status, query.status) : void 0
|
|
259
|
+
)).orderBy(desc(schema.shiftSwaps.openedAt), asc(schema.shiftSwaps.id)).limit(500);
|
|
260
|
+
return shiftsSwapDtos(rows.map((row) => row.swap), { offers: true });
|
|
261
|
+
}
|
|
262
|
+
export async function shiftsOffer(event, swap, userId, offeredAssignmentId) {
|
|
263
|
+
if (swap.status !== "open" && swap.status !== "offered") throw apiError(409, "swap_closed", "This swap takes no offers now.");
|
|
264
|
+
const owner = await getShiftAssignmentEntryOr404(swap.assignmentId);
|
|
265
|
+
if (swap.userId === userId || owner.assignment.userId === userId) throw apiError(409, "own_swap", "You cannot offer on your own swap.");
|
|
266
|
+
const kind = swap.kind;
|
|
267
|
+
if (kind === "release" && offeredAssignmentId) {
|
|
268
|
+
throw validationError([{ path: ["assignmentId"], message: "This shift is given away: offer to take it over without a shift in return." }]);
|
|
269
|
+
}
|
|
270
|
+
if (kind === "trade" && !offeredAssignmentId) {
|
|
271
|
+
throw apiError(409, "handover_not_allowed", "This is a trade: offer one of your own shifts in return.", { kind });
|
|
272
|
+
}
|
|
273
|
+
shiftsAssertHandover(scheduleSettingsOf(owner.schedule), offerKind({ offeredAssignmentId }));
|
|
274
|
+
if (swap.wantedAssignmentId && offeredAssignmentId !== swap.wantedAssignmentId) {
|
|
275
|
+
throw apiError(409, "not_wanted", "This trade asks for another shift in return.", { wantedAssignmentId: swap.wantedAssignmentId });
|
|
276
|
+
}
|
|
277
|
+
const back = offeredAssignmentId ? await findShiftAssignmentEntry(offeredAssignmentId) : null;
|
|
278
|
+
if (offeredAssignmentId && (!back || back.assignment.userId !== userId)) throw notFound("Assignment not found.");
|
|
279
|
+
const [existing] = await db.select({ id: schema.shiftSwapOffers.id }).from(schema.shiftSwapOffers).where(and(
|
|
280
|
+
eq(schema.shiftSwapOffers.swapId, swap.id),
|
|
281
|
+
eq(schema.shiftSwapOffers.userId, userId),
|
|
282
|
+
inArray(schema.shiftSwapOffers.status, LIVE_OFFER_STATUSES)
|
|
283
|
+
)).limit(1);
|
|
284
|
+
if (existing) throw apiError(409, "offer_exists", "You have offered on this swap already. Withdraw that offer first.");
|
|
285
|
+
await shiftsAssertExchange(event, owner, userId, back ?? null);
|
|
286
|
+
const [offer] = await db.insert(schema.shiftSwapOffers).values({ id: newId("sho"), swapId: swap.id, userId, offeredAssignmentId: back?.assignment.id ?? null, status: "offered" }).returning();
|
|
287
|
+
const [first] = await db.select({ id: schema.shiftSwapOffers.id }).from(schema.shiftSwapOffers).where(and(
|
|
288
|
+
eq(schema.shiftSwapOffers.swapId, swap.id),
|
|
289
|
+
eq(schema.shiftSwapOffers.userId, userId),
|
|
290
|
+
inArray(schema.shiftSwapOffers.status, LIVE_OFFER_STATUSES)
|
|
291
|
+
)).orderBy(asc(schema.shiftSwapOffers.createdAt), asc(schema.shiftSwapOffers.id)).limit(1);
|
|
292
|
+
if (first && first.id !== offer.id) {
|
|
293
|
+
await db.delete(schema.shiftSwapOffers).where(eq(schema.shiftSwapOffers.id, offer.id));
|
|
294
|
+
throw apiError(409, "offer_exists", "You have offered on this swap already. Withdraw that offer first.");
|
|
295
|
+
}
|
|
296
|
+
const moved = await shiftsMoveSwap(swap, ["open", "offered"], "offered");
|
|
297
|
+
if (!moved) {
|
|
298
|
+
await db.delete(schema.shiftSwapOffers).where(eq(schema.shiftSwapOffers.id, offer.id));
|
|
299
|
+
throw apiError(409, "swap_closed", "This swap takes no offers now.");
|
|
300
|
+
}
|
|
301
|
+
const dto = await shiftsSwapDtoOf(moved);
|
|
302
|
+
const offerDto = await shiftsOfferDtoOf(dto, offer);
|
|
303
|
+
await shiftsFire("shifts:swap:offered", { event, kind: dto.kind, swap: dto, offer: offerDto });
|
|
304
|
+
await shiftsSendSwapEmail(event, "offered", { swap: dto, offer: offerDto, to: owner.assignment.userId });
|
|
305
|
+
return offerDto;
|
|
306
|
+
}
|
|
307
|
+
export async function shiftsWithdrawOffer(swap, offer) {
|
|
308
|
+
if (offer.status === "withdrawn") return toShiftSwapOfferDto(offer);
|
|
309
|
+
if (offer.status === "accepted" && swap.status === "pending_approval") {
|
|
310
|
+
const moved2 = await shiftsMoveOffer(offer, "accepted", "withdrawn");
|
|
311
|
+
if (!moved2) throw apiError(409, "offer_changed", "The offer changed meanwhile.");
|
|
312
|
+
if (!await shiftsMoveSwap(swap, "pending_approval", "open")) {
|
|
313
|
+
await shiftsMoveOffer(moved2, "withdrawn", "accepted", { decidedAt: offer.decidedAt });
|
|
314
|
+
throw apiError(409, "swap_changed", "The swap is being carried out.");
|
|
315
|
+
}
|
|
316
|
+
return toShiftSwapOfferDto(moved2);
|
|
317
|
+
}
|
|
318
|
+
if (offer.status !== "offered") throw apiError(409, "offer_changed", `The offer is ${offer.status} already.`);
|
|
319
|
+
const moved = await shiftsMoveOffer(offer, "offered", "withdrawn");
|
|
320
|
+
if (!moved) throw apiError(409, "offer_changed", "The offer changed meanwhile.");
|
|
321
|
+
await shiftsSyncSwapStatus(swap.id);
|
|
322
|
+
return toShiftSwapOfferDto(moved);
|
|
323
|
+
}
|
|
324
|
+
export async function shiftsRejectOffer(event, swap, offer) {
|
|
325
|
+
if (offer.status === "rejected") return toShiftSwapOfferDto(offer);
|
|
326
|
+
if (offer.status !== "offered") throw apiError(409, "offer_changed", `The offer is ${offer.status} already.`);
|
|
327
|
+
const moved = await shiftsMoveOffer(offer, "offered", "rejected");
|
|
328
|
+
if (!moved) throw apiError(409, "offer_changed", "The offer changed meanwhile.");
|
|
329
|
+
await shiftsSyncSwapStatus(swap.id);
|
|
330
|
+
const dto = await shiftsSwapDtoOf(await findShiftSwap(swap.id));
|
|
331
|
+
const offerDto = await shiftsOfferDtoOf(dto, moved);
|
|
332
|
+
await shiftsFire("shifts:swap:rejected", { event, kind: dto.kind, swap: dto, offer: offerDto });
|
|
333
|
+
await shiftsSendSwapEmail(event, "rejected", { swap: dto, offer: offerDto, to: moved.userId });
|
|
334
|
+
return offerDto;
|
|
335
|
+
}
|
|
336
|
+
export async function shiftsAcceptOffer(event, swap, offer) {
|
|
337
|
+
if (swap.status !== "offered") throw apiError(409, "swap_changed", `The swap is ${swap.status}.`);
|
|
338
|
+
if (offer.status !== "offered") throw apiError(409, "offer_changed", `The offer is ${offer.status} already.`);
|
|
339
|
+
const owner = await getShiftAssignmentEntryOr404(swap.assignmentId);
|
|
340
|
+
if (owner.assignment.userId !== swap.userId) throw apiError(409, "swap_changed", "The shift changed hands meanwhile.");
|
|
341
|
+
shiftsAssertHandover(scheduleSettingsOf(owner.schedule), offerKind(offer));
|
|
342
|
+
const back = offer.offeredAssignmentId ? await findShiftAssignmentEntry(offer.offeredAssignmentId) : null;
|
|
343
|
+
if (offer.offeredAssignmentId && !back) throw apiError(409, "not_swappable", "The shift offered in return no longer exists.");
|
|
344
|
+
await shiftsAssertExchange(event, owner, offer.userId, back ?? null);
|
|
345
|
+
const approval = shiftsNeedsLead(owner, back);
|
|
346
|
+
const claimed = await shiftsMoveSwap(swap, "offered", approval ? "pending_approval" : "accepted");
|
|
347
|
+
if (!claimed) throw apiError(409, "swap_changed", "The swap changed meanwhile.");
|
|
348
|
+
const accepted = await shiftsMoveOffer(offer, "offered", "accepted");
|
|
349
|
+
if (!accepted) {
|
|
350
|
+
await shiftsMoveSwap(claimed, claimed.status, "offered");
|
|
351
|
+
await shiftsSyncSwapStatus(swap.id);
|
|
352
|
+
throw apiError(409, "offer_changed", "The offer was withdrawn meanwhile.");
|
|
353
|
+
}
|
|
354
|
+
await db.update(schema.shiftSwapOffers).set({ status: "superseded", decidedAt: /* @__PURE__ */ new Date() }).where(and(
|
|
355
|
+
eq(schema.shiftSwapOffers.swapId, swap.id),
|
|
356
|
+
eq(schema.shiftSwapOffers.status, "offered"),
|
|
357
|
+
ne(schema.shiftSwapOffers.id, offer.id)
|
|
358
|
+
));
|
|
359
|
+
if (approval) {
|
|
360
|
+
const dto = await shiftsSwapDtoOf(claimed);
|
|
361
|
+
const offerDto = await shiftsOfferDtoOf(dto, accepted);
|
|
362
|
+
await shiftsFire("shifts:swap:accepted", { event, kind: dto.kind, swap: dto, offer: offerDto });
|
|
363
|
+
await shiftsSendSwapEmail(event, "accepted", { swap: dto, offer: offerDto, to: accepted.userId });
|
|
364
|
+
for (const [lead, teamId] of await shiftsSwapApprovers(event, shiftsApprovingTeamIds(owner, back))) {
|
|
365
|
+
if (lead === swap.userId || lead === accepted.userId) continue;
|
|
366
|
+
await shiftsSendSwapEmail(event, "pending", { swap: dto, offer: offerDto, to: lead, teamId });
|
|
367
|
+
}
|
|
368
|
+
return dto;
|
|
369
|
+
}
|
|
370
|
+
return shiftsCompleteSwap(event, claimed, accepted, null, { fireAccepted: true });
|
|
371
|
+
}
|
|
372
|
+
async function shiftsSwapApprovers(event, teamIds) {
|
|
373
|
+
const ids = /* @__PURE__ */ new Map();
|
|
374
|
+
for (const teamId of new Set(teamIds)) {
|
|
375
|
+
for (const member of await teamMembers(event, teamId, { status: true })) {
|
|
376
|
+
if (teamMemberStatusGrants(member.status) && await isTeamLead(event, member.userId, teamId) && !ids.has(member.userId)) ids.set(member.userId, teamId);
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
return ids;
|
|
380
|
+
}
|
|
381
|
+
export async function shiftsApproveSwap(event, swap, by) {
|
|
382
|
+
if (swap.status !== "pending_approval") throw apiError(409, "swap_changed", `The swap is ${swap.status}.`);
|
|
383
|
+
const [offer] = await db.select().from(schema.shiftSwapOffers).where(and(eq(schema.shiftSwapOffers.swapId, swap.id), eq(schema.shiftSwapOffers.status, "accepted"))).limit(1);
|
|
384
|
+
if (!offer) throw apiError(409, "swap_changed", "The swap has no accepted offer.");
|
|
385
|
+
const owner = await getShiftAssignmentEntryOr404(swap.assignmentId);
|
|
386
|
+
if (owner.assignment.userId !== swap.userId) throw apiError(409, "swap_changed", "The shift changed hands meanwhile.");
|
|
387
|
+
shiftsAssertHandover(scheduleSettingsOf(owner.schedule), offerKind(offer));
|
|
388
|
+
const back = offer.offeredAssignmentId ? await findShiftAssignmentEntry(offer.offeredAssignmentId) : null;
|
|
389
|
+
await shiftsAssertExchange(event, owner, offer.userId, back ?? null);
|
|
390
|
+
const claimed = await shiftsMoveSwap(swap, "pending_approval", "accepted");
|
|
391
|
+
if (!claimed) throw apiError(409, "swap_changed", "The swap changed meanwhile.");
|
|
392
|
+
return shiftsCompleteSwap(event, claimed, offer, by);
|
|
393
|
+
}
|
|
394
|
+
export async function shiftsRejectSwap(event, swap, by) {
|
|
395
|
+
if (swap.status !== "pending_approval") throw apiError(409, "swap_changed", `The swap is ${swap.status}.`);
|
|
396
|
+
const reopened = await shiftsMoveSwap(swap, "pending_approval", "open", { decidedAt: /* @__PURE__ */ new Date() });
|
|
397
|
+
if (!reopened) throw apiError(409, "swap_changed", "The swap changed meanwhile.");
|
|
398
|
+
const [offer] = await db.select().from(schema.shiftSwapOffers).where(and(eq(schema.shiftSwapOffers.swapId, swap.id), eq(schema.shiftSwapOffers.status, "accepted"))).limit(1);
|
|
399
|
+
const rejected = offer ? await shiftsMoveOffer(offer, "accepted", "rejected", { approvedBy: by }) : void 0;
|
|
400
|
+
const dto = await shiftsSwapDtoOf(reopened);
|
|
401
|
+
if (rejected) {
|
|
402
|
+
const offerDto = await shiftsOfferDtoOf(dto, rejected);
|
|
403
|
+
await shiftsFire("shifts:swap:rejected", { event, kind: dto.kind, swap: dto, offer: offerDto });
|
|
404
|
+
await shiftsSendSwapEmail(event, "rejected", { swap: dto, offer: offerDto, to: rejected.userId });
|
|
405
|
+
await shiftsSendSwapEmail(event, "rejected", { swap: dto, offer: offerDto, to: dto.owner.userId });
|
|
406
|
+
}
|
|
407
|
+
return dto;
|
|
408
|
+
}
|
|
409
|
+
class ShiftsExchangeConflict extends Error {
|
|
410
|
+
}
|
|
411
|
+
const shiftsOnPostgres = () => is(schema.shiftSwaps, PgTable);
|
|
412
|
+
async function shiftsExchangeWrites(swap, offer, approvedBy) {
|
|
413
|
+
const run = async (exec, undo2) => {
|
|
414
|
+
const handOver = async (id, from, to) => (await exec.update(schema.shiftAssignments).set({ userId: to, checkInCode: newCheckInCode() }).where(and(
|
|
415
|
+
eq(schema.shiftAssignments.id, id),
|
|
416
|
+
eq(schema.shiftAssignments.userId, from),
|
|
417
|
+
eq(schema.shiftAssignments.status, "assigned")
|
|
418
|
+
)).returning())[0];
|
|
419
|
+
const [standing] = await exec.update(schema.shiftSwapOffers).set({ approvedBy: approvedBy ?? offer.approvedBy ?? null }).where(and(eq(schema.shiftSwapOffers.id, offer.id), eq(schema.shiftSwapOffers.status, "accepted"))).returning();
|
|
420
|
+
if (!standing) return null;
|
|
421
|
+
const ownerNew = await handOver(swap.assignmentId, swap.userId, offer.userId);
|
|
422
|
+
if (!ownerNew) return null;
|
|
423
|
+
undo2.push(() => exec.update(schema.shiftAssignments).set({ userId: swap.userId, checkInCode: newCheckInCode() }).where(and(eq(schema.shiftAssignments.id, swap.assignmentId), eq(schema.shiftAssignments.userId, offer.userId))));
|
|
424
|
+
let backNew = null;
|
|
425
|
+
if (offer.offeredAssignmentId) {
|
|
426
|
+
const backId = offer.offeredAssignmentId;
|
|
427
|
+
backNew = await handOver(backId, offer.userId, swap.userId) ?? null;
|
|
428
|
+
if (!backNew) return null;
|
|
429
|
+
undo2.push(() => exec.update(schema.shiftAssignments).set({ userId: offer.userId, checkInCode: newCheckInCode() }).where(and(eq(schema.shiftAssignments.id, backId), eq(schema.shiftAssignments.userId, swap.userId))));
|
|
430
|
+
}
|
|
431
|
+
const [done] = await exec.update(schema.shiftSwaps).set({ status: "completed", decidedAt: /* @__PURE__ */ new Date() }).where(and(eq(schema.shiftSwaps.id, swap.id), eq(schema.shiftSwaps.status, "accepted"))).returning();
|
|
432
|
+
if (!done) return null;
|
|
433
|
+
return { ownerNew, backNew, offer: standing, swap: done };
|
|
434
|
+
};
|
|
435
|
+
if (shiftsOnPostgres()) {
|
|
436
|
+
const transaction = db.transaction;
|
|
437
|
+
try {
|
|
438
|
+
return await transaction.call(db, async (tx) => {
|
|
439
|
+
const result2 = await run(tx, []);
|
|
440
|
+
if (!result2) throw new ShiftsExchangeConflict();
|
|
441
|
+
return result2;
|
|
442
|
+
});
|
|
443
|
+
} catch (error) {
|
|
444
|
+
if (error instanceof ShiftsExchangeConflict) return null;
|
|
445
|
+
throw error;
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
const undo = [];
|
|
449
|
+
const result = await run(db, undo);
|
|
450
|
+
if (!result) for (const step of undo.reverse()) await step();
|
|
451
|
+
return result;
|
|
452
|
+
}
|
|
453
|
+
async function shiftsCompleteSwap(event, swap, offer, approvedBy, options = {}) {
|
|
454
|
+
const owner = await getShiftAssignmentEntryOr404(swap.assignmentId);
|
|
455
|
+
const back = offer.offeredAssignmentId ? await findShiftAssignmentEntry(offer.offeredAssignmentId) : null;
|
|
456
|
+
const result = await shiftsExchangeWrites(swap, offer, approvedBy);
|
|
457
|
+
if (!result) {
|
|
458
|
+
await shiftsMoveOffer(offer, ["accepted", "withdrawn"], "rejected");
|
|
459
|
+
await shiftsMoveSwap(swap, "accepted", "open");
|
|
460
|
+
throw apiError(409, "swap_changed", "One of the shifts changed meanwhile, so the swap could not be carried out.");
|
|
461
|
+
}
|
|
462
|
+
const { ownerNew, backNew } = result;
|
|
463
|
+
await db.delete(schema.shiftReminders).where(inArray(schema.shiftReminders.assignmentId, [ownerNew.id, ...backNew ? [backNew.id] : []]));
|
|
464
|
+
if (back) {
|
|
465
|
+
const others = await db.select().from(schema.shiftSwaps).where(and(
|
|
466
|
+
eq(schema.shiftSwaps.assignmentId, back.assignment.id),
|
|
467
|
+
inArray(schema.shiftSwaps.status, ["open", "offered", "pending_approval"])
|
|
468
|
+
));
|
|
469
|
+
for (const other of others) {
|
|
470
|
+
if (await shiftsMoveSwap(other, other.status, "cancelled", { decidedAt: /* @__PURE__ */ new Date() })) {
|
|
471
|
+
await db.update(schema.shiftSwapOffers).set({ status: "superseded", decidedAt: /* @__PURE__ */ new Date() }).where(and(eq(schema.shiftSwapOffers.swapId, other.id), inArray(schema.shiftSwapOffers.status, LIVE_OFFER_STATUSES)));
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
const ids = [owner.assignment.id, ...back ? [back.assignment.id] : []];
|
|
476
|
+
const stale = await db.select().from(schema.shiftSwapOffers).where(and(
|
|
477
|
+
inArray(schema.shiftSwapOffers.offeredAssignmentId, ids),
|
|
478
|
+
inArray(schema.shiftSwapOffers.status, LIVE_OFFER_STATUSES),
|
|
479
|
+
ne(schema.shiftSwapOffers.id, offer.id)
|
|
480
|
+
));
|
|
481
|
+
for (const item of stale) {
|
|
482
|
+
if (!await shiftsMoveOffer(item, item.status, "superseded")) continue;
|
|
483
|
+
if (item.status === "accepted") await shiftsMoveSwap({ id: item.swapId }, "pending_approval", "open");
|
|
484
|
+
else await shiftsSyncSwapStatus(item.swapId);
|
|
485
|
+
}
|
|
486
|
+
await shiftsFire("shifts:assignment:changed", { event, assignment: toShiftAssignmentDto(ownerNew), previous: toShiftAssignmentDto(owner.assignment) });
|
|
487
|
+
if (back && backNew) await shiftsFire("shifts:assignment:changed", { event, assignment: toShiftAssignmentDto(backNew), previous: toShiftAssignmentDto(back.assignment) });
|
|
488
|
+
const dto = await shiftsSwapDtoOf(result.swap);
|
|
489
|
+
const offerDto = await shiftsOfferDtoOf(dto, result.offer);
|
|
490
|
+
if (options.fireAccepted) await shiftsFire("shifts:swap:accepted", { event, kind: dto.kind, swap: dto, offer: offerDto });
|
|
491
|
+
await shiftsFire("shifts:swap:completed", { event, kind: dto.kind, swap: dto, offer: offerDto });
|
|
492
|
+
await shiftsSendSwapEmail(event, "completed", { swap: dto, offer: offerDto, to: swap.userId });
|
|
493
|
+
await shiftsSendSwapEmail(event, "completed", { swap: dto, offer: offerDto, to: offer.userId });
|
|
494
|
+
return dto;
|
|
495
|
+
}
|
|
496
|
+
export async function shiftsRecoverSwap(swap) {
|
|
497
|
+
const [offer] = await db.select().from(schema.shiftSwapOffers).where(and(eq(schema.shiftSwapOffers.swapId, swap.id), inArray(schema.shiftSwapOffers.status, ["accepted", "withdrawn"]))).orderBy(asc(schema.shiftSwapOffers.status)).limit(1);
|
|
498
|
+
const ownerRow = (await findShiftAssignmentEntry(swap.assignmentId))?.assignment;
|
|
499
|
+
const backRow = offer?.offeredAssignmentId ? (await findShiftAssignmentEntry(offer.offeredAssignmentId))?.assignment : void 0;
|
|
500
|
+
const ownerMoved = !!offer && ownerRow?.userId === offer.userId;
|
|
501
|
+
const backMoved = !!offer && !!backRow && backRow.userId === swap.userId;
|
|
502
|
+
if (offer && ownerMoved && (!offer.offeredAssignmentId || backMoved)) {
|
|
503
|
+
const done = await shiftsMoveSwap(swap, "accepted", "completed", { decidedAt: /* @__PURE__ */ new Date() });
|
|
504
|
+
await shiftsMoveOffer(offer, ["accepted", "withdrawn"], "accepted");
|
|
505
|
+
return done ?? await findShiftSwap(swap.id);
|
|
506
|
+
}
|
|
507
|
+
if (offer && ownerMoved) {
|
|
508
|
+
await db.update(schema.shiftAssignments).set({ userId: swap.userId, checkInCode: newCheckInCode() }).where(and(eq(schema.shiftAssignments.id, swap.assignmentId), eq(schema.shiftAssignments.userId, offer.userId)));
|
|
509
|
+
}
|
|
510
|
+
if (offer && backMoved && !ownerMoved) {
|
|
511
|
+
await db.update(schema.shiftAssignments).set({ userId: offer.userId, checkInCode: newCheckInCode() }).where(and(eq(schema.shiftAssignments.id, offer.offeredAssignmentId), eq(schema.shiftAssignments.userId, swap.userId)));
|
|
512
|
+
}
|
|
513
|
+
const cancelled = await shiftsMoveSwap(swap, "accepted", "cancelled", { decidedAt: /* @__PURE__ */ new Date() });
|
|
514
|
+
await db.update(schema.shiftSwapOffers).set({ status: "superseded", decidedAt: /* @__PURE__ */ new Date() }).where(and(eq(schema.shiftSwapOffers.swapId, swap.id), inArray(schema.shiftSwapOffers.status, [...LIVE_OFFER_STATUSES, "withdrawn"])));
|
|
515
|
+
return cancelled ?? await findShiftSwap(swap.id);
|
|
516
|
+
}
|
|
517
|
+
export async function shiftsCancelSwap(swap) {
|
|
518
|
+
if (swap.status === "accepted") return shiftsSwapDtoOf(await shiftsRecoverSwap(swap));
|
|
519
|
+
const cancelled = await shiftsMoveSwap(swap, ["open", "offered", "pending_approval"], "cancelled", { decidedAt: /* @__PURE__ */ new Date() });
|
|
520
|
+
if (!cancelled) throw apiError(409, "swap_changed", `The swap is ${swap.status}.`);
|
|
521
|
+
await db.update(schema.shiftSwapOffers).set({ status: "superseded", decidedAt: /* @__PURE__ */ new Date() }).where(and(eq(schema.shiftSwapOffers.swapId, swap.id), inArray(schema.shiftSwapOffers.status, LIVE_OFFER_STATUSES)));
|
|
522
|
+
return shiftsSwapDtoOf(cancelled);
|
|
523
|
+
}
|
|
524
|
+
export async function shiftsSwapTeamIds(swap) {
|
|
525
|
+
const { owner, back } = await shiftsSwapSides(swap);
|
|
526
|
+
return [.../* @__PURE__ */ new Set([owner.shift.teamId, ...back ? [back.shift.teamId] : []])];
|
|
527
|
+
}
|
|
528
|
+
async function shiftsSwapSides(swap) {
|
|
529
|
+
const owner = await getShiftAssignmentEntryOr404(swap.assignmentId);
|
|
530
|
+
const [offer] = await db.select().from(schema.shiftSwapOffers).where(and(eq(schema.shiftSwapOffers.swapId, swap.id), eq(schema.shiftSwapOffers.status, "accepted"))).limit(1);
|
|
531
|
+
const back = offer?.offeredAssignmentId ? await findShiftAssignmentEntry(offer.offeredAssignmentId) : null;
|
|
532
|
+
return { owner, offer, back };
|
|
533
|
+
}
|
|
534
|
+
function shiftsApprovingTeamIds(owner, back) {
|
|
535
|
+
const sides = [owner, ...back ? [back] : []];
|
|
536
|
+
const approving = sides.filter((side) => side.schedule.approval === "lead");
|
|
537
|
+
return [...new Set((approving.length ? approving : sides).map((side) => side.shift.teamId))];
|
|
538
|
+
}
|
|
539
|
+
export async function shiftsAuthorizeSwapApprover(event, swap, by) {
|
|
540
|
+
const { owner, offer, back } = await shiftsSwapSides(swap);
|
|
541
|
+
if (await allows(event, "shifts:manage")) return;
|
|
542
|
+
if (by === swap.userId || offer && by === offer.userId) {
|
|
543
|
+
throw apiError(403, "swap_party", "You take part in this swap, so a lead of the team or staff decides it.");
|
|
544
|
+
}
|
|
545
|
+
const teamIds = shiftsApprovingTeamIds(owner, back);
|
|
546
|
+
for (const teamId of teamIds) {
|
|
547
|
+
if (await allows(event, "shifts:lead", { teamId })) return;
|
|
548
|
+
}
|
|
549
|
+
await authorize(event, "shifts:lead", { teamId: teamIds[0] });
|
|
550
|
+
}
|
|
551
|
+
export const shiftsSwapIsLive = (swap) => isLiveSwap(swap.status);
|
|
552
|
+
export const shiftsSwapOwnerId = (swap) => swap.userId;
|
|
553
|
+
export async function shiftsTeamSwaps(teamId, status) {
|
|
554
|
+
const own = await db.select({ swap: schema.shiftSwaps }).from(schema.shiftSwaps).innerJoin(schema.shiftAssignments, eq(schema.shiftAssignments.id, schema.shiftSwaps.assignmentId)).innerJoin(schema.shifts, eq(schema.shifts.id, schema.shiftAssignments.shiftId)).where(and(eq(schema.shifts.teamId, teamId), status ? eq(schema.shiftSwaps.status, status) : void 0)).orderBy(desc(schema.shiftSwaps.openedAt)).limit(500);
|
|
555
|
+
const backs = await db.select({ swap: schema.shiftSwaps }).from(schema.shiftSwapOffers).innerJoin(schema.shiftSwaps, eq(schema.shiftSwaps.id, schema.shiftSwapOffers.swapId)).innerJoin(schema.shiftAssignments, eq(schema.shiftAssignments.id, schema.shiftSwapOffers.offeredAssignmentId)).innerJoin(schema.shifts, eq(schema.shifts.id, schema.shiftAssignments.shiftId)).where(and(eq(schema.shifts.teamId, teamId), eq(schema.shiftSwapOffers.status, "accepted"), status ? eq(schema.shiftSwaps.status, status) : void 0)).limit(500);
|
|
556
|
+
const rows = /* @__PURE__ */ new Map();
|
|
557
|
+
for (const { swap } of [...own, ...backs]) rows.set(swap.id, swap);
|
|
558
|
+
return shiftsSwapDtos([...rows.values()].sort((a, b) => b.openedAt.getTime() - a.openedAt.getTime()), { offers: true });
|
|
559
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import type { H3Event } from 'h3';
|
|
2
|
+
import type { MyShiftDetailDto, MyShiftDto, ShiftCandidateDto, ShiftPassDto, ShiftPersonDto, ShiftsLedTeamDto, ShiftsMyNowDto, ShiftsNowDto, ShiftsOperationTeamDto, ShiftWithAssigneesDto } from '../../../types.js';
|
|
3
|
+
import type { ShiftRow, ShiftScheduleRow } from './shifts-dto.js';
|
|
4
|
+
import type { ShiftAssignmentEntry } from './shifts-core.js';
|
|
5
|
+
/**
|
|
6
|
+
* My shifts in published schedules, soonest first (`from`, `to` narrow it to shifts that overlap the range). Cancelled
|
|
7
|
+
* assignments are left out.
|
|
8
|
+
*/
|
|
9
|
+
export declare function shiftsMyShifts(userId: number, range?: {
|
|
10
|
+
from?: Date;
|
|
11
|
+
to?: Date;
|
|
12
|
+
}): Promise<MyShiftDto[]>;
|
|
13
|
+
/** My shift DTOs of assignment entries: the operation, schedule, team and role names, and my live swap. */
|
|
14
|
+
export declare function shiftsMyShiftDtos(rows: ShiftAssignmentEntry[]): Promise<MyShiftDto[]>;
|
|
15
|
+
/**
|
|
16
|
+
* The leads (lead role and up) of a team who hold a place on one of its shifts overlapping `[startsAt, endsAt)`, names only.
|
|
17
|
+
* `exclude` leaves a person out (yourself).
|
|
18
|
+
*/
|
|
19
|
+
export declare function shiftsLeadsOnDuty(event: H3Event | undefined, teamId: string, startsAt: Date, endsAt: Date, exclude?: number): Promise<ShiftPersonDto[]>;
|
|
20
|
+
/** One of my shifts with the leads on duty, my live swap (with its offers) and the pass window. */
|
|
21
|
+
export declare function shiftsMyShiftDetail(event: H3Event, entry: ShiftAssignmentEntry): Promise<MyShiftDetailDto>;
|
|
22
|
+
/**
|
|
23
|
+
* The QR pass of an assignment: the code (and its QR code as SVG) only while check-in is open and the assignment holds a
|
|
24
|
+
* place. The perks are added by the route (`shiftsPassPerks`).
|
|
25
|
+
*/
|
|
26
|
+
export declare function shiftsPassOf(event: H3Event, entry: ShiftAssignmentEntry): Omit<ShiftPassDto, 'perks' | 'checkIn' | 'eligible'>;
|
|
27
|
+
/**
|
|
28
|
+
* The pass of an assignment as `GET /api/shifts/me/:assignmentId/pass` answers it. Read-only: when the person no longer
|
|
29
|
+
* holds a role on the team, it is `available: false, eligible: false` with no code and no perks (`POST .../settle`
|
|
30
|
+
* cancels their future shifts there). Without a station key `checkIn.modes` leaves `station` out.
|
|
31
|
+
*/
|
|
32
|
+
export declare function shiftsPassDto(event: H3Event, entry: ShiftAssignmentEntry): Promise<ShiftPassDto>;
|
|
33
|
+
/** A team's shifts in published schedules (`from`, `to` narrow it), with who works them; contact details only with `pii`. */
|
|
34
|
+
export declare function shiftsTeamShifts(teamId: string, range: {
|
|
35
|
+
from?: Date;
|
|
36
|
+
to?: Date;
|
|
37
|
+
}, pii: boolean): Promise<ShiftWithAssigneesDto[]>;
|
|
38
|
+
/**
|
|
39
|
+
* Who is on shift now in an operation: the shifts of its published schedules running at `at`, with the people on them
|
|
40
|
+
* (`assigned` and `attended`) and, per team, the leads among them. Contact details only with `pii`.
|
|
41
|
+
* `teamIds` narrows it to those teams in the query, so the people and leads of other teams are never loaded or resolved
|
|
42
|
+
* (finding a lead resolves the person's team roles, which costs queries per person).
|
|
43
|
+
*/
|
|
44
|
+
export declare function shiftsNow(event: H3Event, operationId: string, pii: boolean, at?: Date, teamIds?: string[]): Promise<ShiftsNowDto>;
|
|
45
|
+
/**
|
|
46
|
+
* The teams of mine that have a shift running now: for each operation I hold a team role in (explicit or mapped), the
|
|
47
|
+
* "on shift now" view narrowed to my teams. An operation I may not read the view of is left out. Names only.
|
|
48
|
+
*/
|
|
49
|
+
export declare function shiftsMyNow(event: H3Event, userId: number, at?: Date): Promise<ShiftsMyNowDto>;
|
|
50
|
+
/**
|
|
51
|
+
* The people staff could assign to a shift: the explicit members of its team who hold their role (approved crew and
|
|
52
|
+
* members added in Teams), `q` matching the name, with whether they are eligible for the shift role, and the guards they
|
|
53
|
+
* would break. Mapped-only people are not listed: they cannot be assigned.
|
|
54
|
+
*/
|
|
55
|
+
export declare function shiftsCandidates(event: H3Event, entry: {
|
|
56
|
+
shift: ShiftRow;
|
|
57
|
+
schedule: ShiftScheduleRow;
|
|
58
|
+
}, q: string | undefined, pii: boolean, shiftRoleId?: string): Promise<ShiftCandidateDto[]>;
|
|
59
|
+
/** The teams of operations a user leads (a role at or above the ladder's `leadFrom`, explicit or mapped). */
|
|
60
|
+
export declare function shiftsLedTeams(event: H3Event, userId: number): Promise<ShiftsLedTeamDto[]>;
|
|
61
|
+
/** The teams of an operation, in the order staff set, with their kind's ladder: what the Shifts tab's forms need. */
|
|
62
|
+
export declare function shiftsOperationTeams(event: H3Event, operationId: string): Promise<ShiftsOperationTeamDto[]>;
|