Skip to content

Commit

Permalink
add controller code
Browse files Browse the repository at this point in the history
  • Loading branch information
jiangplus committed Sep 6, 2024
1 parent 6d8a882 commit aea0dde
Show file tree
Hide file tree
Showing 22 changed files with 492 additions and 307 deletions.
92 changes: 12 additions & 80 deletions app/controllers/api/badge_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def transfer
authorize badge, :own?

# need test
raise AppError.new("invalid state") unless badge.status == "accepted"
raise AppError.new("invalid state") unless badge.status == "minted"
raise AppError.new("invalid badge_type") if badge.badge_class.transferable
raise AppError.new("invalid target id") if target.nil? || profile.id == target.id

Expand All @@ -34,32 +34,12 @@ def transfer
render json: { result: "ok" }
end

def consume
profile = current_profile!

badge = Badge.find(params[:badge_id])
badge_class = badge.badge_class

authorize badge, :own?

# need test
raise AppError.new("invalid gift") unless badge_class.weighted
raise AppError.new("invalid value") unless params[:delta].to_i > 0
raise AppError.new("invalid badge value") unless badge.value >= params[:delta].to_i

badge.decrement!(:value, params[:delta].to_i)
badge.touch(:last_value_used_at)
activity = Activity.create(item: badge, initiator_id: profile.id, action: "badge/consume")

render json: { badge: badge }
end

def burn
profile = current_profile!

badge = Badge.find(params[:badge_id])
authorize badge, :own?
raise AppError.new("invalid state") unless badge.status == "accepted"
raise AppError.new("invalid state") unless badge.status == "minted"

badge.update(status: "burned")
activity = Activity.create(item: badge, initiator_id: profile.id, action: "badge/burn")
Expand All @@ -73,11 +53,11 @@ def swap_code
badge = Badge.find(params[:badge_id])
authorize badge, :own?

payload = {
badge_id: badge.id,
auth_type: "swap"
}
token = JWT.encode payload, $hmac_secret, "HS256"
# need test
raise AppError.new("invalid state") unless badge.status == "minted"
raise AppError.new("invalid badge_type") if badge.badge_class.transferable

token = badge.gen_swap_code
activity = Activity.create(item: badge, initiator_id: profile.id, action: "badge/swap_code")

render json: { result: "ok", token: token, badge_id: badge.id }
Expand All @@ -87,69 +67,21 @@ def swap
profile = current_profile!
badge = Badge.find(params[:badge_id])
authorize badge, :own?
swap_token = params[:swap_token]

decoded_token = JWT.decode swap_token, $hmac_secret, true, { algorithm: "HS256" }
target_badge_id = decoded_token[0]["badge_id"]
Badge.decode_swap_code(params[:swap_token])
target_badge = Badge.find(target_badge_id)
target_badge_owner_id = target_badge.owner_id

raise AppError.new("invalid state") unless target_badge.status == "minted"
raise AppError.new("invalid badge_type") if target_badge.badge_class.transferable

badge.update(owner_id: target_badge_owner_id)
target_badge.update(owner_id: profile.id)
activity = Activity.create(item: badge, initiator_id: profile.id, action: "badge/swap_code")

render json: { result: "ok" }
end

def wamo_go_merge
profile = current_profile!
badges = Badge.where(id: params[:badge_ids])
color = params[:color]
required_badge_count = 2
ids = badges.ids
unless (ids.uniq.count == params[:badge_ids].count) && (ids.uniq.count == required_badge_count)
raise AppError.new("invalid badge for merge")
end
badges.each do |badge|
authorize badge, :own?
unless badge.badge_class.permissions.include?("wamo-normal")
raise AppError.new("invalid badge state for merge: require wamo-normal permission")
end

unless JSON.parse(badge.badge_class.metadata || "{}")["color"] == color
raise AppError.new("invalid badge state for merge: color empty")
end

unless JSON.parse(badge.metadata || "{}")["merged"].blank?
raise AppError.new("invalid badge state for merge: has merged")
end
end

# todo : randomly select from colored merger badges
badge_class = BadgeClass.find_by(id: params[:new_badge_id])
raise AppError.new("invalid target badge for merge") unless badge_class.permissions.include?("wamo-merge")

badge = Badge.new(
index: badge_class.counter,
content: badge_class.content,
status: "accepted",
badge_class_id: badge_class.id,
creator_id: badge_class.creator.id,
owner_id: profile.id,
title: badge_class.title,
image_url: badge_class.image_url,
)

badge.save
badge_class.increment!(:counter)

badges.each do |badge|
badge.status = "burned"
badge.metadata = JSON.dump({ merged: badge_class.id })
badge.save
activity = Activity.create(item: badge, initiator_id: profile.id, action: "badge/burn")
end

render json: { result: "ok", badge_id: badge.id }
def merge
end
end
46 changes: 38 additions & 8 deletions app/controllers/api/event_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ def create
raise AppError.new("group is empty")
end

# todo : allow group setting for pending event

# todo : move badge_class to voucher
if params[:badge_class_id]
badge_class = BadgeClass.find(params[:badge_class_id])
Expand All @@ -29,9 +31,13 @@ def create
owner: profile,
group: group,
display: "normal",
event_type: "event", # todo : could be "group_ticket"
event_type: params[:event_type] || "event", # todo : could be "group_ticket"
)

if params[:event_type] == 'group_ticket'

Check failure on line 37 in app/controllers/api/event_controller.rb

View workflow job for this annotation

GitHub Actions / lint

Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.
group.update(group_ticket_event_id: event.id)
end

group.increment!(:events_count) if group

if @send_approval_email_to_manager && ENV["DO_NOT_SEND_EMAIL"].blank?
Expand All @@ -47,11 +53,31 @@ def create
render json: { result: "ok", event: event.as_json }
end

def set_badge
profile = current_profile!
event = Event.find(params[:id])
badge_class = BadgeClass.find(params[:badge_class_id])
authorize event, :update?
authorize badge_class, :send?

voucher = Voucher.new(
sender: profile,
badge_class: badge_class,
item_type: "Event", item_id: event.id,
# need test
strategy: "event",
counter: 1,
)
voucher.save

render json: { result: "ok", event: event.as_json }
end

def send_badge
profile = current_profile!
event = Event.find(params[:id])
badge_class = event.badge_class
raise AppError.new("event badge_class not set") unless badge_class
voucher = Voucher.find_by(item_type: "Event", item_id: event.id)
raise AppError.new("event voucher not set") unless voucher

authorize event, :update?

Expand Down Expand Up @@ -85,7 +111,7 @@ def update
event = Event.find(params[:id])
authorize event, :update?

if params[:event][:venue_id] && params[:event][:venue_id] != event.venue_id
if params[:event][:venue_id] != event.venue_id
venue = Venue.find_by(id: params[:venue_id], group_id: group.id)
raise AppError.new("group venue not exists") unless venue

Expand Down Expand Up @@ -192,17 +218,21 @@ def join
profile: profile,
event: event,
status: status,
register_time: DateTime.now,
)
else
participant.status = status
participant.register_time = DateTime.now
end

participant.save

event.increment!(:participants_count)

# if profile.email.present?
# recipient = profile.email
# event.send_mail_new_event(recipient)
# end
if profile.email.present?
recipient = profile.email
event.send_mail_new_event(recipient)
end

render json: { participant: participant.as_json }
end
Expand Down
10 changes: 5 additions & 5 deletions app/controllers/api/group_invite_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def request_invite
return render json: { receiver_id: receiver_id, result: "error", message: "membership exists" }
end

if GroupInvite.find_by(receiver_id: profile.id, group_id: group.id, role: params[:role])
if GroupInvite.find_by(receiver_id: profile.id, group_id: group.id, role: params[:role], status: "requesting")
return render json: { receiver_id: receiver_id, result: "error", message: "group invite exists" }
end

Expand All @@ -28,11 +28,10 @@ def accept_request
group = Group.find(group_invite.group_id)
authorize group, :manage?, policy_class: GroupPolicy

raise AppError.new("invite has been accepted") if group_invite.status == "accepted"
raise AppError.new("invalid status") unless group_invite.status == "requesting"
raise AppError.new("invite expired") unless DateTime.now < group_invite.expires_at

unless group.is_owner(profile.id) && [ "member", "issuer", "manager" ].include?(group_invite.role) || [ "member", "issuer" ].include?(group_invite.role)
unless group.is_owner(profile.id) && [ "member", "operator", "manager" ].include?(group_invite.role) || [ "member", "operator" ].include?(group_invite.role)
raise AppError.new("invalid role")
end

Expand All @@ -54,6 +53,7 @@ def send_invite
group = Group.find(params[:group_id])
authorize group, :manage?, policy_class: GroupPolicy
role = params[:role]
expires_at = (DateTime.now + 30.days)

group_invites = []
params[:receivers].map do |receiver|
Expand All @@ -74,7 +74,7 @@ def send_invite
group_id: group.id,
message: params[:message],
role: role,
expires_at: (DateTime.now + 30.days),
expires_at: expires_at,
receiver_id: receiver_id,
)
activity = Activity.create(item: invite, initiator_id: profile.id, action: "group_invite/send", receiver_type: "id", receiver_id: receiver.id)
Expand All @@ -91,7 +91,7 @@ def send_invite
group_id: group.id,
message: params[:message],
role: role,
expires_at: (DateTime.now + 30.days),
expires_at: expires_at,
receiver_address_type: "email",
receiver_address: receiver,
)
Expand Down
19 changes: 19 additions & 0 deletions app/controllers/api/makrer_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,25 @@ def remove
render json: { result: "ok" }
end

def checkin
profile = current_profile!
marker = Marker.find(params[:id])
authorize marker, :update?

comment = Comment.create(
item: marker,
comment_type: 'checkin',

Check failure on line 42 in app/controllers/api/makrer_controller.rb

View workflow job for this annotation

GitHub Actions / lint

Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.
profile: profile,
title: params[:title],
content: params[:content],
content_type: params[:content_type],
reply_parent_id: params[:reply_parent_id],
icon_url: params[:icon_url],
)

render json: { result: "ok", comment: comment.as_json }
end

private

def marker_params
Expand Down
44 changes: 22 additions & 22 deletions app/controllers/api/point_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ def create
target = receiver_value[:receiver]
value = receiver_value[:value]
receiver = Profile.find_by(address: target) || Profile.find_by(handle: target)
point_item = PointTransfer.create(
point_transfer = PointTransfer.create(
point_class_id: point_class.id,
value: value,
sender_id: profile.id,
receiver_id: receiver.id
)
activity = Activity.create(item: point_item, initiator_id: profile.id, action: "point/send", receiver_type: "id", receiver_id: receiver.id, data: point_item.value.to_s)
activity = Activity.create(item: point_transfer, initiator_id: profile.id, action: "point/send", receiver_type: "id", receiver_id: receiver.id, data: point_transfer.value.to_s)

point_item
point_transfer
end

render json: { result: "ok", point_items: point_items.as_json }
Expand All @@ -31,22 +31,22 @@ def create
def accept
profile = current_profile!

point_item = PointItem.find(params[:point_item_id])
point_transfer = PointTransfer.find(params[:point_transfer_id])

raise AppError.new("access denied") unless point_item.owner_id == profile.id
raise AppError.new("invalid state") unless point_item.status == "pending"
raise AppError.new("access denied") unless point_transfer.receiver_id == profile.id
raise AppError.new("invalid state") unless point_transfer.status == "pending"

point_class = point_item.point_class
point_class.increment!(:total_supply, point_item.value)
point = PointBalance.find_by(point_class_id: point_item.point_class_id, owner_id: profile.id)
point_class = point_transfer.point_class
point_class.increment!(:total_supply, point_transfer.value)
point = PointBalance.find_by(point_class_id: point_transfer.point_class_id, owner_id: profile.id)
if point
point.increment!(:value, point_item.value)
point.increment!(:value, point_transfer.value)
else
point = PointBalance.create(point_class_id: point_item.point_class_id, creator_id: point_class.creator_id, owner_id: point_item.owner_id, value: point_item.value)
point = PointBalance.create(point_class_id: point_transfer.point_class_id, creator_id: point_class.creator_id, owner_id: point_transfer.receiver_id, value: point_transfer.value)
end
point_item.update(status: "accepted")
activity = Activity.create(item: point_item, initiator_id: profile.id, action: "point/accept", data: point_item.value.to_s)
render json: { result: "ok", point_item: point_item.as_json }
point_transfer.update(status: "accepted")
activity = Activity.create(item: point_transfer, initiator_id: profile.id, action: "point/accept", data: point_transfer.value.to_s)
render json: { result: "ok", point_transfer: point_transfer.as_json }
end

def transfer
Expand All @@ -63,19 +63,19 @@ def transfer
source_point.decrement!(:value, params[:amount].to_i)
point = PointBalance.create(point_class_id: source_point.point_class_id, creator_id: source_point.creator_id, owner_id: params[:target_profile_id], value: params[:amount])
end
point_item = PointItem.create(point_class_id: point.point_class_id, sender_id: source_point.owner_id, owner_id: params[:target_profile_id], value: params[:amount].to_i, status: "transfered")
render json: { result: "ok", point_item: point_item.as_json }
point_transfer = PointTransfer.create(point_class_id: point.point_class_id, sender_id: source_point.owner_id, owner_id: params[:target_profile_id], value: params[:amount].to_i, status: "transfered")
render json: { result: "ok", point_transfer: point_transfer.as_json }
end

def reject
profile = current_profile!

point_item = PointItem.find(params[:point_item_id])
raise AppError.new("access denied") unless point_item.owner_id == profile.id
raise AppError.new("invalid state") unless point_item.status == "pending"
point_transfer = PointTransfer.find(params[:point_transfer_id])
raise AppError.new("access denied") unless point_transfer.owner_id == profile.id
raise AppError.new("invalid state") unless point_transfer.status == "pending"

point_item.update(status: "rejected")
activity = Activity.create(item: point_item, initiator_id: profile.id, action: "point/reject")
render json: { result: "ok", point_item: point_item.as_json }
point_transfer.update(status: "rejected")
activity = Activity.create(item: point_transfer, initiator_id: profile.id, action: "point/reject")
render json: { result: "ok", point_transfer: point_transfer.as_json }
end
end
Loading

0 comments on commit aea0dde

Please sign in to comment.