addio-admin-sdk 1.6.13 → 1.6.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (499) hide show
  1. package/.babelrc +17 -17
  2. package/.env.dev +14 -14
  3. package/.github/workflows/publish_coverage.yaml +69 -69
  4. package/addio-sdk-doc.md +1380 -1380
  5. package/changelog.md +362 -353
  6. package/dist/Interfaces/Address/index.d.ts +21 -21
  7. package/dist/Interfaces/Address/index.js +16 -16
  8. package/dist/Interfaces/Algolia/index.d.ts +145 -145
  9. package/dist/Interfaces/Algolia/index.js +2 -2
  10. package/dist/Interfaces/Attachement/index.d.ts +5 -5
  11. package/dist/Interfaces/Attachement/index.js +2 -2
  12. package/dist/Interfaces/Attribute/index.d.ts +47 -47
  13. package/dist/Interfaces/Attribute/index.js +10 -10
  14. package/dist/Interfaces/BGJob/index.d.ts +56 -56
  15. package/dist/Interfaces/BGJob/index.js +36 -36
  16. package/dist/Interfaces/BaseClass/index.d.ts +8 -8
  17. package/dist/Interfaces/BaseClass/index.js +2 -2
  18. package/dist/Interfaces/Blog/IBlogArticle.d.ts +18 -18
  19. package/dist/Interfaces/Blog/IBlogArticle.js +2 -2
  20. package/dist/Interfaces/Blog/IBlogAuthor.d.ts +7 -7
  21. package/dist/Interfaces/Blog/IBlogAuthor.js +2 -2
  22. package/dist/Interfaces/Blog/IBlogCategory.d.ts +9 -9
  23. package/dist/Interfaces/Blog/IBlogCategory.js +2 -2
  24. package/dist/Interfaces/Blog/IBlogComment.d.ts +13 -13
  25. package/dist/Interfaces/Blog/IBlogComment.js +2 -2
  26. package/dist/Interfaces/Blog/IBlogKeywords.d.ts +9 -9
  27. package/dist/Interfaces/Blog/IBlogKeywords.js +2 -2
  28. package/dist/Interfaces/Booking/index.d.ts +68 -68
  29. package/dist/Interfaces/Booking/index.js +17 -17
  30. package/dist/Interfaces/Brand/index.d.ts +13 -13
  31. package/dist/Interfaces/Brand/index.js +2 -2
  32. package/dist/Interfaces/CMS/index.d.ts +25 -25
  33. package/dist/Interfaces/CMS/index.js +2 -2
  34. package/dist/Interfaces/Cart/IPandaPayload.d.ts +64 -64
  35. package/dist/Interfaces/Cart/IPandaPayload.js +18 -18
  36. package/dist/Interfaces/Cart/IPaymentCard.d.ts +22 -22
  37. package/dist/Interfaces/Cart/IPaymentCard.js +44 -44
  38. package/dist/Interfaces/Cart/IPaypalPayload.d.ts +9 -9
  39. package/dist/Interfaces/Cart/IPaypalPayload.js +2 -2
  40. package/dist/Interfaces/Cart/IPaysafePayload.d.ts +72 -72
  41. package/dist/Interfaces/Cart/IPaysafePayload.js +2 -2
  42. package/dist/Interfaces/Cart/IStripePayload.d.ts +66 -66
  43. package/dist/Interfaces/Cart/IStripePayload.js +2 -2
  44. package/dist/Interfaces/Cart/index.d.ts +397 -397
  45. package/dist/Interfaces/Cart/index.js +181 -181
  46. package/dist/Interfaces/Catalogue/index.d.ts +68 -68
  47. package/dist/Interfaces/Catalogue/index.js +37 -37
  48. package/dist/Interfaces/Category/index.d.ts +26 -26
  49. package/dist/Interfaces/Category/index.js +2 -2
  50. package/dist/Interfaces/Chart/index.d.ts +11 -11
  51. package/dist/Interfaces/Chart/index.js +2 -2
  52. package/dist/Interfaces/Class/index.d.ts +8 -8
  53. package/dist/Interfaces/Class/index.js +2 -2
  54. package/dist/Interfaces/Consent/index.d.ts +6 -6
  55. package/dist/Interfaces/Consent/index.js +2 -2
  56. package/dist/Interfaces/Context/IContextProps.d.ts +5 -5
  57. package/dist/Interfaces/Context/IContextProps.js +2 -2
  58. package/dist/Interfaces/Context/IContextState.d.ts +15 -15
  59. package/dist/Interfaces/Context/IContextState.js +2 -2
  60. package/dist/Interfaces/Customer/ICustomerBadge.d.ts +2 -2
  61. package/dist/Interfaces/Customer/ICustomerBadge.js +2 -2
  62. package/dist/Interfaces/Customer/ICustomerRating.d.ts +10 -10
  63. package/dist/Interfaces/Customer/ICustomerRating.js +2 -2
  64. package/dist/Interfaces/Customer/index.d.ts +92 -92
  65. package/dist/Interfaces/Customer/index.js +158 -158
  66. package/dist/Interfaces/Declination/index.d.ts +106 -106
  67. package/dist/Interfaces/Declination/index.js +64 -64
  68. package/dist/Interfaces/Document/index.d.ts +8 -8
  69. package/dist/Interfaces/Document/index.js +2 -2
  70. package/dist/Interfaces/Elastic/index.d.ts +89 -89
  71. package/dist/Interfaces/Elastic/index.js +2 -2
  72. package/dist/Interfaces/Email/CustomGR/I3DPrintEmail.d.ts +11 -11
  73. package/dist/Interfaces/Email/CustomGR/I3DPrintEmail.js +2 -2
  74. package/dist/Interfaces/Email/CustomGR/IBorrowToolsEmail.d.ts +15 -15
  75. package/dist/Interfaces/Email/CustomGR/IBorrowToolsEmail.js +2 -2
  76. package/dist/Interfaces/Email/CustomGR/IPickupMaterialsEmail.d.ts +12 -12
  77. package/dist/Interfaces/Email/CustomGR/IPickupMaterialsEmail.js +2 -2
  78. package/dist/Interfaces/Email/CustomGR/IPreMeetingFormEmail.d.ts +36 -36
  79. package/dist/Interfaces/Email/CustomGR/IPreMeetingFormEmail.js +2 -2
  80. package/dist/Interfaces/Email/CustomGR/ISponsorRequestEmail.d.ts +12 -12
  81. package/dist/Interfaces/Email/CustomGR/ISponsorRequestEmail.js +2 -2
  82. package/dist/Interfaces/Email/CustomGR/ITransportEmail.d.ts +12 -12
  83. package/dist/Interfaces/Email/CustomGR/ITransportEmail.js +2 -2
  84. package/dist/Interfaces/Email/CustomGR/ITransportQuoteEmail.d.ts +12 -12
  85. package/dist/Interfaces/Email/CustomGR/ITransportQuoteEmail.js +2 -2
  86. package/dist/Interfaces/Email/CustomGR/IVegetalsEmail.d.ts +16 -16
  87. package/dist/Interfaces/Email/CustomGR/IVegetalsEmail.js +2 -2
  88. package/dist/Interfaces/Email/CustomGR/IWishlistEmail.d.ts +15 -15
  89. package/dist/Interfaces/Email/CustomGR/IWishlistEmail.js +2 -2
  90. package/dist/Interfaces/Email/IAbandonnedCartEmail.d.ts +29 -29
  91. package/dist/Interfaces/Email/IAbandonnedCartEmail.js +2 -2
  92. package/dist/Interfaces/Email/IAccountConfirmationEmail.d.ts +22 -22
  93. package/dist/Interfaces/Email/IAccountConfirmationEmail.js +2 -2
  94. package/dist/Interfaces/Email/IApplicationEmail.d.ts +19 -19
  95. package/dist/Interfaces/Email/IApplicationEmail.js +2 -2
  96. package/dist/Interfaces/Email/IBackInStockEmail.d.ts +21 -21
  97. package/dist/Interfaces/Email/IBackInStockEmail.js +2 -2
  98. package/dist/Interfaces/Email/IContactEmail.d.ts +20 -20
  99. package/dist/Interfaces/Email/IContactEmail.js +2 -2
  100. package/dist/Interfaces/Email/ICustomerBookingAdminEmail.d.ts +15 -15
  101. package/dist/Interfaces/Email/ICustomerBookingAdminEmail.js +2 -2
  102. package/dist/Interfaces/Email/ICustomerBookingEmail.d.ts +14 -14
  103. package/dist/Interfaces/Email/ICustomerBookingEmail.js +2 -2
  104. package/dist/Interfaces/Email/IEmail.d.ts +20 -20
  105. package/dist/Interfaces/Email/IEmail.js +69 -69
  106. package/dist/Interfaces/Email/IOrderConfirmationEmail.d.ts +47 -47
  107. package/dist/Interfaces/Email/IOrderConfirmationEmail.js +2 -2
  108. package/dist/Interfaces/Email/IPasswordResetEmail.d.ts +17 -17
  109. package/dist/Interfaces/Email/IPasswordResetEmail.js +2 -2
  110. package/dist/Interfaces/Email/IPhotoShootEmail.d.ts +14 -14
  111. package/dist/Interfaces/Email/IPhotoShootEmail.js +2 -2
  112. package/dist/Interfaces/Email/IPickupConfirmation.d.ts +23 -23
  113. package/dist/Interfaces/Email/IPickupConfirmation.js +2 -2
  114. package/dist/Interfaces/Email/IShippingConfirmation.d.ts +23 -23
  115. package/dist/Interfaces/Email/IShippingConfirmation.js +2 -2
  116. package/dist/Interfaces/Email/IWelcomeEmail.d.ts +19 -19
  117. package/dist/Interfaces/Email/IWelcomeEmail.js +2 -2
  118. package/dist/Interfaces/ExportSchema/index.d.ts +7 -7
  119. package/dist/Interfaces/ExportSchema/index.js +2 -2
  120. package/dist/Interfaces/ExternalService/BaseExternalService.d.ts +12 -12
  121. package/dist/Interfaces/ExternalService/BaseExternalService.js +2 -2
  122. package/dist/Interfaces/ExternalService/index.d.ts +9 -9
  123. package/dist/Interfaces/ExternalService/index.js +2 -2
  124. package/dist/Interfaces/G2/index.d.ts +286 -286
  125. package/dist/Interfaces/G2/index.js +31 -31
  126. package/dist/Interfaces/Indexed/Product/IIndexedLastSavedProduct.d.ts +20 -20
  127. package/dist/Interfaces/Indexed/Product/IIndexedLastSavedProduct.js +2 -2
  128. package/dist/Interfaces/Indexed/Product/index.d.ts +91 -91
  129. package/dist/Interfaces/Indexed/Product/index.js +315 -315
  130. package/dist/Interfaces/Inventory/index.d.ts +19 -19
  131. package/dist/Interfaces/Inventory/index.js +8 -8
  132. package/dist/Interfaces/Invoice/index.d.ts +4 -4
  133. package/dist/Interfaces/Invoice/index.js +2 -2
  134. package/dist/Interfaces/MarketplacePublication/MarketplaceOrderData.d.ts +3 -3
  135. package/dist/Interfaces/MarketplacePublication/MarketplaceOrderData.js +2 -2
  136. package/dist/Interfaces/MarketplacePublication/index.d.ts +23 -23
  137. package/dist/Interfaces/MarketplacePublication/index.js +33 -33
  138. package/dist/Interfaces/Operations/ICreatedBy.d.ts +6 -6
  139. package/dist/Interfaces/Operations/ICreatedBy.js +2 -2
  140. package/dist/Interfaces/Operations/IUpdate.d.ts +5 -5
  141. package/dist/Interfaces/Operations/IUpdate.js +2 -2
  142. package/dist/Interfaces/Order/index.d.ts +4 -4
  143. package/dist/Interfaces/Order/index.js +2 -2
  144. package/dist/Interfaces/Org/IRelUser.d.ts +9 -9
  145. package/dist/Interfaces/Org/IRelUser.js +2 -2
  146. package/dist/Interfaces/Org/index.d.ts +29 -29
  147. package/dist/Interfaces/Org/index.js +12 -12
  148. package/dist/Interfaces/Payment/index.d.ts +11 -11
  149. package/dist/Interfaces/Payment/index.js +2 -2
  150. package/dist/Interfaces/Product/IAttributeFamily.d.ts +6 -6
  151. package/dist/Interfaces/Product/IAttributeFamily.js +2 -2
  152. package/dist/Interfaces/Product/IImportProduct.d.ts +42 -42
  153. package/dist/Interfaces/Product/IImportProduct.js +966 -966
  154. package/dist/Interfaces/Product/IInventoryItem.d.ts +81 -81
  155. package/dist/Interfaces/Product/IInventoryItem.js +36 -36
  156. package/dist/Interfaces/Product/IPricing.d.ts +177 -177
  157. package/dist/Interfaces/Product/IPricing.js +174 -174
  158. package/dist/Interfaces/Product/index.d.ts +121 -121
  159. package/dist/Interfaces/Product/index.js +56 -56
  160. package/dist/Interfaces/ProductAttribute/index.d.ts +11 -11
  161. package/dist/Interfaces/ProductAttribute/index.js +2 -2
  162. package/dist/Interfaces/PromiseToPurchase/BankType.d.ts +41 -41
  163. package/dist/Interfaces/PromiseToPurchase/BankType.js +42 -42
  164. package/dist/Interfaces/PromiseToPurchase/IIndexedBankUsed.d.ts +33 -33
  165. package/dist/Interfaces/PromiseToPurchase/IIndexedBankUsed.js +15 -15
  166. package/dist/Interfaces/PromiseToPurchase/IIndexedCustomerPromiseToPurchase.d.ts +19 -19
  167. package/dist/Interfaces/PromiseToPurchase/IIndexedCustomerPromiseToPurchase.js +2 -2
  168. package/dist/Interfaces/PromiseToPurchase/index.d.ts +50 -50
  169. package/dist/Interfaces/PromiseToPurchase/index.js +13 -13
  170. package/dist/Interfaces/PurchaseOrder/index.d.ts +17 -17
  171. package/dist/Interfaces/PurchaseOrder/index.js +2 -2
  172. package/dist/Interfaces/Query/IBrandQueryOptions.d.ts +11 -11
  173. package/dist/Interfaces/Query/IBrandQueryOptions.js +2 -2
  174. package/dist/Interfaces/Query/ICategoryQueryOptions.d.ts +11 -11
  175. package/dist/Interfaces/Query/ICategoryQueryOptions.js +2 -2
  176. package/dist/Interfaces/Query/IProductQueryOptions.d.ts +15 -15
  177. package/dist/Interfaces/Query/IProductQueryOptions.js +2 -2
  178. package/dist/Interfaces/Quote/index.d.ts +5 -5
  179. package/dist/Interfaces/Quote/index.js +2 -2
  180. package/dist/Interfaces/Rules/IDiscountRule.d.ts +131 -131
  181. package/dist/Interfaces/Rules/IDiscountRule.js +95 -95
  182. package/dist/Interfaces/Services/IAddServiceResponse.d.ts +4 -4
  183. package/dist/Interfaces/Services/IAddServiceResponse.js +2 -2
  184. package/dist/Interfaces/Services/IService.d.ts +12 -12
  185. package/dist/Interfaces/Services/IService.js +2 -2
  186. package/dist/Interfaces/Services/IServiceSecret.d.ts +8 -8
  187. package/dist/Interfaces/Services/IServiceSecret.js +2 -2
  188. package/dist/Interfaces/Shipping/Shipstation/carriers.d.ts +10 -10
  189. package/dist/Interfaces/Shipping/Shipstation/carriers.js +2 -2
  190. package/dist/Interfaces/Shipping/Shipstation/order.d.ts +227 -227
  191. package/dist/Interfaces/Shipping/Shipstation/order.js +2 -2
  192. package/dist/Interfaces/Shipping/Shipstation/quote.d.ts +31 -31
  193. package/dist/Interfaces/Shipping/Shipstation/quote.js +2 -2
  194. package/dist/Interfaces/Shipping/Shiptime/order.d.ts +54 -54
  195. package/dist/Interfaces/Shipping/Shiptime/order.js +48 -48
  196. package/dist/Interfaces/Shipping/Shiptime/quote.d.ts +111 -111
  197. package/dist/Interfaces/Shipping/Shiptime/quote.js +2 -2
  198. package/dist/Interfaces/Slider/GR.d.ts +130 -130
  199. package/dist/Interfaces/Slider/GR.js +271 -271
  200. package/dist/Interfaces/Slider/index.d.ts +14 -14
  201. package/dist/Interfaces/Slider/index.js +2 -2
  202. package/dist/Interfaces/Space/IMember.d.ts +16 -16
  203. package/dist/Interfaces/Space/IMember.js +9 -9
  204. package/dist/Interfaces/Space/index.d.ts +311 -311
  205. package/dist/Interfaces/Space/index.js +77 -77
  206. package/dist/Interfaces/Specials/gr.d.ts +61 -61
  207. package/dist/Interfaces/Specials/gr.js +2 -2
  208. package/dist/Interfaces/StockNotification/index.d.ts +7 -7
  209. package/dist/Interfaces/StockNotification/index.js +2 -2
  210. package/dist/Interfaces/Store/index.d.ts +60 -60
  211. package/dist/Interfaces/Store/index.js +27 -27
  212. package/dist/Interfaces/Supplier/ICommand.d.ts +11 -11
  213. package/dist/Interfaces/Supplier/ICommand.js +2 -2
  214. package/dist/Interfaces/Supplier/index.d.ts +36 -36
  215. package/dist/Interfaces/Supplier/index.js +24 -24
  216. package/dist/Interfaces/Tag/index.d.ts +18 -18
  217. package/dist/Interfaces/Tag/index.js +2 -2
  218. package/dist/Interfaces/Tax/index.d.ts +12 -12
  219. package/dist/Interfaces/Tax/index.js +2 -2
  220. package/dist/Interfaces/TransferRequest/index.d.ts +32 -32
  221. package/dist/Interfaces/TransferRequest/index.js +26 -26
  222. package/dist/Interfaces/Translation/index.d.ts +4 -4
  223. package/dist/Interfaces/Translation/index.js +2 -2
  224. package/dist/Interfaces/Update/index.d.ts +10 -10
  225. package/dist/Interfaces/Update/index.js +2 -2
  226. package/dist/Interfaces/User/IUser.d.ts +29 -29
  227. package/dist/Interfaces/User/IUser.js +8 -8
  228. package/dist/Interfaces/Version/index.d.ts +11 -11
  229. package/dist/Interfaces/Version/index.js +2 -2
  230. package/dist/Interfaces/Wishlist/index.d.ts +7 -7
  231. package/dist/Interfaces/Wishlist/index.js +2 -2
  232. package/dist/constants/Interfaces/IBaseApi.d.ts +15 -15
  233. package/dist/constants/Interfaces/IBaseApi.js +2 -2
  234. package/dist/constants/Interfaces/IEndpointResult.d.ts +9 -9
  235. package/dist/constants/Interfaces/IEndpointResult.js +2 -2
  236. package/dist/constants/enums.d.ts +6 -6
  237. package/dist/constants/enums.js +10 -10
  238. package/dist/constants/version.d.ts +2 -2
  239. package/dist/constants/version.js +5 -5
  240. package/dist/index.d.ts +32 -32
  241. package/dist/index.js +416 -416
  242. package/dist/lib/Attribute/index.d.ts +14 -14
  243. package/dist/lib/Attribute/index.js +249 -249
  244. package/dist/lib/BGJob/index.d.ts +9 -9
  245. package/dist/lib/BGJob/index.js +376 -376
  246. package/dist/lib/BackgroundWorker/index.d.ts +34 -34
  247. package/dist/lib/BackgroundWorker/index.js +72 -72
  248. package/dist/lib/Blog/BlogArticle.d.ts +5 -5
  249. package/dist/lib/Blog/BlogArticle.js +28 -28
  250. package/dist/lib/Blog/BlogAuthor.d.ts +5 -5
  251. package/dist/lib/Blog/BlogAuthor.js +28 -28
  252. package/dist/lib/Blog/BlogCategory.d.ts +5 -5
  253. package/dist/lib/Blog/BlogCategory.js +28 -28
  254. package/dist/lib/Blog/BlogKeyword.d.ts +5 -5
  255. package/dist/lib/Blog/BlogKeyword.js +28 -28
  256. package/dist/lib/Booking/index.d.ts +63 -63
  257. package/dist/lib/Booking/index.js +813 -813
  258. package/dist/lib/Brand/index.d.ts +17 -17
  259. package/dist/lib/Brand/index.js +276 -276
  260. package/dist/lib/Cart/index.d.ts +184 -184
  261. package/dist/lib/Cart/index.js +3653 -3653
  262. package/dist/lib/Catalogue/index.d.ts +9 -9
  263. package/dist/lib/Catalogue/index.js +208 -208
  264. package/dist/lib/Category/index.d.ts +26 -26
  265. package/dist/lib/Category/index.js +361 -361
  266. package/dist/lib/Class/index.d.ts +12 -12
  267. package/dist/lib/Class/index.js +159 -159
  268. package/dist/lib/Consent/index.d.ts +4 -4
  269. package/dist/lib/Consent/index.js +79 -79
  270. package/dist/lib/Customer/index.d.ts +10 -10
  271. package/dist/lib/Customer/index.js +140 -140
  272. package/dist/lib/Declination/index.d.ts +10 -10
  273. package/dist/lib/Declination/index.js +85 -85
  274. package/dist/lib/Discount/index.d.ts +18 -18
  275. package/dist/lib/Discount/index.js +759 -759
  276. package/dist/lib/ElasticSearch/index.d.ts +8 -8
  277. package/dist/lib/ElasticSearch/index.js +101 -101
  278. package/dist/lib/Email/AbandonedCartEmail.d.ts +8 -8
  279. package/dist/lib/Email/AbandonedCartEmail.js +78 -78
  280. package/dist/lib/Email/AccountConfirmationEmail.d.ts +11 -11
  281. package/dist/lib/Email/AccountConfirmationEmail.js +95 -95
  282. package/dist/lib/Email/AdminOrderConfirmationEmail.d.ts +8 -8
  283. package/dist/lib/Email/AdminOrderConfirmationEmail.js +78 -78
  284. package/dist/lib/Email/ApplicationEmail.d.ts +8 -8
  285. package/dist/lib/Email/ApplicationEmail.js +78 -78
  286. package/dist/lib/Email/BackInStockEmail.d.ts +8 -8
  287. package/dist/lib/Email/BackInStockEmail.js +78 -78
  288. package/dist/lib/Email/ContactEmail.d.ts +8 -8
  289. package/dist/lib/Email/ContactEmail.js +78 -78
  290. package/dist/lib/Email/CustomerBookingAdminEmail.d.ts +8 -8
  291. package/dist/lib/Email/CustomerBookingAdminEmail.js +78 -78
  292. package/dist/lib/Email/CustomerBookingEmail.d.ts +8 -8
  293. package/dist/lib/Email/CustomerBookingEmail.js +78 -78
  294. package/dist/lib/Email/OrderConfirmationEmail.d.ts +8 -8
  295. package/dist/lib/Email/OrderConfirmationEmail.js +78 -78
  296. package/dist/lib/Email/OrderDenounciationEmail.d.ts +8 -8
  297. package/dist/lib/Email/OrderDenounciationEmail.js +78 -78
  298. package/dist/lib/Email/OrderSampleEmail.d.ts +8 -8
  299. package/dist/lib/Email/OrderSampleEmail.js +78 -78
  300. package/dist/lib/Email/OtherTypeEmails.d.ts +8 -8
  301. package/dist/lib/Email/OtherTypeEmails.js +90 -90
  302. package/dist/lib/Email/PasswordResetEmail.d.ts +8 -8
  303. package/dist/lib/Email/PasswordResetEmail.js +78 -78
  304. package/dist/lib/Email/PhotoShootEmail.d.ts +8 -8
  305. package/dist/lib/Email/PhotoShootEmail.js +78 -78
  306. package/dist/lib/Email/PickUpConfirmation.d.ts +8 -8
  307. package/dist/lib/Email/PickUpConfirmation.js +78 -78
  308. package/dist/lib/Email/ShippingConfirmation.d.ts +8 -8
  309. package/dist/lib/Email/ShippingConfirmation.js +78 -78
  310. package/dist/lib/Email/WelcomeEmail.d.ts +8 -8
  311. package/dist/lib/Email/WelcomeEmail.js +78 -78
  312. package/dist/lib/Email/index.d.ts +8 -8
  313. package/dist/lib/Email/index.js +82 -82
  314. package/dist/lib/ExternalService/Ebay.d.ts +63 -63
  315. package/dist/lib/ExternalService/Ebay.js +185 -185
  316. package/dist/lib/ExternalService/index.d.ts +5 -5
  317. package/dist/lib/ExternalService/index.js +28 -28
  318. package/dist/lib/Indexed/IndexedCart.d.ts +13 -13
  319. package/dist/lib/Indexed/IndexedCart.js +194 -194
  320. package/dist/lib/Indexed/Product.d.ts +17 -17
  321. package/dist/lib/Indexed/Product.js +116 -116
  322. package/dist/lib/Inventory/index.d.ts +31 -31
  323. package/dist/lib/Inventory/index.js +560 -560
  324. package/dist/lib/Invoice/index.d.ts +5 -5
  325. package/dist/lib/Invoice/index.js +28 -28
  326. package/dist/lib/MarketplacePublication/index.d.ts +9 -9
  327. package/dist/lib/MarketplacePublication/index.js +221 -221
  328. package/dist/lib/MongoDB/BaseAPI.d.ts +18 -18
  329. package/dist/lib/MongoDB/BaseAPI.js +308 -308
  330. package/dist/lib/Order/index.d.ts +7 -7
  331. package/dist/lib/Order/index.js +62 -62
  332. package/dist/lib/Org/index.d.ts +26 -26
  333. package/dist/lib/Org/index.js +286 -286
  334. package/dist/lib/PandaPay/index.d.ts +16 -16
  335. package/dist/lib/PandaPay/index.js +219 -219
  336. package/dist/lib/Paysafe/index.d.ts +11 -11
  337. package/dist/lib/Paysafe/index.js +100 -100
  338. package/dist/lib/Product/index.d.ts +143 -143
  339. package/dist/lib/Product/index.js +1326 -1326
  340. package/dist/lib/ProductAttribute/index.d.ts +15 -15
  341. package/dist/lib/ProductAttribute/index.js +114 -114
  342. package/dist/lib/PromiseToPurchase/index.d.ts +35 -35
  343. package/dist/lib/PromiseToPurchase/index.js +474 -474
  344. package/dist/lib/PurchaseOrder/index.d.ts +9 -9
  345. package/dist/lib/PurchaseOrder/index.js +72 -72
  346. package/dist/lib/Queue/index.d.ts +40 -40
  347. package/dist/lib/Queue/index.js +254 -254
  348. package/dist/lib/Service/Enum/ServiceEnum.d.ts +3 -3
  349. package/dist/lib/Service/Enum/ServiceEnum.js +7 -7
  350. package/dist/lib/Service/index.d.ts +5 -5
  351. package/dist/lib/Service/index.js +57 -57
  352. package/dist/lib/Shipment/index.d.ts +28 -28
  353. package/dist/lib/Shipment/index.js +559 -559
  354. package/dist/lib/Slider/index.d.ts +16 -16
  355. package/dist/lib/Slider/index.js +235 -235
  356. package/dist/lib/Space/index.d.ts +769 -769
  357. package/dist/lib/Space/index.js +6688 -6688
  358. package/dist/lib/StockNotification/index.d.ts +4 -4
  359. package/dist/lib/StockNotification/index.js +26 -26
  360. package/dist/lib/Store/index.d.ts +5 -5
  361. package/dist/lib/Store/index.js +30 -30
  362. package/dist/lib/Supplier/Command.d.ts +5 -5
  363. package/dist/lib/Supplier/Command.js +28 -28
  364. package/dist/lib/Supplier/index.d.ts +25 -25
  365. package/dist/lib/Supplier/index.js +92 -92
  366. package/dist/lib/Tags/index.d.ts +6 -6
  367. package/dist/lib/Tags/index.js +84 -84
  368. package/dist/lib/Tax/index.d.ts +8 -8
  369. package/dist/lib/Tax/index.js +136 -136
  370. package/dist/lib/TransferRequest/index.d.ts +21 -21
  371. package/dist/lib/TransferRequest/index.js +302 -302
  372. package/dist/lib/Update/index.d.ts +10 -10
  373. package/dist/lib/Update/index.js +71 -71
  374. package/dist/lib/User/index.d.ts +33 -33
  375. package/dist/lib/User/index.js +360 -360
  376. package/dist/lib/Wishlist/index.d.ts +5 -5
  377. package/dist/lib/Wishlist/index.js +28 -28
  378. package/dist/lib/base.d.ts +14 -14
  379. package/dist/lib/base.js +135 -135
  380. package/dist/lib/baseService.d.ts +17 -17
  381. package/dist/lib/baseService.js +113 -113
  382. package/dist/rules/GR/constants/data.d.ts +28 -28
  383. package/dist/rules/GR/constants/data.js +57 -57
  384. package/dist/rules/GR/constants/products.d.ts +8 -8
  385. package/dist/rules/GR/constants/products.js +234 -234
  386. package/dist/rules/GR/interfaces/cart.d.ts +68 -68
  387. package/dist/rules/GR/interfaces/cart.js +49 -49
  388. package/dist/rules/GR/interfaces/data.d.ts +97 -97
  389. package/dist/rules/GR/interfaces/data.js +104 -104
  390. package/dist/rules/GR/interfaces/products.d.ts +61 -61
  391. package/dist/rules/GR/interfaces/products.js +10 -10
  392. package/dist/rules/GR/utils/cart.d.ts +39 -39
  393. package/dist/rules/GR/utils/cart.js +644 -644
  394. package/dist/rules/GR/utils/data.d.ts +3 -3
  395. package/dist/rules/GR/utils/data.js +25 -25
  396. package/dist/rules/GR/utils/expedition.d.ts +70 -70
  397. package/dist/rules/GR/utils/expedition.js +1584 -1584
  398. package/dist/rules/GR/utils/expedition.js.map +1 -1
  399. package/dist/rules/GR/utils/g2.d.ts +153 -153
  400. package/dist/rules/GR/utils/g2.js +914 -914
  401. package/dist/rules/GR/utils/invoices.d.ts +5 -5
  402. package/dist/rules/GR/utils/invoices.js +140 -140
  403. package/dist/rules/GR/utils/products.d.ts +105 -105
  404. package/dist/rules/GR/utils/products.js +1013 -1013
  405. package/dist/rules/GR/utils/upsells.d.ts +75 -75
  406. package/dist/rules/GR/utils/upsells.js +278 -278
  407. package/dist/services/database/BaseProvider.d.ts +28 -28
  408. package/dist/services/database/BaseProvider.js +77 -77
  409. package/dist/services/database/BatchUtils.d.ts +14 -14
  410. package/dist/services/database/BatchUtils.js +88 -88
  411. package/dist/services/database/Config.d.ts +2 -2
  412. package/dist/services/database/Config.js +5 -5
  413. package/dist/services/database/DatabaseService.d.ts +41 -41
  414. package/dist/services/database/DatabaseService.js +311 -311
  415. package/dist/services/database/FirebaseProvider.d.ts +17 -17
  416. package/dist/services/database/FirebaseProvider.js +300 -300
  417. package/dist/services/database/NotFirebaseProvider.d.ts +35 -35
  418. package/dist/services/database/NotFirebaseProvider.js +367 -367
  419. package/dist/utils/algolia.d.ts +6 -6
  420. package/dist/utils/algolia.js +22 -22
  421. package/dist/utils/anonymisation.d.ts +30 -30
  422. package/dist/utils/anonymisation.js +269 -269
  423. package/dist/utils/array.d.ts +5 -5
  424. package/dist/utils/array.js +36 -36
  425. package/dist/utils/aws.d.ts +14 -14
  426. package/dist/utils/aws.js +220 -220
  427. package/dist/utils/booking.d.ts +22 -22
  428. package/dist/utils/booking.js +175 -175
  429. package/dist/utils/cart.d.ts +511 -511
  430. package/dist/utils/cart.js +503 -503
  431. package/dist/utils/cmv.d.ts +10 -10
  432. package/dist/utils/cmv.js +107 -107
  433. package/dist/utils/console.d.ts +6 -6
  434. package/dist/utils/console.js +121 -121
  435. package/dist/utils/context.d.ts +19 -19
  436. package/dist/utils/context.js +41 -41
  437. package/dist/utils/currency.d.ts +6 -6
  438. package/dist/utils/currency.js +62 -62
  439. package/dist/utils/data.d.ts +40 -40
  440. package/dist/utils/data.js +226 -226
  441. package/dist/utils/date.d.ts +1 -1
  442. package/dist/utils/date.js +16 -16
  443. package/dist/utils/dimensions.d.ts +3 -3
  444. package/dist/utils/dimensions.js +18 -18
  445. package/dist/utils/discount.d.ts +33 -33
  446. package/dist/utils/discount.js +906 -906
  447. package/dist/utils/errors.d.ts +113 -113
  448. package/dist/utils/errors.js +113 -113
  449. package/dist/utils/file-admin.d.ts +8 -8
  450. package/dist/utils/file-admin.js +94 -94
  451. package/dist/utils/file.d.ts +38 -38
  452. package/dist/utils/file.js +318 -318
  453. package/dist/utils/firebase-admin.d.ts +2 -2
  454. package/dist/utils/firebase-admin.js +118 -118
  455. package/dist/utils/firebase.d.ts +41 -41
  456. package/dist/utils/firebase.js +220 -220
  457. package/dist/utils/import.d.ts +1 -1
  458. package/dist/utils/import.js +81 -81
  459. package/dist/utils/inventories.d.ts +26 -26
  460. package/dist/utils/inventories.js +355 -355
  461. package/dist/utils/locale.d.ts +13 -13
  462. package/dist/utils/locale.js +67 -67
  463. package/dist/utils/mailchimp.d.ts +57 -57
  464. package/dist/utils/mailchimp.js +735 -735
  465. package/dist/utils/mongodb.d.ts +1 -1
  466. package/dist/utils/mongodb.js +68 -68
  467. package/dist/utils/object.d.ts +6 -6
  468. package/dist/utils/object.js +124 -124
  469. package/dist/utils/payment.d.ts +10 -10
  470. package/dist/utils/payment.js +25 -25
  471. package/dist/utils/products.d.ts +26 -26
  472. package/dist/utils/products.js +301 -301
  473. package/dist/utils/promiseToPurchase.d.ts +56 -56
  474. package/dist/utils/promiseToPurchase.js +228 -228
  475. package/dist/utils/prosprsearch.d.ts +21 -21
  476. package/dist/utils/prosprsearch.js +276 -276
  477. package/dist/utils/sdc.d.ts +10 -10
  478. package/dist/utils/sdc.js +1308 -1308
  479. package/dist/utils/searchIndexes.d.ts +20 -20
  480. package/dist/utils/searchIndexes.js +273 -273
  481. package/dist/utils/services.d.ts +38 -38
  482. package/dist/utils/services.js +394 -394
  483. package/dist/utils/shipment.d.ts +110 -110
  484. package/dist/utils/shipment.js +410 -410
  485. package/dist/utils/simpletexting.d.ts +2 -2
  486. package/dist/utils/simpletexting.js +88 -88
  487. package/dist/utils/sliders.d.ts +4 -4
  488. package/dist/utils/sliders.js +70 -70
  489. package/dist/utils/string.d.ts +16 -16
  490. package/dist/utils/string.js +194 -194
  491. package/dist/utils/update.d.ts +7 -7
  492. package/dist/utils/update.js +244 -244
  493. package/dist/utils/weight.d.ts +3 -3
  494. package/dist/utils/weight.js +18 -18
  495. package/dist/utils/zones.d.ts +10 -10
  496. package/dist/utils/zones.js +287 -287
  497. package/package.json +106 -106
  498. package/publish.sh +143 -143
  499. package/readme.md +182 -182
@@ -1,1327 +1,1327 @@
1
- "use strict";
2
- var __extends = (this && this.__extends) || (function () {
3
- var extendStatics = function (d, b) {
4
- extendStatics = Object.setPrototypeOf ||
5
- ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
- function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7
- return extendStatics(d, b);
8
- };
9
- return function (d, b) {
10
- if (typeof b !== "function" && b !== null)
11
- throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12
- extendStatics(d, b);
13
- function __() { this.constructor = d; }
14
- d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
- };
16
- })();
17
- var __assign = (this && this.__assign) || function () {
18
- __assign = Object.assign || function(t) {
19
- for (var s, i = 1, n = arguments.length; i < n; i++) {
20
- s = arguments[i];
21
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
22
- t[p] = s[p];
23
- }
24
- return t;
25
- };
26
- return __assign.apply(this, arguments);
27
- };
28
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
29
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
30
- return new (P || (P = Promise))(function (resolve, reject) {
31
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
32
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
33
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
34
- step((generator = generator.apply(thisArg, _arguments || [])).next());
35
- });
36
- };
37
- var __generator = (this && this.__generator) || function (thisArg, body) {
38
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
39
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
40
- function verb(n) { return function (v) { return step([n, v]); }; }
41
- function step(op) {
42
- if (f) throw new TypeError("Generator is already executing.");
43
- while (g && (g = 0, op[0] && (_ = 0)), _) try {
44
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
45
- if (y = 0, t) op = [op[0] & 2, t.value];
46
- switch (op[0]) {
47
- case 0: case 1: t = op; break;
48
- case 4: _.label++; return { value: op[1], done: false };
49
- case 5: _.label++; y = op[1]; op = [0]; continue;
50
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
51
- default:
52
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
53
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
54
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
55
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
56
- if (t[2]) _.ops.pop();
57
- _.trys.pop(); continue;
58
- }
59
- op = body.call(thisArg, _);
60
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
61
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
62
- }
63
- };
64
- var __rest = (this && this.__rest) || function (s, e) {
65
- var t = {};
66
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
67
- t[p] = s[p];
68
- if (s != null && typeof Object.getOwnPropertySymbols === "function")
69
- for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
70
- if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
71
- t[p[i]] = s[p[i]];
72
- }
73
- return t;
74
- };
75
- var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
76
- if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
77
- if (ar || !(i in from)) {
78
- if (!ar) ar = Array.prototype.slice.call(from, 0, i);
79
- ar[i] = from[i];
80
- }
81
- }
82
- return to.concat(ar || Array.prototype.slice.call(from));
83
- };
84
- var __importDefault = (this && this.__importDefault) || function (mod) {
85
- return (mod && mod.__esModule) ? mod : { "default": mod };
86
- };
87
- Object.defineProperty(exports, "__esModule", { value: true });
88
- var Product_1 = require("../../Interfaces/Indexed/Product");
89
- var lodash_1 = require("lodash");
90
- var file_1 = require("../../utils/file");
91
- var context_1 = require("../../utils/context");
92
- var console_1 = require("../../utils/console");
93
- var currency_1 = require("../../utils/currency");
94
- var object_1 = require("../../utils/object");
95
- var services_1 = require("../../utils/services");
96
- var locale_1 = require("../../utils/locale");
97
- var algolia_1 = require("../../utils/algolia");
98
- var __1 = require("../..");
99
- var MarketplacePublication_1 = __importDefault(require("../MarketplacePublication"));
100
- var ProductAttribute_1 = __importDefault(require("../ProductAttribute"));
101
- var Product_2 = __importDefault(require("../Indexed/Product"));
102
- var Declination_1 = __importDefault(require("../Declination"));
103
- var Category_1 = __importDefault(require("../Category"));
104
- var Discount_1 = __importDefault(require("../Discount"));
105
- var moment_1 = __importDefault(require("moment"));
106
- var Brand_1 = __importDefault(require("../Brand"));
107
- var Space_1 = __importDefault(require("../Space"));
108
- var g2_1 = require("../../rules/GR/utils/g2");
109
- var baseService_1 = require("../baseService");
110
- var DatabaseService_1 = __importDefault(require("../../services/database/DatabaseService"));
111
- var Product = (function (_super) {
112
- __extends(Product, _super);
113
- function Product() {
114
- var _this = _super !== null && _super.apply(this, arguments) || this;
115
- _this.data = function () { return _this._data; };
116
- _this.toCSV = function () {
117
- return __assign(__assign({}, _this.data()), { product_attributes: !!_this.data().product_attributes
118
- ? _this.data().product_attributes.map(function (pa) { return pa.attribute_id; })
119
- : undefined, declinations: !!_this.data().declinations
120
- ? _this.data().declinations.map(function (d) { return d.name; })
121
- : undefined, brand: !!_this.data().brand ? _this.data().brand.key : undefined, classes: !!_this.data().classes ? _this.data().classes.map(function (c) { return c.key; }) : undefined, category: !!_this.data().category ? _this.data().category.key : undefined });
122
- };
123
- _this.allRelationships = function () { return __awaiter(_this, void 0, void 0, function () {
124
- return __generator(this, function (_c) {
125
- switch (_c.label) {
126
- case 0: return [4, this.relationships(['brands'])];
127
- case 1: return [2, _c.sent()];
128
- }
129
- });
130
- }); };
131
- _this.getDefaultSupplier = function () {
132
- var data = _this.data();
133
- if (!!!data.suppliers)
134
- return undefined;
135
- var supplier = data.suppliers.filter(function (supplier) {
136
- return supplier.supplier_order == 0;
137
- })[0];
138
- return supplier.supplier_id;
139
- };
140
- _this.relationships = function (relations) { return __awaiter(_this, void 0, void 0, function () {
141
- var relationships, _getKeyFromRelationType, _i, relations_1, relation, relationReq, relationData;
142
- return __generator(this, function (_c) {
143
- switch (_c.label) {
144
- case 0:
145
- if (relations.length === 0)
146
- return [2, {}];
147
- relationships = {};
148
- _getKeyFromRelationType = function (_relation) {
149
- switch (_relation) {
150
- case 'brands':
151
- default:
152
- return 'brand';
153
- }
154
- };
155
- _i = 0, relations_1 = relations;
156
- _c.label = 1;
157
- case 1:
158
- if (!(_i < relations_1.length)) return [3, 4];
159
- relation = relations_1[_i];
160
- if (!!relationships[relation])
161
- return [3, 3];
162
- if (!!!this._data[_getKeyFromRelationType(relation)])
163
- return [3, 3];
164
- return [4, DatabaseService_1.default.getDocument(DatabaseService_1.default.addToPath(DatabaseService_1.default.getPathParent(this._collection.path), relation), this._data[_getKeyFromRelationType(relation)].key)];
165
- case 2:
166
- relationReq = _c.sent();
167
- if (!relationReq.exists)
168
- return [3, 3];
169
- relationData = relationReq.data;
170
- if (!!!relationData)
171
- return [3, 3];
172
- relationships[_getKeyFromRelationType(relation)] = __assign(__assign({}, relationData), { id: relationReq.id });
173
- _c.label = 3;
174
- case 3:
175
- _i++;
176
- return [3, 1];
177
- case 4: return [2, relationships];
178
- }
179
- });
180
- }); };
181
- _this.save = function (partialProduct, options) { return __awaiter(_this, void 0, void 0, function () {
182
- var beforeData, _c, id;
183
- return __generator(this, function (_e) {
184
- switch (_e.label) {
185
- case 0:
186
- beforeData = __assign({}, this._data);
187
- this._data = __assign(__assign({}, this._data), partialProduct);
188
- _c = this;
189
- return [4, this._updateDependancies(this._data, beforeData, !!options ? options.space : undefined)];
190
- case 1:
191
- _c._data = (_e.sent());
192
- this._data = __assign(__assign({}, this._data), { last_update_by: this._user });
193
- return [4, _super.prototype.save.call(this, this._data, undefined, true, true, true, beforeData)];
194
- case 2:
195
- id = _e.sent();
196
- if (!!id && !!!this._data.id && typeof id === 'string') {
197
- this._data = __assign(__assign({}, this._data), { id: id });
198
- }
199
- if (!!!options) {
200
- return [2, true];
201
- }
202
- return [2, id];
203
- }
204
- });
205
- }); };
206
- _this._updateDependancies = function (product, beforeData, space) { return __awaiter(_this, void 0, void 0, function () {
207
- var productToUpdate, useService, error_1;
208
- return __generator(this, function (_c) {
209
- switch (_c.label) {
210
- case 0:
211
- (0, console_1.workingMessage)('Updating images and stuff');
212
- productToUpdate = product;
213
- _c.label = 1;
214
- case 1:
215
- _c.trys.push([1, 10, , 11]);
216
- if (!!!!space) return [3, 3];
217
- return [4, Space_1.default.getCurrentSpaceWithRef(this._collection.path, this._user)];
218
- case 2:
219
- space = _c.sent();
220
- _c.label = 3;
221
- case 3:
222
- useService = space.preferences.storage.getServiceToUse();
223
- return [4, this._uploadDeclinationsImages(productToUpdate, useService)];
224
- case 4:
225
- productToUpdate = _c.sent();
226
- return [4, this._updateSEOImage(productToUpdate, useService)];
227
- case 5:
228
- productToUpdate = _c.sent();
229
- return [4, this._updatePanelDescriptionsImage(productToUpdate, useService)];
230
- case 6:
231
- productToUpdate = _c.sent();
232
- return [4, this._updateDeclinationsDiscountRules(productToUpdate, beforeData)];
233
- case 7:
234
- productToUpdate = _c.sent();
235
- return [4, this._updateBarcode(productToUpdate, space)];
236
- case 8:
237
- productToUpdate = _c.sent();
238
- return [4, this._updateInventoryItem(productToUpdate, beforeData, space)];
239
- case 9:
240
- _c.sent();
241
- return [2, productToUpdate];
242
- case 10:
243
- error_1 = _c.sent();
244
- (0, console_1.errorMessage)("Error while updating dependancies: ".concat(error_1, " - ").concat(JSON.stringify(error_1)));
245
- return [2, product];
246
- case 11: return [2];
247
- }
248
- });
249
- }); };
250
- _this._updateSEOImage = function (product, useService) { return __awaiter(_this, void 0, void 0, function () {
251
- var _c, _e, _f, _g;
252
- var _h, _j, _k;
253
- return __generator(this, function (_l) {
254
- switch (_l.label) {
255
- case 0:
256
- _c = [__assign({}, product)];
257
- _h = {};
258
- _e = [__assign({}, product.seo_info)];
259
- _j = {};
260
- if (!(!!product.seo_info &&
261
- !!product.seo_info.seoImage &&
262
- !!product.seo_info.seoImage.img_path &&
263
- typeof product.seo_info.seoImage.img_path === 'string')) return [3, 1];
264
- _f = product.seo_info.seoImage;
265
- return [3, 5];
266
- case 1:
267
- _k = {};
268
- if (!(!!product.seo_info && !!product.seo_info.seoImage && !!product.seo_info.seoImage.img_path)) return [3, 3];
269
- return [4, (0, file_1.uploadFileToPath)(useService, product.seo_info.seoImage.img_path.file, (0, context_1.getPathTo)("products/".concat(product.slug), (0, context_1.getIdsBasedOnSpaceDocRef)(DatabaseService_1.default.getPathParent(this._collection.path))))];
270
- case 2:
271
- _g = _l.sent();
272
- return [3, 4];
273
- case 3:
274
- _g = '';
275
- _l.label = 4;
276
- case 4:
277
- _f = (_k.img_path = _g,
278
- _k);
279
- _l.label = 5;
280
- case 5: return [2, __assign.apply(void 0, _c.concat([(_h.seo_info = __assign.apply(void 0, _e.concat([(_j.seoImage = _f, _j)])), _h)]))];
281
- }
282
- });
283
- }); };
284
- _this._updatePanelDescriptionsImage = function (product, useService) { return __awaiter(_this, void 0, void 0, function () {
285
- var _c;
286
- var _e;
287
- var _this = this;
288
- return __generator(this, function (_f) {
289
- switch (_f.label) {
290
- case 0:
291
- if (!!!product.panel_descriptions || !!!product.panel_descriptions.length)
292
- return [2, product];
293
- _c = [__assign({}, product)];
294
- _e = {};
295
- return [4, Promise.all(product.panel_descriptions.map(function (panel) { return __awaiter(_this, void 0, void 0, function () {
296
- var _c, _e, _f, _g;
297
- var _h, _j, _k, _l;
298
- return __generator(this, function (_m) {
299
- switch (_m.label) {
300
- case 0:
301
- if (!!!panel.img)
302
- return [2, panel];
303
- if (typeof panel.img.img_path === 'string' && !panel.img.img_path.includes('base64'))
304
- return [2, panel];
305
- if (!(typeof panel.img.img_path === 'string')) return [3, 2];
306
- _c = [__assign({}, panel)];
307
- _h = {};
308
- _e = [__assign({}, panel.img)];
309
- _j = {};
310
- return [4, (0, file_1.uploadStringToPath)(useService, panel.img.img_path, (0, context_1.getPathTo)("products/".concat(product.slug, "/panels"), (0, context_1.getIdsBasedOnSpaceDocRef)(DatabaseService_1.default.getPathParent(this._collection.path))), (0, locale_1.getPrincipalWord)(panel.label_name), 'jpg')];
311
- case 1: return [2, __assign.apply(void 0, _c.concat([(_h.img = __assign.apply(void 0, _e.concat([(_j.img_path = _m.sent(), _j)])), _h)]))];
312
- case 2:
313
- if (!!!panel.img.img_path.file) return [3, 4];
314
- _f = [__assign({}, panel)];
315
- _k = {};
316
- _g = [__assign({}, panel.img)];
317
- _l = {};
318
- return [4, (0, file_1.uploadFileToPath)(useService, panel.img.img_path.file, (0, context_1.getPathTo)("products/".concat(product.slug, "/panels"), (0, context_1.getIdsBasedOnSpaceDocRef)(DatabaseService_1.default.getPathParent(this._collection.path))))];
319
- case 3: return [2, __assign.apply(void 0, _f.concat([(_k.img = __assign.apply(void 0, _g.concat([(_l.img_path = _m.sent(), _l)])), _k)]))];
320
- case 4: return [2, __assign(__assign({}, panel), { img: undefined })];
321
- }
322
- });
323
- }); }))];
324
- case 1: return [2, __assign.apply(void 0, _c.concat([(_e.panel_descriptions = _f.sent(), _e)]))];
325
- }
326
- });
327
- }); };
328
- _this._updateBarcode = function (product, space) { return __awaiter(_this, void 0, void 0, function () {
329
- var _c, error_2;
330
- var _e;
331
- var _this = this;
332
- return __generator(this, function (_f) {
333
- switch (_f.label) {
334
- case 0:
335
- _f.trys.push([0, 4, , 5]);
336
- if (!!!!space) return [3, 2];
337
- return [4, Space_1.default.getCurrentSpaceWithRef(this._collection.path, this._user)];
338
- case 1:
339
- space = _f.sent();
340
- _f.label = 2;
341
- case 2:
342
- if (!!!space.data().product_preferences.generate_barcode)
343
- return [2, product];
344
- _c = [__assign({}, product)];
345
- _e = {};
346
- return [4, Promise.all((product.declinations || []).map(function (_d) { return __awaiter(_this, void 0, void 0, function () {
347
- var upc;
348
- return __generator(this, function (_c) {
349
- switch (_c.label) {
350
- case 0:
351
- if (!!_d.upc)
352
- return [2, _d];
353
- if (!!!space)
354
- return [2, _d];
355
- upc = space.data().product_preferences.barcode_next;
356
- return [4, space.save({
357
- product_preferences: __assign(__assign({}, space.data().product_preferences), { barcode_next: "".concat((0, lodash_1.toNumber)(upc) + 1) })
358
- })];
359
- case 1:
360
- _c.sent();
361
- return [2, __assign(__assign({}, _d), { upc: upc })];
362
- }
363
- });
364
- }); }))];
365
- case 3: return [2, __assign.apply(void 0, _c.concat([(_e.declinations = _f.sent(), _e)]))];
366
- case 4:
367
- error_2 = _f.sent();
368
- console.log(error_2);
369
- return [2, product];
370
- case 5: return [2];
371
- }
372
- });
373
- }); };
374
- _this._updateInventoryItem = function (product, beforeData, space) { return __awaiter(_this, void 0, void 0, function () {
375
- var declis, allSameProductInventories, _i, allSameProductInventories_1, inventory, e_1, allSameProductInventoriesSamples, allSameProductInventoriesNotSamples, deletedSkusSamples, deletedSkusNotSamples, skusToDelete, e_2;
376
- var _this = this;
377
- return __generator(this, function (_c) {
378
- switch (_c.label) {
379
- case 0:
380
- declis = product.declinations;
381
- if (!!!declis || !!!declis.length)
382
- return [2];
383
- if (!!!!space) return [3, 2];
384
- return [4, Space_1.default.getCurrentSpaceWithRef(this._collection.path, this._user)];
385
- case 1:
386
- space = _c.sent();
387
- _c.label = 2;
388
- case 2: return [4, space.inventoryItems.getAllFromSameProduct(!!product.slug ? product.slug : this.data().slug)];
389
- case 3:
390
- allSameProductInventories = _c.sent();
391
- if (!!!allSameProductInventories || !!!allSameProductInventories.length)
392
- return [2, true];
393
- if (!(!!beforeData && !!product.is_service && beforeData.is_service !== product.is_service)) return [3, 11];
394
- _c.label = 4;
395
- case 4:
396
- _c.trys.push([4, 9, , 10]);
397
- _i = 0, allSameProductInventories_1 = allSameProductInventories;
398
- _c.label = 5;
399
- case 5:
400
- if (!(_i < allSameProductInventories_1.length)) return [3, 8];
401
- inventory = allSameProductInventories_1[_i];
402
- return [4, inventory.delete()];
403
- case 6:
404
- _c.sent();
405
- _c.label = 7;
406
- case 7:
407
- _i++;
408
- return [3, 5];
409
- case 8: return [3, 10];
410
- case 9:
411
- e_1 = _c.sent();
412
- console.log('error on deleting inventories of now service product: ', e_1);
413
- return [3, 10];
414
- case 10: return [2, true];
415
- case 11:
416
- allSameProductInventoriesSamples = allSameProductInventories.filter(function (i) { return !!i.data().is_sample; });
417
- allSameProductInventoriesNotSamples = allSameProductInventories.filter(function (i) { return !!!i.data().is_sample; });
418
- deletedSkusSamples = allSameProductInventoriesSamples.filter(function (inventory) {
419
- var decliSku = inventory.data().decli_sku_associated_to_sample;
420
- var foundDecli = declis.find(function (d) { return d.sku === decliSku; });
421
- return !!!foundDecli || !foundDecli.is_sample;
422
- });
423
- deletedSkusNotSamples = allSameProductInventoriesNotSamples.filter(function (inventory) { return !!!declis.some(function (d) { return d.sku === inventory.data().sku; }) && !!!inventory.data().is_sample; });
424
- skusToDelete = __spreadArray(__spreadArray([], deletedSkusSamples, true), deletedSkusNotSamples, true);
425
- if (!!!skusToDelete.length)
426
- return [2, true];
427
- _c.label = 12;
428
- case 12:
429
- _c.trys.push([12, 14, , 15]);
430
- return [4, Promise.all(skusToDelete.map(function (inventory) { return __awaiter(_this, void 0, void 0, function () {
431
- return __generator(this, function (_c) {
432
- switch (_c.label) {
433
- case 0: return [4, inventory.delete()];
434
- case 1:
435
- _c.sent();
436
- return [2];
437
- }
438
- });
439
- }); }))];
440
- case 13:
441
- _c.sent();
442
- return [3, 15];
443
- case 14:
444
- e_2 = _c.sent();
445
- console.log('error on deleting inventories of deleted skus: ', e_2);
446
- return [3, 15];
447
- case 15: return [2, true];
448
- }
449
- });
450
- }); };
451
- _this._updateDeclinationsDiscountRules = function (product, beforeData) { return __awaiter(_this, void 0, void 0, function () {
452
- var lastPushedTime, _c, _e;
453
- var _f;
454
- var _this = this;
455
- return __generator(this, function (_g) {
456
- switch (_g.label) {
457
- case 0:
458
- lastPushedTime = '';
459
- _c = [__assign({}, product)];
460
- _f = {};
461
- if (!!!product.declinations) return [3, 2];
462
- return [4, Promise.all(product.declinations.map(function (declination, index) { return __awaiter(_this, void 0, void 0, function () {
463
- var timestamp, idString, duplicateTime, currentDataDeclis, sameInCurrentData, discountsToDelete, _i, discountsToDelete_1, discount, discountObj, e_3, _c, _e;
464
- var _f;
465
- var _this = this;
466
- return __generator(this, function (_g) {
467
- switch (_g.label) {
468
- case 0:
469
- timestamp = (0, moment_1.default)().toDate().getTime();
470
- idString = String(timestamp) + '-' + String(index);
471
- duplicateTime = idString === lastPushedTime;
472
- lastPushedTime = String(timestamp) + '-' + String(index);
473
- currentDataDeclis = !!beforeData.declinations ? beforeData.declinations : [];
474
- sameInCurrentData = currentDataDeclis.find(function (d) { return !!d && d.sku === declination.sku; });
475
- discountsToDelete = [];
476
- if (!!sameInCurrentData) {
477
- discountsToDelete = (sameInCurrentData.discount_rules || []).filter(function (d) {
478
- return !!d &&
479
- d.discountRule_type === 'tier-pricing' &&
480
- (!!!declination.discount_rules ||
481
- !!!declination.discount_rules.length ||
482
- !!!declination.discount_rules.some(function (_d) { return _d.id === d.id; }));
483
- });
484
- }
485
- if (!!duplicateTime) {
486
- idString =
487
- idString.split('-')[0] + '-' + String(index + product.declinations.length);
488
- }
489
- if (!(!!discountsToDelete &&
490
- !!discountsToDelete.length &&
491
- DatabaseService_1.default.getPathParent(this._collection.path) !== '/')) return [3, 6];
492
- console.log('** Going to delete tier-pricing discount_rules no longer in declination **');
493
- _i = 0, discountsToDelete_1 = discountsToDelete;
494
- _g.label = 1;
495
- case 1:
496
- if (!(_i < discountsToDelete_1.length)) return [3, 6];
497
- discount = discountsToDelete_1[_i];
498
- _g.label = 2;
499
- case 2:
500
- _g.trys.push([2, 4, , 5]);
501
- discountObj = new Discount_1.default(discount, {
502
- path: DatabaseService_1.default.addToPath(DatabaseService_1.default.getPathParent(this._collection.path), 'discountRules')
503
- }, this._user);
504
- return [4, discountObj.delete()];
505
- case 3:
506
- _g.sent();
507
- return [3, 5];
508
- case 4:
509
- e_3 = _g.sent();
510
- console.log('error on deleting discount: ', e_3);
511
- return [3, 5];
512
- case 5:
513
- _i++;
514
- return [3, 1];
515
- case 6:
516
- _c = [__assign({}, declination)];
517
- _f = { id: !!declination.id ? declination.id : idString };
518
- if (!(!!declination.discount_rules && !!declination.discount_rules.length)) return [3, 8];
519
- return [4, Promise.all((0, lodash_1.sortedUniqBy)(declination.discount_rules, function (dr) {
520
- var rule = !!dr.rules && dr.rules.find(function (r) { return r.field === 'quantity'; });
521
- if (!!!rule)
522
- return false;
523
- return rule.ruleDetails[0].value[0];
524
- }).map(function (rule) { return __awaiter(_this, void 0, void 0, function () {
525
- var _rule, id;
526
- return __generator(this, function (_c) {
527
- switch (_c.label) {
528
- case 0:
529
- if (DatabaseService_1.default.getPathParent(this._collection.path) === '/')
530
- return [2, rule];
531
- if (rule.discountRule_type !== 'tier-pricing')
532
- return [2, rule];
533
- _rule = new Discount_1.default(rule, {
534
- path: DatabaseService_1.default.addToPath(DatabaseService_1.default.getPathParent(this._collection.path), 'discountRules')
535
- }, this._user);
536
- return [4, _rule.save()];
537
- case 1:
538
- id = _c.sent();
539
- return [2, __assign(__assign({}, rule), (!!!rule.id && typeof id === 'string' && !!id ? { id: id } : {}))];
540
- }
541
- });
542
- }); }))];
543
- case 7:
544
- _e = _g.sent();
545
- return [3, 9];
546
- case 8:
547
- _e = [];
548
- _g.label = 9;
549
- case 9: return [2, __assign.apply(void 0, _c.concat([(_f.discount_rules = _e, _f)]))];
550
- }
551
- });
552
- }); }))];
553
- case 1:
554
- _e = _g.sent();
555
- return [3, 3];
556
- case 2:
557
- _e = [];
558
- _g.label = 3;
559
- case 3: return [2, __assign.apply(void 0, _c.concat([(_f.declinations = _e, _f)]))];
560
- }
561
- });
562
- }); };
563
- _this._uploadDeclinationsImages = function (product, useService) { return __awaiter(_this, void 0, void 0, function () {
564
- var _c, _e, _f;
565
- var _g;
566
- var _this = this;
567
- return __generator(this, function (_h) {
568
- switch (_h.label) {
569
- case 0:
570
- _c = [__assign({}, product)];
571
- _g = {};
572
- if (!(!!!product.image_files || !!!product.image_files.length)) return [3, 1];
573
- _e = [];
574
- return [3, 3];
575
- case 1: return [4, Promise.all(product.image_files
576
- .filter(function (i) { return !!i && !!i.img_path; })
577
- .map(function (imageInfo) { return __awaiter(_this, void 0, void 0, function () {
578
- var _c, _e, img_path;
579
- var _f, _g;
580
- return __generator(this, function (_h) {
581
- switch (_h.label) {
582
- case 0:
583
- if (!(!!imageInfo.img_path.dataURL && !!!imageInfo.img_path.file)) return [3, 3];
584
- if (!(typeof imageInfo.img_path.dataURL === 'string' &&
585
- !!imageInfo.img_path.dataURL.includes('base64'))) return [3, 2];
586
- _c = [__assign({}, imageInfo)];
587
- _f = {};
588
- return [4, (0, file_1.uploadStringToPath)(useService, imageInfo.img_path.dataURL, (0, context_1.getPathTo)("products/".concat(product.slug), (0, context_1.getIdsBasedOnSpaceDocRef)(DatabaseService_1.default.getPathParent(this._collection.path))), (0, locale_1.getPrincipalWord)(product.name), imageInfo.img_path.dataURL.split(';')[0].split('/')[1])];
589
- case 1: return [2, __assign.apply(void 0, _c.concat([(_f.img_path = (_h.sent()), _f)]))];
590
- case 2: return [2, __assign(__assign({}, imageInfo), { img_path: imageInfo.img_path.dataURL })];
591
- case 3:
592
- if (!(typeof imageInfo.img_path === 'string')) return [3, 6];
593
- if (!!!imageInfo.img_path.includes('base64')) return [3, 5];
594
- _e = [__assign({}, imageInfo)];
595
- _g = {};
596
- return [4, (0, file_1.uploadStringToPath)(useService, imageInfo.img_path, (0, context_1.getPathTo)("products/".concat(product.slug), (0, context_1.getIdsBasedOnSpaceDocRef)(DatabaseService_1.default.getPathParent(this._collection.path))), (0, locale_1.getPrincipalWord)(product.name), imageInfo.img_path.split(';')[0].split('/')[1])];
597
- case 4: return [2, __assign.apply(void 0, _e.concat([(_g.img_path = (_h.sent()), _g)]))];
598
- case 5: return [2, imageInfo];
599
- case 6: return [4, (0, file_1.uploadFileToPath)(useService, imageInfo.img_path.file, (0, context_1.getPathTo)("products/".concat(product.slug), (0, context_1.getIdsBasedOnSpaceDocRef)(DatabaseService_1.default.getPathParent(this._collection.path))))];
600
- case 7:
601
- img_path = _h.sent();
602
- return [2, __assign(__assign({}, imageInfo), { img_path: img_path })];
603
- }
604
- });
605
- }); }))];
606
- case 2:
607
- _e = _h.sent();
608
- _h.label = 3;
609
- case 3:
610
- _g.image_files = _e;
611
- if (!(!!!product.declinations || !!!product.declinations.length)) return [3, 4];
612
- _f = [];
613
- return [3, 6];
614
- case 4: return [4, Promise.all(product.declinations.map(function (declination) { return __awaiter(_this, void 0, void 0, function () {
615
- var _c;
616
- var _e;
617
- var _this = this;
618
- return __generator(this, function (_f) {
619
- switch (_f.label) {
620
- case 0:
621
- if (!!!declination.image_files)
622
- return [2, declination];
623
- if (!!!declination.image_files.length)
624
- return [2, declination];
625
- if (!!!declination.image_files.find(function (imageInfo) { return !!imageInfo.img_path && !!imageInfo.img_path.file; }))
626
- return [2, declination];
627
- _c = [__assign({}, declination)];
628
- _e = {};
629
- return [4, Promise.all(declination.image_files
630
- .filter(function (i) { return !!i && !!i.img_path; })
631
- .map(function (imageInfo) { return __awaiter(_this, void 0, void 0, function () {
632
- var _c, img_path;
633
- var _e;
634
- return __generator(this, function (_f) {
635
- switch (_f.label) {
636
- case 0:
637
- if (!(typeof imageInfo.img_path === 'string')) return [3, 3];
638
- if (!!!imageInfo.img_path.includes('base64')) return [3, 2];
639
- _c = [__assign({}, imageInfo)];
640
- _e = {};
641
- return [4, (0, file_1.uploadStringToPath)(imageInfo.img_path, (0, context_1.getPathTo)("brands/".concat(product.slug), (0, context_1.getIdsBasedOnSpaceDocRef)(DatabaseService_1.default.getPathParent(this._collection.path))), (0, locale_1.getPrincipalWord)(declination.name), imageInfo.img_path.split(';')[0].split('/')[1])];
642
- case 1: return [2, __assign.apply(void 0, _c.concat([(_e.img_path = (_f.sent()), _e)]))];
643
- case 2: return [2, imageInfo];
644
- case 3: return [4, (0, file_1.uploadFileToPath)(useService, imageInfo.img_path.file, (0, context_1.getPathTo)("products/".concat(product.slug), (0, context_1.getIdsBasedOnSpaceDocRef)(DatabaseService_1.default.getPathParent(this._collection.path))))];
645
- case 4:
646
- img_path = (_f.sent());
647
- return [2, __assign(__assign({}, imageInfo), { img_path: img_path })];
648
- }
649
- });
650
- }); }))];
651
- case 1: return [2, __assign.apply(void 0, _c.concat([(_e.image_files = _f.sent(), _e)]))];
652
- }
653
- });
654
- }); }))];
655
- case 5:
656
- _f = _h.sent();
657
- _h.label = 6;
658
- case 6: return [2, __assign.apply(void 0, _c.concat([(_g.declinations = _f, _g)]))];
659
- }
660
- });
661
- }); };
662
- _this._getAttributes = function () {
663
- try {
664
- if (!!!_this.data().product_attributes || !!!_this._data.id)
665
- return [];
666
- return _this.data().product_attributes.map(function (d) {
667
- return new ProductAttribute_1.default(d, DatabaseService_1.default.addToPath(DatabaseService_1.default.addToPath(_this._collection.path, _this._data.id), 'attributes'), _this._user);
668
- });
669
- }
670
- catch (err) {
671
- throw new Error("Error while getting attributes: ".concat(err));
672
- }
673
- };
674
- _this._addAttribute = function (productAttribute) { return __awaiter(_this, void 0, void 0, function () {
675
- var pas, existingPA, _attributes;
676
- return __generator(this, function (_c) {
677
- switch (_c.label) {
678
- case 0:
679
- pas = !!this.data().product_attributes ? this.data().product_attributes : [];
680
- existingPA = pas.find(function (a) { return a.attribute_id === productAttribute.attribute_id; });
681
- if (!!!existingPA) return [3, 2];
682
- _attributes = pas.map(function (pa) {
683
- if (pa.id !== existingPA.id)
684
- return pa;
685
- return __assign(__assign({}, pa), productAttribute);
686
- });
687
- return [4, this.save({ product_attributes: _attributes })];
688
- case 1: return [2, _c.sent()];
689
- case 2: return [4, this.save({
690
- product_attributes: __spreadArray(__spreadArray([], pas, true), [productAttribute], false)
691
- })];
692
- case 3: return [2, _c.sent()];
693
- }
694
- });
695
- }); };
696
- _this._getCategory = function () { return __awaiter(_this, void 0, void 0, function () {
697
- var categorySnap, err_1;
698
- return __generator(this, function (_c) {
699
- switch (_c.label) {
700
- case 0:
701
- if (!!!this._data.category)
702
- throw new Error("Product categoy is not yet defined");
703
- _c.label = 1;
704
- case 1:
705
- _c.trys.push([1, 3, , 4]);
706
- return [4, DatabaseService_1.default.getDocument(DatabaseService_1.default.addToPath(DatabaseService_1.default.getPathParent(this._collection.path), 'categories'), this._data.category)];
707
- case 2:
708
- categorySnap = _c.sent();
709
- if (!!!categorySnap)
710
- throw new Error("Inexisting category");
711
- if (!categorySnap.exists)
712
- throw new Error("Impossible to retreive the category");
713
- if (!!!categorySnap.data)
714
- throw new Error("Impossible te retreive the category data");
715
- return [2, new Category_1.default(__assign(__assign({}, categorySnap.data), { id: categorySnap.id }), {
716
- path: DatabaseService_1.default.addToPath(DatabaseService_1.default.getPathParent(this._collection.path), 'categories')
717
- }, this._user)];
718
- case 3:
719
- err_1 = _c.sent();
720
- throw new Error("Error while getting categories: ".concat(err_1));
721
- case 4: return [2];
722
- }
723
- });
724
- }); };
725
- _this._getBrand = function () { return __awaiter(_this, void 0, void 0, function () {
726
- var brandSnap, err_2;
727
- return __generator(this, function (_c) {
728
- switch (_c.label) {
729
- case 0:
730
- if (!!!this._data.brand)
731
- return [2, undefined];
732
- _c.label = 1;
733
- case 1:
734
- _c.trys.push([1, 3, , 4]);
735
- return [4, DatabaseService_1.default.getDocument(DatabaseService_1.default.addToPath(DatabaseService_1.default.getPathParent(this._collection.path), 'brands'), this._data.brand)];
736
- case 2:
737
- brandSnap = _c.sent();
738
- if (!!!brandSnap)
739
- throw new Error("Inexisting brand");
740
- if (!brandSnap.exists)
741
- throw new Error("Impossible to retreive the brand");
742
- if (!!!brandSnap.data)
743
- throw new Error("Impossible te retreive the brand data");
744
- return [2, new Brand_1.default(__assign(__assign({}, brandSnap.data), { id: brandSnap.id }), {
745
- path: DatabaseService_1.default.addToPath(DatabaseService_1.default.getPathParent(this._collection.path), 'brands')
746
- }, this._user)];
747
- case 3:
748
- err_2 = _c.sent();
749
- throw new Error("Error while getting brands: ".concat(err_2));
750
- case 4: return [2];
751
- }
752
- });
753
- }); };
754
- _this._getDeclinations = function () {
755
- if (!!!_this._data.id)
756
- throw new Error("Product must be saved to create declinations.");
757
- var declinationCollectionRef = DatabaseService_1.default.addToPath(DatabaseService_1.default.addToPath(_this._collection.path, _this._data.id), 'declinations');
758
- if (!!!_this.data().declinations)
759
- return [];
760
- return _this.data().declinations.map(function (d) { return new Declination_1.default(d, declinationCollectionRef, _this._user); });
761
- };
762
- _this._addDeclination = function (declination) { return __awaiter(_this, void 0, void 0, function () {
763
- return __generator(this, function (_c) {
764
- if (!!!this._data.id)
765
- throw new Error("Product must be saved to create declinations.");
766
- if (!!!this.data().declinations)
767
- return [2];
768
- return [2, this.save({
769
- declinations: __spreadArray(__spreadArray([], this.data().declinations, true), [declination], false)
770
- })];
771
- });
772
- }); };
773
- _this._saveIndex = function (category) { return __awaiter(_this, void 0, void 0, function () {
774
- var brand, declinations, e_4, tree, _c, space, indexed, error_3;
775
- return __generator(this, function (_e) {
776
- switch (_e.label) {
777
- case 0:
778
- if (!!!this._data.id)
779
- throw new Error('Product must be saved first.');
780
- if (DatabaseService_1.default.getPathParent(this._collection.path) == '/')
781
- throw new Error('BD badly initialised. Products collection must have a parent.');
782
- brand = undefined;
783
- declinations = this.data().declinations;
784
- if (!!!declinations) {
785
- console.log("No declinations to update");
786
- return [2, true];
787
- }
788
- _e.label = 1;
789
- case 1:
790
- _e.trys.push([1, 15, , 16]);
791
- if (!(!!!category && !!!this.data().is_service)) return [3, 3];
792
- return [4, this.category.get()];
793
- case 2:
794
- category = _e.sent();
795
- if (!!!category)
796
- throw new Error("A category must be set");
797
- _e.label = 3;
798
- case 3:
799
- if (!!!this._data.brand) return [3, 7];
800
- _e.label = 4;
801
- case 4:
802
- _e.trys.push([4, 6, , 7]);
803
- return [4, this.brand.get()];
804
- case 5:
805
- brand = _e.sent();
806
- return [3, 7];
807
- case 6:
808
- e_4 = _e.sent();
809
- console.log('could not fetch brand with id ', this._data.brand, ': ', e_4.toString());
810
- return [3, 7];
811
- case 7:
812
- if (!!!category) return [3, 9];
813
- return [4, category.getParentTree()];
814
- case 8:
815
- _c = _e.sent();
816
- return [3, 10];
817
- case 9:
818
- _c = [];
819
- _e.label = 10;
820
- case 10:
821
- tree = _c;
822
- return [4, Space_1.default.getCurrentSpaceWithRef(this._collection.path, this._user)];
823
- case 11:
824
- space = _e.sent();
825
- return [4, space.initCurrencies()];
826
- case 12:
827
- _e.sent();
828
- return [4, (0, Product_1.populateIndexedProduct)(this.data(), tree, space, category, brand)];
829
- case 13:
830
- indexed = _e.sent();
831
- console.log('index is created');
832
- return [4, DatabaseService_1.default.saveDocument(DatabaseService_1.default.addToPath(DatabaseService_1.default.getPathParent(this._collection.path), 'indexes/products/public'), this._data.id, (0, moment_1.default)().unix(), (0, object_1.stripObjectForFirebase)(__assign(__assign({}, indexed), { declinations: indexed.declinations.map(function (declination) {
833
- if (!!declination.promo_price || typeof declination.promo_price !== 'boolean')
834
- return declination;
835
- var promo_price = declination.promo_price, rest = __rest(declination, ["promo_price"]);
836
- return __assign({}, rest);
837
- }) })), { merge: true })];
838
- case 14:
839
- _e.sent();
840
- return [2, true];
841
- case 15:
842
- error_3 = _e.sent();
843
- console.log("error while indexing", error_3.toString());
844
- console.trace("indexing error trace");
845
- throw error_3;
846
- case 16: return [2];
847
- }
848
- });
849
- }); };
850
- _this._getIndex = function () { return __awaiter(_this, void 0, void 0, function () {
851
- var indexedProductsDocs;
852
- var _this = this;
853
- return __generator(this, function (_c) {
854
- switch (_c.label) {
855
- case 0:
856
- if (!!!this._data.id)
857
- throw new Error("Product has never been saved");
858
- if (DatabaseService_1.default.getPathParent(this._collection.path) == '/')
859
- throw new Error("Can't access indexes collection");
860
- return [4, DatabaseService_1.default.getDocuments(DatabaseService_1.default.addToPath(DatabaseService_1.default.getPathParent(this._collection.path), 'indexes/products/public'), { query: [{ field: 'product_id', operator: '==', value: this._data.id }] })];
861
- case 1:
862
- indexedProductsDocs = _c.sent();
863
- return [4, Promise.all(indexedProductsDocs
864
- .filter(function (d) { return !!d.exists && !!d.data; })
865
- .map(function (doc) {
866
- return new Product_2.default(__assign(__assign({}, doc.data), { id: doc.id }), {
867
- path: DatabaseService_1.default.addToPath(DatabaseService_1.default.getPathParent(_this._collection.path), 'indexes/products/public')
868
- }, _this._user);
869
- }))];
870
- case 2: return [2, _c.sent()];
871
- }
872
- });
873
- }); };
874
- _this._deleteIndex = function () { return __awaiter(_this, void 0, void 0, function () {
875
- var error_4;
876
- return __generator(this, function (_c) {
877
- switch (_c.label) {
878
- case 0:
879
- if (DatabaseService_1.default.getPathParent(this._collection.path) == '/')
880
- throw new Error("Can't access parent collection");
881
- _c.label = 1;
882
- case 1:
883
- _c.trys.push([1, 3, , 4]);
884
- if (!!!this._data.id)
885
- throw new Error('No id for selected product');
886
- return [4, DatabaseService_1.default.deleteDocument(DatabaseService_1.default.addToPath(DatabaseService_1.default.getPathParent(this._collection.path), 'indexes/products/public'), this._data.id)];
887
- case 2:
888
- _c.sent();
889
- return [3, 4];
890
- case 3:
891
- error_4 = _c.sent();
892
- throw error_4;
893
- case 4: return [2];
894
- }
895
- });
896
- }); };
897
- _this.callPublicService = function (service, fromServer) {
898
- if (fromServer === void 0) { fromServer = false; }
899
- return __awaiter(_this, void 0, void 0, function () {
900
- var requestHeader, url, method, quoteReq, details, error_5;
901
- return __generator(this, function (_c) {
902
- switch (_c.label) {
903
- case 0:
904
- requestHeader = (0, services_1.productServiceUrl)(__assign({}, service.external_service_infos), service.external_service_name);
905
- if (!!!requestHeader)
906
- throw new Error("Can't access service provider. Please validate your external services configuration");
907
- url = requestHeader.url, method = requestHeader.type;
908
- _c.label = 1;
909
- case 1:
910
- _c.trys.push([1, 4, , 5]);
911
- return [4, fetch(!!fromServer ? "".concat(url, "&q=").concat(this.data().name[0].value) : '/api/services/cors', {
912
- headers: { 'content-type': 'application/json' },
913
- method: !!fromServer ? method : 'POST',
914
- body: JSON.stringify({
915
- props: { method: method, headers: { 'content-type': 'application/json' } },
916
- url: "".concat(url, "&q=").concat(this.data().name[0].value)
917
- })
918
- })];
919
- case 2:
920
- quoteReq = _c.sent();
921
- if (quoteReq.status.toString() !== '200')
922
- throw new Error("Error while getting service: ".concat(quoteReq.statusText));
923
- return [4, quoteReq.json()];
924
- case 3:
925
- details = _c.sent();
926
- return [2, { confirmed: true, details: details }];
927
- case 4:
928
- error_5 = _c.sent();
929
- (0, console_1.errorMessage)("Error while calling order service ".concat(error_5, " - ").concat(JSON.stringify(error_5)));
930
- return [2, { confirmed: false, error: error_5 }];
931
- case 5: return [2];
932
- }
933
- });
934
- });
935
- };
936
- _this._updateAllDeclinationsPrice = function (pricingMapFromExternalOrCustom, attributes, saveToDatabase, updateRegular, spaceToUse) {
937
- if (saveToDatabase === void 0) { saveToDatabase = true; }
938
- if (updateRegular === void 0) { updateRegular = true; }
939
- return __awaiter(_this, void 0, void 0, function () {
940
- var space, _c, spacePrice, declinations, declinationsWithAttributeLinkedToService, _loop_1, this_1, _i, declinationsWithAttributeLinkedToService_1, declinationToUpdate, state_1, error_6;
941
- return __generator(this, function (_e) {
942
- switch (_e.label) {
943
- case 0:
944
- _e.trys.push([0, 9, , 10]);
945
- if (!!!spaceToUse) return [3, 1];
946
- _c = spaceToUse;
947
- return [3, 3];
948
- case 1: return [4, Space_1.default.getCurrentSpaceWithRef(this._collection.path, this._user)];
949
- case 2:
950
- _c = _e.sent();
951
- _e.label = 3;
952
- case 3:
953
- space = _c;
954
- spacePrice = this.adaptPriceChartingValues(pricingMapFromExternalOrCustom, space);
955
- declinations = this.data().declinations;
956
- if (!!!declinations)
957
- return [2, false];
958
- declinationsWithAttributeLinkedToService = declinations.filter(function (d) {
959
- return !!d.attributes.find(function (pa) {
960
- var attr = attributes.find(function (_a) { return _a.id === pa.attribute_id; });
961
- if (!!!attr)
962
- return false;
963
- if (!!!attr.is_service_related)
964
- return false;
965
- if (!!!attr.service_links_to_attribute)
966
- return false;
967
- return true;
968
- });
969
- });
970
- if (!!!declinationsWithAttributeLinkedToService.length)
971
- return [2, false];
972
- _loop_1 = function (declinationToUpdate) {
973
- var relationProductAttribute, relationAttribute, propsToLink, price, priceDetails, _declinations;
974
- return __generator(this, function (_f) {
975
- switch (_f.label) {
976
- case 0:
977
- relationProductAttribute = declinationToUpdate.attributes.find(function (pa) {
978
- var attr = attributes.find(function (_a) { return _a.id === pa.attribute_id; });
979
- if (!!!attr)
980
- return false;
981
- if (!!!attr.is_service_related)
982
- return false;
983
- return true;
984
- });
985
- if (!!!relationProductAttribute)
986
- return [2, "continue"];
987
- relationAttribute = attributes.find(function (a) { return a.id === relationProductAttribute.attribute_id; });
988
- if (!!!relationAttribute)
989
- return [2, "continue"];
990
- if (!!!relationAttribute.service_links_to_attribute)
991
- return [2, "continue"];
992
- propsToLink = relationAttribute.service_links_to_attribute.find(function (slta) {
993
- return slta.value_slugs.find(function (vs) { return !!relationProductAttribute.values.find(function (value) { return value.slug === vs.slug; }); });
994
- });
995
- if (!!!propsToLink)
996
- return [2, "continue"];
997
- if (!!!spacePrice[propsToLink.service_field])
998
- return [2, "continue"];
999
- price = (0, lodash_1.toNumber)(spacePrice[propsToLink.service_field]) / 100;
1000
- priceDetails = propsToLink.value_slugs.find(function (vs) { return !!relationProductAttribute.values.find(function (value) { return value.slug === vs.slug; }); });
1001
- if (!!priceDetails) {
1002
- if (!!priceDetails.percent_variation) {
1003
- price = price * (1 + priceDetails.percent_variation / 100);
1004
- }
1005
- }
1006
- _declinations = declinations.map(function (declination) {
1007
- if (declination.sku !== declinationToUpdate.sku)
1008
- return declination;
1009
- return __assign(__assign({}, declination), { price: __assign(__assign(__assign({}, declination.price), { pdsf: { amount: (0, lodash_1.toNumber)(price) } }), (!!updateRegular
1010
- ? {
1011
- regular: {
1012
- amount: (0, lodash_1.ceil)((0, lodash_1.toNumber)(price)) - 0.01 >= (0, lodash_1.toNumber)(declination.price.regular.amount)
1013
- ? (0, lodash_1.ceil)((0, lodash_1.toNumber)(price)) - 0.01
1014
- : (0, lodash_1.toNumber)(declination.price.regular.amount)
1015
- }
1016
- }
1017
- : {})) });
1018
- });
1019
- if (!!!saveToDatabase)
1020
- return [2, { value: {
1021
- declinations: _declinations,
1022
- services: { price_charting: spacePrice }
1023
- } }];
1024
- return [4, this_1.save({
1025
- declinations: _declinations
1026
- }, { index: true })];
1027
- case 1:
1028
- _f.sent();
1029
- return [2];
1030
- }
1031
- });
1032
- };
1033
- this_1 = this;
1034
- _i = 0, declinationsWithAttributeLinkedToService_1 = declinationsWithAttributeLinkedToService;
1035
- _e.label = 4;
1036
- case 4:
1037
- if (!(_i < declinationsWithAttributeLinkedToService_1.length)) return [3, 7];
1038
- declinationToUpdate = declinationsWithAttributeLinkedToService_1[_i];
1039
- return [5, _loop_1(declinationToUpdate)];
1040
- case 5:
1041
- state_1 = _e.sent();
1042
- if (typeof state_1 === "object")
1043
- return [2, state_1.value];
1044
- _e.label = 6;
1045
- case 6:
1046
- _i++;
1047
- return [3, 4];
1048
- case 7: return [4, this.save({
1049
- services: __assign(__assign({}, this.data().services), { price_charting: spacePrice })
1050
- })];
1051
- case 8:
1052
- _e.sent();
1053
- return [2, true];
1054
- case 9:
1055
- error_6 = _e.sent();
1056
- if (process.env.NODE_ENV !== 'production')
1057
- console.log(error_6);
1058
- return [2, false];
1059
- case 10: return [2];
1060
- }
1061
- });
1062
- });
1063
- };
1064
- _this.adaptPriceChartingValues = function (pricingMap, space) {
1065
- var returnValue = {};
1066
- (0, lodash_1.forEach)(pricingMap, function (val, key) {
1067
- if (['loose-price', 'cib-price', 'loose-price', 'box-only-price', 'manual-only-price', 'new-price'].includes(key)) {
1068
- returnValue[key] = (0, currency_1.calculateAmountByCurrency)(space, space.preferences.default.currency(), (0, lodash_1.toNumber)(val), 'USD');
1069
- return;
1070
- }
1071
- });
1072
- return __assign(__assign({}, pricingMap), returnValue);
1073
- };
1074
- _this._updateAllDeclinationsPriceAsync = function (pricingMapFromExternalOrCustom) { return __awaiter(_this, void 0, void 0, function () {
1075
- var attributesRes, attributes, error_7;
1076
- return __generator(this, function (_c) {
1077
- switch (_c.label) {
1078
- case 0:
1079
- if (DatabaseService_1.default.getPathParent(this._collection.path) == '/')
1080
- throw new Error("Can't fetch attributes");
1081
- _c.label = 1;
1082
- case 1:
1083
- _c.trys.push([1, 4, , 5]);
1084
- return [4, DatabaseService_1.default.getDocuments(DatabaseService_1.default.addToPath(DatabaseService_1.default.getPathParent(this._collection.path), 'attributes'))];
1085
- case 2:
1086
- attributesRes = _c.sent();
1087
- attributes = attributesRes
1088
- .filter(function (d) { return !!d.exists && !!d.data; })
1089
- .map(function (d) { return (__assign(__assign({}, d.data), { id: d.id })); });
1090
- return [4, this._updateAllDeclinationsPrice(pricingMapFromExternalOrCustom, attributes)];
1091
- case 3: return [2, _c.sent()];
1092
- case 4:
1093
- error_7 = _c.sent();
1094
- (0, console_1.errorMessage)("Error while fetching price and attributes");
1095
- return [2, []];
1096
- case 5: return [2];
1097
- }
1098
- });
1099
- }); };
1100
- _this._getMarketplacePublications = function (options) { return __awaiter(_this, void 0, void 0, function () {
1101
- var publications, error_8;
1102
- return __generator(this, function (_c) {
1103
- switch (_c.label) {
1104
- case 0:
1105
- if (DatabaseService_1.default.getPathParent(this._collection.path) == '/')
1106
- throw new Error("Can't fetch marketplace publications");
1107
- _c.label = 1;
1108
- case 1:
1109
- _c.trys.push([1, 3, , 4]);
1110
- return [4, DatabaseService_1.default.getDocuments(DatabaseService_1.default.addToPath(DatabaseService_1.default.getPathParent(this._collection.path), 'marketplace_publications'), options)];
1111
- case 2:
1112
- publications = _c.sent();
1113
- return [2, publications.map(function (d) { return (__assign(__assign({}, d.data), { id: d.id })); })];
1114
- case 3:
1115
- error_8 = _c.sent();
1116
- (0, console_1.errorMessage)("Error while fetching marketplace publications");
1117
- return [2, []];
1118
- case 4: return [2];
1119
- }
1120
- });
1121
- }); };
1122
- _this._marketplacePublicationsListner = function (callback, options) {
1123
- if (DatabaseService_1.default.getPathParent(_this._collection.path) == '/')
1124
- throw new Error("Can't fetch marketplace publications");
1125
- try {
1126
- var call = DatabaseService_1.default.addToPath(DatabaseService_1.default.getPathParent(_this._collection.path), 'marketplace_publications');
1127
- return DatabaseService_1.default.registerListener(function (dataSnap) {
1128
- if (!!!dataSnap.length)
1129
- callback([]);
1130
- (0, console_1.workingMessage)("Marketplace publications has been udpated. Refreshing...");
1131
- callback(dataSnap.map(function (d) { return (__assign(__assign({}, d.data), { id: d.id })); }));
1132
- }, call, !!options ? options : { query: [{ field: 'parent_slug', operator: '==', value: _this._data.slug }] });
1133
- }
1134
- catch (error) {
1135
- (0, console_1.errorMessage)("Error while listening marketplace publications");
1136
- return '';
1137
- }
1138
- };
1139
- _this._createNewPublication = function (sku, marketplacePublication, space) { return __awaiter(_this, void 0, void 0, function () {
1140
- var productInvetory, error_9, newMarketplacePublication, error_10;
1141
- return __generator(this, function (_c) {
1142
- switch (_c.label) {
1143
- case 0:
1144
- if (DatabaseService_1.default.getPathParent(this._collection.path) == '/')
1145
- throw new Error("Can't fetch marketplace publications");
1146
- _c.label = 1;
1147
- case 1:
1148
- _c.trys.push([1, 3, , 4]);
1149
- return [4, space.inventoryItems.getOne(sku)];
1150
- case 2:
1151
- productInvetory = _c.sent();
1152
- if (!!!productInvetory || !!!productInvetory.web_inventory)
1153
- throw new Error("Can't find inventory item with sku: ".concat(sku));
1154
- if (!!!productInvetory.web_inventory.available)
1155
- throw new Error("Inventory item with sku: ".concat(sku, " is not available"));
1156
- if (marketplacePublication.quantity < 1)
1157
- throw new Error("No quantity available");
1158
- if ((0, lodash_1.toNumber)(productInvetory.web_inventory.available) < 1)
1159
- throw new Error("No quantity available");
1160
- if ((0, lodash_1.toNumber)(productInvetory.web_inventory.available) < marketplacePublication.quantity)
1161
- throw new Error("Inventory item with sku: ".concat(sku, " is not available"));
1162
- return [3, 4];
1163
- case 3:
1164
- error_9 = _c.sent();
1165
- (0, console_1.errorMessage)("Error while fetching inventory item with sku: ".concat(sku));
1166
- return [2, false];
1167
- case 4:
1168
- _c.trys.push([4, 6, , 7]);
1169
- newMarketplacePublication = new MarketplacePublication_1.default(__assign(__assign({}, marketplacePublication), { sku: sku, parent_slug: this.data().slug, created_at: new Date(), created_by: this._user }), {
1170
- path: DatabaseService_1.default.addToPath(DatabaseService_1.default.getPathParent(this._collection.path), 'marketplace_publications')
1171
- }, this._user);
1172
- return [4, newMarketplacePublication.save()];
1173
- case 5: return [2, _c.sent()];
1174
- case 6:
1175
- error_10 = _c.sent();
1176
- (0, console_1.errorMessage)("Error while creating marketplace publications");
1177
- return [2, false];
1178
- case 7: return [2];
1179
- }
1180
- });
1181
- }); };
1182
- _this._updateMarketplacePublication = function (sku, marketplacePublication) { return __awaiter(_this, void 0, void 0, function () {
1183
- var marketplacePublicationObj, error_11;
1184
- return __generator(this, function (_c) {
1185
- switch (_c.label) {
1186
- case 0:
1187
- if (DatabaseService_1.default.getPathParent(this._collection.path) == '/')
1188
- throw new Error("Can't update marketplace publications");
1189
- _c.label = 1;
1190
- case 1:
1191
- _c.trys.push([1, 4, , 5]);
1192
- return [4, MarketplacePublication_1.default.getMarketplacePublicationObj(sku, DatabaseService_1.default.addToPath(DatabaseService_1.default.getPathParent(this._collection.path), 'marketplace_publications'), this._user)];
1193
- case 2:
1194
- marketplacePublicationObj = _c.sent();
1195
- if (!!!marketplacePublicationObj)
1196
- throw new Error("No marketplace publication found");
1197
- return [4, marketplacePublicationObj.save(marketplacePublication)];
1198
- case 3: return [2, _c.sent()];
1199
- case 4:
1200
- error_11 = _c.sent();
1201
- (0, console_1.errorMessage)("Error while updating marketplace publications");
1202
- return [2, false];
1203
- case 5: return [2];
1204
- }
1205
- });
1206
- }); };
1207
- _this._removeMarketplacePublication = function (publicationId) { return __awaiter(_this, void 0, void 0, function () {
1208
- var marketplacePublicationObj, error_12;
1209
- return __generator(this, function (_c) {
1210
- switch (_c.label) {
1211
- case 0:
1212
- if (DatabaseService_1.default.getPathParent(this._collection.path) == '/')
1213
- throw new Error("Can't remove marketplace publications");
1214
- _c.label = 1;
1215
- case 1:
1216
- _c.trys.push([1, 4, , 5]);
1217
- return [4, MarketplacePublication_1.default.getMarketplacePublicationObjById(publicationId, DatabaseService_1.default.addToPath(DatabaseService_1.default.getPathParent(this._collection.path), 'marketplace_publications'), this._user)];
1218
- case 2:
1219
- marketplacePublicationObj = _c.sent();
1220
- if (!!!marketplacePublicationObj)
1221
- throw new Error("No marketplace publication found");
1222
- return [4, marketplacePublicationObj.delete()];
1223
- case 3: return [2, _c.sent()];
1224
- case 4:
1225
- error_12 = _c.sent();
1226
- (0, console_1.errorMessage)("Error while deleting marketplace publications");
1227
- return [2, false];
1228
- case 5: return [2];
1229
- }
1230
- });
1231
- }); };
1232
- _this.attributes = {
1233
- get: _this._getAttributes,
1234
- add: _this._addAttribute
1235
- };
1236
- _this.brand = {
1237
- get: _this._getBrand
1238
- };
1239
- _this.category = {
1240
- get: _this._getCategory
1241
- };
1242
- _this.publications = {
1243
- get: _this._getMarketplacePublications,
1244
- listen: _this._marketplacePublicationsListner,
1245
- update: _this._updateMarketplacePublication,
1246
- create: _this._createNewPublication,
1247
- remove: _this._removeMarketplacePublication
1248
- };
1249
- _this.declinations = {
1250
- get: _this._getDeclinations,
1251
- add: _this._addDeclination,
1252
- update: {
1253
- priceSync: _this._updateAllDeclinationsPrice,
1254
- priceAsync: _this._updateAllDeclinationsPriceAsync
1255
- }
1256
- };
1257
- _this.index = {
1258
- save: _this._saveIndex,
1259
- get: _this._getIndex,
1260
- delete: _this._deleteIndex
1261
- };
1262
- return _this;
1263
- }
1264
- var _b;
1265
- _b = Product;
1266
- Product.fetch = function (_c) {
1267
- var app = _c.app, api = _c.api, _e = _c.hpp, hpp = _e === void 0 ? 10 : _e, _f = _c.page, page = _f === void 0 ? 0 : _f, _g = _c.query, query = _g === void 0 ? '' : _g, _h = _c.filters, filters = _h === void 0 ? '' : _h, _j = _c.indice, indice = _j === void 0 ? 'products' : _j, _k = _c.f, f = _k === void 0 ? '' : _k, space = _c.space, _l = _c.useProspr, useProspr = _l === void 0 ? false : _l;
1268
- return __awaiter(void 0, void 0, void 0, function () {
1269
- var prosprSearch, prosprService, algolia, results, error_13;
1270
- return __generator(_b, function (_m) {
1271
- switch (_m.label) {
1272
- case 0:
1273
- _m.trys.push([0, 2, , 3]);
1274
- prosprSearch = false;
1275
- if (!!space) {
1276
- prosprService = space
1277
- .data()
1278
- .external_services.find(function (es) { return es.external_service_name.toLowerCase().trim() === 'prosprsearch'; });
1279
- prosprSearch = !!prosprService && prosprService.is_active;
1280
- }
1281
- algolia = (0, algolia_1.createClient)({
1282
- app: app,
1283
- indexId: "".concat(__1.isTestEnv ? 'test' : 'prod', "_").concat(indice),
1284
- key: !!useProspr ? api : prosprSearch && !!space ? space.preferences.prosprsearch.app() : api,
1285
- prospr: !!prosprSearch || !!useProspr
1286
- });
1287
- return [4, algolia.search(query, {
1288
- hitsPerPage: hpp,
1289
- facetFilters: filters,
1290
- filters: f,
1291
- page: page,
1292
- facets: ['*']
1293
- })];
1294
- case 1:
1295
- results = _m.sent();
1296
- return [2, results];
1297
- case 2:
1298
- error_13 = _m.sent();
1299
- (0, console_1.errorMessage)(error_13);
1300
- throw error_13;
1301
- case 3: return [2];
1302
- }
1303
- });
1304
- });
1305
- };
1306
- Product.checkExternalValuesForDecli = function (sku, space, org) { return __awaiter(void 0, void 0, void 0, function () {
1307
- var externalValues, serviceQuery;
1308
- return __generator(_b, function (_c) {
1309
- switch (_c.label) {
1310
- case 0:
1311
- if (!!!space)
1312
- return [2];
1313
- externalValues = undefined;
1314
- serviceQuery = space.services({ field: 'external_service_name', value: 'G2' });
1315
- if (!(!!serviceQuery && serviceQuery.length)) return [3, 2];
1316
- return [4, (0, g2_1.getG2ProductExternalValues)(sku, serviceQuery[0], space, org)];
1317
- case 1:
1318
- externalValues = _c.sent();
1319
- _c.label = 2;
1320
- case 2: return [2, externalValues];
1321
- }
1322
- });
1323
- }); };
1324
- return Product;
1325
- }(baseService_1.BaseServiceClass));
1326
- exports.default = Product;
1
+ "use strict";
2
+ var __extends = (this && this.__extends) || (function () {
3
+ var extendStatics = function (d, b) {
4
+ extendStatics = Object.setPrototypeOf ||
5
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
+ function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7
+ return extendStatics(d, b);
8
+ };
9
+ return function (d, b) {
10
+ if (typeof b !== "function" && b !== null)
11
+ throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12
+ extendStatics(d, b);
13
+ function __() { this.constructor = d; }
14
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
+ };
16
+ })();
17
+ var __assign = (this && this.__assign) || function () {
18
+ __assign = Object.assign || function(t) {
19
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
20
+ s = arguments[i];
21
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
22
+ t[p] = s[p];
23
+ }
24
+ return t;
25
+ };
26
+ return __assign.apply(this, arguments);
27
+ };
28
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
29
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
30
+ return new (P || (P = Promise))(function (resolve, reject) {
31
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
32
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
33
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
34
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
35
+ });
36
+ };
37
+ var __generator = (this && this.__generator) || function (thisArg, body) {
38
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
39
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
40
+ function verb(n) { return function (v) { return step([n, v]); }; }
41
+ function step(op) {
42
+ if (f) throw new TypeError("Generator is already executing.");
43
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
44
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
45
+ if (y = 0, t) op = [op[0] & 2, t.value];
46
+ switch (op[0]) {
47
+ case 0: case 1: t = op; break;
48
+ case 4: _.label++; return { value: op[1], done: false };
49
+ case 5: _.label++; y = op[1]; op = [0]; continue;
50
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
51
+ default:
52
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
53
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
54
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
55
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
56
+ if (t[2]) _.ops.pop();
57
+ _.trys.pop(); continue;
58
+ }
59
+ op = body.call(thisArg, _);
60
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
61
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
62
+ }
63
+ };
64
+ var __rest = (this && this.__rest) || function (s, e) {
65
+ var t = {};
66
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
67
+ t[p] = s[p];
68
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
69
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
70
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
71
+ t[p[i]] = s[p[i]];
72
+ }
73
+ return t;
74
+ };
75
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
76
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
77
+ if (ar || !(i in from)) {
78
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
79
+ ar[i] = from[i];
80
+ }
81
+ }
82
+ return to.concat(ar || Array.prototype.slice.call(from));
83
+ };
84
+ var __importDefault = (this && this.__importDefault) || function (mod) {
85
+ return (mod && mod.__esModule) ? mod : { "default": mod };
86
+ };
87
+ Object.defineProperty(exports, "__esModule", { value: true });
88
+ var Product_1 = require("../../Interfaces/Indexed/Product");
89
+ var lodash_1 = require("lodash");
90
+ var file_1 = require("../../utils/file");
91
+ var context_1 = require("../../utils/context");
92
+ var console_1 = require("../../utils/console");
93
+ var currency_1 = require("../../utils/currency");
94
+ var object_1 = require("../../utils/object");
95
+ var services_1 = require("../../utils/services");
96
+ var locale_1 = require("../../utils/locale");
97
+ var algolia_1 = require("../../utils/algolia");
98
+ var __1 = require("../..");
99
+ var MarketplacePublication_1 = __importDefault(require("../MarketplacePublication"));
100
+ var ProductAttribute_1 = __importDefault(require("../ProductAttribute"));
101
+ var Product_2 = __importDefault(require("../Indexed/Product"));
102
+ var Declination_1 = __importDefault(require("../Declination"));
103
+ var Category_1 = __importDefault(require("../Category"));
104
+ var Discount_1 = __importDefault(require("../Discount"));
105
+ var moment_1 = __importDefault(require("moment"));
106
+ var Brand_1 = __importDefault(require("../Brand"));
107
+ var Space_1 = __importDefault(require("../Space"));
108
+ var g2_1 = require("../../rules/GR/utils/g2");
109
+ var baseService_1 = require("../baseService");
110
+ var DatabaseService_1 = __importDefault(require("../../services/database/DatabaseService"));
111
+ var Product = (function (_super) {
112
+ __extends(Product, _super);
113
+ function Product() {
114
+ var _this = _super !== null && _super.apply(this, arguments) || this;
115
+ _this.data = function () { return _this._data; };
116
+ _this.toCSV = function () {
117
+ return __assign(__assign({}, _this.data()), { product_attributes: !!_this.data().product_attributes
118
+ ? _this.data().product_attributes.map(function (pa) { return pa.attribute_id; })
119
+ : undefined, declinations: !!_this.data().declinations
120
+ ? _this.data().declinations.map(function (d) { return d.name; })
121
+ : undefined, brand: !!_this.data().brand ? _this.data().brand.key : undefined, classes: !!_this.data().classes ? _this.data().classes.map(function (c) { return c.key; }) : undefined, category: !!_this.data().category ? _this.data().category.key : undefined });
122
+ };
123
+ _this.allRelationships = function () { return __awaiter(_this, void 0, void 0, function () {
124
+ return __generator(this, function (_c) {
125
+ switch (_c.label) {
126
+ case 0: return [4, this.relationships(['brands'])];
127
+ case 1: return [2, _c.sent()];
128
+ }
129
+ });
130
+ }); };
131
+ _this.getDefaultSupplier = function () {
132
+ var data = _this.data();
133
+ if (!!!data.suppliers)
134
+ return undefined;
135
+ var supplier = data.suppliers.filter(function (supplier) {
136
+ return supplier.supplier_order == 0;
137
+ })[0];
138
+ return supplier.supplier_id;
139
+ };
140
+ _this.relationships = function (relations) { return __awaiter(_this, void 0, void 0, function () {
141
+ var relationships, _getKeyFromRelationType, _i, relations_1, relation, relationReq, relationData;
142
+ return __generator(this, function (_c) {
143
+ switch (_c.label) {
144
+ case 0:
145
+ if (relations.length === 0)
146
+ return [2, {}];
147
+ relationships = {};
148
+ _getKeyFromRelationType = function (_relation) {
149
+ switch (_relation) {
150
+ case 'brands':
151
+ default:
152
+ return 'brand';
153
+ }
154
+ };
155
+ _i = 0, relations_1 = relations;
156
+ _c.label = 1;
157
+ case 1:
158
+ if (!(_i < relations_1.length)) return [3, 4];
159
+ relation = relations_1[_i];
160
+ if (!!relationships[relation])
161
+ return [3, 3];
162
+ if (!!!this._data[_getKeyFromRelationType(relation)])
163
+ return [3, 3];
164
+ return [4, DatabaseService_1.default.getDocument(DatabaseService_1.default.addToPath(DatabaseService_1.default.getPathParent(this._collection.path), relation), this._data[_getKeyFromRelationType(relation)].key)];
165
+ case 2:
166
+ relationReq = _c.sent();
167
+ if (!relationReq.exists)
168
+ return [3, 3];
169
+ relationData = relationReq.data;
170
+ if (!!!relationData)
171
+ return [3, 3];
172
+ relationships[_getKeyFromRelationType(relation)] = __assign(__assign({}, relationData), { id: relationReq.id });
173
+ _c.label = 3;
174
+ case 3:
175
+ _i++;
176
+ return [3, 1];
177
+ case 4: return [2, relationships];
178
+ }
179
+ });
180
+ }); };
181
+ _this.save = function (partialProduct, options) { return __awaiter(_this, void 0, void 0, function () {
182
+ var beforeData, _c, id;
183
+ return __generator(this, function (_e) {
184
+ switch (_e.label) {
185
+ case 0:
186
+ beforeData = __assign({}, this._data);
187
+ this._data = __assign(__assign({}, this._data), partialProduct);
188
+ _c = this;
189
+ return [4, this._updateDependancies(this._data, beforeData, !!options ? options.space : undefined)];
190
+ case 1:
191
+ _c._data = (_e.sent());
192
+ this._data = __assign(__assign({}, this._data), { last_update_by: this._user });
193
+ return [4, _super.prototype.save.call(this, this._data, undefined, true, true, true, beforeData)];
194
+ case 2:
195
+ id = _e.sent();
196
+ if (!!id && !!!this._data.id && typeof id === 'string') {
197
+ this._data = __assign(__assign({}, this._data), { id: id });
198
+ }
199
+ if (!!!options) {
200
+ return [2, true];
201
+ }
202
+ return [2, id];
203
+ }
204
+ });
205
+ }); };
206
+ _this._updateDependancies = function (product, beforeData, space) { return __awaiter(_this, void 0, void 0, function () {
207
+ var productToUpdate, useService, error_1;
208
+ return __generator(this, function (_c) {
209
+ switch (_c.label) {
210
+ case 0:
211
+ (0, console_1.workingMessage)('Updating images and stuff');
212
+ productToUpdate = product;
213
+ _c.label = 1;
214
+ case 1:
215
+ _c.trys.push([1, 10, , 11]);
216
+ if (!!!!space) return [3, 3];
217
+ return [4, Space_1.default.getCurrentSpaceWithRef(this._collection.path, this._user)];
218
+ case 2:
219
+ space = _c.sent();
220
+ _c.label = 3;
221
+ case 3:
222
+ useService = space.preferences.storage.getServiceToUse();
223
+ return [4, this._uploadDeclinationsImages(productToUpdate, useService)];
224
+ case 4:
225
+ productToUpdate = _c.sent();
226
+ return [4, this._updateSEOImage(productToUpdate, useService)];
227
+ case 5:
228
+ productToUpdate = _c.sent();
229
+ return [4, this._updatePanelDescriptionsImage(productToUpdate, useService)];
230
+ case 6:
231
+ productToUpdate = _c.sent();
232
+ return [4, this._updateDeclinationsDiscountRules(productToUpdate, beforeData)];
233
+ case 7:
234
+ productToUpdate = _c.sent();
235
+ return [4, this._updateBarcode(productToUpdate, space)];
236
+ case 8:
237
+ productToUpdate = _c.sent();
238
+ return [4, this._updateInventoryItem(productToUpdate, beforeData, space)];
239
+ case 9:
240
+ _c.sent();
241
+ return [2, productToUpdate];
242
+ case 10:
243
+ error_1 = _c.sent();
244
+ (0, console_1.errorMessage)("Error while updating dependancies: ".concat(error_1, " - ").concat(JSON.stringify(error_1)));
245
+ return [2, product];
246
+ case 11: return [2];
247
+ }
248
+ });
249
+ }); };
250
+ _this._updateSEOImage = function (product, useService) { return __awaiter(_this, void 0, void 0, function () {
251
+ var _c, _e, _f, _g;
252
+ var _h, _j, _k;
253
+ return __generator(this, function (_l) {
254
+ switch (_l.label) {
255
+ case 0:
256
+ _c = [__assign({}, product)];
257
+ _h = {};
258
+ _e = [__assign({}, product.seo_info)];
259
+ _j = {};
260
+ if (!(!!product.seo_info &&
261
+ !!product.seo_info.seoImage &&
262
+ !!product.seo_info.seoImage.img_path &&
263
+ typeof product.seo_info.seoImage.img_path === 'string')) return [3, 1];
264
+ _f = product.seo_info.seoImage;
265
+ return [3, 5];
266
+ case 1:
267
+ _k = {};
268
+ if (!(!!product.seo_info && !!product.seo_info.seoImage && !!product.seo_info.seoImage.img_path)) return [3, 3];
269
+ return [4, (0, file_1.uploadFileToPath)(useService, product.seo_info.seoImage.img_path.file, (0, context_1.getPathTo)("products/".concat(product.slug), (0, context_1.getIdsBasedOnSpaceDocRef)(DatabaseService_1.default.getPathParent(this._collection.path))))];
270
+ case 2:
271
+ _g = _l.sent();
272
+ return [3, 4];
273
+ case 3:
274
+ _g = '';
275
+ _l.label = 4;
276
+ case 4:
277
+ _f = (_k.img_path = _g,
278
+ _k);
279
+ _l.label = 5;
280
+ case 5: return [2, __assign.apply(void 0, _c.concat([(_h.seo_info = __assign.apply(void 0, _e.concat([(_j.seoImage = _f, _j)])), _h)]))];
281
+ }
282
+ });
283
+ }); };
284
+ _this._updatePanelDescriptionsImage = function (product, useService) { return __awaiter(_this, void 0, void 0, function () {
285
+ var _c;
286
+ var _e;
287
+ var _this = this;
288
+ return __generator(this, function (_f) {
289
+ switch (_f.label) {
290
+ case 0:
291
+ if (!!!product.panel_descriptions || !!!product.panel_descriptions.length)
292
+ return [2, product];
293
+ _c = [__assign({}, product)];
294
+ _e = {};
295
+ return [4, Promise.all(product.panel_descriptions.map(function (panel) { return __awaiter(_this, void 0, void 0, function () {
296
+ var _c, _e, _f, _g;
297
+ var _h, _j, _k, _l;
298
+ return __generator(this, function (_m) {
299
+ switch (_m.label) {
300
+ case 0:
301
+ if (!!!panel.img)
302
+ return [2, panel];
303
+ if (typeof panel.img.img_path === 'string' && !panel.img.img_path.includes('base64'))
304
+ return [2, panel];
305
+ if (!(typeof panel.img.img_path === 'string')) return [3, 2];
306
+ _c = [__assign({}, panel)];
307
+ _h = {};
308
+ _e = [__assign({}, panel.img)];
309
+ _j = {};
310
+ return [4, (0, file_1.uploadStringToPath)(useService, panel.img.img_path, (0, context_1.getPathTo)("products/".concat(product.slug, "/panels"), (0, context_1.getIdsBasedOnSpaceDocRef)(DatabaseService_1.default.getPathParent(this._collection.path))), (0, locale_1.getPrincipalWord)(panel.label_name), 'jpg')];
311
+ case 1: return [2, __assign.apply(void 0, _c.concat([(_h.img = __assign.apply(void 0, _e.concat([(_j.img_path = _m.sent(), _j)])), _h)]))];
312
+ case 2:
313
+ if (!!!panel.img.img_path.file) return [3, 4];
314
+ _f = [__assign({}, panel)];
315
+ _k = {};
316
+ _g = [__assign({}, panel.img)];
317
+ _l = {};
318
+ return [4, (0, file_1.uploadFileToPath)(useService, panel.img.img_path.file, (0, context_1.getPathTo)("products/".concat(product.slug, "/panels"), (0, context_1.getIdsBasedOnSpaceDocRef)(DatabaseService_1.default.getPathParent(this._collection.path))))];
319
+ case 3: return [2, __assign.apply(void 0, _f.concat([(_k.img = __assign.apply(void 0, _g.concat([(_l.img_path = _m.sent(), _l)])), _k)]))];
320
+ case 4: return [2, __assign(__assign({}, panel), { img: undefined })];
321
+ }
322
+ });
323
+ }); }))];
324
+ case 1: return [2, __assign.apply(void 0, _c.concat([(_e.panel_descriptions = _f.sent(), _e)]))];
325
+ }
326
+ });
327
+ }); };
328
+ _this._updateBarcode = function (product, space) { return __awaiter(_this, void 0, void 0, function () {
329
+ var _c, error_2;
330
+ var _e;
331
+ var _this = this;
332
+ return __generator(this, function (_f) {
333
+ switch (_f.label) {
334
+ case 0:
335
+ _f.trys.push([0, 4, , 5]);
336
+ if (!!!!space) return [3, 2];
337
+ return [4, Space_1.default.getCurrentSpaceWithRef(this._collection.path, this._user)];
338
+ case 1:
339
+ space = _f.sent();
340
+ _f.label = 2;
341
+ case 2:
342
+ if (!!!space.data().product_preferences.generate_barcode)
343
+ return [2, product];
344
+ _c = [__assign({}, product)];
345
+ _e = {};
346
+ return [4, Promise.all((product.declinations || []).map(function (_d) { return __awaiter(_this, void 0, void 0, function () {
347
+ var upc;
348
+ return __generator(this, function (_c) {
349
+ switch (_c.label) {
350
+ case 0:
351
+ if (!!_d.upc)
352
+ return [2, _d];
353
+ if (!!!space)
354
+ return [2, _d];
355
+ upc = space.data().product_preferences.barcode_next;
356
+ return [4, space.save({
357
+ product_preferences: __assign(__assign({}, space.data().product_preferences), { barcode_next: "".concat((0, lodash_1.toNumber)(upc) + 1) })
358
+ })];
359
+ case 1:
360
+ _c.sent();
361
+ return [2, __assign(__assign({}, _d), { upc: upc })];
362
+ }
363
+ });
364
+ }); }))];
365
+ case 3: return [2, __assign.apply(void 0, _c.concat([(_e.declinations = _f.sent(), _e)]))];
366
+ case 4:
367
+ error_2 = _f.sent();
368
+ console.log(error_2);
369
+ return [2, product];
370
+ case 5: return [2];
371
+ }
372
+ });
373
+ }); };
374
+ _this._updateInventoryItem = function (product, beforeData, space) { return __awaiter(_this, void 0, void 0, function () {
375
+ var declis, allSameProductInventories, _i, allSameProductInventories_1, inventory, e_1, allSameProductInventoriesSamples, allSameProductInventoriesNotSamples, deletedSkusSamples, deletedSkusNotSamples, skusToDelete, e_2;
376
+ var _this = this;
377
+ return __generator(this, function (_c) {
378
+ switch (_c.label) {
379
+ case 0:
380
+ declis = product.declinations;
381
+ if (!!!declis || !!!declis.length)
382
+ return [2];
383
+ if (!!!!space) return [3, 2];
384
+ return [4, Space_1.default.getCurrentSpaceWithRef(this._collection.path, this._user)];
385
+ case 1:
386
+ space = _c.sent();
387
+ _c.label = 2;
388
+ case 2: return [4, space.inventoryItems.getAllFromSameProduct(!!product.slug ? product.slug : this.data().slug)];
389
+ case 3:
390
+ allSameProductInventories = _c.sent();
391
+ if (!!!allSameProductInventories || !!!allSameProductInventories.length)
392
+ return [2, true];
393
+ if (!(!!beforeData && !!product.is_service && beforeData.is_service !== product.is_service)) return [3, 11];
394
+ _c.label = 4;
395
+ case 4:
396
+ _c.trys.push([4, 9, , 10]);
397
+ _i = 0, allSameProductInventories_1 = allSameProductInventories;
398
+ _c.label = 5;
399
+ case 5:
400
+ if (!(_i < allSameProductInventories_1.length)) return [3, 8];
401
+ inventory = allSameProductInventories_1[_i];
402
+ return [4, inventory.delete()];
403
+ case 6:
404
+ _c.sent();
405
+ _c.label = 7;
406
+ case 7:
407
+ _i++;
408
+ return [3, 5];
409
+ case 8: return [3, 10];
410
+ case 9:
411
+ e_1 = _c.sent();
412
+ console.log('error on deleting inventories of now service product: ', e_1);
413
+ return [3, 10];
414
+ case 10: return [2, true];
415
+ case 11:
416
+ allSameProductInventoriesSamples = allSameProductInventories.filter(function (i) { return !!i.data().is_sample; });
417
+ allSameProductInventoriesNotSamples = allSameProductInventories.filter(function (i) { return !!!i.data().is_sample; });
418
+ deletedSkusSamples = allSameProductInventoriesSamples.filter(function (inventory) {
419
+ var decliSku = inventory.data().decli_sku_associated_to_sample;
420
+ var foundDecli = declis.find(function (d) { return d.sku === decliSku; });
421
+ return !!!foundDecli || !foundDecli.is_sample;
422
+ });
423
+ deletedSkusNotSamples = allSameProductInventoriesNotSamples.filter(function (inventory) { return !!!declis.some(function (d) { return d.sku === inventory.data().sku; }) && !!!inventory.data().is_sample; });
424
+ skusToDelete = __spreadArray(__spreadArray([], deletedSkusSamples, true), deletedSkusNotSamples, true);
425
+ if (!!!skusToDelete.length)
426
+ return [2, true];
427
+ _c.label = 12;
428
+ case 12:
429
+ _c.trys.push([12, 14, , 15]);
430
+ return [4, Promise.all(skusToDelete.map(function (inventory) { return __awaiter(_this, void 0, void 0, function () {
431
+ return __generator(this, function (_c) {
432
+ switch (_c.label) {
433
+ case 0: return [4, inventory.delete()];
434
+ case 1:
435
+ _c.sent();
436
+ return [2];
437
+ }
438
+ });
439
+ }); }))];
440
+ case 13:
441
+ _c.sent();
442
+ return [3, 15];
443
+ case 14:
444
+ e_2 = _c.sent();
445
+ console.log('error on deleting inventories of deleted skus: ', e_2);
446
+ return [3, 15];
447
+ case 15: return [2, true];
448
+ }
449
+ });
450
+ }); };
451
+ _this._updateDeclinationsDiscountRules = function (product, beforeData) { return __awaiter(_this, void 0, void 0, function () {
452
+ var lastPushedTime, _c, _e;
453
+ var _f;
454
+ var _this = this;
455
+ return __generator(this, function (_g) {
456
+ switch (_g.label) {
457
+ case 0:
458
+ lastPushedTime = '';
459
+ _c = [__assign({}, product)];
460
+ _f = {};
461
+ if (!!!product.declinations) return [3, 2];
462
+ return [4, Promise.all(product.declinations.map(function (declination, index) { return __awaiter(_this, void 0, void 0, function () {
463
+ var timestamp, idString, duplicateTime, currentDataDeclis, sameInCurrentData, discountsToDelete, _i, discountsToDelete_1, discount, discountObj, e_3, _c, _e;
464
+ var _f;
465
+ var _this = this;
466
+ return __generator(this, function (_g) {
467
+ switch (_g.label) {
468
+ case 0:
469
+ timestamp = (0, moment_1.default)().toDate().getTime();
470
+ idString = String(timestamp) + '-' + String(index);
471
+ duplicateTime = idString === lastPushedTime;
472
+ lastPushedTime = String(timestamp) + '-' + String(index);
473
+ currentDataDeclis = !!beforeData.declinations ? beforeData.declinations : [];
474
+ sameInCurrentData = currentDataDeclis.find(function (d) { return !!d && d.sku === declination.sku; });
475
+ discountsToDelete = [];
476
+ if (!!sameInCurrentData) {
477
+ discountsToDelete = (sameInCurrentData.discount_rules || []).filter(function (d) {
478
+ return !!d &&
479
+ d.discountRule_type === 'tier-pricing' &&
480
+ (!!!declination.discount_rules ||
481
+ !!!declination.discount_rules.length ||
482
+ !!!declination.discount_rules.some(function (_d) { return _d.id === d.id; }));
483
+ });
484
+ }
485
+ if (!!duplicateTime) {
486
+ idString =
487
+ idString.split('-')[0] + '-' + String(index + product.declinations.length);
488
+ }
489
+ if (!(!!discountsToDelete &&
490
+ !!discountsToDelete.length &&
491
+ DatabaseService_1.default.getPathParent(this._collection.path) !== '/')) return [3, 6];
492
+ console.log('** Going to delete tier-pricing discount_rules no longer in declination **');
493
+ _i = 0, discountsToDelete_1 = discountsToDelete;
494
+ _g.label = 1;
495
+ case 1:
496
+ if (!(_i < discountsToDelete_1.length)) return [3, 6];
497
+ discount = discountsToDelete_1[_i];
498
+ _g.label = 2;
499
+ case 2:
500
+ _g.trys.push([2, 4, , 5]);
501
+ discountObj = new Discount_1.default(discount, {
502
+ path: DatabaseService_1.default.addToPath(DatabaseService_1.default.getPathParent(this._collection.path), 'discountRules')
503
+ }, this._user);
504
+ return [4, discountObj.delete()];
505
+ case 3:
506
+ _g.sent();
507
+ return [3, 5];
508
+ case 4:
509
+ e_3 = _g.sent();
510
+ console.log('error on deleting discount: ', e_3);
511
+ return [3, 5];
512
+ case 5:
513
+ _i++;
514
+ return [3, 1];
515
+ case 6:
516
+ _c = [__assign({}, declination)];
517
+ _f = { id: !!declination.id ? declination.id : idString };
518
+ if (!(!!declination.discount_rules && !!declination.discount_rules.length)) return [3, 8];
519
+ return [4, Promise.all((0, lodash_1.sortedUniqBy)(declination.discount_rules, function (dr) {
520
+ var rule = !!dr.rules && dr.rules.find(function (r) { return r.field === 'quantity'; });
521
+ if (!!!rule)
522
+ return false;
523
+ return rule.ruleDetails[0].value[0];
524
+ }).map(function (rule) { return __awaiter(_this, void 0, void 0, function () {
525
+ var _rule, id;
526
+ return __generator(this, function (_c) {
527
+ switch (_c.label) {
528
+ case 0:
529
+ if (DatabaseService_1.default.getPathParent(this._collection.path) === '/')
530
+ return [2, rule];
531
+ if (rule.discountRule_type !== 'tier-pricing')
532
+ return [2, rule];
533
+ _rule = new Discount_1.default(rule, {
534
+ path: DatabaseService_1.default.addToPath(DatabaseService_1.default.getPathParent(this._collection.path), 'discountRules')
535
+ }, this._user);
536
+ return [4, _rule.save()];
537
+ case 1:
538
+ id = _c.sent();
539
+ return [2, __assign(__assign({}, rule), (!!!rule.id && typeof id === 'string' && !!id ? { id: id } : {}))];
540
+ }
541
+ });
542
+ }); }))];
543
+ case 7:
544
+ _e = _g.sent();
545
+ return [3, 9];
546
+ case 8:
547
+ _e = [];
548
+ _g.label = 9;
549
+ case 9: return [2, __assign.apply(void 0, _c.concat([(_f.discount_rules = _e, _f)]))];
550
+ }
551
+ });
552
+ }); }))];
553
+ case 1:
554
+ _e = _g.sent();
555
+ return [3, 3];
556
+ case 2:
557
+ _e = [];
558
+ _g.label = 3;
559
+ case 3: return [2, __assign.apply(void 0, _c.concat([(_f.declinations = _e, _f)]))];
560
+ }
561
+ });
562
+ }); };
563
+ _this._uploadDeclinationsImages = function (product, useService) { return __awaiter(_this, void 0, void 0, function () {
564
+ var _c, _e, _f;
565
+ var _g;
566
+ var _this = this;
567
+ return __generator(this, function (_h) {
568
+ switch (_h.label) {
569
+ case 0:
570
+ _c = [__assign({}, product)];
571
+ _g = {};
572
+ if (!(!!!product.image_files || !!!product.image_files.length)) return [3, 1];
573
+ _e = [];
574
+ return [3, 3];
575
+ case 1: return [4, Promise.all(product.image_files
576
+ .filter(function (i) { return !!i && !!i.img_path; })
577
+ .map(function (imageInfo) { return __awaiter(_this, void 0, void 0, function () {
578
+ var _c, _e, img_path;
579
+ var _f, _g;
580
+ return __generator(this, function (_h) {
581
+ switch (_h.label) {
582
+ case 0:
583
+ if (!(!!imageInfo.img_path.dataURL && !!!imageInfo.img_path.file)) return [3, 3];
584
+ if (!(typeof imageInfo.img_path.dataURL === 'string' &&
585
+ !!imageInfo.img_path.dataURL.includes('base64'))) return [3, 2];
586
+ _c = [__assign({}, imageInfo)];
587
+ _f = {};
588
+ return [4, (0, file_1.uploadStringToPath)(useService, imageInfo.img_path.dataURL, (0, context_1.getPathTo)("products/".concat(product.slug), (0, context_1.getIdsBasedOnSpaceDocRef)(DatabaseService_1.default.getPathParent(this._collection.path))), (0, locale_1.getPrincipalWord)(product.name), imageInfo.img_path.dataURL.split(';')[0].split('/')[1])];
589
+ case 1: return [2, __assign.apply(void 0, _c.concat([(_f.img_path = (_h.sent()), _f)]))];
590
+ case 2: return [2, __assign(__assign({}, imageInfo), { img_path: imageInfo.img_path.dataURL })];
591
+ case 3:
592
+ if (!(typeof imageInfo.img_path === 'string')) return [3, 6];
593
+ if (!!!imageInfo.img_path.includes('base64')) return [3, 5];
594
+ _e = [__assign({}, imageInfo)];
595
+ _g = {};
596
+ return [4, (0, file_1.uploadStringToPath)(useService, imageInfo.img_path, (0, context_1.getPathTo)("products/".concat(product.slug), (0, context_1.getIdsBasedOnSpaceDocRef)(DatabaseService_1.default.getPathParent(this._collection.path))), (0, locale_1.getPrincipalWord)(product.name), imageInfo.img_path.split(';')[0].split('/')[1])];
597
+ case 4: return [2, __assign.apply(void 0, _e.concat([(_g.img_path = (_h.sent()), _g)]))];
598
+ case 5: return [2, imageInfo];
599
+ case 6: return [4, (0, file_1.uploadFileToPath)(useService, imageInfo.img_path.file, (0, context_1.getPathTo)("products/".concat(product.slug), (0, context_1.getIdsBasedOnSpaceDocRef)(DatabaseService_1.default.getPathParent(this._collection.path))))];
600
+ case 7:
601
+ img_path = _h.sent();
602
+ return [2, __assign(__assign({}, imageInfo), { img_path: img_path })];
603
+ }
604
+ });
605
+ }); }))];
606
+ case 2:
607
+ _e = _h.sent();
608
+ _h.label = 3;
609
+ case 3:
610
+ _g.image_files = _e;
611
+ if (!(!!!product.declinations || !!!product.declinations.length)) return [3, 4];
612
+ _f = [];
613
+ return [3, 6];
614
+ case 4: return [4, Promise.all(product.declinations.map(function (declination) { return __awaiter(_this, void 0, void 0, function () {
615
+ var _c;
616
+ var _e;
617
+ var _this = this;
618
+ return __generator(this, function (_f) {
619
+ switch (_f.label) {
620
+ case 0:
621
+ if (!!!declination.image_files)
622
+ return [2, declination];
623
+ if (!!!declination.image_files.length)
624
+ return [2, declination];
625
+ if (!!!declination.image_files.find(function (imageInfo) { return !!imageInfo.img_path && !!imageInfo.img_path.file; }))
626
+ return [2, declination];
627
+ _c = [__assign({}, declination)];
628
+ _e = {};
629
+ return [4, Promise.all(declination.image_files
630
+ .filter(function (i) { return !!i && !!i.img_path; })
631
+ .map(function (imageInfo) { return __awaiter(_this, void 0, void 0, function () {
632
+ var _c, img_path;
633
+ var _e;
634
+ return __generator(this, function (_f) {
635
+ switch (_f.label) {
636
+ case 0:
637
+ if (!(typeof imageInfo.img_path === 'string')) return [3, 3];
638
+ if (!!!imageInfo.img_path.includes('base64')) return [3, 2];
639
+ _c = [__assign({}, imageInfo)];
640
+ _e = {};
641
+ return [4, (0, file_1.uploadStringToPath)(imageInfo.img_path, (0, context_1.getPathTo)("brands/".concat(product.slug), (0, context_1.getIdsBasedOnSpaceDocRef)(DatabaseService_1.default.getPathParent(this._collection.path))), (0, locale_1.getPrincipalWord)(declination.name), imageInfo.img_path.split(';')[0].split('/')[1])];
642
+ case 1: return [2, __assign.apply(void 0, _c.concat([(_e.img_path = (_f.sent()), _e)]))];
643
+ case 2: return [2, imageInfo];
644
+ case 3: return [4, (0, file_1.uploadFileToPath)(useService, imageInfo.img_path.file, (0, context_1.getPathTo)("products/".concat(product.slug), (0, context_1.getIdsBasedOnSpaceDocRef)(DatabaseService_1.default.getPathParent(this._collection.path))))];
645
+ case 4:
646
+ img_path = (_f.sent());
647
+ return [2, __assign(__assign({}, imageInfo), { img_path: img_path })];
648
+ }
649
+ });
650
+ }); }))];
651
+ case 1: return [2, __assign.apply(void 0, _c.concat([(_e.image_files = _f.sent(), _e)]))];
652
+ }
653
+ });
654
+ }); }))];
655
+ case 5:
656
+ _f = _h.sent();
657
+ _h.label = 6;
658
+ case 6: return [2, __assign.apply(void 0, _c.concat([(_g.declinations = _f, _g)]))];
659
+ }
660
+ });
661
+ }); };
662
+ _this._getAttributes = function () {
663
+ try {
664
+ if (!!!_this.data().product_attributes || !!!_this._data.id)
665
+ return [];
666
+ return _this.data().product_attributes.map(function (d) {
667
+ return new ProductAttribute_1.default(d, DatabaseService_1.default.addToPath(DatabaseService_1.default.addToPath(_this._collection.path, _this._data.id), 'attributes'), _this._user);
668
+ });
669
+ }
670
+ catch (err) {
671
+ throw new Error("Error while getting attributes: ".concat(err));
672
+ }
673
+ };
674
+ _this._addAttribute = function (productAttribute) { return __awaiter(_this, void 0, void 0, function () {
675
+ var pas, existingPA, _attributes;
676
+ return __generator(this, function (_c) {
677
+ switch (_c.label) {
678
+ case 0:
679
+ pas = !!this.data().product_attributes ? this.data().product_attributes : [];
680
+ existingPA = pas.find(function (a) { return a.attribute_id === productAttribute.attribute_id; });
681
+ if (!!!existingPA) return [3, 2];
682
+ _attributes = pas.map(function (pa) {
683
+ if (pa.id !== existingPA.id)
684
+ return pa;
685
+ return __assign(__assign({}, pa), productAttribute);
686
+ });
687
+ return [4, this.save({ product_attributes: _attributes })];
688
+ case 1: return [2, _c.sent()];
689
+ case 2: return [4, this.save({
690
+ product_attributes: __spreadArray(__spreadArray([], pas, true), [productAttribute], false)
691
+ })];
692
+ case 3: return [2, _c.sent()];
693
+ }
694
+ });
695
+ }); };
696
+ _this._getCategory = function () { return __awaiter(_this, void 0, void 0, function () {
697
+ var categorySnap, err_1;
698
+ return __generator(this, function (_c) {
699
+ switch (_c.label) {
700
+ case 0:
701
+ if (!!!this._data.category)
702
+ throw new Error("Product categoy is not yet defined");
703
+ _c.label = 1;
704
+ case 1:
705
+ _c.trys.push([1, 3, , 4]);
706
+ return [4, DatabaseService_1.default.getDocument(DatabaseService_1.default.addToPath(DatabaseService_1.default.getPathParent(this._collection.path), 'categories'), this._data.category)];
707
+ case 2:
708
+ categorySnap = _c.sent();
709
+ if (!!!categorySnap)
710
+ throw new Error("Inexisting category");
711
+ if (!categorySnap.exists)
712
+ throw new Error("Impossible to retreive the category");
713
+ if (!!!categorySnap.data)
714
+ throw new Error("Impossible te retreive the category data");
715
+ return [2, new Category_1.default(__assign(__assign({}, categorySnap.data), { id: categorySnap.id }), {
716
+ path: DatabaseService_1.default.addToPath(DatabaseService_1.default.getPathParent(this._collection.path), 'categories')
717
+ }, this._user)];
718
+ case 3:
719
+ err_1 = _c.sent();
720
+ throw new Error("Error while getting categories: ".concat(err_1));
721
+ case 4: return [2];
722
+ }
723
+ });
724
+ }); };
725
+ _this._getBrand = function () { return __awaiter(_this, void 0, void 0, function () {
726
+ var brandSnap, err_2;
727
+ return __generator(this, function (_c) {
728
+ switch (_c.label) {
729
+ case 0:
730
+ if (!!!this._data.brand)
731
+ return [2, undefined];
732
+ _c.label = 1;
733
+ case 1:
734
+ _c.trys.push([1, 3, , 4]);
735
+ return [4, DatabaseService_1.default.getDocument(DatabaseService_1.default.addToPath(DatabaseService_1.default.getPathParent(this._collection.path), 'brands'), this._data.brand)];
736
+ case 2:
737
+ brandSnap = _c.sent();
738
+ if (!!!brandSnap)
739
+ throw new Error("Inexisting brand");
740
+ if (!brandSnap.exists)
741
+ throw new Error("Impossible to retreive the brand");
742
+ if (!!!brandSnap.data)
743
+ throw new Error("Impossible te retreive the brand data");
744
+ return [2, new Brand_1.default(__assign(__assign({}, brandSnap.data), { id: brandSnap.id }), {
745
+ path: DatabaseService_1.default.addToPath(DatabaseService_1.default.getPathParent(this._collection.path), 'brands')
746
+ }, this._user)];
747
+ case 3:
748
+ err_2 = _c.sent();
749
+ throw new Error("Error while getting brands: ".concat(err_2));
750
+ case 4: return [2];
751
+ }
752
+ });
753
+ }); };
754
+ _this._getDeclinations = function () {
755
+ if (!!!_this._data.id)
756
+ throw new Error("Product must be saved to create declinations.");
757
+ var declinationCollectionRef = DatabaseService_1.default.addToPath(DatabaseService_1.default.addToPath(_this._collection.path, _this._data.id), 'declinations');
758
+ if (!!!_this.data().declinations)
759
+ return [];
760
+ return _this.data().declinations.map(function (d) { return new Declination_1.default(d, declinationCollectionRef, _this._user); });
761
+ };
762
+ _this._addDeclination = function (declination) { return __awaiter(_this, void 0, void 0, function () {
763
+ return __generator(this, function (_c) {
764
+ if (!!!this._data.id)
765
+ throw new Error("Product must be saved to create declinations.");
766
+ if (!!!this.data().declinations)
767
+ return [2];
768
+ return [2, this.save({
769
+ declinations: __spreadArray(__spreadArray([], this.data().declinations, true), [declination], false)
770
+ })];
771
+ });
772
+ }); };
773
+ _this._saveIndex = function (category) { return __awaiter(_this, void 0, void 0, function () {
774
+ var brand, declinations, e_4, tree, _c, space, indexed, error_3;
775
+ return __generator(this, function (_e) {
776
+ switch (_e.label) {
777
+ case 0:
778
+ if (!!!this._data.id)
779
+ throw new Error('Product must be saved first.');
780
+ if (DatabaseService_1.default.getPathParent(this._collection.path) == '/')
781
+ throw new Error('BD badly initialised. Products collection must have a parent.');
782
+ brand = undefined;
783
+ declinations = this.data().declinations;
784
+ if (!!!declinations) {
785
+ console.log("No declinations to update");
786
+ return [2, true];
787
+ }
788
+ _e.label = 1;
789
+ case 1:
790
+ _e.trys.push([1, 15, , 16]);
791
+ if (!(!!!category && !!!this.data().is_service)) return [3, 3];
792
+ return [4, this.category.get()];
793
+ case 2:
794
+ category = _e.sent();
795
+ if (!!!category)
796
+ throw new Error("A category must be set");
797
+ _e.label = 3;
798
+ case 3:
799
+ if (!!!this._data.brand) return [3, 7];
800
+ _e.label = 4;
801
+ case 4:
802
+ _e.trys.push([4, 6, , 7]);
803
+ return [4, this.brand.get()];
804
+ case 5:
805
+ brand = _e.sent();
806
+ return [3, 7];
807
+ case 6:
808
+ e_4 = _e.sent();
809
+ console.log('could not fetch brand with id ', this._data.brand, ': ', e_4.toString());
810
+ return [3, 7];
811
+ case 7:
812
+ if (!!!category) return [3, 9];
813
+ return [4, category.getParentTree()];
814
+ case 8:
815
+ _c = _e.sent();
816
+ return [3, 10];
817
+ case 9:
818
+ _c = [];
819
+ _e.label = 10;
820
+ case 10:
821
+ tree = _c;
822
+ return [4, Space_1.default.getCurrentSpaceWithRef(this._collection.path, this._user)];
823
+ case 11:
824
+ space = _e.sent();
825
+ return [4, space.initCurrencies()];
826
+ case 12:
827
+ _e.sent();
828
+ return [4, (0, Product_1.populateIndexedProduct)(this.data(), tree, space, category, brand)];
829
+ case 13:
830
+ indexed = _e.sent();
831
+ console.log('index is created');
832
+ return [4, DatabaseService_1.default.saveDocument(DatabaseService_1.default.addToPath(DatabaseService_1.default.getPathParent(this._collection.path), 'indexes/products/public'), this._data.id, (0, moment_1.default)().unix(), (0, object_1.stripObjectForFirebase)(__assign(__assign({}, indexed), { declinations: indexed.declinations.map(function (declination) {
833
+ if (!!declination.promo_price || typeof declination.promo_price !== 'boolean')
834
+ return declination;
835
+ var promo_price = declination.promo_price, rest = __rest(declination, ["promo_price"]);
836
+ return __assign({}, rest);
837
+ }) })), { merge: true })];
838
+ case 14:
839
+ _e.sent();
840
+ return [2, true];
841
+ case 15:
842
+ error_3 = _e.sent();
843
+ console.log("error while indexing", error_3.toString());
844
+ console.trace("indexing error trace");
845
+ throw error_3;
846
+ case 16: return [2];
847
+ }
848
+ });
849
+ }); };
850
+ _this._getIndex = function () { return __awaiter(_this, void 0, void 0, function () {
851
+ var indexedProductsDocs;
852
+ var _this = this;
853
+ return __generator(this, function (_c) {
854
+ switch (_c.label) {
855
+ case 0:
856
+ if (!!!this._data.id)
857
+ throw new Error("Product has never been saved");
858
+ if (DatabaseService_1.default.getPathParent(this._collection.path) == '/')
859
+ throw new Error("Can't access indexes collection");
860
+ return [4, DatabaseService_1.default.getDocuments(DatabaseService_1.default.addToPath(DatabaseService_1.default.getPathParent(this._collection.path), 'indexes/products/public'), { query: [{ field: 'product_id', operator: '==', value: this._data.id }] })];
861
+ case 1:
862
+ indexedProductsDocs = _c.sent();
863
+ return [4, Promise.all(indexedProductsDocs
864
+ .filter(function (d) { return !!d.exists && !!d.data; })
865
+ .map(function (doc) {
866
+ return new Product_2.default(__assign(__assign({}, doc.data), { id: doc.id }), {
867
+ path: DatabaseService_1.default.addToPath(DatabaseService_1.default.getPathParent(_this._collection.path), 'indexes/products/public')
868
+ }, _this._user);
869
+ }))];
870
+ case 2: return [2, _c.sent()];
871
+ }
872
+ });
873
+ }); };
874
+ _this._deleteIndex = function () { return __awaiter(_this, void 0, void 0, function () {
875
+ var error_4;
876
+ return __generator(this, function (_c) {
877
+ switch (_c.label) {
878
+ case 0:
879
+ if (DatabaseService_1.default.getPathParent(this._collection.path) == '/')
880
+ throw new Error("Can't access parent collection");
881
+ _c.label = 1;
882
+ case 1:
883
+ _c.trys.push([1, 3, , 4]);
884
+ if (!!!this._data.id)
885
+ throw new Error('No id for selected product');
886
+ return [4, DatabaseService_1.default.deleteDocument(DatabaseService_1.default.addToPath(DatabaseService_1.default.getPathParent(this._collection.path), 'indexes/products/public'), this._data.id)];
887
+ case 2:
888
+ _c.sent();
889
+ return [3, 4];
890
+ case 3:
891
+ error_4 = _c.sent();
892
+ throw error_4;
893
+ case 4: return [2];
894
+ }
895
+ });
896
+ }); };
897
+ _this.callPublicService = function (service, fromServer) {
898
+ if (fromServer === void 0) { fromServer = false; }
899
+ return __awaiter(_this, void 0, void 0, function () {
900
+ var requestHeader, url, method, quoteReq, details, error_5;
901
+ return __generator(this, function (_c) {
902
+ switch (_c.label) {
903
+ case 0:
904
+ requestHeader = (0, services_1.productServiceUrl)(__assign({}, service.external_service_infos), service.external_service_name);
905
+ if (!!!requestHeader)
906
+ throw new Error("Can't access service provider. Please validate your external services configuration");
907
+ url = requestHeader.url, method = requestHeader.type;
908
+ _c.label = 1;
909
+ case 1:
910
+ _c.trys.push([1, 4, , 5]);
911
+ return [4, fetch(!!fromServer ? "".concat(url, "&q=").concat(this.data().name[0].value) : '/api/services/cors', {
912
+ headers: { 'content-type': 'application/json' },
913
+ method: !!fromServer ? method : 'POST',
914
+ body: JSON.stringify({
915
+ props: { method: method, headers: { 'content-type': 'application/json' } },
916
+ url: "".concat(url, "&q=").concat(this.data().name[0].value)
917
+ })
918
+ })];
919
+ case 2:
920
+ quoteReq = _c.sent();
921
+ if (quoteReq.status.toString() !== '200')
922
+ throw new Error("Error while getting service: ".concat(quoteReq.statusText));
923
+ return [4, quoteReq.json()];
924
+ case 3:
925
+ details = _c.sent();
926
+ return [2, { confirmed: true, details: details }];
927
+ case 4:
928
+ error_5 = _c.sent();
929
+ (0, console_1.errorMessage)("Error while calling order service ".concat(error_5, " - ").concat(JSON.stringify(error_5)));
930
+ return [2, { confirmed: false, error: error_5 }];
931
+ case 5: return [2];
932
+ }
933
+ });
934
+ });
935
+ };
936
+ _this._updateAllDeclinationsPrice = function (pricingMapFromExternalOrCustom, attributes, saveToDatabase, updateRegular, spaceToUse) {
937
+ if (saveToDatabase === void 0) { saveToDatabase = true; }
938
+ if (updateRegular === void 0) { updateRegular = true; }
939
+ return __awaiter(_this, void 0, void 0, function () {
940
+ var space, _c, spacePrice, declinations, declinationsWithAttributeLinkedToService, _loop_1, this_1, _i, declinationsWithAttributeLinkedToService_1, declinationToUpdate, state_1, error_6;
941
+ return __generator(this, function (_e) {
942
+ switch (_e.label) {
943
+ case 0:
944
+ _e.trys.push([0, 9, , 10]);
945
+ if (!!!spaceToUse) return [3, 1];
946
+ _c = spaceToUse;
947
+ return [3, 3];
948
+ case 1: return [4, Space_1.default.getCurrentSpaceWithRef(this._collection.path, this._user)];
949
+ case 2:
950
+ _c = _e.sent();
951
+ _e.label = 3;
952
+ case 3:
953
+ space = _c;
954
+ spacePrice = this.adaptPriceChartingValues(pricingMapFromExternalOrCustom, space);
955
+ declinations = this.data().declinations;
956
+ if (!!!declinations)
957
+ return [2, false];
958
+ declinationsWithAttributeLinkedToService = declinations.filter(function (d) {
959
+ return !!d.attributes.find(function (pa) {
960
+ var attr = attributes.find(function (_a) { return _a.id === pa.attribute_id; });
961
+ if (!!!attr)
962
+ return false;
963
+ if (!!!attr.is_service_related)
964
+ return false;
965
+ if (!!!attr.service_links_to_attribute)
966
+ return false;
967
+ return true;
968
+ });
969
+ });
970
+ if (!!!declinationsWithAttributeLinkedToService.length)
971
+ return [2, false];
972
+ _loop_1 = function (declinationToUpdate) {
973
+ var relationProductAttribute, relationAttribute, propsToLink, price, priceDetails, _declinations;
974
+ return __generator(this, function (_f) {
975
+ switch (_f.label) {
976
+ case 0:
977
+ relationProductAttribute = declinationToUpdate.attributes.find(function (pa) {
978
+ var attr = attributes.find(function (_a) { return _a.id === pa.attribute_id; });
979
+ if (!!!attr)
980
+ return false;
981
+ if (!!!attr.is_service_related)
982
+ return false;
983
+ return true;
984
+ });
985
+ if (!!!relationProductAttribute)
986
+ return [2, "continue"];
987
+ relationAttribute = attributes.find(function (a) { return a.id === relationProductAttribute.attribute_id; });
988
+ if (!!!relationAttribute)
989
+ return [2, "continue"];
990
+ if (!!!relationAttribute.service_links_to_attribute)
991
+ return [2, "continue"];
992
+ propsToLink = relationAttribute.service_links_to_attribute.find(function (slta) {
993
+ return slta.value_slugs.find(function (vs) { return !!relationProductAttribute.values.find(function (value) { return value.slug === vs.slug; }); });
994
+ });
995
+ if (!!!propsToLink)
996
+ return [2, "continue"];
997
+ if (!!!spacePrice[propsToLink.service_field])
998
+ return [2, "continue"];
999
+ price = (0, lodash_1.toNumber)(spacePrice[propsToLink.service_field]) / 100;
1000
+ priceDetails = propsToLink.value_slugs.find(function (vs) { return !!relationProductAttribute.values.find(function (value) { return value.slug === vs.slug; }); });
1001
+ if (!!priceDetails) {
1002
+ if (!!priceDetails.percent_variation) {
1003
+ price = price * (1 + priceDetails.percent_variation / 100);
1004
+ }
1005
+ }
1006
+ _declinations = declinations.map(function (declination) {
1007
+ if (declination.sku !== declinationToUpdate.sku)
1008
+ return declination;
1009
+ return __assign(__assign({}, declination), { price: __assign(__assign(__assign({}, declination.price), { pdsf: { amount: (0, lodash_1.toNumber)(price) } }), (!!updateRegular
1010
+ ? {
1011
+ regular: {
1012
+ amount: (0, lodash_1.ceil)((0, lodash_1.toNumber)(price)) - 0.01 >= (0, lodash_1.toNumber)(declination.price.regular.amount)
1013
+ ? (0, lodash_1.ceil)((0, lodash_1.toNumber)(price)) - 0.01
1014
+ : (0, lodash_1.toNumber)(declination.price.regular.amount)
1015
+ }
1016
+ }
1017
+ : {})) });
1018
+ });
1019
+ if (!!!saveToDatabase)
1020
+ return [2, { value: {
1021
+ declinations: _declinations,
1022
+ services: { price_charting: spacePrice }
1023
+ } }];
1024
+ return [4, this_1.save({
1025
+ declinations: _declinations
1026
+ }, { index: true })];
1027
+ case 1:
1028
+ _f.sent();
1029
+ return [2];
1030
+ }
1031
+ });
1032
+ };
1033
+ this_1 = this;
1034
+ _i = 0, declinationsWithAttributeLinkedToService_1 = declinationsWithAttributeLinkedToService;
1035
+ _e.label = 4;
1036
+ case 4:
1037
+ if (!(_i < declinationsWithAttributeLinkedToService_1.length)) return [3, 7];
1038
+ declinationToUpdate = declinationsWithAttributeLinkedToService_1[_i];
1039
+ return [5, _loop_1(declinationToUpdate)];
1040
+ case 5:
1041
+ state_1 = _e.sent();
1042
+ if (typeof state_1 === "object")
1043
+ return [2, state_1.value];
1044
+ _e.label = 6;
1045
+ case 6:
1046
+ _i++;
1047
+ return [3, 4];
1048
+ case 7: return [4, this.save({
1049
+ services: __assign(__assign({}, this.data().services), { price_charting: spacePrice })
1050
+ })];
1051
+ case 8:
1052
+ _e.sent();
1053
+ return [2, true];
1054
+ case 9:
1055
+ error_6 = _e.sent();
1056
+ if (process.env.NODE_ENV !== 'production')
1057
+ console.log(error_6);
1058
+ return [2, false];
1059
+ case 10: return [2];
1060
+ }
1061
+ });
1062
+ });
1063
+ };
1064
+ _this.adaptPriceChartingValues = function (pricingMap, space) {
1065
+ var returnValue = {};
1066
+ (0, lodash_1.forEach)(pricingMap, function (val, key) {
1067
+ if (['loose-price', 'cib-price', 'loose-price', 'box-only-price', 'manual-only-price', 'new-price'].includes(key)) {
1068
+ returnValue[key] = (0, currency_1.calculateAmountByCurrency)(space, space.preferences.default.currency(), (0, lodash_1.toNumber)(val), 'USD');
1069
+ return;
1070
+ }
1071
+ });
1072
+ return __assign(__assign({}, pricingMap), returnValue);
1073
+ };
1074
+ _this._updateAllDeclinationsPriceAsync = function (pricingMapFromExternalOrCustom) { return __awaiter(_this, void 0, void 0, function () {
1075
+ var attributesRes, attributes, error_7;
1076
+ return __generator(this, function (_c) {
1077
+ switch (_c.label) {
1078
+ case 0:
1079
+ if (DatabaseService_1.default.getPathParent(this._collection.path) == '/')
1080
+ throw new Error("Can't fetch attributes");
1081
+ _c.label = 1;
1082
+ case 1:
1083
+ _c.trys.push([1, 4, , 5]);
1084
+ return [4, DatabaseService_1.default.getDocuments(DatabaseService_1.default.addToPath(DatabaseService_1.default.getPathParent(this._collection.path), 'attributes'))];
1085
+ case 2:
1086
+ attributesRes = _c.sent();
1087
+ attributes = attributesRes
1088
+ .filter(function (d) { return !!d.exists && !!d.data; })
1089
+ .map(function (d) { return (__assign(__assign({}, d.data), { id: d.id })); });
1090
+ return [4, this._updateAllDeclinationsPrice(pricingMapFromExternalOrCustom, attributes)];
1091
+ case 3: return [2, _c.sent()];
1092
+ case 4:
1093
+ error_7 = _c.sent();
1094
+ (0, console_1.errorMessage)("Error while fetching price and attributes");
1095
+ return [2, []];
1096
+ case 5: return [2];
1097
+ }
1098
+ });
1099
+ }); };
1100
+ _this._getMarketplacePublications = function (options) { return __awaiter(_this, void 0, void 0, function () {
1101
+ var publications, error_8;
1102
+ return __generator(this, function (_c) {
1103
+ switch (_c.label) {
1104
+ case 0:
1105
+ if (DatabaseService_1.default.getPathParent(this._collection.path) == '/')
1106
+ throw new Error("Can't fetch marketplace publications");
1107
+ _c.label = 1;
1108
+ case 1:
1109
+ _c.trys.push([1, 3, , 4]);
1110
+ return [4, DatabaseService_1.default.getDocuments(DatabaseService_1.default.addToPath(DatabaseService_1.default.getPathParent(this._collection.path), 'marketplace_publications'), options)];
1111
+ case 2:
1112
+ publications = _c.sent();
1113
+ return [2, publications.map(function (d) { return (__assign(__assign({}, d.data), { id: d.id })); })];
1114
+ case 3:
1115
+ error_8 = _c.sent();
1116
+ (0, console_1.errorMessage)("Error while fetching marketplace publications");
1117
+ return [2, []];
1118
+ case 4: return [2];
1119
+ }
1120
+ });
1121
+ }); };
1122
+ _this._marketplacePublicationsListner = function (callback, options) {
1123
+ if (DatabaseService_1.default.getPathParent(_this._collection.path) == '/')
1124
+ throw new Error("Can't fetch marketplace publications");
1125
+ try {
1126
+ var call = DatabaseService_1.default.addToPath(DatabaseService_1.default.getPathParent(_this._collection.path), 'marketplace_publications');
1127
+ return DatabaseService_1.default.registerListener(function (dataSnap) {
1128
+ if (!!!dataSnap.length)
1129
+ callback([]);
1130
+ (0, console_1.workingMessage)("Marketplace publications has been udpated. Refreshing...");
1131
+ callback(dataSnap.map(function (d) { return (__assign(__assign({}, d.data), { id: d.id })); }));
1132
+ }, call, !!options ? options : { query: [{ field: 'parent_slug', operator: '==', value: _this._data.slug }] });
1133
+ }
1134
+ catch (error) {
1135
+ (0, console_1.errorMessage)("Error while listening marketplace publications");
1136
+ return '';
1137
+ }
1138
+ };
1139
+ _this._createNewPublication = function (sku, marketplacePublication, space) { return __awaiter(_this, void 0, void 0, function () {
1140
+ var productInvetory, error_9, newMarketplacePublication, error_10;
1141
+ return __generator(this, function (_c) {
1142
+ switch (_c.label) {
1143
+ case 0:
1144
+ if (DatabaseService_1.default.getPathParent(this._collection.path) == '/')
1145
+ throw new Error("Can't fetch marketplace publications");
1146
+ _c.label = 1;
1147
+ case 1:
1148
+ _c.trys.push([1, 3, , 4]);
1149
+ return [4, space.inventoryItems.getOne(sku)];
1150
+ case 2:
1151
+ productInvetory = _c.sent();
1152
+ if (!!!productInvetory || !!!productInvetory.web_inventory)
1153
+ throw new Error("Can't find inventory item with sku: ".concat(sku));
1154
+ if (!!!productInvetory.web_inventory.available)
1155
+ throw new Error("Inventory item with sku: ".concat(sku, " is not available"));
1156
+ if (marketplacePublication.quantity < 1)
1157
+ throw new Error("No quantity available");
1158
+ if ((0, lodash_1.toNumber)(productInvetory.web_inventory.available) < 1)
1159
+ throw new Error("No quantity available");
1160
+ if ((0, lodash_1.toNumber)(productInvetory.web_inventory.available) < marketplacePublication.quantity)
1161
+ throw new Error("Inventory item with sku: ".concat(sku, " is not available"));
1162
+ return [3, 4];
1163
+ case 3:
1164
+ error_9 = _c.sent();
1165
+ (0, console_1.errorMessage)("Error while fetching inventory item with sku: ".concat(sku));
1166
+ return [2, false];
1167
+ case 4:
1168
+ _c.trys.push([4, 6, , 7]);
1169
+ newMarketplacePublication = new MarketplacePublication_1.default(__assign(__assign({}, marketplacePublication), { sku: sku, parent_slug: this.data().slug, created_at: new Date(), created_by: this._user }), {
1170
+ path: DatabaseService_1.default.addToPath(DatabaseService_1.default.getPathParent(this._collection.path), 'marketplace_publications')
1171
+ }, this._user);
1172
+ return [4, newMarketplacePublication.save()];
1173
+ case 5: return [2, _c.sent()];
1174
+ case 6:
1175
+ error_10 = _c.sent();
1176
+ (0, console_1.errorMessage)("Error while creating marketplace publications");
1177
+ return [2, false];
1178
+ case 7: return [2];
1179
+ }
1180
+ });
1181
+ }); };
1182
+ _this._updateMarketplacePublication = function (sku, marketplacePublication) { return __awaiter(_this, void 0, void 0, function () {
1183
+ var marketplacePublicationObj, error_11;
1184
+ return __generator(this, function (_c) {
1185
+ switch (_c.label) {
1186
+ case 0:
1187
+ if (DatabaseService_1.default.getPathParent(this._collection.path) == '/')
1188
+ throw new Error("Can't update marketplace publications");
1189
+ _c.label = 1;
1190
+ case 1:
1191
+ _c.trys.push([1, 4, , 5]);
1192
+ return [4, MarketplacePublication_1.default.getMarketplacePublicationObj(sku, DatabaseService_1.default.addToPath(DatabaseService_1.default.getPathParent(this._collection.path), 'marketplace_publications'), this._user)];
1193
+ case 2:
1194
+ marketplacePublicationObj = _c.sent();
1195
+ if (!!!marketplacePublicationObj)
1196
+ throw new Error("No marketplace publication found");
1197
+ return [4, marketplacePublicationObj.save(marketplacePublication)];
1198
+ case 3: return [2, _c.sent()];
1199
+ case 4:
1200
+ error_11 = _c.sent();
1201
+ (0, console_1.errorMessage)("Error while updating marketplace publications");
1202
+ return [2, false];
1203
+ case 5: return [2];
1204
+ }
1205
+ });
1206
+ }); };
1207
+ _this._removeMarketplacePublication = function (publicationId) { return __awaiter(_this, void 0, void 0, function () {
1208
+ var marketplacePublicationObj, error_12;
1209
+ return __generator(this, function (_c) {
1210
+ switch (_c.label) {
1211
+ case 0:
1212
+ if (DatabaseService_1.default.getPathParent(this._collection.path) == '/')
1213
+ throw new Error("Can't remove marketplace publications");
1214
+ _c.label = 1;
1215
+ case 1:
1216
+ _c.trys.push([1, 4, , 5]);
1217
+ return [4, MarketplacePublication_1.default.getMarketplacePublicationObjById(publicationId, DatabaseService_1.default.addToPath(DatabaseService_1.default.getPathParent(this._collection.path), 'marketplace_publications'), this._user)];
1218
+ case 2:
1219
+ marketplacePublicationObj = _c.sent();
1220
+ if (!!!marketplacePublicationObj)
1221
+ throw new Error("No marketplace publication found");
1222
+ return [4, marketplacePublicationObj.delete()];
1223
+ case 3: return [2, _c.sent()];
1224
+ case 4:
1225
+ error_12 = _c.sent();
1226
+ (0, console_1.errorMessage)("Error while deleting marketplace publications");
1227
+ return [2, false];
1228
+ case 5: return [2];
1229
+ }
1230
+ });
1231
+ }); };
1232
+ _this.attributes = {
1233
+ get: _this._getAttributes,
1234
+ add: _this._addAttribute
1235
+ };
1236
+ _this.brand = {
1237
+ get: _this._getBrand
1238
+ };
1239
+ _this.category = {
1240
+ get: _this._getCategory
1241
+ };
1242
+ _this.publications = {
1243
+ get: _this._getMarketplacePublications,
1244
+ listen: _this._marketplacePublicationsListner,
1245
+ update: _this._updateMarketplacePublication,
1246
+ create: _this._createNewPublication,
1247
+ remove: _this._removeMarketplacePublication
1248
+ };
1249
+ _this.declinations = {
1250
+ get: _this._getDeclinations,
1251
+ add: _this._addDeclination,
1252
+ update: {
1253
+ priceSync: _this._updateAllDeclinationsPrice,
1254
+ priceAsync: _this._updateAllDeclinationsPriceAsync
1255
+ }
1256
+ };
1257
+ _this.index = {
1258
+ save: _this._saveIndex,
1259
+ get: _this._getIndex,
1260
+ delete: _this._deleteIndex
1261
+ };
1262
+ return _this;
1263
+ }
1264
+ var _b;
1265
+ _b = Product;
1266
+ Product.fetch = function (_c) {
1267
+ var app = _c.app, api = _c.api, _e = _c.hpp, hpp = _e === void 0 ? 10 : _e, _f = _c.page, page = _f === void 0 ? 0 : _f, _g = _c.query, query = _g === void 0 ? '' : _g, _h = _c.filters, filters = _h === void 0 ? '' : _h, _j = _c.indice, indice = _j === void 0 ? 'products' : _j, _k = _c.f, f = _k === void 0 ? '' : _k, space = _c.space, _l = _c.useProspr, useProspr = _l === void 0 ? false : _l;
1268
+ return __awaiter(void 0, void 0, void 0, function () {
1269
+ var prosprSearch, prosprService, algolia, results, error_13;
1270
+ return __generator(_b, function (_m) {
1271
+ switch (_m.label) {
1272
+ case 0:
1273
+ _m.trys.push([0, 2, , 3]);
1274
+ prosprSearch = false;
1275
+ if (!!space) {
1276
+ prosprService = space
1277
+ .data()
1278
+ .external_services.find(function (es) { return es.external_service_name.toLowerCase().trim() === 'prosprsearch'; });
1279
+ prosprSearch = !!prosprService && prosprService.is_active;
1280
+ }
1281
+ algolia = (0, algolia_1.createClient)({
1282
+ app: app,
1283
+ indexId: "".concat(__1.isTestEnv ? 'test' : 'prod', "_").concat(indice),
1284
+ key: !!useProspr ? api : prosprSearch && !!space ? space.preferences.prosprsearch.app() : api,
1285
+ prospr: !!prosprSearch || !!useProspr
1286
+ });
1287
+ return [4, algolia.search(query, {
1288
+ hitsPerPage: hpp,
1289
+ facetFilters: filters,
1290
+ filters: f,
1291
+ page: page,
1292
+ facets: ['*']
1293
+ })];
1294
+ case 1:
1295
+ results = _m.sent();
1296
+ return [2, results];
1297
+ case 2:
1298
+ error_13 = _m.sent();
1299
+ (0, console_1.errorMessage)(error_13);
1300
+ throw error_13;
1301
+ case 3: return [2];
1302
+ }
1303
+ });
1304
+ });
1305
+ };
1306
+ Product.checkExternalValuesForDecli = function (sku, space, org) { return __awaiter(void 0, void 0, void 0, function () {
1307
+ var externalValues, serviceQuery;
1308
+ return __generator(_b, function (_c) {
1309
+ switch (_c.label) {
1310
+ case 0:
1311
+ if (!!!space)
1312
+ return [2];
1313
+ externalValues = undefined;
1314
+ serviceQuery = space.services({ field: 'external_service_name', value: 'G2' });
1315
+ if (!(!!serviceQuery && serviceQuery.length)) return [3, 2];
1316
+ return [4, (0, g2_1.getG2ProductExternalValues)(sku, serviceQuery[0], space, org)];
1317
+ case 1:
1318
+ externalValues = _c.sent();
1319
+ _c.label = 2;
1320
+ case 2: return [2, externalValues];
1321
+ }
1322
+ });
1323
+ }); };
1324
+ return Product;
1325
+ }(baseService_1.BaseServiceClass));
1326
+ exports.default = Product;
1327
1327
  //# sourceMappingURL=index.js.map