import_loop.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. import { autocomplete_request } from "./autocomplete_request";
  2. import { create_request } from "./create_request";
  3. import { import_process_fail } from "./import_process_fail.js";
  4. import { product_get_request } from "./product_get_request.js";
  5. export class import_loop {
  6. #content;
  7. #on_autocomplete;
  8. #on_create;
  9. #on_single_fail;
  10. #on_skip;
  11. #on_single_success;
  12. #finally;
  13. on_autocomplete(target) {
  14. this.#on_autocomplete = target;
  15. return this;
  16. }
  17. on_create(target) {
  18. this.#on_create = target;
  19. return this;
  20. }
  21. on_single_fail(target) {
  22. this.#on_single_fail = target;
  23. return this;
  24. }
  25. on_skip(target) {
  26. this.#on_skip = target;
  27. return this;
  28. }
  29. on_single_success(target) {
  30. this.#on_single_success = target;
  31. return this;
  32. }
  33. finally(target) {
  34. this.#finally = target;
  35. return this;
  36. }
  37. constructor(dataset) {
  38. this.#content = dataset;
  39. this.#on_autocomplete = null;
  40. this.#on_create = null;
  41. this.#on_single_fail = null;
  42. this.#on_skip = null;
  43. this.#on_single_success = null;
  44. this.#finally = null;
  45. }
  46. async #autocomplete(target) {
  47. if (this.#on_autocomplete !== null) {
  48. try {
  49. this.#on_autocomplete(target);
  50. } catch (error) {
  51. console.log(error);
  52. }
  53. }
  54. const request = new autocomplete_request(target.barcode);
  55. const response = await request.connect();
  56. if (!response.result) {
  57. throw new Error(response.cause);
  58. }
  59. const found = response.found;
  60. target.description = found.description;
  61. if (found.image.length === 0) {
  62. throw new Error("Image for " + target.barcode + " not found.");
  63. }
  64. return new create_request(target, found.image);
  65. }
  66. async process() {
  67. for (const count of this.#content) {
  68. try {
  69. await this.#create(count);
  70. } catch(error) {
  71. if (this.#on_single_fail !== null) {
  72. try {
  73. const fail = new import_process_fail(count, error);
  74. this.#on_single_fail(fail);
  75. } catch (error) {
  76. console.log(error);
  77. }
  78. }
  79. }
  80. }
  81. if (this.#finally !== null) {
  82. try {
  83. this.#finally();
  84. } catch (error) {
  85. console.log(error);
  86. }
  87. }
  88. return this;
  89. }
  90. async #exists(target) {
  91. const request = new product_get_request(target.barcode);
  92. const response = await request.connect();
  93. return response.product !== null;
  94. }
  95. async #create(target) {
  96. if (await this.#exists(target)) {
  97. try {
  98. const result = new import_process_fail(target, null);
  99. this.#on_skip(result);
  100. } catch (error) {
  101. console.log(error);
  102. }
  103. return;
  104. }
  105. const request = await this.#autocomplete(target);
  106. if (this.on_create !== null) {
  107. try {
  108. this.#on_create(target);
  109. } catch (error) {
  110. console.log(error);
  111. }
  112. }
  113. const response = await request.connect();
  114. if (!response.result) {
  115. throw new Error(response.cause);
  116. }
  117. if (this.#on_single_success !== null) {
  118. try {
  119. this.#on_single_success(target);
  120. } catch (error) {
  121. console.log(error);
  122. }
  123. }
  124. }
  125. }