Skip to content

Commit

Permalink
Merge pull request #355 from stustapay/milo/transmit-user-roles-for-u…
Browse files Browse the repository at this point in the history
…ser-info

feat(terminalserver): transmit user roles at till node with user info
  • Loading branch information
mikonse committed May 20, 2024
2 parents 7ee52d5 + b68ab91 commit 9f9c557
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 10 deletions.
18 changes: 16 additions & 2 deletions stustapay/core/schema/till.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from pydantic import BaseModel

from stustapay.core.schema.user import UserWithoutId
from stustapay.core.schema.user import Privilege, User


class NewTillButton(BaseModel):
Expand Down Expand Up @@ -101,9 +101,23 @@ class CashRegisterStocking(NewCashRegisterStocking):
total: float


class UserInfo(UserWithoutId):
class UserRoleInfo(BaseModel):
id: int
name: str
is_privileged: bool
privileges: list[Privilege]

node_id: int
node_name: str

is_at_current_node: bool


class UserInfo(User):
user_tag_uid: int
cash_register_id: Optional[int] = None
cash_register_name: Optional[str] = None
cash_drawer_balance: Optional[float] = None
transport_account_balance: Optional[float] = None

assigned_roles: list[UserRoleInfo]
27 changes: 24 additions & 3 deletions stustapay/core/service/till/till.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from stustapay.core.schema.account import Account
from stustapay.core.schema.order import Order
from stustapay.core.schema.terminal import CurrentTerminal
from stustapay.core.schema.till import NewTill, Till, UserInfo
from stustapay.core.schema.till import NewTill, Till, UserInfo, UserRoleInfo
from stustapay.core.schema.tree import Node, ObjectType
from stustapay.core.schema.user import (
CurrentUser,
Expand Down Expand Up @@ -267,7 +267,9 @@ async def logout_user(self, *, conn: Connection, current_terminal: CurrentTermin

@with_db_transaction(read_only=True)
@requires_terminal()
async def get_user_info(self, *, conn: Connection, current_user: CurrentUser, user_tag_uid: int) -> UserInfo:
async def get_user_info(
self, *, conn: Connection, current_user: CurrentUser, node: Node, user_tag_uid: int
) -> UserInfo:
if (
Privilege.node_administration not in current_user.privileges
and Privilege.user_management not in current_user.privileges
Expand All @@ -282,7 +284,8 @@ async def get_user_info(self, *, conn: Connection, current_user: CurrentUser, us
" cash_a.balance as cash_drawer_balance, "
" transp_a.balance as transport_account_balance, "
" cr.id as cash_register_id, "
" cr.name as cash_register_name "
" cr.name as cash_register_name,"
" '[]'::json as assigned_roles "
"from user_with_tag u "
"left join account cash_a on cash_a.id = u.cashier_account_id "
"left join account transp_a on transp_a.id = u.transport_account_id "
Expand All @@ -292,6 +295,24 @@ async def get_user_info(self, *, conn: Connection, current_user: CurrentUser, us
)
if info is None:
raise InvalidArgument(f"There is no user registered for tag {format_user_tag_uid(user_tag_uid)}")

assigned_roles = await conn.fetch_many(
UserRoleInfo,
"select "
" ur.*, "
" utr.node_id,"
" n.name as node_name, "
" utr.node_id = $3 as is_at_current_node "
"from user_role_with_privileges ur "
"join user_to_role utr on ur.id = utr.role_id "
"join node n on utr.node_id = n.id "
"where n.id = any($2) and utr.user_id = $1",
info.id,
node.ids_to_root,
node.id,
)

info.assigned_roles = assigned_roles
return info

@with_db_transaction(read_only=True)
Expand Down
2 changes: 1 addition & 1 deletion stustapay/core/service/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ async def update_user_roles_terminal(

roles = await conn.fetch_many(
UserRole,
"select ur.* from user_role ur join user_to_role utr on ur.id = utr.role_id "
"select ur.* from user_role_with_privileges ur join user_to_role utr on ur.id = utr.role_id "
"where utr.node_id = $1 and utr.user_id = $2",
node.id,
user_id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,13 @@ const ProductSelection: React.FC<ProductSelectProps> = ({ productIds, onChange }
</ListItemSecondaryAction>
</ListItem>
))}
<ProductSelect label={t("button.addProductToButton")} variant="standard" value={null} onChange={addProduct} />
<ProductSelect
label={t("button.addProductToButton")}
variant="standard"
value={null}
onChange={addProduct}
onlyLocked
/>
</List>
);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@ import { useCurrentNode } from "@/hooks";
import { Select, SelectProps } from "@stustapay/components";
import * as React from "react";

export type ProductSelectProps = Omit<SelectProps<Product, false>, "options" | "formatOption" | "multiple">;
export type ProductSelectProps = {
onlyLocked?: boolean;
} & Omit<SelectProps<Product, false>, "options" | "formatOption" | "multiple">;

export const ProductSelect: React.FC<ProductSelectProps> = (props) => {
export const ProductSelect: React.FC<ProductSelectProps> = ({ onlyLocked = false, ...props }) => {
const { currentNode } = useCurrentNode();
const { products } = useListProductsQuery(
{ nodeId: currentNode.id },
{
selectFromResult: ({ data, ...rest }) => ({
...rest,
products: data ? selectProductAll(data) : [],
products: data ? selectProductAll(data).filter((p) => p.is_locked || !onlyLocked) : [],
}),
}
);
Expand Down

0 comments on commit 9f9c557

Please sign in to comment.