From 55ceda600d98e936db485a149e46b3ad8ee29305 Mon Sep 17 00:00:00 2001 From: jiangplus Date: Thu, 12 Sep 2024 19:25:21 +0800 Subject: [PATCH] add group track api --- app/controllers/api/group_controller.rb | 21 ++++++- app/mailers/event_mailer.rb | 61 ------------------- app/models/event.rb | 1 + app/models/track.rb | 2 + app/models/track_role.rb | 5 ++ config/routes.rb | 2 + .../20240912110911_create_track_roles.rb | 13 ++++ db/schema.rb | 12 +++- test/controllers/api/group_controller_test.rb | 60 ++++++++++++++++++ test/fixtures/track_roles.yml | 11 ++++ test/models/track_role_test.rb | 7 +++ 11 files changed, 131 insertions(+), 64 deletions(-) create mode 100644 app/models/track_role.rb create mode 100644 db/migrate/20240912110911_create_track_roles.rb create mode 100644 test/fixtures/track_roles.yml create mode 100644 test/models/track_role_test.rb diff --git a/app/controllers/api/group_controller.rb b/app/controllers/api/group_controller.rb index 8fe275b..23e43d7 100644 --- a/app/controllers/api/group_controller.rb +++ b/app/controllers/api/group_controller.rb @@ -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]) @@ -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 diff --git a/app/mailers/event_mailer.rb b/app/mailers/event_mailer.rb index 2708d70..fea5f07 100644 --- a/app/mailers/event_mailer.rb +++ b/app/mailers/event_mailer.rb @@ -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:send@app.sola.day", 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:send@app.sola.day", 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 diff --git a/app/models/event.rb b/app/models/event.rb index 35509a9..32e264b 100644 --- a/app/models/event.rb +++ b/app/models/event.rb @@ -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 diff --git a/app/models/track.rb b/app/models/track.rb index 1593515..76fc09f 100644 --- a/app/models/track.rb +++ b/app/models/track.rb @@ -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 diff --git a/app/models/track_role.rb b/app/models/track_role.rb new file mode 100644 index 0000000..955002b --- /dev/null +++ b/app/models/track_role.rb @@ -0,0 +1,5 @@ +class TrackRole < ApplicationRecord + belongs_to :track + belongs_to :profile + validates :role, inclusion: { in: %w(member manager) } +end diff --git a/config/routes.rb b/config/routes.rb index 87d68e5..4e155f6 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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" diff --git a/db/migrate/20240912110911_create_track_roles.rb b/db/migrate/20240912110911_create_track_roles.rb new file mode 100644 index 0000000..5160990 --- /dev/null +++ b/db/migrate/20240912110911_create_track_roles.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index e6630a4..dfc21a8 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.2].define(version: 2024_09_10_061102) do +ActiveRecord::Schema[7.2].define(version: 2024_09_12_110911) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -176,6 +176,7 @@ t.string "external_url" t.text "notes" t.jsonb "extra" + t.integer "track_id" end create_table "group_invites", force: :cascade do |t| @@ -450,6 +451,15 @@ t.string "tracks_allowed", array: true end + create_table "track_roles", force: :cascade do |t| + t.integer "track_id" + t.integer "profile_id" + t.string "receiver_address" + t.string "role", default: "member" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + create_table "tracks", force: :cascade do |t| t.string "tag" t.string "title" diff --git a/test/controllers/api/group_controller_test.rb b/test/controllers/api/group_controller_test.rb index 1fa5190..6faae4a 100644 --- a/test/controllers/api/group_controller_test.rb +++ b/test/controllers/api/group_controller_test.rb @@ -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") diff --git a/test/fixtures/track_roles.yml b/test/fixtures/track_roles.yml new file mode 100644 index 0000000..d7a3329 --- /dev/null +++ b/test/fixtures/track_roles.yml @@ -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 diff --git a/test/models/track_role_test.rb b/test/models/track_role_test.rb new file mode 100644 index 0000000..224c048 --- /dev/null +++ b/test/models/track_role_test.rb @@ -0,0 +1,7 @@ +require "test_helper" + +class TrackRoleTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end