Skip to content

Commit

Permalink
TASK-418 Disallow working shifts that cross 7am (#341)
Browse files Browse the repository at this point in the history
* add check

* handle non working shifts

* Change function names and moves 7 to const

Co-authored-by: Bohdan Forostianyi <[email protected]>
  • Loading branch information
kaplonpaulina and Qwebeck committed Apr 16, 2021
1 parent 34de886 commit 89d040e
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 26 deletions.
Binary file modified cypress/fixtures/small_test_schedule.xlsx
Binary file not shown.
7 changes: 3 additions & 4 deletions src/common-models/shift-info.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */

import { SCHEDULE_CONTAINERS_LENGTH, ScheduleContainerType } from "./schedule-data.model";
import * as _ from "lodash";
import { ScheduleContainerType, SCHEDULE_CONTAINERS_LENGTH } from "./schedule-data.model";

interface BaseShift {
code: string;
Expand All @@ -12,9 +12,8 @@ interface BaseShift {
to: number;
color: string;
}

export interface WorkingShift extends BaseShift {
isWorkingShift?: true;
isWorkingShift: true;
}

export enum NotWorkingShiftType {
Expand All @@ -23,7 +22,7 @@ export enum NotWorkingShiftType {
Util = "util",
}
export interface NotWorkingShift extends BaseShift {
isWorkingShift?: false;
isWorkingShift: false;
normSubtraction?: number;
// TODO: remove optionality after migration
type?: NotWorkingShiftType;
Expand Down
15 changes: 10 additions & 5 deletions src/components/workers-page/shifts-tab/shifts-tab.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { useDispatch, useSelector } from "react-redux";
import { ScheduleDataActionCreator } from "../../../state/reducers/month-state/schedule-data/schedule-data.action-creator";
import { ApplicationStateModel } from "../../../state/models/application-state.model";
import { ShiftsActionCreator } from "../../../state/reducers/shifts.action-creator";
import { ParserHelper } from "../../../helpers/parser.helper";

const useStyles = makeStyles(() =>
createStyles({
Expand Down Expand Up @@ -64,13 +65,17 @@ export default function ShiftTab(): JSX.Element {
}
const dispatcher = useDispatch();
const handleChangeItem = (createdShift: Shift): void => {
if (mode === ShiftDrawerMode.ADD_NEW) {
dispatcher(ScheduleDataActionCreator.addNewShift(createdShift));
if (!ParserHelper.shiftPassesDayStart(createdShift)) {
if (mode === ShiftDrawerMode.ADD_NEW) {
dispatcher(ScheduleDataActionCreator.addNewShift(createdShift));
} else {
dispatcher(ScheduleDataActionCreator.modifyShift(createdShift, selectedShift));
}

toggleClose();
} else {
dispatcher(ScheduleDataActionCreator.modifyShift(createdShift, selectedShift));
// TODO. Handle unappropriately created shift
}

toggleClose();
};

const handleRemoveItem = (shift: Shift): void => {
Expand Down
11 changes: 11 additions & 0 deletions src/helpers/parser.helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */

import { Shift } from "../common-models/shift-info.model";
import { DEFAULT_FROM } from "../logic/schedule-parser/shifts-types-info.parser";

export const EMPTY_ROW_SIZE = 40;
export const EMPTY_ROW = Array(EMPTY_ROW_SIZE).fill("");

Expand Down Expand Up @@ -106,4 +109,12 @@ export class ParserHelper {
public static getShiftColorHeaderIndex(): number {
return this.getShiftHeaderIndex(KOLOR);
}

public static shiftPassesDayStart(shift: Shift): boolean {
return shift.isWorkingShift
? shift.from <= shift.to
? shift.from < DEFAULT_FROM && DEFAULT_FROM < shift.to
: DEFAULT_FROM < shift.to || shift.from < DEFAULT_FROM
: false;
}
}
4 changes: 2 additions & 2 deletions src/logic/schedule-exporter/shift-export.logic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ export class ShiftExportLogic {
shifts.push([
name.name,
name.code,
name.from,
name.to,
name.isWorkingShift ? name.from : "-",
name.isWorkingShift ? name.to : "-",
ParserHelper.translateBooleanToString(name.isWorkingShift).toUpperCase(),
name.color,
])
Expand Down
57 changes: 42 additions & 15 deletions src/logic/schedule-parser/shifts-types-info.parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import { AcronymGenerator } from "../../helpers/acronym-generator.helper";
import { ParserHelper } from "../../helpers/parser.helper";

const DEFAULT_SHIFT_NAME = "Zmiana";
const DEFAULT_FROM = 7;
export const DEFAULT_FROM = 7;
const DEFAULT_NON_WORKING_FROM = 0;
const DEFAULT_TO = 7;
const DEFAULT_NON_WORKING_TO = 24;
const DEFAULT_IS_WORKING = false;
const DEFAULT_COLOR = { name: "czerwony", value: "FF0000" };

Expand All @@ -21,7 +23,11 @@ export class ShiftsTypesInfoParser {
this.namelessShifts = 0;
data.forEach((a) => {
const shift = this.mapShift(a);
this._shiftsInfoRows[shift.code] = shift;
if (!ParserHelper.shiftPassesDayStart(shift)) {
this._shiftsInfoRows[shift.code] = shift;
} else {
this.logLoadFileError("Nie dodano zmiany " + shift.name + ". Przecina godzinę 7:00.");
}
});
}

Expand Down Expand Up @@ -71,10 +77,20 @@ export class ShiftsTypesInfoParser {
}
}

private parseShiftFrom(shiftRow: string[], name: string): number {
private parseShiftFrom(shiftRow: string[], name: string, isWorkingShift: boolean): number {
const index = ParserHelper.getShiftStartHeaderIndex();

if (index >= 0 && shiftRow[index]) {
if (!isWorkingShift) {
const from = shiftRow[index].trim();
if (from !== "" && from !== "-") {
this.logLoadFileError(
"Nieoczekiwana wartość dla początku dla niepracującej zmiany: " + name
);
}
return DEFAULT_NON_WORKING_FROM;
}

const number = parseInt(shiftRow[index].trim());
if (isNaN(number) || number < 0 || number > 24) {
this.logLoadFileError(
Expand All @@ -84,31 +100,42 @@ export class ShiftsTypesInfoParser {
return number;
}
} else {
this.logLoadFileError(
"Nie ustawiono początku zmiany: " + name + ". Ustawiono: " + DEFAULT_FROM
);
if (isWorkingShift) {
this.logLoadFileError(
"Nie ustawiono początku zmiany: " + name + ". Ustawiono: " + DEFAULT_FROM
);
}
}
return DEFAULT_FROM;
return isWorkingShift ? DEFAULT_FROM : DEFAULT_NON_WORKING_FROM;
}

private parseShiftTo(shiftRow: string[], name: string): number {
private parseShiftTo(shiftRow: string[], name: string, isWorkingShift: boolean): number {
const index = ParserHelper.getShiftEndHeaderIndex();

if (index >= 0 && shiftRow[index]) {
if (!isWorkingShift) {
const to = shiftRow[index].trim();
if (to !== "" && to !== "-") {
this.logLoadFileError(
"Nieoczekiwana wartość dla końca dla niepracującej zmiany: " + name
);
}
return DEFAULT_NON_WORKING_TO;
}
const number = parseInt(shiftRow[index].trim());
if (isNaN(number) || number < 0 || number > 24) {
this.logLoadFileError(
"Nieoczekiwana wartość dla początku zmiany: " + name + ". Ustawiono: " + DEFAULT_TO
"Nieoczekiwana wartość dla końca zmiany: " + name + ". Ustawiono: " + DEFAULT_TO
);
} else {
return number;
}
} else {
this.logLoadFileError(
"Nie ustawiono początku zmiany: " + name + ". Ustawiono: " + DEFAULT_TO
);
if (isWorkingShift) {
this.logLoadFileError("Nie ustawiono końca zmiany: " + name + ". Ustawiono: " + DEFAULT_TO);
}
}
return DEFAULT_TO;
return isWorkingShift ? DEFAULT_TO : DEFAULT_NON_WORKING_TO;
}

private parseShiftIsWorking(shiftRow: string[], name: string): boolean {
Expand Down Expand Up @@ -152,9 +179,9 @@ export class ShiftsTypesInfoParser {
private mapShift(row: string[]): Shift {
const name = this.parseShiftName(row);
const code = this.parseShiftCode(row, name);
const from = this.parseShiftFrom(row, name);
const to = this.parseShiftTo(row, name);
const isWorkingShift = this.parseShiftIsWorking(row, name);
const from = this.parseShiftFrom(row, name, isWorkingShift);
const to = this.parseShiftTo(row, name, isWorkingShift);
const color = this.parseShiftColor(row, name);

return {
Expand Down

0 comments on commit 89d040e

Please sign in to comment.