import_loop.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. import { autocomplete_request } from "./autocomplete_request";
  2. import { create_request } from "./create_request";
  3. import { product_get_request } from "./product_get_request.js";
  4. export class import_loop {
  5. #content;
  6. #failed;
  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.#failed = new Array();
  40. this.#on_autocomplete = null;
  41. this.#on_create = null;
  42. this.#on_single_fail = null;
  43. this.#on_skip = null;
  44. this.#on_single_success = null;
  45. this.#finally = null;
  46. }
  47. async #autocomplete(target) {
  48. if (this.#on_autocomplete !== null) {
  49. try {
  50. this.#on_autocomplete(target);
  51. } catch (error) {
  52. console.log(error);
  53. }
  54. }
  55. const request = new autocomplete_request(target.barcode);
  56. const response = await request.connect();
  57. if (!response.result) {
  58. throw new Error(response.cause);
  59. }
  60. const found = response.found;
  61. target.description = found.description;
  62. if (found.image.length === 0) {
  63. throw new Error("Image for " + target.barcode + " not found.");
  64. }
  65. return new create_request(target, found.image);
  66. }
  67. async process() {
  68. for (const count of this.#content) {
  69. try {
  70. await this.#create(count);
  71. } catch(error) {
  72. console.log(error);
  73. if (this.#on_single_fail !== null) {
  74. try {
  75. this.#on_single_fail(count);
  76. } catch (error) {
  77. console.log(error);
  78. }
  79. }
  80. this.#failed.push(count);
  81. }
  82. }
  83. if (this.#finally !== null) {
  84. try {
  85. this.#finally(this.#failed);
  86. } catch (error) {
  87. console.log(error);
  88. }
  89. }
  90. return this;
  91. }
  92. async #exists(target) {
  93. const request = new product_get_request(target.barcode);
  94. const response = await request.connect();
  95. return response.product !== null;
  96. }
  97. async #create(target) {
  98. if (await this.#exists(target)) {
  99. try {
  100. this.#on_skip(target);
  101. } catch (error) {
  102. console.log(error);
  103. }
  104. return;
  105. }
  106. const request = await this.#autocomplete(target);
  107. if (this.on_create !== null) {
  108. try {
  109. this.#on_create(target);
  110. } catch (error) {
  111. console.log(error);
  112. }
  113. }
  114. const response = await request.connect();
  115. if (!response.result) {
  116. throw new Error(response.cause);
  117. }
  118. if (this.#on_single_success !== null) {
  119. try {
  120. this.#on_single_success(target);
  121. } catch (error) {
  122. console.log(error);
  123. }
  124. }
  125. }
  126. }