Skip to content

Commit

Permalink
refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
jiangplus committed Sep 12, 2024
1 parent 0f8a2fb commit 6cc8925
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 62 deletions.
20 changes: 6 additions & 14 deletions app/controllers/api/event_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,7 @@ def update

if @send_update_email
event.participants.each do |participant|
if participant.profile.email
recipient = participant.profile.email
event.send_mail_update_event(recipient)
end
participant.profile.send_mail_update_event(event)
end
end

Expand All @@ -150,19 +147,20 @@ def unpublish
event.group.decrement!(:events_count)

event.participants.each do |participant|
participant.email_notify!(:cancel)
participant.profile.send_mail_cancel_event(event)
end

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

# todo test
def check_group_permission
profile = current_profile!
event = Event.find(params[:id])
group = event.group
tz = group.timezone

if !group.group_ticket_enabled
if !group.group_ticket_event_id
return render json: { result: "ok", check: true, message: "action allowed" }
end

Expand Down Expand Up @@ -229,10 +227,7 @@ def join

event.increment!(:participants_count)

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

render json: { participant: participant.as_json }
end
Expand Down Expand Up @@ -261,10 +256,7 @@ def cancel
participant.update(status: "cancelled")
event.decrement!(:participants_count)

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

render json: { participant: participant.as_json }
end
Expand Down
3 changes: 1 addition & 2 deletions app/controllers/api/group_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ def create
Domain.create(handle: handle, fullname: "#{handle}.sola.day", item_type: "Group", item_id: group.id)
end

Membership.create(profile_id: profile.id, group_id: group.id, role: "owner", status: "active")
group.increment!(:memberships_count)
group.add_member(profile.id, "owner")
render json: { result: "ok", group: group }
end

Expand Down
18 changes: 4 additions & 14 deletions app/controllers/api/group_invite_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,7 @@ def accept_request
end

group_invite.update(status: "accepted")
membership = Membership.find_by(profile_id: group_invite.receiver_id, group_id: group.id)
if membership
membership.update(role: group_invite.role)
else
membership = Membership.create(profile_id: group_invite.receiver_id, group_id: group.id, role: group_invite.role, status: "active")
group.increment!(:memberships_count)
end
membership = group.add_member(group_invite.receiver_id, group_invite.role)

render json: { result: "ok", membership: membership.as_json }
end
Expand All @@ -63,8 +57,9 @@ def send_invite
receiver_id = receiver.id

membership = Membership.find_by(profile_id: receiver.id, group_id: group.id)
if membership && membership.role == "member"
if membership && membership.role == "member" && role != "member"
membership.update(role: role)
activity = Activity.create(initiator_id: profile.id, action: "group_invite/update_role", receiver_type: "id", receiver_id: receiver.id)
invite = { receiver_id: receiver_id, result: "ok", message: "membership updated" }
elsif membership
invite = { receiver_id: receiver_id, result: "error", message: "membership exists" }
Expand Down Expand Up @@ -118,12 +113,7 @@ def accept_invite
group_invite.update(status: "accepted")
raise AppError.new("invite expired") unless DateTime.now < group_invite.expires_at

if Membership.find_by(profile_id: profile.id, group_id: group.id)
raise AppError.new("membership exists")
return
end
Membership.create(profile_id: profile.id, group_id: group.id, role: group_invite.role, status: "active")
group.increment!(:memberships_count)
group.add_member(profile.id, group_invite.role)
render json: { result: "ok" }
end

Expand Down
5 changes: 1 addition & 4 deletions app/controllers/api/profile_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,7 @@ def verify
data = RestClient.get("https://sola.deno.dev/seedao/getname/#{address}")
domain = JSON.parse(data.body)["domain"]
if domain.present? && seedao_group
membership = Membership.find_by(profile_id: profile.id, target_id: seedao_group.id)
if !membership
Membership.create(profile_id: profile.id, target_id: seedao_group.id, role: "member", status: "normal")
end
seedao_group.add_member(profile.id, "member")
end
end
render json: { result: "ok", auth_token: profile.gen_auth_token, address: address, id: profile.id }
Expand Down
22 changes: 0 additions & 22 deletions app/models/event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,26 +62,4 @@ def location_url
self.geo_lat.present? ? "https://www.google.com/maps/search/?api=1&query=#{self.geo_lat}%2C#{self.geo_lng}" : ""
end

### legacy


def send_mail_new_event(recipient)
mailer = EventMailer.with(event: self, recipient: recipient).event_created
mailer.deliver_now!
end

def send_mail_event_invite(recipient)
mailer = EventMailer.with(event: self, recipient: recipient).event_invited
mailer.deliver_now!
end

def send_mail_update_event(recipient)
mailer = EventMailer.with(event: self, recipient: recipient).event_updated
mailer.deliver_now!
end

def send_mail_cancel_event(recipient)
mailer = EventMailer.with(event: self, recipient: recipient).event_updated
mailer.deliver_now!
end
end
19 changes: 15 additions & 4 deletions app/models/group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,30 @@ def get_owner
end

def is_owner(profile_id)
Membership.find_by(profile_id: profile_id, group_id: id, role: 'owner')
Membership.find_by(profile_id: profile_id, group_id: id, role: 'owner', status: "active")
end

def is_manager(profile_id)
Membership.find_by(profile_id: profile_id, group_id: id, role: %w[manager owner])
Membership.find_by(profile_id: profile_id, group_id: id, role: %w[manager owner], status: "active")
end

def is_operator(profile_id)
Membership.find_by(profile_id: profile_id, group_id: id, role: %w[operator manager owner])
Membership.find_by(profile_id: profile_id, group_id: id, role: %w[operator manager owner], status: "active")
end

def is_member(profile_id)
Membership.find_by(profile_id: profile_id, group_id: id, role: %w[member operator manager owner])
Membership.find_by(profile_id: profile_id, group_id: id, role: %w[member operator manager owner], status: "active")
end

def add_member(profile_id, role)
membership = Membership.find_by(profile_id: profile_id, group_id: self.id)
if membership
membership.update(role: role)
else
membership = Membership.create(profile_id: profile_id, group_id: id, role: role, status: "active")
self.increment!(:memberships_count)
end
membership
end

def display_name
Expand Down
28 changes: 28 additions & 0 deletions app/models/profile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,32 @@ def gen_auth_token
}
auth_token = JWT.encode payload, $hmac_secret, "HS256"
end

def send_mail_new_event(event)
if self.email.present?
mailer = EventMailer.with(event: event, recipient: self.email).event_created
mailer.deliver_now!
end
end

def send_mail_event_invite(event)
if self.email.present?
mailer = EventMailer.with(event: event, recipient: self.email).event_invited
mailer.deliver_now!
end
end

def send_mail_update_event(event)
if self.email.present?
mailer = EventMailer.with(event: event, recipient: self.email).event_updated
mailer.deliver_now!
end
end

def send_mail_cancel_event(event)
if self.email.present?
mailer = EventMailer.with(event: event, recipient: self.email).event_updated
mailer.deliver_now!
end
end
end
3 changes: 1 addition & 2 deletions test/controllers/api/event_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ class Api::EventControllerTest < ActionDispatch::IntegrationTest
profile = Profile.find_by(handle: "cookie")
auth_token = profile.gen_auth_token

get api_event_list_url, params: { auth_token: auth_token }
get api_event_list_url, params: { auth_token: auth_token, group_id: 1 }
assert_response :success

response_events = JSON.parse(response.body)
Expand All @@ -245,7 +245,6 @@ class Api::EventControllerTest < ActionDispatch::IntegrationTest
auth_token = profile.gen_auth_token

get api_event_private_list_url, params: { auth_token: auth_token }
p response.body
assert_response :success

response_events = JSON.parse(response.body)
Expand Down

0 comments on commit 6cc8925

Please sign in to comment.