Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TASK-350 Save shifts from next month #283

Merged
merged 6 commits into from
Mar 18, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 19 additions & 10 deletions src/api/local-storage-provider.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
} from "../common-models/schedule-data.model";

import {
getRevisionTypeFromKey,
MonthRevision,
PersistenceStoreProvider,
RevisionKey,
Expand All @@ -37,7 +38,6 @@ export class LocalStorageProvider extends PersistenceStoreProvider {
super();
this.storage = new PouchDB(DATABASE_NAME);
}

async reloadDb(): Promise<void> {
try {
await this.storage.destroy();
Expand All @@ -61,10 +61,16 @@ export class LocalStorageProvider extends PersistenceStoreProvider {
const oppositeRevision = await this.getMonthRevision(
monthDataModel.scheduleKey.getRevisionKey(oppositeRevisionType)
);
const isMonthInFuture = VerboseDateHelper.isMonthInFuture(
monthDataModel.scheduleKey.month,
monthDataModel.scheduleKey.year
);

if (
_.isNil(oppositeRevision) ||
oppositeRevision.isAutoGenerated ||
oppositeRevisionType === "actual"
oppositeRevisionType === "actual" ||
isMonthInFuture
) {
await this.saveMonthRevision(oppositeRevisionType, monthDataModel);
}
Expand All @@ -86,6 +92,7 @@ export class LocalStorageProvider extends PersistenceStoreProvider {
// eslint-disable-next-line no-console
error.status !== 404 && console.error(error);
}

const monthRev: MonthRevision = {
_id: revisionKey,
data: monthDataModel,
Expand All @@ -112,8 +119,11 @@ export class LocalStorageProvider extends PersistenceStoreProvider {
);

if (daysMissingFromNextMonth !== 0) {
const nextMonthRevisionKey = getScheduleKey(scheduleDataModel).nextMonthKey.getRevisionKey(
type
);
await this.updateMonthPartBasedOnScheduleDM(
getScheduleKey(scheduleDataModel).nextMonthKey.getRevisionKey(type),
nextMonthRevisionKey,
scheduleDataModel,
daysMissingFromNextMonth,
"HEAD"
Expand All @@ -128,8 +138,8 @@ export class LocalStorageProvider extends PersistenceStoreProvider {
updatePosition: ArrayPositionPointer
): Promise<void> {
try {
const document = await this.storage.get(revisionKey);
const updatedMonthDataModel = document.data;
const updatedMonthDataModel = await this.getMonthRevision(revisionKey);
if (_.isNil(updatedMonthDataModel)) return;

const newShifts = _.cloneDeep(updatedMonthDataModel.shifts);

Expand Down Expand Up @@ -160,11 +170,10 @@ export class LocalStorageProvider extends PersistenceStoreProvider {
};

validateMonthDM(updatedMonthDataModel);
this.storage.put({
_rev: document._rev,
_id: revisionKey,
data: updatedMonthDataModel,
});
await this.saveBothMonthRevisionsIfNeeded(
getRevisionTypeFromKey(revisionKey),
updatedMonthDataModel
);
} catch (error) {
// eslint-disable-next-line no-console
error.status !== 404 && console.error(error);
Expand Down
4 changes: 2 additions & 2 deletions src/helpers/month.helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export class MonthHelper {
): T[] {
const { daysMissingFromNextMonth } = this.calculateMissingFullWeekDays(scheduleKey);
const monthLen = monthData.length;
let lastWeek: T[] = [];
let lastWeek: T[];
if (daysMissingFromNextMonth > 0) {
const daysFromCurrentMonthInLastWeek = NUMBER_OF_DAYS_IN_WEEK - daysMissingFromNextMonth;
const currentMonthDataPart = monthData.slice(
Expand All @@ -34,7 +34,7 @@ export class MonthHelper {
}

if (lastWeek.length !== NUMBER_OF_DAYS_IN_WEEK) {
throw new Error(`Week must have ${NUMBER_OF_DAYS_IN_WEEK} days`);
throw new Error(`Week must have ${NUMBER_OF_DAYS_IN_WEEK} days not ${lastWeek.length}`);
}

return lastWeek;
Expand Down
24 changes: 13 additions & 11 deletions src/logic/month-copy/month-copy.logic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,20 @@ export function copyShifts(
baseShifts: ShiftInfoModel
): ShiftInfoModel {
const newMonthWorkersShifts: ShiftInfoModel = {};
const { daysMissingFromPrevMonth } = MonthHelper.calculateMissingFullWeekDays(currentScheduleKey);
const replacementStart = daysMissingFromPrevMonth > 0 ? NUMBER_OF_DAYS_IN_WEEK : 0;

Object.keys(baseShifts).forEach((workerKey) => {
const copiedShifts = copyMonthData(
currentScheduleKey,
baseShifts[workerKey],
ShiftCode.W,
currentScheduleShifts[workerKey]
);
newMonthWorkersShifts[workerKey] = ShiftHelper.replaceFreeShiftsWithFreeDay(copiedShifts);
newMonthWorkersShifts[workerKey] = ShiftHelper.replaceFreeShiftsWithFreeDay(
copiedShifts,
replacementStart
);
});
return newMonthWorkersShifts;
}
Expand Down Expand Up @@ -96,18 +102,14 @@ function copyMonthData<T>(
if (isMonthStartInMonday) {
return copiedData;
} else {
return concatWithLastWeekFromPrevMonth(monthKey, copiedData, baseMonthData, currentMonthData);
const prevMonthLastWeekData = MonthHelper.getMonthLastWeekData(
monthKey.prevMonthKey,
baseMonthData,
currentMonthData
);
return prevMonthLastWeekData.concat(copiedData);
}
}

function concatWithLastWeekFromPrevMonth<T>(
monthKey: ScheduleKey,
copiedData: T[],
prevMonth: T[],
currentMonth: T[]
): T[] {
return MonthHelper.getMonthLastWeekData(monthKey, prevMonth, currentMonth).concat(copiedData);
}
//returns always 4 or 5 weeks due by the algorithm which operates on whole weeks instead of months
function getNumberOfDaysToBeCopied(monthKey: ScheduleKey): number {
return MonthHelper.findFirstMonthMondayIdx(monthKey.year, monthKey.month) +
Expand Down