Skip to content

Power up spree to a multi service platform to model any business applications.

License

Notifications You must be signed in to change notification settings

channainfo/commissioner

Repository files navigation

SpreeCmCommissioner

Commission gem build status

An application platform built on top of Spree commerce for modeling any bussiness applications.

Installation

1; Add this extension to your Gemfile with this line:

gem 'spree_cm_commissioner'

2; Install the gem using Bundler

bundle install

3; Copy & run migrations

bundle exec rails g spree_cm_commissioner:install

4; Restart your server

If your server was running, restart it so that it can find the assets properly.

Config

Rake tasks

# Seed province data for Cambodia country
rake data:seed_kh_provinces

# Seed option values and type location
rake data:seed_kh_location_option_values

# Reindex Elasticsearch on Vendor model
rake searchkick:reindex CLASS=Spree::Vendor

Google Map

CM commissioner required Google Map key for map components.

# .env
GOOGLE_MAP_KEY = ""
DEFAULT_LATLON = "10.627543,103.522141"

Elasticsearch

Commissioner required elasticsearch version 8.5.2. We recommend using evm to manage their version.

1, Install EVM (Elasticsearch Version Manager):

sudo curl -o /usr/local/bin/evm https://raw.githubusercontent.com/duydo/evm/master/evm
sudo chmod +x /usr/local/bin/evm

2, Install elasticsearch

evm install 8.5.2

# To start elasticsearch
evm start

# To stop elasticsearch
evm stop

Visual Studio Code Editor && Rubocop

  • Install VScode Extensions: ruby-rubocop

  • Make sure have below settings in VScode User Settings. It will auto-correct with rubocop after save file

{
  "ruby.rubocop.executePath": "/Users/USER_NAME/.rbenv/shims/"
}
  • We can run auto correction
#  Autocorrect offenses (only when it's safe)
$ bundle exec rubocop -a # or bundle exec rubocop --auto-correct

# Autocorrect offenses (safe and unsafe).
$ bundle exec rubocop -A # or bundle exec rubocop --auto-correct-all

All environments

Following are required varialbles inside .env

GOOGLE_MAP_KEY = ""
DEFAULT_LATLON = "10.627543,103.522141"
ACCOMMODATION_MAX_STAY_DAYS = 10
DEFAULT_TELEGRAM_BOT_API_TOKEN = ""

PIN_CODE_DEBUG_NOTIFIY_TELEGRAM_ENABLE="yes"
RECAPTCHA_TOKEN_VALIDATOR_ENABLE="yes"

EXCEPTION_NOTIFY_ENABLE="yes" # yes or no
EXCEPTION_TELEGRAM_BOT_TOKEN=""
EXCEPTION_NOTIFIER_TELEGRAM_CHANNEL_ID=""

Using Deface DSL (.deface files)

  • Make sure the path of override should match the path of view template
  • The .deface can be use with :erb, :html, or :text

Example:

View Template file: app/views/spree/admin/vendors/_form
Override file: app/overrides/spree/admin/vendors/_form/logo.html.erb.deface

https://github.com/spree/deface#using-the-deface-dsl-deface-files

Schedule Jobs

  • Create a schedule to update vendor min and max price
  • Frequently: every 24 hours
  • Run time: mid night is preferable
  • Command: rake "spree_cm_commissioner:vendor_update_price_range"
  • Customer_notification
customer_notification:
  cron: '0 0 * * * *'   # will trigger every hour, every day of the month, every month, and every day of the week
  class: SpreeCmCommissioner::CustomerNotificationCron

Testing

First bundle your dependencies, then run rake. rake will default to building the dummy app if it does not exist, then it will run specs. The dummy app can be regenerated by using rake test_app.

bundle update
bundle exec rake test_app

When testing your applications integration with this extension you may use it's factories. Simply add this require statement to your spec_helper:

require 'spree_cm_commissioner/factories'

Releasing

bundle exec gem bump -p -t
bundle exec gem release

For more options please see gem-release REAMDE

Contributing

If you'd like to contribute, please take a look at the instructions for installing dependencies and crafting a good pull request.

Copyright (c) 2022 [name of extension creator], released under the New BSD License

Account Deletion Cron Job

  • AccountDeletionCronJob
  • Frequently: every 24 hours
  • Deleted Account will last for 1 month before it is permanently deleted

Multiple databases

In most cases, Rails is able to infer the database connection. However in some instances, for example, in the spree_backend gem, it uses the GET request to destroy the session which in turn triggers database update that require the writing role. To fix this we need to explicitly tell Rails to use the right database connection.

Error using a wrong database connection looks like this:

raise ActiveRecord::ReadOnlyError, "Write query attempted while in readonly mode: #{sql}"

An ActiveRecord::ReadOnlyError occurred in orders#new.

module SpreeCmCommissioner
  module Admin
    module UserSessionsControllerDecorator
      def self.prepended(base)
        # spree_devise_auth gem use get as destroy
        # get '/logout' => 'user_sessions#destroy', :as => :logout
        base.around_action :set_writing_role, only: %i[destroy]
      end
    end
  end
end

unless Spree::Admin::UserSessionsController.ancestors.include?(SpreeCmCommissioner::Admin::UserSessionsControllerDecorator)
  Spree::Admin::UserSessionsController.prepend(SpreeCmCommissioner::Admin::UserSessionsControllerDecorator)
end

Avoid Excessive Looping

This test ensures that the query for fetching events does not loop excessively for each item.

Test Explanation

The test is structured as follows:

  1. Capture Rails Logger: It captures the Rails logger to inspect logs generated during the test execution.
  2. Execute the Query: It executes the query to fetch events.
  3. Expectation 1: It expects the query execution to complete without raising any errors.
  4. Expectation 2: It checks the captured Rails logs for any indication of excessive looping. This is done by verifying that the log messages do not include the phrase "Loop detected".
  5. Restore Original Rails Logger: Finally, it restores the original Rails logger.

Test Code (Ruby RSpec)

it 'should not loop excessively for each item' do
  # Capture the Rails logger to inspect logs
  logs = StringIO.new
  Rails.logger = Logger.new(logs)

  # Execute the query
  query = SpreeCmCommissioner::DashboardCrewEventQuery.new(user_id: user_a.id, section: 'incoming')

  # Expectation: The query should complete without raising any errors
  expect { query.events }.not_to raise_error

  # Expectation: Check Rails logs for excessive looping
  expect(logs.string).not_to include('Loop detected')  # Adjust this log message according to your implementation

  # Restore the original Rails logger
  Rails.logger = ActiveSupport::Logger.new(STDOUT)
end

About

Power up spree to a multi service platform to model any business applications.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages