mock-data.service.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import faker from 'faker/locale/en_GB';
  2. import gql from 'graphql-tag';
  3. import { CREATE_CHANNEL } from '../../../admin-ui/src/app/data/definitions/settings-definitions';
  4. import { CREATE_SHIPPING_METHOD } from '../../../admin-ui/src/app/data/definitions/shipping-definitions';
  5. import {
  6. Channel,
  7. CreateAddressInput,
  8. CreateCustomerInput,
  9. CurrencyCode,
  10. LanguageCode,
  11. ProductVariant,
  12. } from '../e2e/graphql/generated-e2e-admin-types';
  13. import { defaultShippingCalculator } from '../src/config/shipping-method/default-shipping-calculator';
  14. import { defaultShippingEligibilityChecker } from '../src/config/shipping-method/default-shipping-eligibility-checker';
  15. import { Customer } from '../src/entity/customer/customer.entity';
  16. import { SimpleGraphQLClient } from './simple-graphql-client';
  17. // tslint:disable:no-console
  18. /**
  19. * A service for creating mock data via the GraphQL API.
  20. */
  21. export class MockDataService {
  22. apiUrl: string;
  23. constructor(private client: SimpleGraphQLClient, private logging = true) {
  24. // make the generated results deterministic
  25. faker.seed(1);
  26. }
  27. async populateCustomers(count: number = 5): Promise<any> {
  28. for (let i = 0; i < count; i++) {
  29. const firstName = faker.name.firstName();
  30. const lastName = faker.name.lastName();
  31. const query1 = gql`
  32. mutation CreateCustomer($input: CreateCustomerInput!, $password: String) {
  33. createCustomer(input: $input, password: $password) {
  34. id
  35. emailAddress
  36. }
  37. }
  38. `;
  39. const variables1 = {
  40. input: {
  41. firstName,
  42. lastName,
  43. emailAddress: faker.internet.email(firstName, lastName),
  44. phoneNumber: faker.phone.phoneNumber(),
  45. } as CreateCustomerInput,
  46. password: 'test',
  47. };
  48. const customer: { id: string; emailAddress: string } | void = await this.client
  49. .query(query1, variables1)
  50. .then((data: any) => data.createCustomer, err => this.log(err));
  51. if (customer) {
  52. const query2 = gql`
  53. mutation($customerId: ID!, $input: CreateAddressInput!) {
  54. createCustomerAddress(customerId: $customerId, input: $input) {
  55. id
  56. streetLine1
  57. }
  58. }
  59. `;
  60. const variables2 = {
  61. input: {
  62. fullName: `${firstName} ${lastName}`,
  63. streetLine1: faker.address.streetAddress(),
  64. city: faker.address.city(),
  65. province: faker.address.county(),
  66. postalCode: faker.address.zipCode(),
  67. countryCode: 'GB',
  68. } as CreateAddressInput,
  69. customerId: customer.id,
  70. };
  71. await this.client.query(query2, variables2).catch(err => this.log(err));
  72. }
  73. }
  74. this.log(`Created ${count} Customers`);
  75. }
  76. private log(...args: any[]) {
  77. if (this.logging) {
  78. console.log(...args);
  79. }
  80. }
  81. }