Skip to content

Commit

Permalink
Restore prev storage initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
pectom committed Mar 17, 2021
1 parent be11f72 commit b34f619
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 21 deletions.
15 changes: 10 additions & 5 deletions src/api/local-storage-provider.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,16 @@ export const DATABASE_NAME = "nurse-scheduling";
type MonthDMToRevisionKeyDict = { [revisionKey: string]: MonthDataModel };

export class LocalStorageProvider extends PersistenceStoreProvider {
private storage: PouchDB.Database<MonthRevision>;

constructor() {
super();
this.storage = new PouchDB(DATABASE_NAME);
}
async reloadDb(): Promise<void> {
try {
const storage: PouchDB.Database<MonthRevision> = new PouchDB(DATABASE_NAME);
await storage.destroy();
await this.storage.destroy();
this.storage = new PouchDB(DATABASE_NAME);
} catch (err) {
// eslint-disable-next-line no-console
console.log(err);
Expand Down Expand Up @@ -78,10 +84,9 @@ export class LocalStorageProvider extends PersistenceStoreProvider {
): Promise<void> {
validateMonthDM(monthDataModel);
const revisionKey = monthDataModel.scheduleKey.getRevisionKey(revisionType);
const storage: PouchDB.Database<MonthRevision> = new PouchDB(DATABASE_NAME);
let revision;
try {
const document = await storage.get(revisionKey);
const document = await this.storage.get(revisionKey);
revision = document._rev;
} catch (error) {
// eslint-disable-next-line no-console
Expand All @@ -98,7 +103,7 @@ export class LocalStorageProvider extends PersistenceStoreProvider {
}

try {
storage.put(monthRev);
this.storage.put(monthRev);
} catch (error) {
// eslint-disable-next-line no-console
console.error(error);
Expand Down
22 changes: 6 additions & 16 deletions src/logic/month-copy/month-copy.logic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,24 +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[] {
const prevMonthLastWeek = MonthHelper.getMonthLastWeekData(
monthKey.prevMonthKey,
prevMonth,
currentMonth
);
return prevMonthLastWeek ? prevMonthLastWeek.concat(copiedData) : 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

0 comments on commit b34f619

Please sign in to comment.