Skip to content

Commit

Permalink
add group track api
Browse files Browse the repository at this point in the history
  • Loading branch information
jiangplus committed Sep 12, 2024
1 parent 6cc8925 commit 55ceda6
Show file tree
Hide file tree
Showing 11 changed files with 131 additions and 64 deletions.
21 changes: 19 additions & 2 deletions app/controllers/api/group_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@ def update
render json: { result: "ok", group: group }
end

def update_track
profile = current_profile!
track = Track.find(params[:track_id])
authorize track.group, :manage?, policy_class: GroupPolicy

track.update(track_params)
render json: { result: "ok", track: track }
end

def transfer_owner
profile = current_profile!
group = Group.find(params[:id])
Expand Down Expand Up @@ -153,10 +162,18 @@ def leave

def group_params
params.require(:group).permit(
:chain, :image_url, :nickname, :about, :parent_id, :status, :group_ticket_enabled,
:chain, :image_url, :nickname, :about, :status, :group_ticket_enabled,
:tags, :event_taglist, :venue_taglist, :can_publish_event, :can_join_event, :can_view_event,
:customizer, :logo_url, :banner_link_url, :banner_image_url,
:timezone, :location, :metadata, :social_links,
)
tracks_attributes: [ :id, :tag, :title, :kind, :icon_url, :about, :start_date, :end_date, :_destroy ],
)
end

def track_params
params.require(:track).permit(
:tag, :title, :kind, :icon_url, :about, :start_date, :end_date,
track_roles_attributes: [ :id, :role, :receiver_address, :profile_id, :_destroy ],
)
end
end
61 changes: 0 additions & 61 deletions app/mailers/event_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,65 +28,4 @@ def event_invited()
mail(to: [@recipient], subject: subject)
end

# def update_event
# ev = Event.find(params[:event_id])
# cal = Icalendar::Calendar.new
# cal.event do |e|
# e.dtstart = Icalendar::Values::DateTime.new(ev.start_time.in_time_zone(ev.timezone))
# e.dtend = Icalendar::Values::DateTime.new(ev.end_time.in_time_zone(ev.timezone))
# e.summary = ev.title || ""
# e.description = ev.content || ""
# e.uid = "sola-#{ev.id}"
# e.status = "CONFIRMED"
# e.organizer = Icalendar::Values::CalAddress.new("mailto:[email protected]", cn: params[:group_name])
# e.attendee = ["mailto:#{params[:recipient]}"]
# e.url = "https://app.sola.day/event/detail/#{ev.id}"
# e.location = "https://app.sola.day/event/detail/#{ev.id}"
# end

# ics = cal.to_ical
# attachments['invite.ics'] = {:mime_type => 'text/calendar', :content => ics}

# @recipient = params[:recipient]
# @email_title = params[:email_title]
# @event_title = params[:event_title]
# @event_group_url = params[:group_url]
# @event_group_name = params[:group_name]
# @event_timeinfo = params[:timeinfo]
# @event_location = params[:location]
# @event_location_url = params[:location_url]
# @event_url = params[:url]
# @event_object = ev
# mail(to: [@recipient], subject: params[:subject])
# end

# def event_invite
# ev = Event.find(params[:event_id])
# cal = Icalendar::Calendar.new
# cal.event do |e|
# e.dtstart = Icalendar::Values::DateTime.new(ev.start_time)
# e.dtend = Icalendar::Values::DateTime.new(ev.end_time)
# e.summary = ev.title || ""
# e.description = ev.content || ""
# e.uid = "sola-#{ev.id}"
# e.status = "CONFIRMED"
# e.organizer = Icalendar::Values::CalAddress.new("mailto:[email protected]", cn: params[:group_name])
# e.attendee = ["mailto:#{params[:recipient]}"]
# e.url = "https://app.sola.day/event/detail/#{ev.id}"
# e.location = "https://app.sola.day/event/detail/#{ev.id}"
# end

# ics = cal.to_ical
# attachments['invite.ics'] = {:mime_type => 'text/calendar', :content => ics}

# @recipient = params[:recipient]
# @event_title = params[:title]
# @event_group_url = params[:group_url]
# @event_group_name = params[:group_name]
# @event_timeinfo = params[:timeinfo]
# @event_location = params[:location]
# @event_location_url = params[:location_url]
# @event_url = params[:url]
# mail(to: [@recipient], subject: 'Social Layer Event Invite')
# end
end
1 change: 1 addition & 0 deletions app/models/event.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
class Event < ApplicationRecord
belongs_to :owner, class_name: "Profile", foreign_key: "owner_id"
belongs_to :group, optional: true
belongs_to :track, optional: true
belongs_to :venue, optional: true
belongs_to :badge_class, optional: true
belongs_to :recurring, optional: true
Expand Down
2 changes: 2 additions & 0 deletions app/models/track.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
class Track < ApplicationRecord
belongs_to :group
has_many :track_roles, dependent: :delete_all

accepts_nested_attributes_for :track_roles, allow_destroy: true
validates :kind, inclusion: { in: %w(public private) }
end
5 changes: 5 additions & 0 deletions app/models/track_role.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class TrackRole < ApplicationRecord
belongs_to :track
belongs_to :profile
validates :role, inclusion: { in: %w(member manager) }
end
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
post "group/add_operator", to: "group#add_operator"
post "group/leave", to: "group#leave"

post "group/update_track", to: "group#update_track"

post "group/send_invite", to: "group_invite#send_invite"
post "group/accept_invite", to: "group_invite#accept_invite"
post "group/cancel_invite", to: "group_invite#cancel_invite"
Expand Down
13 changes: 13 additions & 0 deletions db/migrate/20240912110911_create_track_roles.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class CreateTrackRoles < ActiveRecord::Migration[7.2]
def change
create_table :track_roles do |t|
t.integer "track_id"
t.integer "profile_id"
t.string "receiver_address"
t.string "role", default: "member"
t.timestamps
end

add_column :events, :track_id, :integer
end
end
12 changes: 11 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

60 changes: 60 additions & 0 deletions test/controllers/api/group_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,66 @@ class Api::GroupControllerTest < ActionDispatch::IntegrationTest
assert Domain.find_by(handle: "newworld", item_type: "Group", item_id: group.id).present?
end

test "api#group/create with track" do
profile = Profile.find_by(handle: "cookie")
auth_token = profile.gen_auth_token

post api_group_create_url,
params: { auth_token: auth_token, handle: "newworld", group: {
timezone: "asia/shanghai",
can_publish_event: "all",
can_join_event: "all",
can_view_event: "all",
tracks_attributes: [
{ tag: "track1", title: "Track 1", kind: "public", icon_url: "https://example.com/icon1.png", about: "About Track 1", start_date: "2024-01-01", end_date: "2024-12-31" }
]
} }
assert_response :success
group = Group.find_by(handle: "newworld")
assert group
assert group.active?
assert group.is_owner(profile.id)
assert group.memberships_count == 1
assert Domain.find_by(handle: "newworld", item_type: "Group", item_id: group.id).present?
assert group.tracks.count == 1
track = group.tracks.first
assert_equal "track1", track.tag
assert_equal "Track 1", track.title
assert_equal "public", track.kind
assert_equal "https://example.com/icon1.png", track.icon_url
assert_equal "About Track 1", track.about
assert_equal Date.parse("2024-01-01"), track.start_date
assert_equal Date.parse("2024-12-31"), track.end_date
end

test "api#group/update_track" do
profile = Profile.find_by(handle: "cookie")
profile2 = Profile.find_by(handle: "mooncake")
auth_token = profile.gen_auth_token
group = Group.find_by(handle: "guildx")
track = group.tracks.create(tag: "track1", title: "Track 1", kind: "public", icon_url: "https://example.com/icon1.png", about: "About Track 1", start_date: "2024-01-01", end_date: "2024-12-31")

post api_group_update_track_url,
params: { auth_token: auth_token, track_id: track.id, track: {
title: "Updated Track 1", kind: "private", icon_url: "https://example.com/icon1_updated.png", about: "Updated About Track 1", start_date: "2024-02-01", end_date: "2024-11-30",
track_roles_attributes: [
{ role: "manager", profile_id: profile2.id }
]
} }
assert_response :success

track.reload
assert_equal "Updated Track 1", track.title
assert_equal "private", track.kind
assert_equal "https://example.com/icon1_updated.png", track.icon_url
assert_equal "Updated About Track 1", track.about
assert_equal Date.parse("2024-02-01"), track.start_date
assert_equal Date.parse("2024-11-30"), track.end_date
assert track.track_roles.count == 1
assert track.track_roles.first.role == "manager"
assert track.track_roles.first.profile_id == profile2.id
end

test "api#group/update" do # optimize this test function
assert_changes "Group.find_by(handle: 'guildx').timezone" do
profile = Profile.find_by(handle: "cookie")
Expand Down
11 changes: 11 additions & 0 deletions test/fixtures/track_roles.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

# This model initially had no columns defined. If you add columns to the
# model remove the "{}" from the fixture names and add the columns immediately
# below each fixture, per the syntax in the comments below
#
one: {}
# column: value
#
two: {}
# column: value
7 changes: 7 additions & 0 deletions test/models/track_role_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require "test_helper"

class TrackRoleTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end

0 comments on commit 55ceda6

Please sign in to comment.