mock-data.service.ts 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. import * as faker from "faker/locale/en_GB";
  2. import { Connection, createConnection } from "typeorm";
  3. import { CustomerEntity } from "../core/entity/customer/customer.entity";
  4. import { ProductVariantEntity } from "../core/entity/product-variant/product-variant.entity";
  5. import { ProductEntity } from "../core/entity/product/product.entity";
  6. import { ProductVariantTranslationEntity } from "../core/entity/product-variant/product-variant-translation.entity";
  7. import { AddressEntity } from "../core/entity/address/address.entity";
  8. import { Role } from "../core/auth/roles";
  9. import { ProductTranslationEntity } from "../core/entity/product/product-translation.entity";
  10. import { PasswordService } from "../core/auth/password.service";
  11. import { UserEntity } from "../core/entity/user/user.entity";
  12. import { AdministratorEntity } from "../core/entity/administrator/administrator.entity";
  13. /**
  14. * A Class used for generating mock data.
  15. */
  16. export class MockDataService {
  17. connection: Connection;
  18. populate(): Promise<any> {
  19. return createConnection({
  20. type: 'mysql',
  21. entities: ['./**/entity/**/*.entity.ts'],
  22. synchronize: true,
  23. logging: false,
  24. host: '192.168.99.100',
  25. port: 3306,
  26. username: 'root',
  27. password: '',
  28. database: 'test',
  29. }).then(async connection => {
  30. this.connection = connection;
  31. await this.clearAllTables();
  32. await this.populateCustomersAndAddresses();
  33. await this.populateProducts();
  34. await this.populateAdministrators();
  35. });
  36. }
  37. async clearAllTables() {
  38. await this.connection.synchronize(true);
  39. console.log('Cleared all tables');
  40. }
  41. async populateProducts() {
  42. for (let i = 0; i < 5; i++) {
  43. const product = new ProductEntity();
  44. product.image = faker.image.imageUrl();
  45. const name = faker.commerce.productName();
  46. const slug = name.toLowerCase().replace(/\s+/g, '-');
  47. const description = faker.lorem.sentence();
  48. const translation1 = this.makeProductTranslation('en', name, slug, description);
  49. const translation2 = this.makeProductTranslation('de', name, slug, description);
  50. await this.connection.manager.save(translation1);
  51. await this.connection.manager.save(translation2);
  52. // 1 - 4 variants
  53. const variantCount = Math.floor(Math.random() * 4) + 1;
  54. let variants = [];
  55. for (let j = 0; j < variantCount; j++) {
  56. const variant = new ProductVariantEntity();
  57. const variantName = `${name} variant ${j + 1}`;
  58. variant.image = faker.image.imageUrl();
  59. variant.price = faker.commerce.price(100, 12000, 0);
  60. const variantTranslation1 = this.makeProductVariantTranslation('en', variantName);
  61. const variantTranslation2 = this.makeProductVariantTranslation('de', variantName);
  62. await this.connection.manager.save(variantTranslation1);
  63. await this.connection.manager.save(variantTranslation2);
  64. variant.translations = [variantTranslation1, variantTranslation2];
  65. await this.connection.manager.save(variant);
  66. console.log(`${j + 1}. created product variant ${variantName}`);
  67. variants.push(variant);
  68. }
  69. product.variants = variants;
  70. product.translations = [translation1, translation2];
  71. await this.connection.manager.save(product);
  72. console.log(`${i + 1}. created product & translations for ${translation1.name}`);
  73. }
  74. }
  75. async populateCustomersAndAddresses() {
  76. const passwordService = new PasswordService();
  77. for (let i = 0; i < 5; i++) {
  78. const customer = new CustomerEntity();
  79. customer.firstName = faker.name.firstName();
  80. customer.lastName = faker.name.lastName();
  81. customer.emailAddress = faker.internet.email(customer.firstName, customer.lastName);
  82. customer.phoneNumber = faker.phone.phoneNumber();
  83. const user = new UserEntity();
  84. user.passwordHash = await passwordService.hash('test');
  85. user.identifier = customer.emailAddress;
  86. user.roles = [Role.Customer];
  87. await this.connection.manager.save(user);
  88. const address = new AddressEntity();
  89. address.fullName = `${customer.firstName} ${customer.lastName}`;
  90. address.streetLine1 = faker.address.streetAddress();
  91. address.city = faker.address.city();
  92. address.province = faker.address.county();
  93. address.postalCode = faker.address.zipCode();
  94. address.country = faker.address.countryCode();
  95. await this.connection.manager.save(address);
  96. customer.addresses = [address];
  97. customer.user = user;
  98. await this.connection.manager.save(customer);
  99. console.log('created customer, user and address for ' + customer.firstName + ' ' + customer.lastName);
  100. }
  101. }
  102. async populateAdministrators() {
  103. const passwordService = new PasswordService();
  104. const user = new UserEntity();
  105. user.passwordHash = await passwordService.hash('admin');
  106. user.identifier = 'admin';
  107. user.roles = [Role.Superadmin];
  108. await this.connection.manager.save(user);
  109. const administrator = new AdministratorEntity();
  110. administrator.emailAddress = 'admin@test.com';
  111. administrator.firstName = 'Super';
  112. administrator.lastName = 'Admin';
  113. administrator.user = user;
  114. await this.connection.manager.save(administrator);
  115. }
  116. private makeProductTranslation(
  117. langCode: string,
  118. name: string,
  119. slug: string,
  120. description: string,
  121. ): ProductTranslationEntity {
  122. const productTranslation = new ProductTranslationEntity();
  123. productTranslation.languageCode = langCode;
  124. productTranslation.name = `${langCode} ${name}`;
  125. productTranslation.slug = `${langCode} ${slug}`;
  126. productTranslation.description = `${langCode} ${description}`;
  127. return productTranslation;
  128. }
  129. private makeProductVariantTranslation(langCode: string, name: string): ProductVariantTranslationEntity {
  130. const productVariantTranslation = new ProductVariantTranslationEntity();
  131. productVariantTranslation.languageCode = langCode;
  132. productVariantTranslation.name = `${langCode} ${name}`;;
  133. return productVariantTranslation;
  134. }
  135. }