mock-data-client.service.ts 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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: [
  36. { languageCode: 'en', name: 'Small' },
  37. { languageCode: 'de', name: 'Klein' },
  38. ],
  39. },
  40. {
  41. code: 'large',
  42. translations: [
  43. { languageCode: 'en', name: 'Large' },
  44. { languageCode: 'de', name: 'Groß' },
  45. ],
  46. },
  47. ],
  48. } as CreateProductOptionGroupDto,
  49. };
  50. await request(this.apiUrl, query, variables).then(
  51. data => console.log('Created Administrator:', data),
  52. err => console.log(err),
  53. );
  54. console.log('created size options');
  55. }
  56. async populateAdmins(): Promise<any> {
  57. const query = `mutation($input: CreateAdministratorInput!) {
  58. createAdministrator(input: $input) { id, emailAddress }
  59. }`;
  60. const variables = {
  61. input: {
  62. firstName: 'Super',
  63. lastName: 'Admin',
  64. emailAddress: 'admin@test.com',
  65. password: 'test',
  66. } as CreateAdministratorDto,
  67. };
  68. await request(this.apiUrl, query, variables).then(
  69. data => console.log('Created Administrator:', data),
  70. err => console.log(err),
  71. );
  72. }
  73. async populateCustomers(count: number = 5): Promise<any> {
  74. const passwordService = new PasswordService();
  75. for (let i = 0; i < count; i++) {
  76. const firstName = faker.name.firstName();
  77. const lastName = faker.name.lastName();
  78. const query1 = `mutation CreateCustomer($input: CreateCustomerInput!, $password: String) {
  79. createCustomer(input: $input, password: $password) { id, emailAddress }
  80. }`;
  81. const variables1 = {
  82. input: {
  83. firstName,
  84. lastName,
  85. emailAddress: faker.internet.email(firstName, lastName),
  86. phoneNumber: faker.phone.phoneNumber(),
  87. } as CreateCustomerDto,
  88. password: 'test',
  89. };
  90. const customer: Customer | void = await request(this.apiUrl, query1, variables1).then(
  91. (data: any) => {
  92. console.log('Created Customer:', data);
  93. return data.createCustomer as Customer;
  94. },
  95. err => console.log(err),
  96. );
  97. if (customer) {
  98. const query2 = `mutation($customerId: ID!, $input: CreateAddressInput) {
  99. createCustomerAddress(customerId: $customerId, input: $input) {
  100. id
  101. streetLine1
  102. }
  103. }`;
  104. const variables2 = {
  105. input: {
  106. fullName: `${firstName} ${lastName}`,
  107. streetLine1: faker.address.streetAddress(),
  108. city: faker.address.city(),
  109. province: faker.address.county(),
  110. postalCode: faker.address.zipCode(),
  111. country: faker.address.country(),
  112. } as CreateAddressDto,
  113. customerId: customer.id,
  114. };
  115. await request(this.apiUrl, query2, variables2).then(
  116. data => {
  117. console.log('Created Customer:', data);
  118. return data as Customer;
  119. },
  120. err => console.log(err),
  121. );
  122. }
  123. }
  124. }
  125. async populateProducts(count: number = 5): Promise<any> {
  126. for (let i = 0; i < count; i++) {
  127. const query = `mutation CreateProduct($input: CreateProductInput) {
  128. createProduct(input: $input) { id, name }
  129. }`;
  130. const name = faker.commerce.productName();
  131. const slug = name.toLowerCase().replace(/\s+/g, '-');
  132. const description = faker.lorem.sentence();
  133. const languageCodes = [LanguageCode.EN, LanguageCode.DE, LanguageCode.ES];
  134. const variables = {
  135. input: {
  136. image: faker.image.imageUrl(),
  137. optionGroupCodes: ['size'],
  138. translations: languageCodes.map(code =>
  139. this.makeProductTranslation(code, name, slug, description),
  140. ),
  141. variants: [
  142. this.makeProductVariant(`${name} Variant 1`, languageCodes),
  143. this.makeProductVariant(`${name} Variant 2`, languageCodes),
  144. ],
  145. } as CreateProductDto,
  146. };
  147. await request(this.apiUrl, query, variables).then(
  148. data => console.log('Created Product:', data),
  149. err => console.log(err),
  150. );
  151. }
  152. }
  153. private makeProductTranslation(
  154. languageCode: LanguageCode,
  155. name: string,
  156. slug: string,
  157. description: string,
  158. ): TranslationInput<Product> {
  159. return {
  160. languageCode,
  161. name: `${languageCode} ${name}`,
  162. slug: `${languageCode} ${slug}`,
  163. description: `${languageCode} ${description}`,
  164. };
  165. }
  166. private makeProductVariant(variantName: string, languageCodes: LanguageCode[]): CreateProductVariantDto {
  167. return {
  168. price: faker.random.number({ min: 100, max: 5000 }),
  169. optionCodes: ['small'],
  170. sku: faker.random.alphaNumeric(8).toUpperCase(),
  171. translations: languageCodes.map(code => ({
  172. languageCode: code,
  173. name: `${variantName} ${code}`,
  174. })),
  175. };
  176. }
  177. }