Skip to content

Commit

Permalink
Order sample data (#346)
Browse files Browse the repository at this point in the history
Why?
We needed to have a setup for sample data in the system.


This change addresses the need by:
Adding order data for past 30 days and displaying them on the order listing page.
range(1-5) orders per day.
range(1-5) line-items per order each with quantity range(1-3).

[ delivers #161198515]
  • Loading branch information
jyotigautam authored and ashish173 committed Jan 1, 2019
1 parent 0f39716 commit 99f57e0
Show file tree
Hide file tree
Showing 19 changed files with 694 additions and 344 deletions.
1 change: 1 addition & 0 deletions .formatter.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"apps/*/test/**/*.{ex,exs}", # tests
# "apps/*/priv/repo/migrations/*.{ex,exs}", # migrations
"apps/*/priv/repo/seed/*.{ex,exs}", # seeds
"apps/*/priv/repo/demo/*.{ex,exs}", #demo
"apps/*/mix.exs", # mix files
"mix.exs", # top-level
]
Expand Down
4 changes: 4 additions & 0 deletions apps/snitch_core/mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ defmodule Snitch.Core.Mixfile do
# state machine
{:beepbop, github: "anantanant2015/beepbop", branch: "develop"},

# time
{:timex, "~> 3.1"},

# auth
{:comeonin, "~> 4.1.1"},
{:argon2_elixir, "~> 1.2"},
Expand Down Expand Up @@ -150,6 +153,7 @@ defmodule Snitch.Core.Mixfile do
# See the documentation for `Mix` for more info on aliases.
defp aliases do
[
"ecto.load.demo": "run priv/repo/demo/demo.exs",
"ecto.setup": ["ecto.create", "ecto.migrate", "run priv/repo/seed/seeds.exs"],
"ecto.reset": ["ecto.drop", "ecto.setup"],
"ecto.rebuild": ["ecto.drop", "ecto.create --quiet", "ecto.migrate"],
Expand Down
3 changes: 2 additions & 1 deletion apps/snitch_core/priv/repo/demo/demo.exs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

alias Snitch.Demo.{
OptionType,
Order,
PaymentMethod,
Product,
StockLocation,
Expand All @@ -16,3 +16,4 @@ StockLocation.create_stock_locations()
VariationTheme.create_variation_themes()
Taxonomy.create_taxonomy()
Product.create_products()
Order.create_orders()
16 changes: 11 additions & 5 deletions apps/snitch_core/priv/repo/demo/demo_data/products.csv
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
name,width,height,depth,selling_price,weight,maximum_retail_price,taxon,image
Men Checked Round Neck T-shirt,50,70,0,480,0.5,1069,Shirts,blue_shirt.jpeg
Women White & Black Top,45,65,0,1369,0.4,2490,Western Wear,polkadot.jpg
Cocoa Powder,0,0,0,600,0.3,809,Diet & Nutrition,neotia.jpg
Stylish Pure Leather Brown Watch,0,0,0,134,0.4,499,SmartWatches,analogwatch.jpeg
name,width,height,depth,selling_price,weight,maximum_retail_price,state,taxon,image
Men Checked Round Neck T-shirt,50,70,0,480,0.5,1069,active,Shirts,blue_shirt.jpeg
Women White & Black Top,45,65,0,1369,0.4,2490,active,Western Wear,polkadot.jpg
Cocoa Powder,0,0,0,600,0.3,809,active,Diet & Nutrition,neotia.jpg
Stylish Pure Leather Brown Watch,0,0,0,134,0.4,499,active,SmartWatches,analogwatch.jpeg
Prestige Minecraft Kids Armor Costume,19,29,0,34,0.4,50,active,Kids Costumes,armor_costume.jpg
Kids Deluxe RavenClaw Student Costume,19,29,0,37,0.4,60,active,Kids Costumes,student_costume.jpg
Marvel Infinity War Boys Teen Groot Costume,19,29,0,24,0.4,55,active,Kids Costumes,groot_costume.jpg
MARVEL Avenger's Infinity War Thanos Men's Latex Mask,19,29,0,24,0.4,55,active,Toys,latex_mask.jpg
Marvel Infinity War Child Infinity Gauntlet,19,29,0,24,0.4,35,active,Toys,gauntlet.jpg
Kids Superhero Boots,3,4,3,24,0.4,35,active,Kids Footwear,kids_boots.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
43 changes: 21 additions & 22 deletions apps/snitch_core/priv/repo/demo/option_type.ex
Original file line number Diff line number Diff line change
@@ -1,29 +1,28 @@
defmodule Snitch.Demo.OptionType do
alias NimbleCSV.RFC4180, as: CSV
alias Snitch.Core.Tools.MultiTenancy.Repo
alias Snitch.Data.Schema.OptionType

alias NimbleCSV.RFC4180, as: CSV
alias Snitch.Core.Tools.MultiTenancy.Repo
alias Snitch.Data.Schema.OptionType
@base_path Application.app_dir(:snitch_core, "priv/repo/demo/demo_data")

@base_path Application.app_dir(:snitch_core, "priv/repo/demo/demo_data")
def create_option_types do
Repo.delete_all(OptionType)
options_path = Path.join(@base_path, "options.csv")

def create_option_types do
options_path
|> File.read!()
|> CSV.parse_string()
|> Enum.each(fn [name, display_name] ->
create_option_type!(name, display_name)
end)
end

Repo.delete_all(OptionType)
options_path = Path.join(@base_path, "options.csv")
options_path
|> File.read!
|> CSV.parse_string
|> Enum.each(fn [name, display_name] ->
create_option_type!(name, display_name)
end)
end

def create_option_type!(name, display_name) do
params = %{
name: name,
display_name: display_name,
}
%OptionType{} |> OptionType.create_changeset(params) |> Repo.insert!
end
def create_option_type!(name, display_name) do
params = %{
name: name,
display_name: display_name
}

%OptionType{} |> OptionType.create_changeset(params) |> Repo.insert!()
end
end
217 changes: 217 additions & 0 deletions apps/snitch_core/priv/repo/demo/order.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
defmodule Snitch.Demo.Order do
use Timex

import Snitch.Tools.Helper.Order, only: [line_items_with_price: 2]

alias Ecto.DateTime

alias Snitch.Data.Schema.{
LineItem,
Order,
OrderAddress,
Package,
PackageItem,
Product,
ShippingCategory,
StockLocation,
Taxon,
User
}

alias Snitch.Data.Model.GeneralConfiguration, as: GCModel
alias Snitch.Core.Tools.MultiTenancy.Repo
alias Snitch.Data.Model.{Country, State}

require Logger

@package_states ["processing", "ready", "shipped", "delivered", "complete"]

@order %{
number: nil,
state: nil,
user_id: nil,
billing_address: nil,
shipping_address: nil,
inserted_at: Timex.now(),
updated_at: Timex.now()
}

defp build_orders(start_time) do
variants = Product |> Repo.all() |> Enum.take_random(5)
user = Repo.all(User) |> Enum.random()

address = %OrderAddress{
first_name: user.first_name,
last_name: user.last_name,
address_line_1: "10-8-80 Malibu Point",
zip_code: "90265",
city: "Malibu",
phone: "1234567890",
state_id: State.get(%{code: "US-CA"}).id,
country_id: Country.get(%{iso: "US"}).id
}

digest = [
%{
quantity: Enum.map(1..5, fn _ -> Enum.random(1..3) end),
user_id: user.id,
state: :confirmed,
address: address
}
]

digest_list = digest |> List.duplicate(Enum.random(1..5)) |> List.flatten()
make_orders(digest_list, variants, start_time)
end

def create_orders do
Repo.delete_all(Package)
Repo.delete_all(Order)
end_time = Timex.now()
start_time = Timex.shift(end_time, months: -1)
seed_orders(start_time)
end

def seed_orders(start_time) do
end_time = Timex.now()

case Timex.before?(start_time, end_time) do
true ->
seed_orders!(start_time)

false ->
nil
end
end

def seed_orders!(start_time) do
currency = GCModel.fetch_currency()

{orders, line_items} = build_orders(start_time)
{count, order_structs} = create_orders(orders)
Logger.info("Inserted #{count} orders.")

packages = build_packages(order_structs, start_time, currency)
{count, package_structs} = create_packages(packages)
Logger.info("Created #{count} packages.")

line_items = build_line_items_for_orders(order_structs, line_items)
{count, line_item_structs} = Repo.insert_all(LineItem, line_items, returning: true)
Logger.info("Inserted #{count} line-items.")

package_items = build_package_items(line_item_structs, start_time, currency)
{count, package_item_structs} = create_package_items(package_items)
Logger.info("Created #{count} package_items.")

start_time = Timex.shift(start_time, days: 1)
seed_orders(start_time)
end

defp create_orders(orders) do
Repo.insert_all(
Order,
orders,
on_conflict: :nothing,
conflict_target: [:number],
returning: true
)
end

defp build_packages(order_structs, start_time, currency) do
order_structs
|> Enum.map(fn order ->
%{
number: Nanoid.generate(),
order_id: order.id,
state: Enum.random(@package_states),
cost: Money.new(currency, 160),
shipping_tax: Money.new(currency, 0),
origin_id: Enum.random(Repo.all(StockLocation)).id,
shipping_category_id: Enum.random(Repo.all(ShippingCategory)).id,
inserted_at: start_time,
updated_at: start_time
}
end)
end

defp create_packages(packages) do
Repo.insert_all(
Package,
packages,
on_conflict: :nothing,
conflict_target: [:number],
returning: true
)
end

defp build_line_items_for_orders(order_structs, line_items) do
order_structs
|> Stream.zip(line_items)
|> Enum.map(fn {%{id: id}, items} ->
Enum.map(items, &Map.put(&1, :order_id, id))
end)
|> List.flatten()
end

defp build_package_items(line_item_structs, start_time, currency) do
line_item_structs
|> Repo.preload(order: :packages)
|> Enum.map(fn item ->
package = item.order.packages |> List.first()

%{
number: Nanoid.generate(),
quantity: item.quantity,
package_id: package.id,
state: "pending",
shipping_tax: Money.new(currency, 0),
tax: Money.new(currency, 0),
backordered?: true,
product_id: item.product_id,
line_item_id: item.id,
inserted_at: start_time,
updated_at: start_time
}
end)
end

defp create_package_items(package_items) do
Repo.insert_all(
PackageItem,
package_items,
on_conflict: :nothing,
conflict_target: [:number],
returning: true
)
end

def make_orders(digest, variants, start_time) do
digest
|> Stream.with_index()
|> Enum.map(fn {manifest, index} ->
number = "#{Nanoid.generate()}-#{index}"
line_items = line_items_with_price(variants, manifest.quantity)

line_items =
Enum.map(line_items, fn line_item ->
line_item
|> Map.put(:inserted_at, start_time)
|> Map.put(:updated_at, start_time)
end)

order = %{
@order
| number: number,
state: "#{manifest.state}",
user_id: manifest[:user_id],
billing_address: manifest[:address],
shipping_address: manifest[:address],
inserted_at: start_time,
updated_at: start_time
}

{order, line_items}
end)
|> Enum.unzip()
end
end
36 changes: 17 additions & 19 deletions apps/snitch_core/priv/repo/demo/payment_methods.ex
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
defmodule Snitch.Demo.PaymentMethod do
alias Snitch.Core.Tools.MultiTenancy.Repo
alias Snitch.Data.Schema.PaymentMethod
alias SnitchPayments.{Gateway, Payment}

alias Snitch.Core.Tools.MultiTenancy.Repo
alias Snitch.Data.Schema.PaymentMethod
alias SnitchPayments.{Gateway, Payment}
def create_payment_methods do
Repo.delete_all(PaymentMethod)
create_payment_method!("card-payments", "ccd", Gateway.PayuBiz)
create_payment_method!("COD", "cod", Payment.CashOnDelivery)
create_payment_method!("Stripe", "str", Gateway.Stripe)
end

def create_payment_methods do

Repo.delete_all(PaymentMethod)
create_payment_method!("card-payments", "ccd", Gateway.PayuBiz)
create_payment_method!("COD", "cod", Payment.CashOnDelivery)
create_payment_method!("Stripe", "str", Gateway.Stripe)
end

def create_payment_method!(name, code, provider) do
params = %{
name: name,
code: code,
provider: provider
}
%PaymentMethod{} |> PaymentMethod.create_changeset(params) |> Repo.insert!
end
def create_payment_method!(name, code, provider) do
params = %{
name: name,
code: code,
provider: provider
}

%PaymentMethod{} |> PaymentMethod.create_changeset(params) |> Repo.insert!()
end
end
Loading

0 comments on commit 99f57e0

Please sign in to comment.