Skip to content

Commit

Permalink
Update worker info using global dispatch
Browse files Browse the repository at this point in the history
  • Loading branch information
pectom committed Mar 19, 2021
1 parent ad3dcac commit 4bbcdbf
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 136 deletions.
10 changes: 10 additions & 0 deletions src/helpers/verbose-date.helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,14 @@ export class VerboseDateHelper {
const currentMonth = new Date().getMonth();
return year > currentYear || (year === currentYear && month > currentMonth);
}

static isCurrentMonth(month: number, year: number): boolean {
const currentYear = new Date().getFullYear();
const currentMonth = new Date().getMonth();
return year === currentYear && month === currentMonth;
}

static isCurrentOrFutureMonth(month: number, year: number): boolean {
return this.isCurrentMonth(month, year) || this.isMonthInFuture(month, year);
}
}
13 changes: 2 additions & 11 deletions src/state/reducers/month-state/employee-info.reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import _ from "lodash";
import { ScheduleDataModel } from "../../../common-models/schedule-data.model";
import { ContractType, WorkersInfoModel } from "../../../common-models/worker-info.model";
import { WorkerHourInfo } from "../../../helpers/worker-hours-info.model";
import { ActionModel } from "../../models/action.model";
import { WorkerActionPayload } from "../worker.action-creator";
import { scheduleDataInitialState } from "./schedule-data/schedule-data-initial-state";
import {
createActionName,
Expand Down Expand Up @@ -53,17 +51,14 @@ function mockWorkerContractType(workerInfo: WorkersInfoModel): WorkersInfoModel
});
return workerInfo;
}

/* eslint-disable @typescript-eslint/camelcase */
export function employeeInfoReducerF(name: string) {
return (
state: WorkersInfoModel = scheduleDataInitialState.employee_info,
action: ScheduleActionModel | ActionModel<WorkerActionPayload>
action: ScheduleActionModel
): WorkersInfoModel => {
let monthEmployeeInfo: WorkersInfoModel;
let updatedEmployeeInfo;
if ((action.payload as WorkerActionPayload) !== undefined) {
({ updatedEmployeeInfo } = action.payload as WorkerActionPayload);
}
switch (action.type) {
case createActionName(name, ScheduleActionType.ADD_NEW):
monthEmployeeInfo = (action.payload as ScheduleDataModel)?.employee_info;
Expand All @@ -75,10 +70,6 @@ export function employeeInfoReducerF(name: string) {
if (!monthEmployeeInfo) return state;
monthEmployeeInfo = mockWorkerContractType(monthEmployeeInfo);
return { ...state, ...monthEmployeeInfo };
case ScheduleActionType.UPDATE_WORKER_INFO:
return {
...updatedEmployeeInfo,
};
default:
return state;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ export type ScheduleActionModel = ActionModel<ScheduleDataModel>;
export enum ScheduleActionType {
UPDATE = "UPDATE_SCHEDULE",
ADD_NEW = "ADD_NEW_SCHEDULE",
UPDATE_WORKER_INFO = "UPDATE_WORKER_INFO",
CLEAN_ERRORS = "CLEAN_ERRORS",
ADD_NEW_SHIFT = "ADD_NEW_SHIFT",
MODIFY_SHIFT = "MODIFY_SHIFT",
Expand Down
8 changes: 0 additions & 8 deletions src/state/reducers/month-state/shifts-info.reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,11 @@ export function scheduleShiftsInfoReducerF(name: string) {
state: ShiftInfoModel = scheduleDataInitialState.shifts,
action: ScheduleActionModel | ActionModel<WorkerActionPayload>
): ShiftInfoModel => {
let updatedShifts;
if (action.payload as WorkerActionPayload) {
({ updatedShifts } = action.payload as WorkerActionPayload);
}
switch (action.type) {
case createActionName(name, ScheduleActionType.ADD_NEW):
case createActionName(name, ScheduleActionType.UPDATE):
const data = (action.payload as ScheduleDataModel)?.shifts;
return { ...data };
case ScheduleActionType.UPDATE_WORKER_INFO:
return {
...updatedShifts,
};
default:
return state;
}
Expand Down
168 changes: 52 additions & 116 deletions src/state/reducers/worker.action-creator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,14 @@ import {
WorkerType,
} from "../../common-models/worker-info.model";
import { WorkerInfoExtendedInterface } from "../../components/namestable/worker-edit.component";
import { ScheduleKey, ThunkFunction } from "../../api/persistance-store.model";
import { RevisionType, ThunkFunction } from "../../api/persistance-store.model";
import _ from "lodash";
import { ScheduleActionType } from "./month-state/schedule-data/schedule.actions";
import { LocalStorageProvider } from "../../api/local-storage-provider.model";
import { MonthDataModel, ScheduleDataModel } from "../../common-models/schedule-data.model";
import { MonthDataModel } from "../../common-models/schedule-data.model";
import { getEmployeeWorkTime } from "./month-state/employee-info.reducer";
import { cropScheduleDMToMonthDM } from "../../logic/schedule-container-convertion/schedule-container-convertion";
import { MonthHelper } from "../../helpers/month.helper";
import { ScheduleDataActionCreator } from "./month-state/schedule-data/schedule-data.action-creator";
import { VerboseDateHelper } from "../../helpers/verbose-date.helper";
import { ThunkDispatch } from "redux-thunk";
import { ActionModel } from "../models/action.model";
import { ApplicationStateModel } from "../models/application-state.model";

export interface WorkerActionPayload {
updatedShifts: ShiftInfoModel;
Expand All @@ -32,20 +30,18 @@ export class WorkerActionCreator {
static addNewWorker(worker: WorkerInfoExtendedInterface): ThunkFunction<WorkerActionPayload> {
return async (dispatch, getState): Promise<void> => {
const actualSchedule = getState().actualState.persistentSchedule.present;
const { month_number: monthNumber, year } = actualSchedule.schedule_info;
const actualMonth = cropScheduleDMToMonthDM(actualSchedule);
const { year, month } = actualMonth.scheduleKey;

const updatedSchedule = WorkerActionCreator.addWorkerInfo(
actualSchedule,
worker,
this.createNewWorkerShifts
);
await WorkerActionCreator.updateStateAndDb(dispatch, updatedSchedule);
await WorkerActionCreator.updateNextMonthInDB(
monthNumber,
year,
const updatedMonth = WorkerActionCreator.addWorkerInfoToMonthDM(
actualMonth,
worker,
this.createNewWorkerShifts
);
const revision: RevisionType = VerboseDateHelper.isCurrentOrFutureMonth(month, year)
? "actual"
: "primary";
dispatch(ScheduleDataActionCreator.setScheduleFromMonthDMAndSaveInDB(updatedMonth, revision));
};
}

Expand All @@ -55,121 +51,64 @@ export class WorkerActionCreator {

const { name } = worker;
const actualSchedule = _.cloneDeep(getState().actualState.persistentSchedule.present);
const updatedSchedule = WorkerActionCreator.deleteWorkerFromScheduleDM(actualSchedule, name);
await WorkerActionCreator.updateStateAndDb(dispatch, updatedSchedule);
const actualMonth = cropScheduleDMToMonthDM(actualSchedule);
const { year, month } = actualMonth.scheduleKey;

const updatedMonth = WorkerActionCreator.deleteWorkerFromMonthDM(actualMonth, name);

const revision: RevisionType = VerboseDateHelper.isCurrentOrFutureMonth(month, year)
? "actual"
: "primary";
dispatch(ScheduleDataActionCreator.setScheduleFromMonthDMAndSaveInDB(updatedMonth, revision));
};
}

static modifyWorker(worker: WorkerInfoExtendedInterface): ThunkFunction<WorkerActionPayload> {
return async (dispatch, getState): Promise<void> => {
const { prevName } = worker;
const actualSchedule = _.cloneDeep(getState().actualState.persistentSchedule.present);
const getUpdatedWorkerShifts = (schedule: ScheduleDataModel | MonthDataModel): ShiftCode[] =>
schedule.shifts[prevName];
let updatedSchedule = WorkerActionCreator.addWorkerInfo(
actualSchedule,
const actualMonth = cropScheduleDMToMonthDM(actualSchedule);
const { year, month } = actualMonth.scheduleKey;
const getUpdatedWorkerShifts = (monthDataModel: MonthDataModel): ShiftCode[] =>
monthDataModel.shifts[prevName];
let updatedMonth = WorkerActionCreator.addWorkerInfoToMonthDM(
actualMonth,
worker,
getUpdatedWorkerShifts
);

if (prevName !== worker.workerName) {
updatedSchedule = WorkerActionCreator.deleteWorkerFromScheduleDM(updatedSchedule, prevName);
updatedMonth = WorkerActionCreator.deleteWorkerFromMonthDM(updatedMonth, prevName);
}
await WorkerActionCreator.updateStateAndDb(dispatch, updatedSchedule);
const { month_number: monthNumber, year } = updatedSchedule.schedule_info;
await WorkerActionCreator.updateNextMonthInDB(
monthNumber,
year,
worker,
getUpdatedWorkerShifts
);
const revision: RevisionType = VerboseDateHelper.isCurrentOrFutureMonth(month, year)
? "actual"
: "primary";
dispatch(ScheduleDataActionCreator.setScheduleFromMonthDMAndSaveInDB(updatedMonth, revision));
};
}

private static async updateStateAndDb(
dispatch: ThunkDispatch<ApplicationStateModel, void, ActionModel<WorkerActionPayload, string>>,
schedule: ScheduleDataModel
): Promise<void> {
const { month_number: monthNumber, year } = schedule.schedule_info;
const action = {
type: ScheduleActionType.UPDATE_WORKER_INFO,
payload: {
updatedShifts: schedule.shifts,
updatedEmployeeInfo: schedule.employee_info,
},
};
dispatch(action);

if (VerboseDateHelper.isMonthInFuture(monthNumber, year)) {
await new LocalStorageProvider().saveSchedule("primary", schedule);
}
await new LocalStorageProvider().saveSchedule("actual", schedule);
}

private static async updateNextMonthInDB(
currentMonth: number,
currentYear: number,
worker: WorkerInfoExtendedInterface,
createWorkerShifts: (schedule: MonthDataModel, year: number, monthNumber: number) => ShiftCode[]
): Promise<void> {
const nextMonthDM = await new LocalStorageProvider().getMonthRevision(
new ScheduleKey(currentMonth, currentYear).nextMonthKey.getRevisionKey("primary")
);
if (_.isNil(nextMonthDM)) return;
const updatedNextMonth = WorkerActionCreator.addWorkerInfoToMonthDM(
nextMonthDM,
worker,
createWorkerShifts
);
updatedNextMonth.scheduleKey = new ScheduleKey(currentMonth, currentYear).nextMonthKey;
await new LocalStorageProvider().saveBothMonthRevisionsIfNeeded("primary", updatedNextMonth);
}

private static deleteWorkerFromScheduleDM(
schedule: ScheduleDataModel,
private static deleteWorkerFromMonthDM(
monthDataModel: MonthDataModel,
workerName: string
): ScheduleDataModel {
const scheduleCopy = _.cloneDeep(schedule);
delete scheduleCopy.employee_info.time[workerName];
delete scheduleCopy.employee_info.type[workerName];
delete scheduleCopy.employee_info.contractType?.[workerName];
delete scheduleCopy.shifts[workerName];

return scheduleCopy;
}
): MonthDataModel {
const monthDataModelCopy = _.cloneDeep(monthDataModel);
delete monthDataModelCopy.employee_info.time[workerName];
delete monthDataModelCopy.employee_info.type[workerName];
delete monthDataModelCopy.employee_info.contractType?.[workerName];
delete monthDataModelCopy.shifts[workerName];

private static addWorkerInfo(
schedule: ScheduleDataModel,
worker: WorkerInfoExtendedInterface,
createWorkerShifts: (
schedule: ScheduleDataModel,
year: number,
monthNumber: number
) => ShiftCode[]
): ScheduleDataModel {
const { year, month_number: monthNumber } = schedule.schedule_info;
return this.addWorkerInfoToSchedule(schedule, worker, year, monthNumber, createWorkerShifts);
return monthDataModelCopy;
}

private static addWorkerInfoToMonthDM(
schedule: MonthDataModel,
monthDataModel: MonthDataModel,
worker: WorkerInfoExtendedInterface,
createWorkerShifts: (schedule: MonthDataModel, year: number, monthNumber: number) => ShiftCode[]
createWorkerShifts: (schedule: MonthDataModel) => ShiftCode[]
): MonthDataModel {
const { year, month: monthNumber } = schedule.scheduleKey;
return this.addWorkerInfoToSchedule(schedule, worker, year, monthNumber, createWorkerShifts);
}

private static addWorkerInfoToSchedule<T extends MonthDataModel | ScheduleDataModel>(
schedule: T,
worker: WorkerInfoExtendedInterface,
year: number,
monthNumber: number,
createWorkerShifts: (schedule: T, year: number, monthNumber: number) => ShiftCode[]
): T {
const updatedSchedule = _.cloneDeep(schedule);
const updatedSchedule = _.cloneDeep(monthDataModel);
const { workerName, workerType, contractType } = worker;
const newWorkerShifts = createWorkerShifts(schedule, year, monthNumber);
const newWorkerShifts = createWorkerShifts(monthDataModel);
const { year, month } = monthDataModel.scheduleKey;

updatedSchedule.shifts = {
...updatedSchedule.shifts,
Expand All @@ -179,7 +118,7 @@ export class WorkerActionCreator {
updatedSchedule.employee_info = {
time: {
...updatedSchedule.employee_info.time,
[workerName]: getEmployeeWorkTime({ ...worker, monthNumber, year }),
[workerName]: getEmployeeWorkTime({ ...worker, monthNumber: month, year }),
},
type: {
...updatedSchedule.employee_info.type,
Expand All @@ -194,14 +133,11 @@ export class WorkerActionCreator {
return updatedSchedule;
}

private static createNewWorkerShifts(
schedule: ScheduleDataModel | MonthDataModel,
year: number,
monthNumber: number
): ShiftCode[] {
private static createNewWorkerShifts(monthDataModel: MonthDataModel): ShiftCode[] {
const today = new Date();
const newWorkerShifts = new Array(schedule.month_info.dates.length).fill(ShiftCode.W);
if (today.getMonth() === monthNumber && today.getFullYear() === year) {
const { year, month } = monthDataModel.scheduleKey;
const newWorkerShifts = new Array(MonthHelper.getMonthLength(year, month)).fill(ShiftCode.W);
if (today.getMonth() === month && today.getFullYear() === year) {
newWorkerShifts.splice(
0,
today.getDate() - 1,
Expand Down

0 comments on commit 4bbcdbf

Please sign in to comment.