mock-data.service.ts 9.8 KB

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