mock-data.service.ts 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. import * as faker from 'faker/locale/en_GB';
  2. import { Connection, createConnection } from 'typeorm';
  3. import { PasswordService } from '../core/auth/password.service';
  4. import { Role } from '../core/auth/role';
  5. import { AddressEntity } from '../core/entity/address/address.entity';
  6. import { AdministratorEntity } from '../core/entity/administrator/administrator.entity';
  7. import { CustomerEntity } from '../core/entity/customer/customer.entity';
  8. import { ProductOptionGroupTranslationEntity } from '../core/entity/product-option-group/product-option-group-translation.entity';
  9. import { ProductOptionGroupEntity } from '../core/entity/product-option-group/product-option-group.entity';
  10. import { ProductOptionTranslationEntity } from '../core/entity/product-option/product-option-translation.entity';
  11. import { ProductOptionEntity } from '../core/entity/product-option/product-option.entity';
  12. import { ProductVariantTranslationEntity } from '../core/entity/product-variant/product-variant-translation.entity';
  13. import { ProductVariantEntity } from '../core/entity/product-variant/product-variant.entity';
  14. import { ProductTranslationEntity } from '../core/entity/product/product-translation.entity';
  15. import { ProductEntity } from '../core/entity/product/product.entity';
  16. import { UserEntity } from '../core/entity/user/user.entity';
  17. // tslint:disable:no-console
  18. /**
  19. * A Class used for generating mock data.
  20. */
  21. export class MockDataService {
  22. connection: Connection;
  23. populate(): Promise<any> {
  24. return createConnection({
  25. type: 'mysql',
  26. entities: ['./**/entity/**/*.entity.ts'],
  27. synchronize: true,
  28. logging: false,
  29. host: '192.168.99.100',
  30. port: 3306,
  31. username: 'root',
  32. password: '',
  33. database: 'test',
  34. }).then(async connection => {
  35. this.connection = connection;
  36. await this.clearAllTables();
  37. await this.populateCustomersAndAddresses();
  38. await this.populateAdministrators();
  39. const sizeOptionGroup = await this.populateOptions();
  40. await this.populateProducts(sizeOptionGroup);
  41. });
  42. }
  43. async clearAllTables() {
  44. await this.connection.synchronize(true);
  45. console.log('Cleared all tables');
  46. }
  47. async populateOptions(): Promise<ProductOptionGroupEntity> {
  48. const sizeGroup = new ProductOptionGroupEntity();
  49. sizeGroup.code = 'size';
  50. const sizeGroupEN = new ProductOptionGroupTranslationEntity();
  51. sizeGroupEN.languageCode = 'en';
  52. sizeGroupEN.name = 'Size';
  53. await this.connection.manager.save(sizeGroupEN);
  54. const sizeGroupDE = new ProductOptionGroupTranslationEntity();
  55. sizeGroupDE.languageCode = 'de';
  56. sizeGroupDE.name = 'Größe';
  57. await this.connection.manager.save(sizeGroupDE);
  58. sizeGroup.translations = [sizeGroupEN, sizeGroupDE];
  59. await this.connection.manager.save(sizeGroup);
  60. await this.populateSizeOptions(sizeGroup);
  61. console.log('created size options');
  62. return sizeGroup;
  63. }
  64. private async populateSizeOptions(sizeGroup: ProductOptionGroupEntity) {
  65. const sizeSmall = new ProductOptionEntity();
  66. sizeSmall.code = 'small';
  67. const sizeSmallEN = new ProductOptionTranslationEntity();
  68. sizeSmallEN.languageCode = 'en';
  69. sizeSmallEN.name = 'Small';
  70. await this.connection.manager.save(sizeSmallEN);
  71. const sizeSmallDE = new ProductOptionTranslationEntity();
  72. sizeSmallDE.languageCode = 'de';
  73. sizeSmallDE.name = 'Klein';
  74. await this.connection.manager.save(sizeSmallDE);
  75. sizeSmall.translations = [sizeSmallEN, sizeSmallDE];
  76. sizeSmall.group = sizeGroup;
  77. await this.connection.manager.save(sizeSmall);
  78. const sizeLarge = new ProductOptionEntity();
  79. sizeLarge.code = 'large';
  80. const sizeLargeEN = new ProductOptionTranslationEntity();
  81. sizeLargeEN.languageCode = 'en';
  82. sizeLargeEN.name = 'Large';
  83. await this.connection.manager.save(sizeLargeEN);
  84. const sizeLargeDE = new ProductOptionTranslationEntity();
  85. sizeLargeDE.languageCode = 'de';
  86. sizeLargeDE.name = 'Groß';
  87. await this.connection.manager.save(sizeLargeDE);
  88. sizeLarge.translations = [sizeLargeEN, sizeLargeDE];
  89. sizeLarge.group = sizeGroup;
  90. await this.connection.manager.save(sizeLarge);
  91. sizeGroup.options = [sizeSmall, sizeLarge];
  92. }
  93. async populateProducts(optionGroup: ProductOptionGroupEntity) {
  94. for (let i = 0; i < 5; i++) {
  95. const addOption = i === 2 || i === 4;
  96. const product = new ProductEntity();
  97. product.image = faker.image.imageUrl();
  98. const name = faker.commerce.productName();
  99. const slug = name.toLowerCase().replace(/\s+/g, '-');
  100. const description = faker.lorem.sentence();
  101. const translation1 = this.makeProductTranslation('en', name, slug, description);
  102. const translation2 = this.makeProductTranslation('de', name, slug, description);
  103. await this.connection.manager.save(translation1);
  104. await this.connection.manager.save(translation2);
  105. // 1 - 4 variants
  106. const variantCount = Math.floor(Math.random() * 4) + 1;
  107. const variants = [];
  108. for (let j = 0; j < variantCount; j++) {
  109. const variant = new ProductVariantEntity();
  110. const variantName = `${name} variant ${j + 1}`;
  111. variant.image = faker.image.imageUrl();
  112. variant.price = faker.commerce.price(100, 12000, 0);
  113. const variantTranslation1 = this.makeProductVariantTranslation('en', variantName);
  114. const variantTranslation2 = this.makeProductVariantTranslation('de', variantName);
  115. await this.connection.manager.save(variantTranslation1);
  116. await this.connection.manager.save(variantTranslation2);
  117. if (addOption) {
  118. variant.options = [optionGroup.options[0]];
  119. } else {
  120. variant.options = [];
  121. }
  122. variant.translations = [variantTranslation1, variantTranslation2];
  123. await this.connection.manager.save(variant);
  124. console.log(`${j + 1}. created product variant ${variantName}`);
  125. variants.push(variant);
  126. }
  127. if (addOption) {
  128. product.optionGroups = [optionGroup];
  129. }
  130. product.variants = variants;
  131. product.translations = [translation1, translation2];
  132. await this.connection.manager.save(product);
  133. console.log(`${i + 1}. created product & translations for ${translation1.name}`);
  134. }
  135. }
  136. async populateCustomersAndAddresses() {
  137. const passwordService = new PasswordService();
  138. for (let i = 0; i < 5; i++) {
  139. const customer = new CustomerEntity();
  140. customer.firstName = faker.name.firstName();
  141. customer.lastName = faker.name.lastName();
  142. customer.emailAddress = faker.internet.email(customer.firstName, customer.lastName);
  143. customer.phoneNumber = faker.phone.phoneNumber();
  144. const user = new UserEntity();
  145. user.passwordHash = await passwordService.hash('test');
  146. user.identifier = customer.emailAddress;
  147. user.roles = [Role.Customer];
  148. await this.connection.manager.save(user);
  149. const address = new AddressEntity();
  150. address.fullName = `${customer.firstName} ${customer.lastName}`;
  151. address.streetLine1 = faker.address.streetAddress();
  152. address.city = faker.address.city();
  153. address.province = faker.address.county();
  154. address.postalCode = faker.address.zipCode();
  155. address.country = faker.address.countryCode();
  156. await this.connection.manager.save(address);
  157. customer.addresses = [address];
  158. customer.user = user;
  159. await this.connection.manager.save(customer);
  160. console.log('created customer, user and address for ' + customer.firstName + ' ' + customer.lastName);
  161. }
  162. }
  163. async populateAdministrators() {
  164. const passwordService = new PasswordService();
  165. const user = new UserEntity();
  166. user.passwordHash = await passwordService.hash('admin');
  167. user.identifier = 'admin';
  168. user.roles = [Role.Superadmin];
  169. await this.connection.manager.save(user);
  170. const administrator = new AdministratorEntity();
  171. administrator.emailAddress = 'admin@test.com';
  172. administrator.firstName = 'Super';
  173. administrator.lastName = 'Admin';
  174. administrator.user = user;
  175. await this.connection.manager.save(administrator);
  176. }
  177. private makeProductTranslation(
  178. langCode: string,
  179. name: string,
  180. slug: string,
  181. description: string,
  182. ): ProductTranslationEntity {
  183. const productTranslation = new ProductTranslationEntity();
  184. productTranslation.languageCode = langCode;
  185. productTranslation.name = `${langCode} ${name}`;
  186. productTranslation.slug = `${langCode} ${slug}`;
  187. productTranslation.description = `${langCode} ${description}`;
  188. return productTranslation;
  189. }
  190. private makeProductVariantTranslation(langCode: string, name: string): ProductVariantTranslationEntity {
  191. const productVariantTranslation = new ProductVariantTranslationEntity();
  192. productVariantTranslation.languageCode = langCode;
  193. productVariantTranslation.name = `${langCode} ${name}`;
  194. return productVariantTranslation;
  195. }
  196. }