generate-past-orders.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import {
  2. bootstrapWorker,
  3. CustomerService,
  4. isGraphQlErrorResult,
  5. Logger,
  6. OrderService,
  7. ProductVariantService,
  8. RequestContextService,
  9. ShippingMethodService,
  10. TransactionalConnection,
  11. } from '@vendure/core';
  12. import dayjs from 'dayjs';
  13. import { devConfig } from '../dev-config';
  14. const loggerCtx = 'DataSync script';
  15. generatePastOrders()
  16. .then(() => process.exit(0))
  17. .catch(() => process.exit(1));
  18. const DAYS_TO_COVER = 30;
  19. // This script generates a large number of past Orders over the past <DAYS_TO_COVER> days.
  20. // It is useful for testing scenarios where there are a large number of Orders in the system.
  21. async function generatePastOrders() {
  22. const { app } = await bootstrapWorker(devConfig);
  23. const requestContextService = app.get(RequestContextService);
  24. const orderService = app.get(OrderService);
  25. const customerService = app.get(CustomerService);
  26. const productVariantService = app.get(ProductVariantService);
  27. const shippingMethodService = app.get(ShippingMethodService);
  28. const connection = app.get(TransactionalConnection);
  29. const ctx = await requestContextService.create({
  30. apiType: 'shop',
  31. });
  32. const ctxAdmin = await requestContextService.create({
  33. apiType: 'admin',
  34. });
  35. const { items: variants } = await productVariantService.findAll(ctxAdmin, { take: 500 });
  36. const { items: customers } = await customerService.findAll(ctxAdmin, { take: 500 }, ['user']);
  37. for (let i = DAYS_TO_COVER; i > 0; i--) {
  38. const numberOfOrders = Math.floor(Math.random() * 10) + 5;
  39. Logger.info(
  40. `Generating ${numberOfOrders} orders for ${dayjs()
  41. .subtract(30 - i, 'day')
  42. .format('YYYY-MM-DD')}`,
  43. );
  44. for (let j = 0; j < numberOfOrders; j++) {
  45. const customer = getRandomItem(customers);
  46. if (!customer.user) {
  47. continue;
  48. }
  49. const order = await orderService.create(ctx, customer.user.id);
  50. const result = await orderService.addItemToOrder(
  51. ctx,
  52. order.id,
  53. getRandomItem(variants).id,
  54. Math.floor(Math.random() * 3) + 1,
  55. );
  56. if (isGraphQlErrorResult(result)) {
  57. Logger.error(result.message);
  58. continue;
  59. }
  60. const eligibleShippingMethods = await orderService.getEligibleShippingMethods(ctx, order.id);
  61. await orderService.setShippingMethod(ctx, order.id, [getRandomItem(eligibleShippingMethods).id]);
  62. const transitionResult = await orderService.transitionToState(ctx, order.id, 'ArrangingPayment');
  63. if (isGraphQlErrorResult(transitionResult)) {
  64. Logger.error(transitionResult.message);
  65. continue;
  66. }
  67. const eligiblePaymentMethods = await orderService.getEligiblePaymentMethods(ctx, order.id);
  68. const paymentResult = await orderService.addPaymentToOrder(ctx, order.id, {
  69. method: getRandomItem(eligiblePaymentMethods).code,
  70. metadata: {},
  71. });
  72. if (isGraphQlErrorResult(paymentResult)) {
  73. Logger.error(paymentResult.message);
  74. continue;
  75. }
  76. const randomHourOfDay = Math.floor(Math.random() * 24);
  77. const placedAt = dayjs()
  78. .subtract(DAYS_TO_COVER - i, 'day')
  79. .startOf('day')
  80. .add(randomHourOfDay, 'hour')
  81. .toDate();
  82. await connection.getRepository(ctx, 'Order').update(order.id, {
  83. orderPlacedAt: placedAt,
  84. });
  85. }
  86. }
  87. }
  88. // get random item from array
  89. function getRandomItem<T>(array: T[]): T {
  90. return array[Math.floor(Math.random() * array.length)];
  91. }