Skip to content

Commit

Permalink
fix updating office visits if desk or area changed
Browse files Browse the repository at this point in the history
  • Loading branch information
piggydoughnut committed Jan 17, 2024
1 parent 1c3e5e4 commit c677e7c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 55 deletions.
49 changes: 16 additions & 33 deletions src/modules/guest-invites/server/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,44 +25,27 @@ export const updateVisitsForManualInvite = async (
deskId: string | null,
dates: Array<string>
) => {
// Need to cancel any office visits dates which were removed
const datesAreTheSame: boolean = compareDates(invite.dates, dates)

let datesToSchedule: Array<string> = []

if (!datesAreTheSame) {
datesToSchedule = dates.filter((date) => !invite.dates.includes(date))
const datesToCancel: Array<string> = invite.dates.filter(
(date) => !dates.includes(date)
)

// cancelling all the office visit dates that were removed from the guest invite
if (!!datesToCancel.length) {
for (const visitDate of datesToCancel) {
const v = await fastify.db.Visit.findOne({
where: {
date: visitDate,
deskId,
areaId,
userId: ROBOT_USER_ID,
officeId: invite.office,
},
transaction,
})
// cancelling all the office visit dates for this guest invite
if (!!invite.dates.length) {
for (const visitDate of invite.dates) {
const v = await fastify.db.Visit.findOne({
where: {
date: visitDate,
deskId: invite.deskId,
areaId: invite.areaId,
userId: ROBOT_USER_ID,
officeId: invite.office,
},
transaction,
})
if (v) {
await v.destroy({ transaction: transaction })
}
}
}

// scheduling all the new visit dates that were added to the guest invite
if (!!datesToSchedule.length) {
const visits = generateVisits(
areaId,
deskId,
datesToSchedule,
invite,
ROBOT_USER_ID
)
if (!!dates.length) {
const visits = generateVisits(areaId, deskId, dates, invite, ROBOT_USER_ID)
// @ts-ignore FIXME:
await fastify.db.Visit.bulkCreate(visits, { transaction })
}
Expand Down
30 changes: 8 additions & 22 deletions src/modules/visits/client/components/DeskPicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,34 +21,27 @@ export const DeskPicker: React.FC<Props> = ({
selectedAreaId,
onSelectArea,
}) => {
const [deskId, setDeskId] = React.useState<string | null>(selectedDeskId)

///////// Area /////////
const { data: areas = [] } = useVisitsAreas(officeId)
const [areaId, setAreaId] = React.useState<string | null>(null)
const area = React.useMemo(() => areas.find((x) => areaId === x.id), [areaId])
const onAreaChange = React.useCallback(
(areaId: string) => {
setAreaId(areaId)
setDeskId(null)
onSelectDesk(null)
onSelectArea && onSelectArea(areaId)
},
[areas]
)

React.useEffect(() => {
setDeskId(selectedDeskId)
}, [selectedDeskId])

React.useEffect(() => {
if (selectedAreaId) {
setAreaId(selectedAreaId)
}
}, [selectedAreaId])

React.useEffect(() => {
if (areas.length) {
if (areas.length && !selectedAreaId) {
setAreaId(areas[0].id)
onSelectArea && onSelectArea(areas[0].id)
}
Expand All @@ -59,19 +52,14 @@ export const DeskPicker: React.FC<Props> = ({
useAvailableDesks(officeId, selectedDates)

const availableAreaDeskIds = React.useMemo(() => {
return availableDesks
const available = availableDesks
.filter((x) => x.areaId === area?.id)
.map((x) => x.deskId)
}, [availableDesks, area])

React.useEffect(() => {
if (
deskId &&
(!availableAreaDeskIds.includes(deskId) || !selectedDates.length)
) {
setDeskId(null)
if (selectedDeskId) {
available.push(selectedDeskId)
}
}, [availableAreaDeskIds, deskId, selectedDates])
return available
}, [availableDesks, area])

///////// UnAvailable area /////////
const [unavailableDeskNames, unavailableArea] = React.useMemo<
Expand All @@ -87,7 +75,6 @@ export const DeskPicker: React.FC<Props> = ({

const onToggleDesk = React.useCallback(
(deskId: string) => {
setDeskId((value) => (value === deskId ? null : deskId))
onSelectDesk(deskId)
},
[area]
Expand Down Expand Up @@ -122,7 +109,6 @@ export const DeskPicker: React.FC<Props> = ({
</span>
)}
</p>
{/* <hr className="my-4" /> */}
</>
) : null}
{areas.length > 1 ? (
Expand All @@ -147,7 +133,7 @@ export const DeskPicker: React.FC<Props> = ({
value: x.id,
disabled: !availableAreaDeskIds.includes(x.id),
}))}
value={deskId || undefined}
value={selectedDeskId}
onChange={onToggleDesk}
placeholder={'Select desk'}
className="w-full"
Expand All @@ -159,7 +145,7 @@ export const DeskPicker: React.FC<Props> = ({
<OfficeFloorMap
area={area}
availableDeskIds={availableAreaDeskIds}
selectedDeskId={deskId}
selectedDeskId={selectedDeskId}
onToggleDesk={onToggleDesk}
/>
</div>
Expand Down

0 comments on commit c677e7c

Please sign in to comment.