diff --git a/db/manifest b/db/manifest index 4da7a1b28..bc0c3e7c2 100755 --- a/db/manifest +++ b/db/manifest @@ -310,3 +310,4 @@ web_playable_jamtracks.sql affiliate_partner_rate.sql track_downloads.sql jam_track_lang_idx.sql +giftcard.sql diff --git a/db/up/giftcard.sql b/db/up/giftcard.sql new file mode 100644 index 000000000..147cfc626 --- /dev/null +++ b/db/up/giftcard.sql @@ -0,0 +1,13 @@ +CREATE TABLE gift_card ( + id VARCHAR(64) PRIMARY KEY DEFAULT uuid_generate_v4(), + code VARCHAR(64) UNIQUE NOT NULL, + user_id VARCHAR (64) REFERENCES users(id) ON DELETE CASCADE, + card_type VARCHAR(64) NOT NULL, + used BOOLEAN NOT NULL DEFAULT FALSE, + created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, + updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP +); + +ALTER TABLE users ADD COLUMN free_jamtracks INTEGER DEFAULT 0; +UPDATE users set free_jamtracks = 1 WHERE has_redeemable_jamtrack = TRUE; +ALTER TABLE users DROP COLUMN has_redeemable_jamtrack; \ No newline at end of file diff --git a/ruby/lib/jam_ruby/models/gift_card.rb b/ruby/lib/jam_ruby/models/gift_card.rb new file mode 100644 index 000000000..2c8e5d0d3 --- /dev/null +++ b/ruby/lib/jam_ruby/models/gift_card.rb @@ -0,0 +1,36 @@ +module JamRuby + class GiftCard < ActiveRecord::Base + + @@log = Logging.logger[GiftCard] + + FIVE_JAM_TRACKS = 'five_jam_tracks' + TEN_JAM_TRACKS = 'ten_jam_tracks' + CARD_TYPES = + [ + FIVE_JAM_TRACKS, + TEN_JAM_TRACKS + ] + + + belongs_to :user, class_name: "JamRuby::User" + + validates :card_type, presence: true, inclusion: {in: CARD_TYPES} + validates :redeemed, inclusion: {in: [true, false]} + validates :code, presence: true, uniqueness: true + + def redeem(user) + + transaction do + update({redeemed: true, user: user}) + + if card_type == FIVE_JAM_TRACK + user.red + elsif card_type == TEN_JAM_TRACK + else + raise 'unknown type' + end + end + end + + end +end diff --git a/ruby/lib/jam_ruby/models/user.rb b/ruby/lib/jam_ruby/models/user.rb index a9c46801a..21b0191b2 100644 --- a/ruby/lib/jam_ruby/models/user.rb +++ b/ruby/lib/jam_ruby/models/user.rb @@ -194,6 +194,7 @@ module JamRuby validates :terms_of_service, :acceptance => {:accept => true, :on => :create, :allow_nil => false } validates :reuse_card, :inclusion => {:in => [true, false]} validates :has_redeemable_jamtrack, :inclusion => {:in => [true, false]} + validates :free_jamtracks, presence: true, minimum: 0, maximum: 100 validates :subscribe_email, :inclusion => {:in => [nil, true, false]} validates :musician, :inclusion => {:in => [true, false]} validates :show_whats_next, :inclusion => {:in => [nil, true, false]}