Skip to content

Commit

Permalink
chore: some refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
codenamejason committed Oct 29, 2023
1 parent aefa7c5 commit 18fa5d4
Show file tree
Hide file tree
Showing 8 changed files with 156 additions and 144 deletions.
121 changes: 27 additions & 94 deletions src/IVOccurrenceManager.sol
Original file line number Diff line number Diff line change
@@ -1,44 +1,29 @@
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity 0.8.20;

import { Occurrence } from "./library/Occurrence.sol";
import { Staff } from "./library/Staff.sol";
import { IVStaffManager } from "./IVStaffManager.sol";
import { Enums } from "./library/Enums.sol";
import { Metadata } from "./library/Metadata.sol";
import { Structs } from "./library/Structs.sol";

import { AccessControl } from "@openzeppelin/contracts/access/AccessControl.sol";
// import { AccessControl } from "@openzeppelin/contracts/access/AccessControl.sol";

/**
* @title IVOccurrenceManager
* @notice The IVOccurrenceManager contract is responsible for managing the occurrences
* @dev We use the term occurrence to describe an event, appointment, or any other type of gathering.
* @author @codenamejason <[email protected]>
*/
contract IVOccurrenceManager is AccessControl {
contract IVOccurrenceManager is IVStaffManager {
bytes32 public constant CREATOR_ROLE = keccak256("CREATOR_ROLE");
bytes32 public constant STAFF_ROLE = keccak256("STAFF_ROLE");

mapping(address => Staff) public staff;
mapping(bytes32 => Occurrence) public occurrences;
mapping(bytes32 => Structs.Occurrence) public occurrences;

modifier onlyCreator() {
require(hasRole(CREATOR_ROLE, msg.sender), "IVOccurrenceManager: caller is not a creator");
_;
}

modifier onlyStaff() {
require(hasRole(STAFF_ROLE, msg.sender), "IVOccurrenceManager: caller is not a staff");
_;
}

modifier onlyAdmin() {
require(
hasRole(DEFAULT_ADMIN_ROLE, msg.sender), "IVOccurrenceManager: caller is not an admin"
);
_;
}

constructor(address _defaultAdmin) {
constructor(address _defaultAdmin) IVStaffManager(_defaultAdmin) {
_grantRole(DEFAULT_ADMIN_ROLE, _defaultAdmin);
}

Expand All @@ -62,9 +47,9 @@ contract IVOccurrenceManager is AccessControl {
uint256 _price,
address _token,
address[] memory _staff,
Metadata memory _metadata
Structs.Metadata memory _metadata
) external returns (bytes32) {
Occurrence memory _occurenceId = Occurrence({
Structs.Occurrence memory _occurenceId = Structs.Occurrence({
id: keccak256(abi.encodePacked(_name, _start, _end)),
creator: msg.sender,
name: _name,
Expand Down Expand Up @@ -92,14 +77,14 @@ contract IVOccurrenceManager is AccessControl {
uint256 _price,
address _token,
address[] memory _staff,
Metadata memory _metadata
Structs.Metadata memory _metadata
) external onlyCreator {
require(
occurrences[_occurenceIdId].id == _occurenceIdId,
"IVOccurrenceManager: occurrence does not exist"
);

Occurrence memory _occurenceId = Occurrence({
Structs.Occurrence memory _occurenceId = Structs.Occurrence({
id: keccak256(abi.encodePacked(_name, _start, _end)),
creator: msg.sender,
name: _name,
Expand All @@ -116,83 +101,23 @@ contract IVOccurrenceManager is AccessControl {
occurrences[_occurenceId.id] = _occurenceId;
}

function addStaffMember(
address _member,
// uint256[] memory _levels,
Metadata memory _metadata
) external onlyCreator {
Staff memory _staff = Staff({
id: keccak256(abi.encodePacked(_member)),
member: _member,
metadata: _metadata,
// levels: _levels,
status: Enums.Status.Pending
});

_grantRole(STAFF_ROLE, _member);

// for (uint256 i = 0; i < _levels.length; i++) {
// _staff.levels[_member].push(_levels[i]);
// }

staff[_staff.member] = _staff;
}

function updateStaffMember(
address _member,
// uint256[] memory _levels,
Metadata memory _metadata
) external onlyCreator {
Staff memory _staff = Staff({
id: keccak256(abi.encodePacked(_member)),
member: _member,
metadata: _metadata,
// levels: _levels,
status: Enums.Status.Pending
});

// for (uint256 i = 0; i < _levels.length; i++) {
// _staff.levels[_member].push(_levels[i]);
// }

staff[_staff.member] = _staff;
}

function updateStaffMemberStatus(address _member, Enums.Status _status) external onlyCreator {
Staff memory _staff = staff[_member];
_staff.status = _status;

staff[_staff.member] = _staff;
}

function updateOccurrenceStatus(bytes32 _occurenceIdId, Enums.Status _status)
function getOccurrence(bytes32 _occurenceIdId)
external
onlyCreator
view
returns (Structs.Occurrence memory)
{
require(
occurrences[_occurenceIdId].id == _occurenceIdId,
"IVOccurrenceManager: occurrence does not exist"
);

Occurrence memory _occurenceId = occurrences[_occurenceIdId];
_occurenceId.status = _status;

occurrences[_occurenceId.id] = _occurenceId;
}

function getOccurrence(bytes32 _occurenceIdId) external view returns (Occurrence memory) {
require(
occurrences[_occurenceIdId].id == _occurenceIdId,
"IVOccurrenceManager: occurrence does not exist"
);

return occurrences[_occurenceIdId];
}

function getStaffMember(bytes32 _occurenceIdId, address _member)
external
view
returns (Staff memory)
returns (Structs.Staff memory)
{
require(
occurrences[_occurenceIdId].id == _occurenceIdId,
Expand All @@ -202,14 +127,18 @@ contract IVOccurrenceManager is AccessControl {
return staff[_member];
}

function getStaffMembers(bytes32 _occurenceIdId) external view returns (Staff[] memory) {
function getStaffMembers(bytes32 _occurenceIdId)
external
view
returns (Structs.Staff[] memory)
{
require(
occurrences[_occurenceIdId].id == _occurenceIdId,
"IVOccurrenceManager: occurrence does not exist"
);

Occurrence memory _occurenceId = occurrences[_occurenceIdId];
Staff[] memory _staff = new Staff[](_occurenceId.staff.length);
Structs.Occurrence memory _occurenceId = occurrences[_occurenceIdId];
Structs.Staff[] memory _staff = new Structs.Staff[](_occurenceId.staff.length);

for (uint256 i = 0; i < _occurenceId.staff.length; i++) {
_staff[i] = staff[_occurenceId.staff[i]];
Expand All @@ -218,11 +147,15 @@ contract IVOccurrenceManager is AccessControl {
return _staff;
}

function getOccurrences() external view returns (Occurrence[] memory) {
function getOccurrences() external view returns (Structs.Occurrence[] memory) {
// TODO:
}

function getOccurrenceById(bytes32 _occurenceIdId) external view returns (Occurrence memory) {
function getOccurrenceById(bytes32 _occurenceIdId)
external
view
returns (Structs.Occurrence memory)
{
return occurrences[_occurenceIdId];
}
}
78 changes: 78 additions & 0 deletions src/IVStaffManager.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity 0.8.20;

import { Enums } from "./library/Enums.sol";
import { Structs } from "../src/library/Structs.sol";

import { AccessControl } from "@openzeppelin/contracts/access/AccessControl.sol";

contract IVStaffManager is AccessControl {
bytes32 public constant STAFF_ROLE = keccak256("STAFF_ROLE");

mapping(address => Structs.Staff) public staff;

modifier onlyAdmin() {
require(
hasRole(DEFAULT_ADMIN_ROLE, msg.sender), "IVOccurrenceManager: caller is not an admin"
);
_;
}

modifier onlyStaff() {
require(hasRole(STAFF_ROLE, msg.sender), "IVOccurrenceManager: caller is not a staff");
_;
}

constructor(address _defaultAdmin) {
_grantRole(DEFAULT_ADMIN_ROLE, _defaultAdmin);
}

function addStaffMember(
address _member,
// uint256[] memory _levels,
Structs.Metadata memory _metadata
) external onlyAdmin {
Structs.Staff memory _staff = Structs.Staff({
id: keccak256(abi.encodePacked(_member)),
member: _member,
metadata: _metadata,
// levels: _levels,
status: Enums.Status.Pending
});

_grantRole(STAFF_ROLE, _member);

// for (uint256 i = 0; i < _levels.length; i++) {
// _staff.levels[_member].push(_levels[i]);
// }

staff[_staff.member] = _staff;
}

function updateStaffMember(
address _member,
// uint256[] memory _levels,
Structs.Metadata memory _metadata
) external onlyAdmin {
Structs.Staff memory _staff = Structs.Staff({
id: keccak256(abi.encodePacked(_member)),
member: _member,
metadata: _metadata,
// levels: _levels,
status: Enums.Status.Pending
});

// for (uint256 i = 0; i < _levels.length; i++) {
// _staff.levels[_member].push(_levels[i]);
// }

staff[_staff.member] = _staff;
}

function updateStaffMemberStatus(address _member, Enums.Status _status) external onlyAdmin {
Structs.Staff memory _staff = staff[_member];
_staff.status = _status;

staff[_staff.member] = _staff;
}
}
12 changes: 0 additions & 12 deletions src/library/Metadata.sol

This file was deleted.

20 changes: 0 additions & 20 deletions src/library/Occurrence.sol

This file was deleted.

11 changes: 11 additions & 0 deletions src/library/Recover.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity 0.8.20;

import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol";

contract Recover {
function recoverERC20(address _token) external {
uint256 balance = IERC20(_token).balanceOf(address(this));
IERC20(_token).transfer(msg.sender, balance);
}
}
15 changes: 0 additions & 15 deletions src/library/Staff.sol

This file was deleted.

38 changes: 38 additions & 0 deletions src/library/Structs.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity 0.8.20;

import { Enums } from "./Enums.sol";

library Structs {
struct Metadata {
/// @notice Protocol ID corresponding to a specific protocol (currently using IPFS = 1)
uint256 protocol;
/// @notice Pointer (hash) to fetch metadata for the specified protocol
string pointer;
}

struct Staff {
// keccak256(abi.encodePacked(_eventId, _member)),
bytes32 id;
address member;
Metadata metadata;
// member address to their level (the user can have multiple levels and use how they want)
// uint256[] levels;
Enums.Status status;
}

struct Occurrence {
// keccak256(abi.encodePacked(_name, _start, _end))
bytes32 id;
address creator;
string name;
string description;
uint256 start;
uint256 end;
uint256 price;
address token;
Enums.Status status;
address[] staff;
Metadata metadata;
}
}
Loading

0 comments on commit 18fa5d4

Please sign in to comment.