snf_core 0.2.0 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 122d529289ccf8b90e6254c1f8aed2c1171fe41fdbf94c40ba2c64d026693586
4
- data.tar.gz: d6543e1461630e153501892f2209b34d5960332f0aaaace71d837a32a4f8f923
3
+ metadata.gz: b148e772bedfc2369e9eb125c9e752f3f7519cb1ac47e5c365416e45fe549796
4
+ data.tar.gz: 1bbb2accd13bc2f00ae5f4b6e55061ea54d47f563a99811c2594be72f4efb4c7
5
5
  SHA512:
6
- metadata.gz: ec7bef961a04963418783f211a3c8c3658c517de69e9ad00412be8b9e0e7c6073420c3ce54ffac1ff38f2ad6546a3aca9e5b649579abbb712601a6cb1cea27c5
7
- data.tar.gz: 55a71a7e898c8965fb50df6c10ec2c88409585ed1cc0e9049d89e32cc263d84cb545f98e540eb391c8c7f5d9e2c813c5114bd4ebf4c5ab2d6d6624b515426784
6
+ metadata.gz: 06a09af28f6ea76d7e2ccdf0ec82b620e0108fae243b399d734400cd37ff76f81acb7b44c64445dc4fa8988109fe8fc2a8203ccd90bd404d9e0e2d1a44b8ca3d
7
+ data.tar.gz: e2650005be78578265bbbaaea8690d68efd8f1f87f63defef03554dbb36d95b79dd129b7c86897d95deac52250095d2ed95d0a24f545916797c50084db77d602
@@ -1,11 +1,11 @@
1
- module SnfCore
2
- class Business < ApplicationRecord
3
- belongs_to :user
4
-
5
- validates :business_name, :tin_number, :business_type, :verification_status, presence: true
6
- validates :tin_number, uniqueness: true
7
-
8
- enum :business_type, { retailer: 0, wholesaler: 1 }
9
- enum :verification_status, { pending: 0, approved: 1, rejected: 2 }
10
- end
11
- end
1
+ module SnfCore
2
+ class Business < ApplicationRecord
3
+ belongs_to :user
4
+
5
+ validates :business_name, :tin_number, :business_type, :verification_status, presence: true
6
+ validates :tin_number, uniqueness: true
7
+
8
+ enum :business_type, { retailer: 0, wholesaler: 1 }
9
+ enum :verification_status, { pending: 0, approved: 1, rejected: 2 }
10
+ end
11
+ end
@@ -1,7 +1,7 @@
1
- module SnfCore
2
- class Category < ApplicationRecord
3
- belongs_to :parent, optional: true, class_name: "SnfCore::Category"
4
-
5
- validates :name, presence: true, uniqueness: true
6
- end
7
- end
1
+ module SnfCore
2
+ class Category < ApplicationRecord
3
+ belongs_to :parent, optional: true, class_name: "SnfCore::Category"
4
+
5
+ validates :name, presence: true, uniqueness: true
6
+ end
7
+ end
@@ -2,5 +2,29 @@ module SnfCore
2
2
  class StoreInventory < ApplicationRecord
3
3
  belongs_to :store
4
4
  belongs_to :product
5
+
6
+ validates :stock_quantity, presence: true, numericality: {
7
+ only_integer: true,
8
+ greater_than_or_equal_to: 0
9
+ }
10
+ validates :base_price, presence: true, numericality: {
11
+ greater_than_or_equal_to: 0
12
+ }
13
+
14
+ validate :unique_store_product_combination
15
+
16
+ scope :out_of_stock, -> { where(stock_quantity: 0) }
17
+ scope :in_stock, -> { where("stock_quantity > 0") }
18
+
19
+ private
20
+
21
+ def unique_store_product_combination
22
+ existing = StoreInventory.where(store_id: store_id, product_id: product_id)
23
+ existing = existing.where.not(id: id) if persisted?
24
+
25
+ if existing.exists?
26
+ errors.add(:base, "Product already exists in this store's inventory")
27
+ end
28
+ end
5
29
  end
6
30
  end
@@ -1,8 +1,18 @@
1
1
  module SnfCore
2
2
  class User < ApplicationRecord
3
+ has_one :wallet, dependent: :destroy
4
+
3
5
  validates :first_name, :middle_name, :last_name, :phone_number, :role, presence: true
4
6
  validates :phone_number, :email, uniqueness: true
5
7
 
6
8
  enum :role, { admin: 0, wholesaler: 1, retailer: 2, consumer: 3 }
9
+
10
+ after_create :create_default_wallet
11
+
12
+ private
13
+
14
+ def create_default_wallet
15
+ create_wallet(balance: 0.0, is_active: true)
16
+ end
7
17
  end
8
18
  end
@@ -0,0 +1,21 @@
1
+ module SnfCore
2
+ class Wallet < ApplicationRecord
3
+ belongs_to :user
4
+
5
+ validates :wallet_number, :balance, :is_active, presence: true
6
+ validates :wallet_number, uniqueness: { case_sensitive: false }
7
+ validates :is_active, inclusion: { in: [ true, false ] }
8
+
9
+ before_validation :generate_wallet_number, on: :create
10
+
11
+ private
12
+
13
+ def generate_wallet_number
14
+ loop do
15
+ random_digits = SecureRandom.random_number(10**10).to_s.rjust(10, "0")
16
+ self.wallet_number = "BB#{random_digits}"
17
+ break unless Wallet.exists?(wallet_number: wallet_number)
18
+ end
19
+ end
20
+ end
21
+ end
@@ -1,11 +1,11 @@
1
- class CreateSnfCoreCategories < ActiveRecord::Migration[8.0]
2
- def change
3
- create_table :snf_core_categories do |t|
4
- t.string :name, null: false
5
- t.string :description
6
- t.references :parent, null: true, foreign_key: { to_table: :snf_core_categories }
7
-
8
- t.timestamps
9
- end
10
- end
11
- end
1
+ class CreateSnfCoreCategories < ActiveRecord::Migration[8.0]
2
+ def change
3
+ create_table :snf_core_categories do |t|
4
+ t.string :name, null: false
5
+ t.string :description
6
+ t.references :parent, null: true, foreign_key: { to_table: :snf_core_categories }
7
+
8
+ t.timestamps
9
+ end
10
+ end
11
+ end
@@ -1,14 +1,14 @@
1
- class CreateSnfCoreBusinesses < ActiveRecord::Migration[8.0]
2
- def change
3
- create_table :snf_core_businesses do |t|
4
- t.references :user, null: false, foreign_key: { to_table: :snf_core_users }
5
- t.string :business_name, null: false
6
- t.string :tin_number, null: false
7
- t.integer :business_type, null: false
8
- t.datetime :verified_at
9
- t.integer :verification_status, null: false
10
-
11
- t.timestamps
12
- end
13
- end
14
- end
1
+ class CreateSnfCoreBusinesses < ActiveRecord::Migration[8.0]
2
+ def change
3
+ create_table :snf_core_businesses do |t|
4
+ t.references :user, null: false, foreign_key: { to_table: :snf_core_users }
5
+ t.string :business_name, null: false
6
+ t.string :tin_number, null: false
7
+ t.integer :business_type, null: false
8
+ t.datetime :verified_at
9
+ t.integer :verification_status, null: false
10
+
11
+ t.timestamps
12
+ end
13
+ end
14
+ end
@@ -1,13 +1,14 @@
1
1
  class CreateSnfCoreStoreInventories < ActiveRecord::Migration[8.0]
2
2
  def change
3
3
  create_table :snf_core_store_inventories do |t|
4
- t.string :stock_quantity
5
- t.float :reorder_level
6
- t.datetime :last_restocked_at
7
4
  t.references :store, null: false, foreign_key: { to_table: :snf_core_stores }
8
5
  t.references :product, null: false, foreign_key: { to_table: :snf_core_products }
6
+ t.integer :stock_quantity, null: false, default: 0
7
+ t.decimal :base_price, precision: 10, scale: 2, null: false
9
8
 
10
9
  t.timestamps
11
10
  end
11
+
12
+ add_index :snf_core_store_inventories, [ :store_id, :product_id ], unique: true
12
13
  end
13
14
  end
@@ -0,0 +1,14 @@
1
+ class CreateSnfCoreWallets < ActiveRecord::Migration[8.0]
2
+ def change
3
+ create_table :snf_core_wallets do |t|
4
+ t.references :user, null: false, foreign_key: { to_table: :snf_core_users }
5
+ t.string :wallet_number, null: false
6
+ t.decimal :balance, null: false
7
+ t.boolean :is_active, null: false, default: true
8
+
9
+ t.timestamps
10
+
11
+ t.index :wallet_number, unique: true
12
+ end
13
+ end
14
+ end
@@ -1,3 +1,3 @@
1
1
  module SnfCore
2
- VERSION = "0.2.0"
2
+ VERSION = "0.2.1"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: snf_core
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Asrat
@@ -299,6 +299,7 @@ files:
299
299
  - app/models/snf_core/store.rb
300
300
  - app/models/snf_core/store_inventory.rb
301
301
  - app/models/snf_core/user.rb
302
+ - app/models/snf_core/wallet.rb
302
303
  - config/routes.rb
303
304
  - db/migrate/20250226042622_create_snf_core_users.rb
304
305
  - db/migrate/20250226064444_create_snf_core_categories.rb
@@ -307,9 +308,10 @@ files:
307
308
  - db/migrate/20250226110217_create_snf_core_addresses.rb
308
309
  - db/migrate/20250226115215_create_snf_core_stores.rb
309
310
  - db/migrate/20250226181007_create_snf_core_products.rb
310
- - db/migrate/20250226190655_create_snf_core_store_inventories.rb
311
+ - db/migrate/20250226190000_create_snf_core_store_inventories.rb
311
312
  - db/migrate/20250226192104_create_snf_core_groups.rb
312
313
  - db/migrate/20250226193938_create_snf_core_customer_groups.rb
314
+ - db/migrate/20250227075048_create_snf_core_wallets.rb
313
315
  - lib/snf_core.rb
314
316
  - lib/snf_core/engine.rb
315
317
  - lib/snf_core/version.rb