Skip to content

Commit

Permalink
update user and employee add new future
Browse files Browse the repository at this point in the history
  • Loading branch information
Bahamin1 committed Jun 18, 2024
1 parent fb68cbe commit 9731969
Show file tree
Hide file tree
Showing 6 changed files with 300 additions and 264 deletions.
93 changes: 93 additions & 0 deletions src/Dorder/Employee.mo
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import Map "mo:map/Map";
import { phash } "mo:map/Map";

import Review "Review";

module {

public type UserRole = {
#Guest;
#Customer;
#Employee;
#Manager;
#Admin;
};

public type Operation = {
#ReserveTable;
#UnreserveTable;
#PayTable;
#MonitorLogs;
#HireManager;
#FireManager;
#HireEmployee;
#FireEmployee;
#ModifyTable;
#ModifyMenuItem;
#ModifyMenuItemPoint;
#ModifyEmployeePoints;
};

public type Employee = {
name : Text;
principal : Principal;
role : UserRole;
allowedOperations : [Operation];
id : Nat;
image : ?Blob;
review : [Review.EmployeeReview]

};

public type EmployeeMap = Map.Map<Principal, Employee>;

public func get(userMap : EmployeeMap, principal : Principal) : ?Employee {
return Map.get(userMap, phash, principal);
};

// put Employee
public func put(userMap : EmployeeMap, p : Principal, user : Employee) : () {
return Map.set(userMap, phash, p, user);
};

// add New user specefic with oprations
public func new(userMap : EmployeeMap, principal : Principal, name : Text, role : UserRole, allowedOperations : [Operation]) : () {
let id = Map.size(userMap) +1;

let user : Employee = {
name = name;
principal = principal;
role = role;
allowedOperations = allowedOperations;
id = id;
image = null;
buyingScore = 0;
review = [];
order = null;
};

put(userMap, principal, user);

return;
};

public func canPerform(userMap : EmployeeMap, p : Principal, operation : Operation) : Bool {
let user = get(userMap, p);

switch (user) {
case (null) {
return false;
};
case (?user) {
if (user.role == #Admin) return true;

for (o in user.allowedOperations.vals()) {
if (operation == o) return true;
};

return false;
};
};
};

};
23 changes: 12 additions & 11 deletions src/Dorder/Log.mo
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module {
id : Nat;
message : Text;
createdAt : Time.Time;
catagory : Catagory
catagory : Catagory;
};

public type Catagory = {
Expand All @@ -18,13 +18,14 @@ module {
#Menu;
#Order;
#EmployeeReview;
#MenuReview
#MenuReview;
#Personnel;
};

public type LogMap = Map.Map<Nat, Log>;

public func put(logMap : LogMap, key : Nat, value : Log) : () {
return Map.set(logMap, nhash, key, value)
return Map.set(logMap, nhash, key, value);
};

public func add(logMap : LogMap, catagory : Catagory, message : Text) : () {
Expand All @@ -34,12 +35,12 @@ module {
id = id;
message = message;
createdAt = Time.now();
catagory = catagory
catagory = catagory;
};

put(logMap, id, newLog);

return
return;
};

public func getLogsByCategory(logMap : LogMap, per : Catagory) : [Log] {
Expand All @@ -50,12 +51,12 @@ module {
case (is) {

if (is == per) {
filteredLogs := Array.append<Log>(filteredLogs, [cata])
}
}
}
filteredLogs := Array.append<Log>(filteredLogs, [cata]);
};
};
};
};
return filteredLogs;

}
}
};
};
65 changes: 30 additions & 35 deletions src/Dorder/Menu.mo
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ module {
price : Nat;
stock : Bool;
description : Text;
star : [Review.MenuReview];
image : ?Blob
score : [Review.MenuReview];
image : ?Blob;
};

public type NewMenuItem = {
Expand All @@ -32,12 +32,12 @@ module {
public type MenuMap = Map.Map<Nat, MenuItem>;

public func get(menuMap : MenuMap, key : Nat) : ?MenuItem {
return Map.get<Nat, MenuItem>(menuMap, nhash, key)
return Map.get<Nat, MenuItem>(menuMap, nhash, key);
};

// MenuItem put to map
public func put(menuMap : MenuMap, key : Nat, value : MenuItem) : () {
return Map.set<Nat, MenuItem>(menuMap, nhash, key, value)
return Map.set<Nat, MenuItem>(menuMap, nhash, key, value);
};

public func new(menuMap : MenuMap, newMenuItem : NewMenuItem) : () {
Expand All @@ -49,18 +49,18 @@ module {
price = newMenuItem.price;
stock = newMenuItem.stock;
description = newMenuItem.description;
star = [];
image = newMenuItem.image
score = [];
image = newMenuItem.image;
};

put(menuMap, itemId, newMenu);
return
return;
};

public func update(menuMap : MenuMap, menuId : Nat, newMenuItem : NewMenuItem) : Result.Result<Text, Text> {
switch (get(menuMap, menuId)) {
case (null) {
return #err("The menu item with id " #Nat.toText(menuId) # " does not exist!")
return #err("The menu item with id " #Nat.toText(menuId) # " does not exist!");
};
case (?item) {
let updateItem : MenuItem = {
Expand All @@ -70,14 +70,14 @@ module {
price = newMenuItem.price;
stock = newMenuItem.stock;
description = newMenuItem.description;
star = item.star;
image = newMenuItem.image
score = item.score;
image = newMenuItem.image;
};

put(menuMap, menuId, updateItem);
return #ok("The menu item with id " #Nat.toText(menuId) # " has been updated!");

}
};
};

};
Expand All @@ -86,49 +86,44 @@ module {
let menu = get(menuMap, menuId);
switch (menu) {
case (null) {
return false
return false;
};
case (?menu) {
for (star in menu.star.vals()) {
for (star in menu.score.vals()) {
if (star.pointBy == p) {
return true
}
}
return true;
};
};
};

};
return false
return false;
};

public func replaceMenuPointByPrincipal(menuMap : MenuMap, itemId : Nat, principal : Principal, newPoint : Review.MenuReview) : Bool {
switch (get(menuMap, itemId)) {
case (?item) {
public func replaceNewItemScore(menuItem : MenuItem, menuId : Nat, principal : Principal, newPoint : Review.MenuReview) : MenuItem {
switch (menuItem) {
case (item) {
// Filter out the specific MenuReview
let updatedPoints = Array.filter<Review.MenuReview>(
item.star,
func(star) {
star.pointBy != principal
item.score,
func(score) {
score.pointBy != principal;
},
);

// Add the new MenuReview
let newPoints = Array.append<Review.MenuReview>(updatedPoints, [newPoint]);

// Update the MenuItem with the new points array

let updatedItem : MenuItem = {
id = itemId;
id = menuId;
name = item.name;
price = item.price;
stock = item.stock;
description = item.description;
star = newPoints;
image = item.image
score = newPoints;
image = item.image;
};
put(menuMap, itemId, updatedItem);
return true
return updatedItem;
};
case null { return false }
}
}
}
};
};
};
58 changes: 11 additions & 47 deletions src/Dorder/Review.mo
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Time "mo:base/Time";
import Nat "mo:base/Nat";
import Time "mo:base/Time";

module {
public type MenuReview = {
Expand All @@ -9,58 +9,22 @@ module {
star : Star;
suggest : Bool;
cratedAt : Time.Time;
image : ?[Blob]
};

public type Star = {
#One;
#Two;
#Three;
#Four;
#Five
image : ?[Blob];
};

public type EmployeeReview = {
pointBy : Principal;
comment : ?Text;
star : Star;
cratedAt : Time.Time
cratedAt : Time.Time;
};

public type Point = Nat;

public type UserPoint = Point;

// let nextPointId : Nat = 0;
// public func addMenuPoint(menuMap : Menu.MenuMap, menuId : Nat, p : Principal, cm : ?Text, star : Star, suggest : Bool, image : [Blob]) : Result.Result<(Text), Text> {

// switch (M.get(menuMap, id)) {
// case (null) {
// return #err("The menu item with id " #Nat.toText(menuId) # " does not exist!");
// };
// case (?menu) {
// let newMenuPoint = Buffer.fromArray<MenuReview>(menu.star);
// newMenuPoint.add({
// comment = cm;
// pointBy = p;
// star = star;
// suggest = suggest;
// cratedAt = Time.now();
// image = [image];
// });

// nextPointId += 1;

// let newMenuPoint : M.MenuItem = {
// name = menu.name;
// price = menu.price;
// description = menu.description;
// star = Buffer.toArray(newMenuPoint);
// image = ?Blob;
// };
// };
// };

// };
public type Star = {
#One;
#Two;
#Three;
#Four;
#Five;
};

}
};
Loading

0 comments on commit 9731969

Please sign in to comment.