mirror of
https://github.com/Sea-Haven-Industries/shoc-frontend-new.git
synced 2026-10-03 11:33:22 +00:00
46 lines
1.4 KiB
TypeScript
46 lines
1.4 KiB
TypeScript
import type { BoardRowOrderByDay } from "@/domain/work-orders/utils/board-row-order-storage";
|
|
import {
|
|
moveIdToDayOrder,
|
|
removeIdFromRowOrder,
|
|
saveBoardRowOrder,
|
|
transferIdToDestinationWeekOrder,
|
|
} from "@/domain/work-orders/utils/board-row-order-storage";
|
|
import { deriveDayFields } from "@/domain/work-orders/utils/table-day-utils";
|
|
import { mondayOf } from "@/domain/work-orders/utils/wizard-date-utils";
|
|
|
|
export type ApplyBoardRowOrderOnScheduleChangeInput = {
|
|
weekMonday: string;
|
|
ownerId: string;
|
|
id: string;
|
|
scheduledOn: string;
|
|
previousDayGroup?: string;
|
|
};
|
|
|
|
export function applyBoardRowOrderOnScheduleChange(
|
|
orderByDay: BoardRowOrderByDay,
|
|
input: ApplyBoardRowOrderOnScheduleChangeInput,
|
|
): BoardRowOrderByDay {
|
|
const { weekMonday, ownerId, id, scheduledOn, previousDayGroup } = input;
|
|
const nextDayGroup = deriveDayFields(scheduledOn).dayGroup;
|
|
if (!previousDayGroup || previousDayGroup === nextDayGroup) {
|
|
return orderByDay;
|
|
}
|
|
|
|
const destinationWeekMonday = mondayOf(scheduledOn.slice(0, 10));
|
|
if (destinationWeekMonday !== weekMonday) {
|
|
transferIdToDestinationWeekOrder({
|
|
sourceWeekMonday: weekMonday,
|
|
destinationWeekMonday,
|
|
ownerId,
|
|
id,
|
|
nextDayGroup,
|
|
});
|
|
return removeIdFromRowOrder(orderByDay, id);
|
|
}
|
|
|
|
const next = moveIdToDayOrder(orderByDay, id, nextDayGroup);
|
|
if (next !== orderByDay) {
|
|
saveBoardRowOrder(weekMonday, next, ownerId);
|
|
}
|
|
return next;
|
|
}
|