generate-past-orders.ts 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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().subtract(i, 'day').format('YYYY-MM-DD')}`,
  41. );
  42. for (let j = 0; j < numberOfOrders; j++) {
  43. const customer = getRandomItem(customers);
  44. if (!customer.user) {
  45. continue;
  46. }
  47. const order = await orderService.create(ctx, customer.user.id);
  48. const result = await orderService.addItemToOrder(
  49. ctx,
  50. order.id,
  51. getRandomItem(variants).id,
  52. Math.floor(Math.random() * 3) + 1,
  53. );
  54. if (isGraphQlErrorResult(result)) {
  55. Logger.error(result.message);
  56. continue;
  57. }
  58. const eligibleShippingMethods = await orderService.getEligibleShippingMethods(ctx, order.id);
  59. await orderService.setShippingMethod(ctx, order.id, [getRandomItem(eligibleShippingMethods).id]);
  60. const transitionResult = await orderService.transitionToState(ctx, order.id, 'ArrangingPayment');
  61. if (isGraphQlErrorResult(transitionResult)) {
  62. Logger.error(transitionResult.message);
  63. continue;
  64. }
  65. const eligiblePaymentMethods = await orderService.getEligiblePaymentMethods(ctx, order.id);
  66. const paymentResult = await orderService.addPaymentToOrder(ctx, order.id, {
  67. method: getRandomItem(eligiblePaymentMethods).code,
  68. metadata: {},
  69. });
  70. if (isGraphQlErrorResult(paymentResult)) {
  71. Logger.error(paymentResult.message);
  72. continue;
  73. }
  74. const randomHourOfDay = Math.floor(Math.random() * 24);
  75. const placedAt = dayjs().subtract(i, 'day').startOf('day').add(randomHourOfDay, 'hour').toDate();
  76. await connection.getRepository(ctx, 'Order').update(order.id, {
  77. orderPlacedAt: placedAt,
  78. });
  79. }
  80. }
  81. }
  82. // get random item from array
  83. function getRandomItem<T>(array: T[]): T {
  84. return array[Math.floor(Math.random() * array.length)];
  85. }