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

Feat/new restaurants api #1393

Draft
wants to merge 7 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
145 changes: 84 additions & 61 deletions packages/uni_app/lib/controller/fetchers/restaurant_fetcher.dart
Original file line number Diff line number Diff line change
@@ -1,47 +1,94 @@
import 'package:uni/controller/networking/network_router.dart';
import 'package:uni/controller/parsers/parser_restaurants.dart';
import 'package:uni/model/entities/meal.dart';
import 'package:uni/model/entities/restaurant.dart';
import 'package:uni/model/utils/day_of_week.dart';
import 'package:uni/session/flows/base/session.dart';
import 'package:up_menus/up_menus.dart';

/// Class for fetching the menu
class RestaurantFetcher {
final String spreadSheetUrl = 'https://docs.google.com/spreadsheets/d/'
'1TJauM0HwIf2RauQU2GmhdZZ1ZicFLMHuBkxWwVOw3Q4';
final String jsonEndpoint = '/gviz/tq?tqx=out:json';

// Format: Date(dd/mm/yyyy), Meal("Almoço", "Jantar), Dish("Sopa", "Carne",
// "Peixe", "Dieta", "Vegetariano", "Salada"), Description(String)
final String sheetsColumnRange = 'A:D';

// List the Restaurant sheet names in the Google Sheets Document
final List<String> restaurantSheets = ['Cantina'];

// Generate the Gsheets endpoints list based on a list of sheets
String buildGSheetsEndpoint(String sheet) {
return Uri.encodeFull(
'$spreadSheetUrl$jsonEndpoint&sheet=$sheet&range=$sheetsColumnRange',
);
}
// Auxliary function to print a list of restaurants.
// void printRestaurants(List<Restaurant> restaurants) {
// for (final restaurant in restaurants) {
// print(restaurant.id);
// print(restaurant.name);
// print(restaurant.reference);
// final meals = restaurant.meals;
// meals.forEach((day, mealList) {
// print(day);
// for (final meal in mealList) {
// print(' - ${meal.name}');
// }
// });
// }
// }

Future<List<Restaurant>> fetchSASUPRestaurants() async {
// TODO: change the implementation to accomodate changes for the new UI.
final upMenus = UPMenusApi();
final establishments = await upMenus.establishments.list();
final restaurants = <Restaurant>[];

String getRestaurantGSheetName(Restaurant restaurant) {
return restaurantSheets.firstWhere(
(sheetName) =>
restaurant.name.toLowerCase().contains(sheetName.toLowerCase()),
orElse: () => '',
);
}
// For every establishement...
for (final establishment in establishments) {
// Get the menu for the current week
if (establishment.dayMenu == false) {
continue;
}
// HACK: hardcoded week number, because SASUP hasn't published the menus for the current week.
final dayMenus = (await upMenus.dayMenus.get(
establishment.id,
Period.lunch,
weekNumber: 40,
year: 2024,
))
.followedBy(
await upMenus.dayMenus.get(
establishment.id,
Period.dinner,
weekNumber: 40,
year: 2024,
),
)
.followedBy(
await upMenus.dayMenus.get(
establishment.id,
Period.snackBar,
weekNumber: 40,
year: 2024,
),
)
.followedBy(
await upMenus.dayMenus.get(
establishment.id,
Period.breakfast,
weekNumber: 40,
year: 2024,
),
);
final meals = <Meal>[];
// For every day...
for (final dayMenu in dayMenus) {
// And for every dish...
for (final dish in dayMenu.dishes) {
// Extract the information about the meal.
meals.add(
Meal(
dish.dishType.namePt,
dish.dish.namePt,
parseDateTime(dayMenu.day),
dayMenu.day,
),
);
}
}

Future<Restaurant> fetchGSheetsRestaurant(
String url,
String restaurantName,
Session session, {
bool isDinner = false,
}) async {
return getRestaurantFromGSheets(
await NetworkRouter.getWithCookies(url, {}, session),
restaurantName,
isDinner: isDinner,
);
restaurants.add(
Restaurant(establishment.id, establishment.namePt, '', meals: meals),
);
}
return restaurants;
}

final List<String> sigarraMenuEndpoints = [
Expand All @@ -64,32 +111,8 @@ class RestaurantFetcher {
}

Future<List<Restaurant>> getRestaurants(Session session) async {
final restaurants = await fetchSigarraRestaurants(session);

// Check for restaurants without associated meals and attempt to parse them
// from GSheets
final restaurantsWithoutMeals =
restaurants.where((restaurant) => restaurant.meals.isEmpty).toList();

for (final restaurant in restaurantsWithoutMeals) {
final sheetName = getRestaurantGSheetName(restaurant);
if (sheetName.isEmpty) {
continue;
}

final gSheetsRestaurant = await fetchGSheetsRestaurant(
buildGSheetsEndpoint(sheetName),
restaurant.name,
session,
isDinner: restaurant.name.toLowerCase().contains('jantar'),
);

restaurants
..removeWhere(
(restaurant) => restaurant.name == gSheetsRestaurant.name,
)
..insert(0, gSheetsRestaurant);
}
final restaurants =
await fetchSASUPRestaurants() + await fetchSigarraRestaurants(session);

return restaurants;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,11 @@ class RestaurantDatabase extends AppDatabase<List<Restaurant>> {
}
});

return filterPastMeals(restaurants);
// TODO: reimplement the filter.

// return filterPastMeals(restaurants);

return restaurants;
}

Future<List<Meal>> getRestaurantMeals(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ class RestaurantProvider extends StateProviderNotifier<List<Restaurant>> {
final db = RestaurantDatabase();
unawaited(db.saveIfPersistentSession(restaurants));

return filterPastMeals(restaurants);
// TODO: enable past meals filtering after SASUP publishes the new menus (ALSO CHANGE IN THE STORAGE DATABASE!!!!).

// return filterPastMeals(restaurants);
return restaurants;
}
}
22 changes: 22 additions & 0 deletions packages/uni_app/lib/model/utils/day_of_week.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,28 @@ enum DayOfWeek {
sunday
}

DayOfWeek parseDateTime(DateTime dateTime) {
final weekDay = dateTime.weekday;
switch (weekDay) {
case DateTime.monday:
return DayOfWeek.monday;
case DateTime.tuesday:
return DayOfWeek.tuesday;
case DateTime.wednesday:
return DayOfWeek.wednesday;
case DateTime.thursday:
return DayOfWeek.thursday;
case DateTime.friday:
return DayOfWeek.friday;
case DateTime.saturday:
return DayOfWeek.saturday;
case DateTime.sunday:
return DayOfWeek.sunday;
default:
throw Exception('Invalid day of week');
}
}

DayOfWeek? parseDayOfWeek(String str) {
final weekDay = str.replaceAll(' ', '').toLowerCase();
if (weekDay == 'segunda-feira') {
Expand Down
16 changes: 16 additions & 0 deletions packages/uni_app/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1147,6 +1147,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "0.28.0"
scrollable_positioned_list:
dependency: transitive
description:
name: scrollable_positioned_list
sha256: "1b54d5f1329a1e263269abc9e2543d90806131aa14fe7c6062a8054d57249287"
url: "https://pub.dev"
source: hosted
version: "0.3.8"
sentry:
dependency: transitive
description:
Expand Down Expand Up @@ -1511,6 +1519,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "2.2.2"
up_menus:
dependency: "direct main"
description:
name: up_menus
sha256: b973555072c6056b757f2740cf7c10fc190ff7f7cd3df3f26c303faf261ce112
url: "https://pub.dev"
source: hosted
version: "1.0.1"
upgrader:
dependency: "direct main"
description:
Expand Down
1 change: 1 addition & 0 deletions packages/uni_app/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ dependencies:
ua_client_hints: ^1.3.1
uni_ui:
path: ../uni_ui
up_menus: ^1.0.1
upgrader: ^10.3.0
url_launcher: ^6.2.2
workmanager: ^0.5.2
Expand Down