-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
1 parent
0f39716
commit 99f57e0
Showing
19 changed files
with
694 additions
and
344 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Binary file added
BIN
+22.5 KB
apps/snitch_core/priv/repo/demo/demo_data/static/product_images/armor_costume.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+41.7 KB
apps/snitch_core/priv/repo/demo/demo_data/static/product_images/gauntlet.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+23.4 KB
apps/snitch_core/priv/repo/demo/demo_data/static/product_images/groot_costume.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+21.1 KB
apps/snitch_core/priv/repo/demo/demo_data/static/product_images/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.
Binary file added
BIN
+39.4 KB
apps/snitch_core/priv/repo/demo/demo_data/static/product_images/latex_mask.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+21.4 KB
.../snitch_core/priv/repo/demo/demo_data/static/product_images/student_costume.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.