mock-data-client.service.ts 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. import * as faker from 'faker/locale/en_GB';
  2. import { request } from 'graphql-request';
  3. import { PasswordService } from '../src/auth/password.service';
  4. import { VendureConfig } from '../src/config/vendure-config';
  5. import { CreateAddressDto } from '../src/entity/address/address.dto';
  6. import { CreateAdministratorDto } from '../src/entity/administrator/administrator.dto';
  7. import { CreateCustomerDto } from '../src/entity/customer/customer.dto';
  8. import { Customer } from '../src/entity/customer/customer.entity';
  9. import { CreateProductOptionGroupDto } from '../src/entity/product-option-group/product-option-group.dto';
  10. import { CreateProductVariantDto } from '../src/entity/product-variant/create-product-variant.dto';
  11. import { CreateProductDto } from '../src/entity/product/product.dto';
  12. import { Product } from '../src/entity/product/product.entity';
  13. import { LanguageCode } from '../src/locale/language-code';
  14. import { TranslationInput } from '../src/locale/locale-types';
  15. // tslint:disable:no-console
  16. /**
  17. * A service for creating mock data via the GraphQL API.
  18. */
  19. export class MockDataClientService {
  20. apiUrl: string;
  21. constructor(config: VendureConfig) {
  22. this.apiUrl = `http://localhost:${config.port}/${config.apiPath}`;
  23. }
  24. async populateOptions(): Promise<any> {
  25. const query = `mutation($input: CreateProductOptionGroupInput) {
  26. createProductOptionGroup(input: $input) { id }
  27. }`;
  28. const variables = {
  29. input: {
  30. code: 'size',
  31. translations: [{ languageCode: 'en', name: 'Size' }, { languageCode: 'de', name: 'Größe' }],
  32. options: [
  33. {
  34. code: 'small',
  35. translations: [{ languageCode: 'en', name: 'Small' }, { languageCode: 'de', name: 'Klein' }],
  36. },
  37. {
  38. code: 'large',
  39. translations: [{ languageCode: 'en', name: 'Large' }, { languageCode: 'de', name: 'Groß' }],
  40. },
  41. ],
  42. } as CreateProductOptionGroupDto,
  43. };
  44. await request(this.apiUrl, query, variables).then(
  45. data => console.log('Created Administrator:', data),
  46. err => console.log(err),
  47. );
  48. console.log('created size options');
  49. }
  50. async populateAdmins(): Promise<any> {
  51. const query = `mutation($input: CreateAdministratorInput!) {
  52. createAdministrator(input: $input) { id, emailAddress }
  53. }`;
  54. const variables = {
  55. input: {
  56. firstName: 'Super',
  57. lastName: 'Admin',
  58. emailAddress: 'admin@test.com',
  59. password: 'test',
  60. } as CreateAdministratorDto,
  61. };
  62. await request(this.apiUrl, query, variables).then(
  63. data => console.log('Created Administrator:', data),
  64. err => console.log(err),
  65. );
  66. }
  67. async populateCustomers(count: number = 5): Promise<any> {
  68. const passwordService = new PasswordService();
  69. for (let i = 0; i < count; i++) {
  70. const firstName = faker.name.firstName();
  71. const lastName = faker.name.lastName();
  72. const query1 = `mutation CreateCustomer($input: CreateCustomerInput!, $password: String) {
  73. createCustomer(input: $input, password: $password) { id, emailAddress }
  74. }`;
  75. const variables1 = {
  76. input: {
  77. firstName,
  78. lastName,
  79. emailAddress: faker.internet.email(firstName, lastName),
  80. phoneNumber: faker.phone.phoneNumber(),
  81. } as CreateCustomerDto,
  82. password: 'test',
  83. };
  84. const customer: Customer | void = await request(this.apiUrl, query1, variables1).then(
  85. (data: any) => {
  86. console.log('Created Customer:', data);
  87. return data.createCustomer as Customer;
  88. },
  89. err => console.log(err),
  90. );
  91. if (customer) {
  92. const query2 = `mutation($customerId: ID!, $input: CreateAddressInput) {
  93. createCustomerAddress(customerId: $customerId, input: $input) {
  94. id
  95. streetLine1
  96. }
  97. }`;
  98. const variables2 = {
  99. input: {
  100. fullName: `${firstName} ${lastName}`,
  101. streetLine1: faker.address.streetAddress(),
  102. city: faker.address.city(),
  103. province: faker.address.county(),
  104. postalCode: faker.address.zipCode(),
  105. country: faker.address.country(),
  106. } as CreateAddressDto,
  107. customerId: customer.id,
  108. };
  109. await request(this.apiUrl, query2, variables2).then(
  110. data => {
  111. console.log('Created Customer:', data);
  112. return data as Customer;
  113. },
  114. err => console.log(err),
  115. );
  116. }
  117. }
  118. }
  119. async populateProducts(count: number = 5): Promise<any> {
  120. for (let i = 0; i < count; i++) {
  121. const query = `mutation CreateProduct($input: CreateProductInput) {
  122. createProduct(input: $input) { id, name }
  123. }`;
  124. const name = faker.commerce.productName();
  125. const slug = name.toLowerCase().replace(/\s+/g, '-');
  126. const description = faker.lorem.sentence();
  127. const languageCodes = [LanguageCode.EN, LanguageCode.DE, LanguageCode.ES];
  128. const variables = {
  129. input: {
  130. image: faker.image.imageUrl(),
  131. optionGroupCodes: ['size'],
  132. translations: languageCodes.map(code => this.makeProductTranslation(code, name, slug, description)),
  133. variants: [
  134. this.makeProductVariant(`${name} Variant 1`, languageCodes),
  135. this.makeProductVariant(`${name} Variant 2`, languageCodes),
  136. ],
  137. } as CreateProductDto,
  138. };
  139. await request(this.apiUrl, query, variables).then(
  140. data => console.log('Created Product:', data),
  141. err => console.log(err),
  142. );
  143. }
  144. }
  145. private makeProductTranslation(
  146. languageCode: LanguageCode,
  147. name: string,
  148. slug: string,
  149. description: string,
  150. ): TranslationInput<Product> {
  151. return {
  152. languageCode,
  153. name: `${languageCode} ${name}`,
  154. slug: `${languageCode} ${slug}`,
  155. description: `${languageCode} ${description}`,
  156. };
  157. }
  158. private makeProductVariant(variantName: string, languageCodes: LanguageCode[]): CreateProductVariantDto {
  159. return {
  160. price: faker.random.number({ min: 100, max: 5000 }),
  161. optionCodes: ['small'],
  162. sku: faker.random.alphaNumeric(8).toUpperCase(),
  163. translations: languageCodes.map(code => ({
  164. languageCode: code,
  165. name: `${variantName} ${code}`,
  166. })),
  167. };
  168. }
  169. }