mock-data.service.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. CreateChannel,
  9. CreateCustomerInput,
  10. CreateShippingMethod,
  11. CurrencyCode,
  12. LanguageCode,
  13. ProductVariant,
  14. } from '../../shared/generated-types';
  15. import { defaultShippingCalculator } from '../src/config/shipping-method/default-shipping-calculator';
  16. import { defaultShippingEligibilityChecker } from '../src/config/shipping-method/default-shipping-eligibility-checker';
  17. import { Customer } from '../src/entity/customer/customer.entity';
  18. import { SimpleGraphQLClient } from './simple-graphql-client';
  19. // tslint:disable:no-console
  20. /**
  21. * A service for creating mock data via the GraphQL API.
  22. */
  23. export class MockDataService {
  24. apiUrl: string;
  25. constructor(private client: SimpleGraphQLClient, private logging = true) {
  26. // make the generated results deterministic
  27. faker.seed(1);
  28. }
  29. async populateChannels(channelCodes: string[]): Promise<Channel.Fragment[]> {
  30. const channels: Channel.Fragment[] = [];
  31. for (const code of channelCodes) {
  32. const channel = await this.client.query<CreateChannel.Mutation, CreateChannel.Variables>(
  33. CREATE_CHANNEL,
  34. {
  35. input: {
  36. code,
  37. pricesIncludeTax: true,
  38. token: `${code}_token`,
  39. currencyCode: CurrencyCode.USD,
  40. defaultLanguageCode: LanguageCode.en,
  41. },
  42. },
  43. );
  44. channels.push(channel.createChannel);
  45. this.log(`Created Channel: ${channel.createChannel.code}`);
  46. }
  47. return channels;
  48. }
  49. async populateCustomers(count: number = 5): Promise<any> {
  50. for (let i = 0; i < count; i++) {
  51. const firstName = faker.name.firstName();
  52. const lastName = faker.name.lastName();
  53. const query1 = gql`
  54. mutation CreateCustomer($input: CreateCustomerInput!, $password: String) {
  55. createCustomer(input: $input, password: $password) {
  56. id
  57. emailAddress
  58. }
  59. }
  60. `;
  61. const variables1 = {
  62. input: {
  63. firstName,
  64. lastName,
  65. emailAddress: faker.internet.email(firstName, lastName),
  66. phoneNumber: faker.phone.phoneNumber(),
  67. } as CreateCustomerInput,
  68. password: 'test',
  69. };
  70. const customer: { id: string; emailAddress: string } | void = await this.client
  71. .query(query1, variables1)
  72. .then((data: any) => data.createCustomer, err => this.log(err));
  73. if (customer) {
  74. const query2 = gql`
  75. mutation($customerId: ID!, $input: CreateAddressInput!) {
  76. createCustomerAddress(customerId: $customerId, input: $input) {
  77. id
  78. streetLine1
  79. }
  80. }
  81. `;
  82. const variables2 = {
  83. input: {
  84. fullName: `${firstName} ${lastName}`,
  85. streetLine1: faker.address.streetAddress(),
  86. city: faker.address.city(),
  87. province: faker.address.county(),
  88. postalCode: faker.address.zipCode(),
  89. countryCode: 'GB',
  90. } as CreateAddressInput,
  91. customerId: customer.id,
  92. };
  93. await this.client.query(query2, variables2).catch(err => this.log(err));
  94. }
  95. }
  96. this.log(`Created ${count} Customers`);
  97. }
  98. private log(...args: any[]) {
  99. if (this.logging) {
  100. console.log(...args);
  101. }
  102. }
  103. }