Skip to content

Commit b9b1a68

Browse files
committed
Merge branch 'main' into prod/festapp
2 parents b5a32a6 + 1ca68a1 commit b9b1a68

11 files changed

+145
-139
lines changed

lib/RouterService.dart

+2-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,8 @@ class RouterService {
105105
static Future<bool> updateOccasionFromLink(LinkModel link) async {
106106
bool canContinue = true;
107107
var checkedObject = await SynchroService.getAppConfig(occasionLink: link.occasionLink, formLink: link.formLink);
108-
RightsService.currentUserOccasion = checkedObject.user;
108+
RightsService.currentOccasionUser = checkedObject.user;
109+
RightsService.currentUnitUser = checkedObject.unitUser;
109110
RightsService.currentOccasion = checkedObject.occasionId;
110111
RightsService.currentLink = checkedObject.link;
111112
RightsService.isAdminField = checkedObject.isAdmin;

lib/dataModels/OccasionLinkModel.dart

+6-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ class OccasionLinkModel {
55
int? occasionId;
66
String? link;
77
OccasionUserModel? user;
8+
OccasionUserModel? unitUser;
89
bool? isAdmin = false;
910
String? versionRecommended; // New field for version_recommended
1011

@@ -13,9 +14,12 @@ class OccasionLinkModel {
1314
bool isNotFound() => code == 404;
1415

1516
factory OccasionLinkModel.fromJson(Map<String, dynamic> json) {
17+
var unitUser = json["unit_user"] != null ? OccasionUserModel.fromJson(json["unit_user"]) : null;
18+
var occasionUser = json["occasion_user"] != null ? OccasionUserModel.fromJson(json["occasion_user"]) : null;
1619
return OccasionLinkModel(
1720
code: json["code"],
18-
user: json["occasion_user"] != null ? OccasionUserModel.fromJson(json["occasion_user"]) : null,
21+
unitUser: unitUser,
22+
user: occasionUser,
1923
link: json["link"],
2024
occasionId: json["occasion"],
2125
isAdmin: json["is_admin"],
@@ -26,6 +30,7 @@ class OccasionLinkModel {
2630
OccasionLinkModel({
2731
this.code,
2832
this.user,
33+
this.unitUser,
2934
this.link,
3035
this.occasionId,
3136
this.isAdmin,

lib/dataServices/AuthService.dart

+2-2
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ class AuthService {
8080
static Future<UserInfoModel> getFullUserInfo() async {
8181
var user = UserInfoModel();
8282
user.occasionUser = await DbUsers.getOccasionUser(AuthService.currentUserId());
83-
if(RightsService.currentUserOccasion?.role != null) {
84-
user.roleString = await getRoleInfo(RightsService.currentUserOccasion!.role!);
83+
if(RightsService.currentOccasionUser?.role != null) {
84+
user.roleString = await getRoleInfo(RightsService.currentOccasionUser!.role!);
8585
}
8686
user.userGroups = await DbGroups.getUserGroups();
8787
var eUserGroup = user.userGroups!.firstWhereOrNull((g)=>g.type == null);

lib/dataServices/DbCompanions.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class DbCompanions {
3333
await _supabase.rpc("create_companion_in_organization", params: {
3434
'org': AppConfig.organization,
3535
'oc': RightsService.currentOccasion,
36-
'usr': RightsService.currentUserOccasion!.user!,
36+
'usr': RightsService.currentOccasionUser!.user!,
3737
'c_name': name,
3838
});
3939
}

lib/dataServices/DbUsers.dart

+11
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,17 @@ class DbUsers {
4545
return [];
4646
}
4747

48+
static Future<List<UserInfoModel>> getAllUsersBasicsForUnit() async {
49+
var oc = RightsService.currentOccasion!;
50+
var result = await _supabase.rpc("get_all_user_basics_from_occasion_unit",
51+
params: {"oc": RightsService.currentOccasion!});
52+
if(result["code"] == 200) {
53+
var t = List<UserInfoModel>.from(result["data"].map((x) => UserInfoModel.fromJson(x)));
54+
return t;
55+
}
56+
return [];
57+
}
58+
4859
static Future<void> updateUserInfo(OccasionUserModel data) async {
4960
await _supabase.rpc("update_user",
5061
params:

lib/dataServices/RightsService.dart

+14-5
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ import 'package:supabase_flutter/supabase_flutter.dart';
99

1010
class RightsService{
1111
static final _supabase = Supabase.instance.client;
12-
static OccasionUserModel? currentUserOccasion;
12+
static OccasionUserModel? currentOccasionUser;
13+
static OccasionUserModel? currentUnitUser;
1314
static int? currentOccasion;
1415
static String? currentLink;
1516
static bool? isAdminField;
@@ -57,22 +58,30 @@ class RightsService{
5758
}
5859

5960
static bool canSignInOutUsersFromEvents() {
60-
return currentUserOccasion?.isEditor??false;
61+
return currentOccasionUser?.isEditor??false;
6162
}
6263

6364
static bool isAdmin() {
6465
return isAdminField??false;
6566
}
6667

6768
static bool isEditor() {
68-
return currentUserOccasion?.isEditor??false;
69+
return currentOccasionUser?.isEditor??false;
70+
}
71+
72+
static bool isUnitEditor() {
73+
return currentUnitUser?.isEditor??false;
6974
}
7075

7176
static bool isManager() {
72-
return currentUserOccasion?.isManager??false;
77+
return currentOccasionUser?.isManager??false;
78+
}
79+
80+
static bool isUnitManager() {
81+
return currentUnitUser?.isManager??false;
7382
}
7483

7584
static bool isApprover() {
76-
return currentUserOccasion?.isApprover??false;
85+
return currentOccasionUser?.isApprover??false;
7786
}
7887
}

lib/pages/AdministrationOccasion/UsersTab.dart

+3-3
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ class _UsersTabState extends State<UsersTab> {
7373
areAllActionsEnabled: RightsService.canUpdateUsers),
7474
headerChildren: [
7575
DataGridAction(name: "Import".tr(), action: (SingleTableDataGrid p0, [_]) => _import(p0), isEnabled: RightsService.canUpdateUsers),
76-
if(RightsService.isAdmin())
76+
if(RightsService.isManager())
7777
DataGridAction(name: "Add existing".tr(), action: (SingleTableDataGrid p0, [_]) => _addExisting(p0)),
7878
DataGridAction(name: "Invite".tr(), action: (SingleTableDataGrid p0, [_]) => _invite(p0), isEnabled: RightsService.canUpdateUsers),
7979
DataGridAction(name: "Change password".tr(), action: (SingleTableDataGrid p0, [_]) => _setPassword(p0), isEnabled: RightsService.canUpdateUsers),
@@ -108,8 +108,8 @@ class _UsersTabState extends State<UsersTab> {
108108
}
109109

110110
Future<void> _addExisting(SingleTableDataGrid dataGrid) async {
111-
if (_allUsers == null) return;
112-
var nonAdded = _allUsers!.where((u) => !_getCheckedUsers(dataGrid).any((cu) => cu.user == u.id)).toList();
111+
var existing = await DbUsers.getAllUsersBasicsForUnit();
112+
var nonAdded = existing.where((u) => !_allUsers!.any((cu) => cu.id == u.id)).toList();
113113
DialogHelper.chooseUser(context, (chosenUser) async {
114114
if (chosenUser != null) {
115115
await DbUsers.addUserToOccasion(chosenUser.id, RightsService.currentOccasion!);

scripts/database/eshop/get_report_for_occasion.sql

+4-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ DECLARE
1616
canceled_orders INT;
1717
total_revenue NUMERIC;
1818
paid_revenue NUMERIC;
19+
result_revenue NUMERIC;
1920
returned_revenue NUMERIC;
2021
remaining_balance NUMERIC;
2122

@@ -82,6 +83,7 @@ BEGIN
8283

8384
-- Calculate the remaining balance
8485
remaining_balance := total_revenue - paid_revenue;
86+
result_revenue := paid_revenue + returned_revenue;
8587

8688
-- Calculate the count of ordered products grouped by product type and product title,
8789
-- excluding orders with state 'storno' and 'ordered'
@@ -141,8 +143,9 @@ BEGIN
141143
product_type_section ||
142144
E'===========\n' ||
143145
E'Suma všech objednávek: ' || to_char(total_revenue, 'FM99999990.00') || E'\n' ||
144-
E'Suma zaplacených částek: ' || to_char(paid_revenue, 'FM99999990.00') || E'\n' ||
146+
E'Suma přijatých částek: ' || to_char(paid_revenue, 'FM99999990.00') || E'\n' ||
145147
E'Suma vrácených částek: ' || to_char(returned_revenue, 'FM99999990.00') || E'\n' ||
148+
E'Výsledná částka: ' || to_char(result_revenue, 'FM99999990.00') || E'\n' ||
146149
E'Zbývající částka: ' || to_char(remaining_balance, 'FM99999990.00') || E'\n' ||
147150
E'===========';
148151
EXCEPTION

scripts/database/functions/get_app_config.sql

+29-2
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@ DECLARE
1111
occasionId bigint;
1212
is_open_bool BOOLEAN;
1313
occasion_user occasion_users%rowtype;
14+
unit_user unit_users%rowtype;
1415
is_admin_bool BOOLEAN;
1516
occasion_link text;
1617
version_recommended text;
18+
occasion_unit bigint;
1719
BEGIN
1820
-- Log the request details in log_app_config table
1921
INSERT INTO public.log_app_config (organization, platform)
@@ -55,7 +57,7 @@ BEGIN
5557
-- If no link or form_link is provided
5658
ELSE
5759
-- Get the default occasion from the organization
58-
SELECT data->>'DEFAULT_OCCASION' INTO occasionId
60+
SELECT (data->>'DEFAULT_OCCASION')::bigint INTO occasionId
5961
FROM organizations
6062
WHERE id = org_id;
6163

@@ -98,13 +100,29 @@ BEGIN
98100
WHERE occasion = occasionId
99101
AND "user" = auth.uid();
100102

103+
-- Retrieve the unit ID from the occasions table
104+
SELECT unit INTO occasion_unit
105+
FROM occasions
106+
WHERE id = occasionId;
107+
108+
-- Get the unit user record if it exists
109+
SELECT * INTO unit_user
110+
FROM unit_users
111+
WHERE unit = occasion_unit
112+
AND "user" = auth.uid();
113+
101114
-- Check if the current user is an admin on the occasion
102115
is_admin_bool := get_is_admin_on_occasion(occasionId);
103116

104117
-- If the occasion is not open, enforce access restrictions
105118
IF is_open_bool = FALSE THEN
106119
IF auth.uid() IS NULL OR (occasion_user IS NULL AND NOT is_admin_bool) THEN
107-
RETURN json_build_object('code', 403, 'message', 'Access forbidden', 'link', occasion_link, 'version_recommended', version_recommended);
120+
RETURN json_build_object(
121+
'code', 403,
122+
'message', 'Access forbidden',
123+
'link', occasion_link,
124+
'version_recommended', version_recommended
125+
);
108126
END IF;
109127
END IF;
110128

@@ -120,11 +138,20 @@ BEGIN
120138
AND "user" = auth.uid();
121139
END IF;
122140

141+
-- Retrieve unit_user again in case the user was added to the occasion and now belongs to a unit
142+
IF unit_user IS NULL AND occasion_unit IS NOT NULL AND auth.uid() IS NOT NULL THEN
143+
SELECT * INTO unit_user
144+
FROM unit_users
145+
WHERE unit = occasion_unit
146+
AND "user" = auth.uid();
147+
END IF;
148+
123149
-- Return final response with all data and status code 200 at the end
124150
RETURN json_build_object(
125151
'code', 200,
126152
'is_admin', is_admin_bool,
127153
'occasion_user', COALESCE(row_to_json(occasion_user)::jsonb, NULL),
154+
'unit_user', COALESCE(row_to_json(unit_user)::jsonb, NULL),
128155
'link', occasion_link,
129156
'occasion', occasionId,
130157
'version_recommended', version_recommended

scripts/database/functions/get_occasion_from_link.sql

-123
This file was deleted.

0 commit comments

Comments
 (0)