Skip to content

Commit

Permalink
feat: add currentWeek, dayLimit, and entryLimit
Browse files Browse the repository at this point in the history
  • Loading branch information
zanix committed May 27, 2024
1 parent 99fd0e1 commit c84291b
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 22 deletions.
61 changes: 48 additions & 13 deletions MMM-MealieMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ Module.register("MMM-MealieMenu", {
groupId: "", // Group ID of the meal plan.

// Meal entries
currentWeek: true, // Only show meals for the current week.
weekStartsOnMonday: false, // Show Monday as the first day of the week.
priorDayLimit: 7, // How many previous days of the current week will be displayed.
dayLimit: 7, // How many days will be displayed after today.
priorDayLimit: 2, // How many previous days will be displayed.
entryLimit: 50, // How many entries from future days should be shown in total.
priorEntryLimit: 50, // How many entries from previous days should be shown in total.

// Look and Feel
Expand Down Expand Up @@ -248,7 +251,10 @@ Module.register("MMM-MealieMenu", {
username: this.config.username,
password: this.config.password,
groupId: this.config.groupId,
weekStartsOnMonday: this.config.weekStartsOnMonday
weekStartsOnMonday: this.config.weekStartsOnMonday,
currentWeek: this.config.currentWeek,
dayLimit: this.config.dayLimit,
priorDayLimit: this.config.priorDayLimit
});
},

Expand Down Expand Up @@ -299,25 +305,44 @@ Module.register("MMM-MealieMenu", {
*/
filterDaysAndEntries (sortedMenu) {
const today = moment().startOf("day");
const reversed = sortedMenu.reverse();
const reversed = sortedMenu.toReversed();
const filtered = [];
let entriesBeforeTodayCount = 0;
let entriesAfterTodayCount = 0;

for (const meal of reversed) {
// Filter future entries first.
for (const meal of sortedMenu) {
// Break when we reach the max number of entries.
if (entriesAfterTodayCount >= this.config.entryLimit) {
break;
}

// days is the number of days between today and this menu items date; a negative value for past items.
const days = moment(meal.rawDate).diff(today, "days");

// If days < 0, this is a previous entry. The config may limit how many of these we show.
if (days < 0) {
entriesBeforeTodayCount += 1;
// If days >= 0, this is a future entry. The config may limit how many of these we show.
// Add it to our result set if it is after today and this.config.entryLimit is not reached.
if (days >= 0 && this.config.entryLimit >= entriesAfterTodayCount) {
entriesAfterTodayCount += 1;
filtered.push(meal);
}
}

filtered.reverse();

for (const meal of reversed) {
// Break when we reach the max number of entries.
if (entriesBeforeTodayCount >= this.config.priorEntryLimit) {
break;
}

// days is the number of days between today and this menu items date; a negative value for past items.
// Add it to our result set if it is 0+ (today or in the future), and if this.config.priorDayLimit + days is 0+.
// days >= 0 can just be rolled into the priorDayLimit check.
// Example: priorDayLimit: 3, menu two days ago -> days: -2, 3 + -2 = 1, add it to the result.
//
// Additionally, only show max this.config.priorEntryLimit.
if (this.config.priorDayLimit + days >= 0 && this.config.priorEntryLimit >= entriesBeforeTodayCount) {
const days = moment(meal.rawDate).diff(today, "days");

// If days < 0, this is a previous entry. The config may limit how many of these we show.
// Add it to our result set if it is before today and this.config.priorEntryLimit is not reached.
if (days < 0 && this.config.priorEntryLimit >= entriesBeforeTodayCount) {
entriesBeforeTodayCount += 1;
filtered.push(meal);
}
}
Expand Down Expand Up @@ -366,11 +391,21 @@ Module.register("MMM-MealieMenu", {
this.config.defaultPicture = this.file(this.config.defaultPicture);
}

if (this.config.dayLimit < 0) {
this.config.dayLimit = 0;
Log.warn("dayLimit should be 0 or greater. Setting to 0.");
}

if (this.config.priorDayLimit < 0) {
this.config.priorDayLimit = 0;
Log.warn("priorDayLimit should be 0 or greater. Setting to 0.");
}

if (this.config.entryLimit < 0) {
this.config.entryLimit = 0;
Log.warn("entryLimit should be 0 or greater. Setting to 0.");
}

if (this.config.priorEntryLimit < 0) {
this.config.priorEntryLimit = 0;
Log.warn("priorEntryLimit should be 0 or greater. Setting to 0.");
Expand Down
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,12 @@ These are the possible options:
| `username` | <p>The username/email for your Mealie account.</p><p>**REQUIRED** When not using `apiKey`<br>**Type:** `string`<br>**Example:** `"yourmeailieemail@email.com"`<br>**Default value:** none</p>|
| `password` | <p>The password for your for Mealie account.</p><p>**REQUIRED** When not using `apiKey`<br>**Type:** `string`<br>**Example:** `"Secret!"`<br>**Default value:** none</p>|
| `groupId` | <p>The Group ID (as a UUID) to use when fetching the meal plan.</p><p>**Type:** `string`<br>**Example:** `"c0aa0c1c-bdbb-4948-823b-2a725fb05ce1"`<br>**Default value:** none ("Home" group)</p><p>**Note 1:** You can get the UUID of a group from Settings > Groups.</p><p>**Note 2:** The default "Home" group is used when this is blank.</p>|
| `weekStartsOnMonday` | <p>Show Monday as the first day of the week. Set to `true` to show Monday as the first day of the week.</p><p>**Type:** `boolean`<br>**Default value:** `false`<br>**Possible values:** `true` and `false`|
| `priorDayLimit` | <p>How many previous days of the current week will be displayed. </p><p>**Type:** `integer`<br>**Example:** `5`<br>**Default value:** `7`<br>**Unit:** `days`</p><p>**Note:** If `0`, only today until the end of the week will be shown.</p>|
| `priorEntryLimit` | <p>How many entries from previous days should be shown in total. </p><p>**Type:** `integer`<br>**Example:** `25`<br>**Default value:** `50`<br>**Unit:** `days`</p><p>**Note:** `priorDayLimit` takes precedence. For example, with `3` entries per day, `priorEntryLimit` set to `5` and `priorDayLimit` set to `1`, you will only see `3` prior entries.</p>|
| `currentWeek` | <p>Only show meals for the current week. Set to `true` to display meals up-to the end of the week. This can be set using `weekStartsOnMonday`. The number of meals is also limited by `dayLimit`, `priorDayLimit`, and `priorEntryLimit`.</p><p>**Type:** `boolean`<br>**Default value:** `false`<br>**Possible values:** `true` and `false`|
| `weekStartsOnMonday` | <p>Show Monday as the first day of the week. Set to `true` to show Monday as the first day of the week.</p><p>**Type:** `boolean`<br>**Default value:** `false`<br>**Possible values:** `true` and `false`</p><p>**Note:** Only valid when `currentWeek` is set to `true`.</p>|
| `dayLimit` | <p>How many days will be displayed after today.</p><p>**Type:** `integer`<br>**Example:** `5`<br>**Default value:** `7`<br>**Unit:** `days`</p><p>**Note:** If `0`, only today will be shown.</p><p>**Note 2:** If `currentWeek` is `true`, the max number of days is until the end of the week.|
| `priorDayLimit` | <p>How many previous days will be displayed. </p><p>**Type:** `integer`<br>**Example:** `5`<br>**Default value:** `7`<br>**Unit:** `days`</p><p>**Note:** If `0` and `currentWeek` is `true`, the max number of days is until the beginning of the week.</p>|
| `entryLimit` | <p>How many entries from future days should be shown in total.</p><p>**Type:** `integer`<br>**Example:** `25`<br>**Default value:** `50`<br>**Unit:** `meals`</p><p>**Note:** `dayLimit` takes precedence. For example, with `3` entries per day, `entryLimit` set to `5` and `dayLimit` set to `1`, you will only see `3` future entries.</p>|
| `priorEntryLimit` | <p>How many entries from previous days should be shown in total. </p><p>**Type:** `integer`<br>**Example:** `25`<br>**Default value:** `50`<br>**Unit:** `meals`</p><p>**Note:** `priorDayLimit` takes precedence. For example, with `3` entries per day, `priorEntryLimit` set to `5` and `priorDayLimit` set to `1`, you will only see `3` prior entries.</p>|
| `fadePriorEntries` | <p>Fade previous days in the current week.</p><p>**Type:** `boolean`<br>**Default value:** `true`<br>**Possible values:** `true` and `false`|
| `showPictures` | <p>Show pictures corresponding to that days meal.</p><p>**Type:** `boolean`<br>**Default value:** `true`<br>**Possible values:** `true` and `false`|
| `roundPictureCorners` | <p>Round the meal picture corners.</p><p>**Type:** `boolean`<br>**Default value:** `false`<br>**Possible values:** `true` and `false`|
Expand Down
31 changes: 25 additions & 6 deletions node_helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,17 +77,36 @@ module.exports = NodeHelper.create({

this.outstandingRequest = true;

const startOfWeek = this.getFirstDayOfWeek(config.weekStartsOnMonday);
const nextWeek = startOfWeek.clone().add(7, "days");
const lastDayOfWeek = nextWeek.clone().subtract(1, "days");
let startDate = moment();
let endDate = moment();

if (config.currentWeek) {
const startOfWeek = this.getFirstDayOfWeek(config.weekStartsOnMonday);
const endOfWeek = startDate.clone().add(6, "days");

// Find the latest start date.
startDate = moment.max([
startOfWeek,
moment().subtract(config.priorDayLimit, "days")
]);

// Find the earliest end date.
endDate = moment.min([
endOfWeek,
moment().add(config.dayLimit, "days")
]);
} else {
startDate.subtract(config.priorDayLimit, "days");
endDate.add(config.dayLimit, "days");
}

Log.info(`[${this.name}] Week starts: ${startOfWeek.format("YYYY-MM-DD")}, next week starts: ${nextWeek.format("YYYY-MM-DD")}`);
Log.info(`[${this.name}] Fetching meals: Start Date ${startDate.format("YYYY-MM-DD")} - End date ${endDate.format("YYYY-MM-DD")}`);

const url = new URL(`${config.host}/api/groups/mealplans`);

const params = new URLSearchParams();
params.append("start_date", startOfWeek.format("YYYY-MM-DD"));
params.append("end_date", lastDayOfWeek.format("YYYY-MM-DD"));
params.append("start_date", startDate.format("YYYY-MM-DD"));
params.append("end_date", endDate.format("YYYY-MM-DD"));
params.append("orderBy", "date");
params.append("orderDirection", "asc");
if (config.groupId) {
Expand Down

0 comments on commit c84291b

Please sign in to comment.