Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

data-generator-retail - rename commands to orders #9800

Merged
merged 5 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/Upgrade.md
Original file line number Diff line number Diff line change
Expand Up @@ -924,6 +924,10 @@ The `<InputHelperText>` component no longer accepts a `touched` prop. This prop

If you were using this prop, you can safely remove it.

## `data-generator-retail` `commands` Have Been Renamed to `orders`

The `data-generator-retail` package has been updated to provide types for all its records. In the process, we renamed the `commands` resource to `orders`. Accordingly, the `nb_commands` property of the `customers` resource has been renamed to `nb_orders` and the `command_id` property of the `invoices` and `reviews` resources has been renamed to `order_id`.

## Upgrading to v4

If you are on react-admin v3, follow the [Upgrading to v4](https://marmelab.com/react-admin/doc/4.16/Upgrade.html) guide before upgrading to v5.
10 changes: 5 additions & 5 deletions examples/data-generator/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ console.log(data);
customers: [ /* ...900 customers */],
categories: [ /* ...12 categories */],
products: [ /* ...120 products */],
commands: [ /* ...600 orders */],
orders: [ /* ...600 orders */],
invoices: [ /* ...about 500 invoices */],
reviews: [ /* ... */],
}
Expand All @@ -42,7 +42,7 @@ console.log(data);
- latest_purchase
- has_newsletter: boolean
- groups: array
- nb_commands: integer
- nb_orders: integer
- total_spent: integer
- categories
- id: number
Expand All @@ -58,7 +58,7 @@ console.log(data);
- image: string
- description: string
- stock: integer
- commands
- orders
- id: integer
- reference: string
- date: date
Expand All @@ -74,7 +74,7 @@ console.log(data);
- invoices
- id: integer
- date: date
- command_id: integer
- order_id: integer
- customer_id: integer
- total_ex_taxes: float
- delivery_fees: float
Expand All @@ -85,7 +85,7 @@ console.log(data);
- id: integer
- date: date
- status: 'pending' | 'accepted' | 'rejected'
- command_id: integer
- order_id: integer
- product_id: integer
- customer_id: integer
- rating: integer
Expand Down
4 changes: 2 additions & 2 deletions examples/data-generator/src/customers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export const generateCustomers = (): Customer[] => {
latest_purchase: null, // finalize
has_newsletter: has_ordered ? weightedBoolean(30) : true,
groups: [], // finalize
nb_commands: 0,
nb_orders: 0,
total_spent: 0,
};
});
Expand All @@ -66,6 +66,6 @@ export type Customer = {
latest_purchase: string;
has_newsletter: boolean;
groups: string[];
nb_commands: number;
nb_orders: number;
total_spent: number;
};
37 changes: 18 additions & 19 deletions examples/data-generator/src/finalize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,31 @@ import { weightedBoolean } from './utils';

export default function (db: Db) {
// set latest purchase date
db.commands.forEach(command => {
let customer = db.customers[command.customer_id];
db.orders.forEach(order => {
let customer = db.customers[order.customer_id];
if (
!customer.latest_purchase ||
customer.latest_purchase < command.date
customer.latest_purchase < order.date
) {
customer.latest_purchase = command.date;
customer.latest_purchase = order.date;
}
customer.total_spent += command.total;
customer.nb_commands++;
customer.total_spent += order.total;
customer.nb_orders++;
});

// set product sales
db.commands.forEach(command => {
command.basket.forEach(item => {
db.orders.forEach(order => {
order.basket.forEach(item => {
db.products[item.product_id].sales += item.quantity;
});
});

// add 'collector' group
const customersBySpending = db.commands.reduce((customers, command) => {
if (!customers[command.customer_id]) {
customers[command.customer_id] = { nbProducts: 0 };
const customersBySpending = db.orders.reduce((customers, order) => {
if (!customers[order.customer_id]) {
customers[order.customer_id] = { nbProducts: 0 };
}
customers[command.customer_id].nbProducts += command.basket.length;
customers[order.customer_id].nbProducts += order.basket.length;
return customers;
}, {});
Object.keys(customersBySpending).forEach(customer_id => {
Expand All @@ -38,7 +38,7 @@ export default function (db: Db) {

// add 'ordered_once' group
db.customers
.filter(customer => customer.nb_commands === 1)
.filter(customer => customer.nb_orders === 1)
.forEach(customer => customer.groups.push('ordered_once'));

// add 'compulsive' group
Expand All @@ -52,14 +52,13 @@ export default function (db: Db) {
.forEach(customer => customer.groups.push('regular'));

// add 'returns' group
db.commands
.filter(command => command.returned)
.forEach(command => {
db.orders
.filter(order => order.returned)
.forEach(order => {
if (
db.customers[command.customer_id].groups.indexOf('returns') ===
-1
db.customers[order.customer_id].groups.indexOf('returns') === -1
) {
db.customers[command.customer_id].groups.push('returns');
db.customers[order.customer_id].groups.push('returns');
}
});

Expand Down
6 changes: 3 additions & 3 deletions examples/data-generator/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { generateCustomers, Customer } from './customers';
import { generateCategories, Category } from './categories';
import { generateProducts, Product } from './products';
import { generateCommands, Command, BasketItem } from './commands';
import { generateOrders, Order, BasketItem } from './orders';
import { generateInvoices, Invoice } from './invoices';
import { generateReviews, Review } from './reviews';
import finalize from './finalize';
Expand All @@ -12,7 +12,7 @@ const generateData = (): Db => {
db.customers = generateCustomers();
db.categories = generateCategories();
db.products = generateProducts(db);
db.commands = generateCommands(db);
db.orders = generateOrders(db);
db.invoices = generateInvoices(db);
db.reviews = generateReviews(db);
finalize(db);
Expand All @@ -25,7 +25,7 @@ export default generateData;
export type {
BasketItem,
Category,
Command,
Order,
Customer,
Db,
Invoice,
Expand Down
24 changes: 12 additions & 12 deletions examples/data-generator/src/invoices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,28 @@ export const generateInvoices = (db: Db): Invoice[] => {
let id = 0;

return (
db.commands
.filter(command => command.status !== 'delivered')
db.orders
.filter(order => order.status !== 'delivered')
// @ts-ignore
.sort((a, b) => new Date(a.date) - new Date(b.date))
.map(command => ({
.map(order => ({
id: id++,
date: command.date,
command_id: command.id,
customer_id: command.customer_id,
total_ex_taxes: command.total_ex_taxes,
delivery_fees: command.delivery_fees,
tax_rate: command.tax_rate,
taxes: command.taxes,
total: command.total,
date: order.date,
order_id: order.id,
customer_id: order.customer_id,
total_ex_taxes: order.total_ex_taxes,
delivery_fees: order.delivery_fees,
tax_rate: order.tax_rate,
taxes: order.taxes,
total: order.total,
}))
);
};

export type Invoice = {
id: number;
date: string;
command_id: number;
order_id: number;
customer_id: number;
total_ex_taxes: number;
delivery_fees: number;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
} from './utils';
import type { Db } from './types';

export const generateCommands = (db: Db): Command[] => {
export const generateOrders = (db: Db): Order[] => {
const today = new Date();
const aMonthAgo = subDays(today, 30);
const realCustomers = db.customers.filter(customer => customer.has_ordered);
Expand Down Expand Up @@ -42,7 +42,7 @@ export const generateCommands = (db: Db): Command[] => {
const customer = random.arrayElement(realCustomers);
const date = randomDate(customer.first_seen, customer.last_seen);

const status: CommandStatus =
const status: OrderStatus =
isAfter(date, aMonthAgo) && random.boolean()
? 'ordered'
: weightedArrayElement(['delivered', 'cancelled'], [10, 1]);
Expand All @@ -65,7 +65,7 @@ export const generateCommands = (db: Db): Command[] => {
});
};

export type Command = {
export type Order = {
id: number;
reference: string;
date: string;
Expand All @@ -76,11 +76,11 @@ export type Command = {
tax_rate: number;
taxes: number;
total: number;
status: CommandStatus;
status: OrderStatus;
returned: boolean;
};

export type CommandStatus = 'ordered' | 'delivered' | 'cancelled';
export type OrderStatus = 'ordered' | 'delivered' | 'cancelled';
export type BasketItem = {
product_id: number;
quantity: number;
Expand Down
16 changes: 8 additions & 8 deletions examples/data-generator/src/reviews.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ export const generateReviews = (db: Db): Review[] => {
.filter(() => weightedBoolean(60)) // only 60% of buyers write reviews
.map(customer => customer.id);

return db.commands
.filter(command => reviewers.indexOf(command.customer_id) !== -1)
return db.orders
.filter(order => reviewers.indexOf(order.customer_id) !== -1)
.reduce(
(acc, command) => [
(acc, order) => [
...acc,
...command.basket
...order.basket
.filter(() => weightedBoolean(40)) // reviewers review 40% of their products
.map(product => {
const date = randomDate(command.date);
const date = randomDate(order.date);
const status = isAfter(aMonthAgo, date)
? weightedArrayElement(
['accepted', 'rejected'],
Expand All @@ -37,9 +37,9 @@ export const generateReviews = (db: Db): Review[] => {
id: id++,
date: date.toISOString(),
status: status,
command_id: command.id,
order_id: order.id,
product_id: product.product_id,
customer_id: command.customer_id,
customer_id: order.customer_id,
rating: random.number({ min: 1, max: 5 }),
comment: Array.apply(
null,
Expand All @@ -58,7 +58,7 @@ export type Review = {
id: number;
date: string;
status: 'accepted' | 'rejected' | 'pending';
command_id: number;
order_id: number;
product_id: number;
customer_id: number;
rating: number;
Expand Down
4 changes: 2 additions & 2 deletions examples/data-generator/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Customer } from './customers';
import type { Category } from './categories';
import type { Product } from './products';
import type { Command } from './commands';
import type { Order } from './orders';
import type { Invoice } from './invoices';
import type { Review } from './reviews';
import { Settings } from './finalize';
Expand All @@ -10,7 +10,7 @@ export interface Db {
customers: Customer[];
categories: Category[];
products: Product[];
commands: Command[];
orders: Order[];
invoices: Invoice[];
reviews: Review[];
settings: Settings;
Expand Down
6 changes: 1 addition & 5 deletions examples/demo/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,7 @@ const App = () => {
<Route path="/segments" element={<Segments />} />
</CustomRoutes>
<Resource name="customers" {...visitors} />
<Resource
name="commands"
{...orders}
options={{ label: 'Orders' }}
/>
<Resource name="orders" {...orders} />
<Resource name="invoices" {...invoices} />
<Resource name="products" {...products} />
<Resource name="categories" {...categories} />
Expand Down
2 changes: 1 addition & 1 deletion examples/demo/src/dashboard/Dashboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const Dashboard = () => {
);
const aMonthAgo = useMemo(() => subDays(startOfDay(new Date()), 30), []);

const { data: orders } = useGetList<Order>('commands', {
const { data: orders } = useGetList<Order>('orders', {
filter: { date_gte: aMonthAgo.toISOString() },
sort: { field: 'date', order: 'DESC' },
pagination: { page: 1, perPage: 50 },
Expand Down
2 changes: 1 addition & 1 deletion examples/demo/src/dashboard/MonthlyRevenue.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const MonthlyRevenue = (props: Props) => {
const translate = useTranslate();
return (
<CardWithIcon
to="/commands"
to="/orders"
icon={DollarIcon}
title={translate('pos.dashboard.monthly_revenue')}
subtitle={value}
Expand Down
2 changes: 1 addition & 1 deletion examples/demo/src/dashboard/NbNewOrders.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const NbNewOrders = (props: Props) => {
const translate = useTranslate();
return (
<CardWithIcon
to="/commands"
to="/orders"
icon={ShoppingCartIcon}
title={translate('pos.dashboard.new_orders')}
subtitle={value}
Expand Down
2 changes: 1 addition & 1 deletion examples/demo/src/dashboard/PendingOrder.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export const PendingOrder = (props: Props) => {
});

return (
<ListItem button component={Link} to={`/commands/${order.id}`}>
<ListItem button component={Link} to={`/orders/${order.id}`}>
<ListItemAvatar>
{isPending ? (
<Avatar />
Expand Down
6 changes: 3 additions & 3 deletions examples/demo/src/dataProvider/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const getGqlResource = (resource: string) => {
case 'categories':
return 'Category';

case 'commands':
case 'orders':
return 'Command';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: shouldn't we return Order as well?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed 👌

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed


case 'products':
Expand Down Expand Up @@ -72,7 +72,7 @@ const customBuildQuery: BuildQueryFactory = introspectionResults => {
$latest_purchase: Date
$has_newsletter: Boolean!
$groups: [String]!
$nb_commands: Int!
$nb_orders: Int!
$total_spent: Float!
) {
createCustomer(
Expand All @@ -90,7 +90,7 @@ const customBuildQuery: BuildQueryFactory = introspectionResults => {
latest_purchase: $latest_purchase
has_newsletter: $has_newsletter
groups: $groups
nb_commands: $nb_commands
nb_orders: $nb_orders
total_spent: $total_spent
) {
id
Expand Down
Loading
Loading