@merchi/sdk 1.40.2

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 (662) hide show
  1. package/.eslintrc.cjs +43 -0
  2. package/.turbo/turbo-build.log +1 -0
  3. package/.turbo/turbo-test.log +849 -0
  4. package/LICENSE +21 -0
  5. package/README.md +244 -0
  6. package/coverage/clover.xml +5057 -0
  7. package/coverage/coverage-final.json +133 -0
  8. package/coverage/lcov-report/base.css +224 -0
  9. package/coverage/lcov-report/block-navigation.js +87 -0
  10. package/coverage/lcov-report/favicon.png +0 -0
  11. package/coverage/lcov-report/index.html +191 -0
  12. package/coverage/lcov-report/prettify.css +1 -0
  13. package/coverage/lcov-report/prettify.js +2 -0
  14. package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
  15. package/coverage/lcov-report/sorter.js +210 -0
  16. package/coverage/lcov-report/src/constants/auto_assign_production_on_actions.ts.html +103 -0
  17. package/coverage/lcov-report/src/constants/call_to_actions.ts.html +127 -0
  18. package/coverage/lcov-report/src/constants/discount_types.ts.html +100 -0
  19. package/coverage/lcov-report/src/constants/domain_types.ts.html +121 -0
  20. package/coverage/lcov-report/src/constants/errors.ts.html +181 -0
  21. package/coverage/lcov-report/src/constants/event_types.ts.html +100 -0
  22. package/coverage/lcov-report/src/constants/field_types.ts.html +133 -0
  23. package/coverage/lcov-report/src/constants/index.html +551 -0
  24. package/coverage/lcov-report/src/constants/index.ts.html +238 -0
  25. package/coverage/lcov-report/src/constants/input_types.ts.html +124 -0
  26. package/coverage/lcov-report/src/constants/inventory_statuses.ts.html +103 -0
  27. package/coverage/lcov-report/src/constants/invoice_types.ts.html +100 -0
  28. package/coverage/lcov-report/src/constants/item_types.ts.html +100 -0
  29. package/coverage/lcov-report/src/constants/job/drafting_status.ts.html +106 -0
  30. package/coverage/lcov-report/src/constants/job/index.html +176 -0
  31. package/coverage/lcov-report/src/constants/job/payment_status.ts.html +106 -0
  32. package/coverage/lcov-report/src/constants/job/production_status.ts.html +121 -0
  33. package/coverage/lcov-report/src/constants/job/shipment_status.ts.html +118 -0
  34. package/coverage/lcov-report/src/constants/job/supply_chain_request_status.ts.html +103 -0
  35. package/coverage/lcov-report/src/constants/job_priorities.ts.html +103 -0
  36. package/coverage/lcov-report/src/constants/job_status.ts.html +232 -0
  37. package/coverage/lcov-report/src/constants/job_types.ts.html +133 -0
  38. package/coverage/lcov-report/src/constants/notification_sections.ts.html +124 -0
  39. package/coverage/lcov-report/src/constants/notification_types.ts.html +385 -0
  40. package/coverage/lcov-report/src/constants/notification_urgencies.ts.html +106 -0
  41. package/coverage/lcov-report/src/constants/payment_types.ts.html +115 -0
  42. package/coverage/lcov-report/src/constants/platform.ts.html +103 -0
  43. package/coverage/lcov-report/src/constants/product_types.ts.html +163 -0
  44. package/coverage/lcov-report/src/constants/rights.ts.html +100 -0
  45. package/coverage/lcov-report/src/constants/robots_meta_directives.ts.html +118 -0
  46. package/coverage/lcov-report/src/constants/roles.ts.html +223 -0
  47. package/coverage/lcov-report/src/constants/shipment_companies.ts.html +142 -0
  48. package/coverage/lcov-report/src/constants/shipment_services.ts.html +94 -0
  49. package/coverage/lcov-report/src/constants/system_roles.ts.html +94 -0
  50. package/coverage/lcov-report/src/constants/theme_status.ts.html +100 -0
  51. package/coverage/lcov-report/src/constants/user_types.ts.html +136 -0
  52. package/coverage/lcov-report/src/constants/white_label_accessibilities.ts.html +103 -0
  53. package/coverage/lcov-report/src/cookie.ts.html +133 -0
  54. package/coverage/lcov-report/src/entities/address.ts.html +280 -0
  55. package/coverage/lcov-report/src/entities/agent_conversation.ts.html +202 -0
  56. package/coverage/lcov-report/src/entities/agent_skill.ts.html +382 -0
  57. package/coverage/lcov-report/src/entities/agent_skill_approval.ts.html +163 -0
  58. package/coverage/lcov-report/src/entities/agent_skill_version.ts.html +277 -0
  59. package/coverage/lcov-report/src/entities/agent_token_usage.ts.html +349 -0
  60. package/coverage/lcov-report/src/entities/assignment.ts.html +382 -0
  61. package/coverage/lcov-report/src/entities/automatic_payment_relationship.ts.html +172 -0
  62. package/coverage/lcov-report/src/entities/bank.ts.html +220 -0
  63. package/coverage/lcov-report/src/entities/cart.ts.html +391 -0
  64. package/coverage/lcov-report/src/entities/cart_item.ts.html +340 -0
  65. package/coverage/lcov-report/src/entities/cart_shipment_group.ts.html +148 -0
  66. package/coverage/lcov-report/src/entities/cart_shipment_quote.ts.html +184 -0
  67. package/coverage/lcov-report/src/entities/category.ts.html +262 -0
  68. package/coverage/lcov-report/src/entities/company.ts.html +1276 -0
  69. package/coverage/lcov-report/src/entities/company_invitation.ts.html +184 -0
  70. package/coverage/lcov-report/src/entities/country_tax.ts.html +223 -0
  71. package/coverage/lcov-report/src/entities/discount.ts.html +235 -0
  72. package/coverage/lcov-report/src/entities/discount_group.ts.html +214 -0
  73. package/coverage/lcov-report/src/entities/domain.ts.html +3349 -0
  74. package/coverage/lcov-report/src/entities/domain_chat_settings.ts.html +331 -0
  75. package/coverage/lcov-report/src/entities/domain_invitation.ts.html +193 -0
  76. package/coverage/lcov-report/src/entities/domain_tag.ts.html +211 -0
  77. package/coverage/lcov-report/src/entities/draft.ts.html +322 -0
  78. package/coverage/lcov-report/src/entities/draft_comment.ts.html +274 -0
  79. package/coverage/lcov-report/src/entities/draft_preview.ts.html +196 -0
  80. package/coverage/lcov-report/src/entities/draft_preview_layer.ts.html +148 -0
  81. package/coverage/lcov-report/src/entities/draft_template.ts.html +328 -0
  82. package/coverage/lcov-report/src/entities/email_address.ts.html +157 -0
  83. package/coverage/lcov-report/src/entities/email_counter.ts.html +145 -0
  84. package/coverage/lcov-report/src/entities/enrolled_domain.ts.html +205 -0
  85. package/coverage/lcov-report/src/entities/exchange_rate.ts.html +151 -0
  86. package/coverage/lcov-report/src/entities/file.ts.html +580 -0
  87. package/coverage/lcov-report/src/entities/index.html +1271 -0
  88. package/coverage/lcov-report/src/entities/internal_tag.ts.html +238 -0
  89. package/coverage/lcov-report/src/entities/inventory.ts.html +274 -0
  90. package/coverage/lcov-report/src/entities/inventory_group.ts.html +193 -0
  91. package/coverage/lcov-report/src/entities/inventory_unit_variation.ts.html +172 -0
  92. package/coverage/lcov-report/src/entities/invoice.ts.html +661 -0
  93. package/coverage/lcov-report/src/entities/item.ts.html +262 -0
  94. package/coverage/lcov-report/src/entities/job.ts.html +1288 -0
  95. package/coverage/lcov-report/src/entities/job_comment.ts.html +244 -0
  96. package/coverage/lcov-report/src/entities/job_note.ts.html +205 -0
  97. package/coverage/lcov-report/src/entities/job_operation_log.ts.html +208 -0
  98. package/coverage/lcov-report/src/entities/matching_inventory.ts.html +172 -0
  99. package/coverage/lcov-report/src/entities/notification.ts.html +403 -0
  100. package/coverage/lcov-report/src/entities/payment.ts.html +316 -0
  101. package/coverage/lcov-report/src/entities/payment_device.ts.html +175 -0
  102. package/coverage/lcov-report/src/entities/phone_number.ts.html +193 -0
  103. package/coverage/lcov-report/src/entities/product.ts.html +1543 -0
  104. package/coverage/lcov-report/src/entities/product_form.ts.html +238 -0
  105. package/coverage/lcov-report/src/entities/product_form_version.ts.html +238 -0
  106. package/coverage/lcov-report/src/entities/product_review.ts.html +748 -0
  107. package/coverage/lcov-report/src/entities/production_comment.ts.html +244 -0
  108. package/coverage/lcov-report/src/entities/quote.ts.html +505 -0
  109. package/coverage/lcov-report/src/entities/quote_item.ts.html +298 -0
  110. package/coverage/lcov-report/src/entities/reminder.ts.html +307 -0
  111. package/coverage/lcov-report/src/entities/session.ts.html +169 -0
  112. package/coverage/lcov-report/src/entities/shipment.ts.html +673 -0
  113. package/coverage/lcov-report/src/entities/shipment_item.ts.html +130 -0
  114. package/coverage/lcov-report/src/entities/shipment_item_fulfillment.ts.html +130 -0
  115. package/coverage/lcov-report/src/entities/shipment_log.ts.html +196 -0
  116. package/coverage/lcov-report/src/entities/shipment_method.ts.html +268 -0
  117. package/coverage/lcov-report/src/entities/shipment_method_variation.ts.html +202 -0
  118. package/coverage/lcov-report/src/entities/short_url.ts.html +193 -0
  119. package/coverage/lcov-report/src/entities/subscription_plan.ts.html +304 -0
  120. package/coverage/lcov-report/src/entities/supply_domain.ts.html +166 -0
  121. package/coverage/lcov-report/src/entities/support_conversation.ts.html +280 -0
  122. package/coverage/lcov-report/src/entities/support_message.ts.html +175 -0
  123. package/coverage/lcov-report/src/entities/system_role.ts.html +136 -0
  124. package/coverage/lcov-report/src/entities/theme.ts.html +331 -0
  125. package/coverage/lcov-report/src/entities/theme_css_setting.ts.html +154 -0
  126. package/coverage/lcov-report/src/entities/user.ts.html +1369 -0
  127. package/coverage/lcov-report/src/entities/user_company.ts.html +166 -0
  128. package/coverage/lcov-report/src/entities/variation.ts.html +277 -0
  129. package/coverage/lcov-report/src/entities/variation_field.ts.html +889 -0
  130. package/coverage/lcov-report/src/entities/variation_fields_option.ts.html +406 -0
  131. package/coverage/lcov-report/src/entities/variation_option.ts.html +259 -0
  132. package/coverage/lcov-report/src/entities/variations_group.ts.html +208 -0
  133. package/coverage/lcov-report/src/entity.ts.html +3379 -0
  134. package/coverage/lcov-report/src/index.html +221 -0
  135. package/coverage/lcov-report/src/index.ts.html +652 -0
  136. package/coverage/lcov-report/src/merchi.ts.html +1420 -0
  137. package/coverage/lcov-report/src/pricing/discount.ts.html +136 -0
  138. package/coverage/lcov-report/src/pricing/estimate.ts.html +1015 -0
  139. package/coverage/lcov-report/src/pricing/index.html +206 -0
  140. package/coverage/lcov-report/src/pricing/index.ts.html +106 -0
  141. package/coverage/lcov-report/src/pricing/inventory.ts.html +352 -0
  142. package/coverage/lcov-report/src/pricing/matrix.ts.html +736 -0
  143. package/coverage/lcov-report/src/pricing/round.ts.html +166 -0
  144. package/coverage/lcov-report/src/pricing/visibility.ts.html +418 -0
  145. package/coverage/lcov-report/src/request.ts.html +673 -0
  146. package/coverage/lcov-report/src/test_util.ts.html +139 -0
  147. package/coverage/lcov-report/src/toasts.ts.html +187 -0
  148. package/coverage/lcov-report/src/util/float.ts.html +121 -0
  149. package/coverage/lcov-report/src/util/index.html +161 -0
  150. package/coverage/lcov-report/src/util/index.ts.html +106 -0
  151. package/coverage/lcov-report/src/util/query_string.ts.html +124 -0
  152. package/coverage/lcov-report/src/util/validation.ts.html +91 -0
  153. package/coverage/lcov-report/src/uuid.ts.html +109 -0
  154. package/coverage/lcov.info +7758 -0
  155. package/dist/constants/auto_assign_production_on_actions.js +7 -0
  156. package/dist/constants/call_to_actions.js +15 -0
  157. package/dist/constants/call_to_actions.test.js +4 -0
  158. package/dist/constants/discount_types.js +6 -0
  159. package/dist/constants/discount_types.test.js +4 -0
  160. package/dist/constants/domain_types.js +13 -0
  161. package/dist/constants/domain_types.test.js +4 -0
  162. package/dist/constants/errors.js +32 -0
  163. package/dist/constants/event_types.js +6 -0
  164. package/dist/constants/event_types.test.js +4 -0
  165. package/dist/constants/field_types.js +17 -0
  166. package/dist/constants/field_types.test.js +4 -0
  167. package/dist/constants/index.js +25 -0
  168. package/dist/constants/input_types.js +14 -0
  169. package/dist/constants/input_types.test.js +4 -0
  170. package/dist/constants/inventory_status.test.js +4 -0
  171. package/dist/constants/inventory_statuses.js +7 -0
  172. package/dist/constants/invoice_types.js +6 -0
  173. package/dist/constants/invoice_types.test.js +4 -0
  174. package/dist/constants/item_types.js +6 -0
  175. package/dist/constants/item_types.test.js +4 -0
  176. package/dist/constants/job/drafting_status.js +8 -0
  177. package/dist/constants/job/drafting_status.test.js +4 -0
  178. package/dist/constants/job/payment_status.js +8 -0
  179. package/dist/constants/job/payment_status.test.js +4 -0
  180. package/dist/constants/job/production_status.js +13 -0
  181. package/dist/constants/job/production_status.test.js +4 -0
  182. package/dist/constants/job/shipment_status.js +12 -0
  183. package/dist/constants/job/shipment_status.test.js +4 -0
  184. package/dist/constants/job/supply_chain_request_status.js +7 -0
  185. package/dist/constants/job/supply_chain_request_status.test.js +4 -0
  186. package/dist/constants/job_priorities.js +7 -0
  187. package/dist/constants/job_priorities.test.js +4 -0
  188. package/dist/constants/job_status.js +50 -0
  189. package/dist/constants/job_status.test.js +4 -0
  190. package/dist/constants/job_types.js +17 -0
  191. package/dist/constants/job_types.test.js +4 -0
  192. package/dist/constants/notification_sections.js +14 -0
  193. package/dist/constants/notification_sections.test.js +4 -0
  194. package/dist/constants/notification_types.js +101 -0
  195. package/dist/constants/notification_types.test.js +4 -0
  196. package/dist/constants/notification_urgencies.js +7 -0
  197. package/dist/constants/notification_urgencies.test.js +4 -0
  198. package/dist/constants/payment_types.js +11 -0
  199. package/dist/constants/payment_types.test.js +4 -0
  200. package/dist/constants/platform.js +5 -0
  201. package/dist/constants/platform.test.js +8 -0
  202. package/dist/constants/product_types.js +27 -0
  203. package/dist/constants/product_types.test.js +4 -0
  204. package/dist/constants/rights.js +6 -0
  205. package/dist/constants/rights.test.js +4 -0
  206. package/dist/constants/robots_meta_directives.js +11 -0
  207. package/dist/constants/robots_meta_directives.test.js +4 -0
  208. package/dist/constants/roles.js +64 -0
  209. package/dist/constants/roles.test.js +4 -0
  210. package/dist/constants/shipment_companies.js +20 -0
  211. package/dist/constants/shipment_companies.test.js +4 -0
  212. package/dist/constants/shipment_services.js +4 -0
  213. package/dist/constants/shipment_services.test.js +4 -0
  214. package/dist/constants/system_roles.js +4 -0
  215. package/dist/constants/system_roles.test.js +4 -0
  216. package/dist/constants/theme_status.js +6 -0
  217. package/dist/constants/theme_status.test.js +4 -0
  218. package/dist/constants/user_types.js +18 -0
  219. package/dist/constants/user_types.test.js +4 -0
  220. package/dist/constants/white_label_accessibilities.js +6 -0
  221. package/dist/constants/white_label_accessibilities.test.js +4 -0
  222. package/dist/cookie.js +15 -0
  223. package/dist/cookie.test.js +10 -0
  224. package/dist/entities/address.js +104 -0
  225. package/dist/entities/address.test.js +6 -0
  226. package/dist/entities/agent_conversation.js +78 -0
  227. package/dist/entities/agent_conversation.test.js +6 -0
  228. package/dist/entities/agent_skill.js +133 -0
  229. package/dist/entities/agent_skill.test.js +11 -0
  230. package/dist/entities/agent_skill_approval.js +61 -0
  231. package/dist/entities/agent_skill_version.js +99 -0
  232. package/dist/entities/agent_token_analytics.js +1 -0
  233. package/dist/entities/agent_token_usage.js +127 -0
  234. package/dist/entities/agent_token_usage.test.js +6 -0
  235. package/dist/entities/assignment.js +135 -0
  236. package/dist/entities/assignment.test.js +30 -0
  237. package/dist/entities/automatic_payment_relationship.js +65 -0
  238. package/dist/entities/automatic_payment_relationship.test.js +6 -0
  239. package/dist/entities/bank.js +85 -0
  240. package/dist/entities/bank.test.js +6 -0
  241. package/dist/entities/cart.js +173 -0
  242. package/dist/entities/cart.test.js +51 -0
  243. package/dist/entities/cart_item.js +125 -0
  244. package/dist/entities/cart_item.test.js +28 -0
  245. package/dist/entities/cart_shipment_group.js +53 -0
  246. package/dist/entities/cart_shipment_quote.js +70 -0
  247. package/dist/entities/category.js +101 -0
  248. package/dist/entities/category.test.js +52 -0
  249. package/dist/entities/company.js +509 -0
  250. package/dist/entities/company.test.js +6 -0
  251. package/dist/entities/company_invitation.js +70 -0
  252. package/dist/entities/company_invitation.test.js +6 -0
  253. package/dist/entities/country_tax.js +80 -0
  254. package/dist/entities/country_tax.test.js +11 -0
  255. package/dist/entities/discount.js +89 -0
  256. package/dist/entities/discount.test.js +34 -0
  257. package/dist/entities/discount_group.js +80 -0
  258. package/dist/entities/discount_group.test.js +6 -0
  259. package/dist/entities/domain.js +567 -0
  260. package/dist/entities/domain.test.js +356 -0
  261. package/dist/entities/domain_chat_settings.js +134 -0
  262. package/dist/entities/domain_chat_settings.test.js +15 -0
  263. package/dist/entities/domain_invitation.js +74 -0
  264. package/dist/entities/domain_invitation.test.js +6 -0
  265. package/dist/entities/domain_tag.js +77 -0
  266. package/dist/entities/domain_tag.test.js +6 -0
  267. package/dist/entities/draft.js +122 -0
  268. package/dist/entities/draft.test.js +26 -0
  269. package/dist/entities/draft_comment.js +107 -0
  270. package/dist/entities/draft_comment.test.js +6 -0
  271. package/dist/entities/draft_preview.js +74 -0
  272. package/dist/entities/draft_preview.test.js +36 -0
  273. package/dist/entities/draft_preview_layer.js +54 -0
  274. package/dist/entities/draft_preview_layer.test.js +6 -0
  275. package/dist/entities/draft_template.js +119 -0
  276. package/dist/entities/draft_template.test.js +12 -0
  277. package/dist/entities/email_address.js +56 -0
  278. package/dist/entities/email_address.test.js +6 -0
  279. package/dist/entities/email_counter.js +53 -0
  280. package/dist/entities/email_counter.test.js +6 -0
  281. package/dist/entities/enrolled_domain.js +74 -0
  282. package/dist/entities/enrolled_domain.test.js +6 -0
  283. package/dist/entities/exchange_rate.js +56 -0
  284. package/dist/entities/exchange_rate.test.js +6 -0
  285. package/dist/entities/file.js +218 -0
  286. package/dist/entities/file.test.js +38 -0
  287. package/dist/entities/internal_tag.js +84 -0
  288. package/dist/entities/internal_tag.test.js +6 -0
  289. package/dist/entities/inventory.js +97 -0
  290. package/dist/entities/inventory.test.js +22 -0
  291. package/dist/entities/inventory_group.js +69 -0
  292. package/dist/entities/inventory_group.test.js +6 -0
  293. package/dist/entities/inventory_unit_variation.js +61 -0
  294. package/dist/entities/inventory_unit_variation.test.js +6 -0
  295. package/dist/entities/invoice.js +268 -0
  296. package/dist/entities/invoice.test.js +45 -0
  297. package/dist/entities/item.js +96 -0
  298. package/dist/entities/item.test.js +17 -0
  299. package/dist/entities/job.js +519 -0
  300. package/dist/entities/job.test.js +83 -0
  301. package/dist/entities/job_comment.js +94 -0
  302. package/dist/entities/job_comment.test.js +6 -0
  303. package/dist/entities/job_note.js +78 -0
  304. package/dist/entities/job_operation_log.js +82 -0
  305. package/dist/entities/job_operation_log.test.js +6 -0
  306. package/dist/entities/matching_inventory.js +63 -0
  307. package/dist/entities/notification.js +163 -0
  308. package/dist/entities/notification.test.js +51 -0
  309. package/dist/entities/payment.js +122 -0
  310. package/dist/entities/payment.test.js +22 -0
  311. package/dist/entities/payment_device.js +65 -0
  312. package/dist/entities/payment_device.test.js +6 -0
  313. package/dist/entities/phone_number.js +72 -0
  314. package/dist/entities/phone_number.test.js +6 -0
  315. package/dist/entities/product.js +619 -0
  316. package/dist/entities/product.test.js +720 -0
  317. package/dist/entities/product_form.js +92 -0
  318. package/dist/entities/product_form_version.js +95 -0
  319. package/dist/entities/product_review.js +234 -0
  320. package/dist/entities/production_comment.js +94 -0
  321. package/dist/entities/production_comment.test.js +6 -0
  322. package/dist/entities/quote.js +168 -0
  323. package/dist/entities/quote.test.js +86 -0
  324. package/dist/entities/quote_item.js +99 -0
  325. package/dist/entities/quote_item.test.js +40 -0
  326. package/dist/entities/reminder.js +120 -0
  327. package/dist/entities/reminder.test.js +14 -0
  328. package/dist/entities/session.js +63 -0
  329. package/dist/entities/session.test.js +6 -0
  330. package/dist/entities/shipment.js +256 -0
  331. package/dist/entities/shipment.test.js +20 -0
  332. package/dist/entities/shipment_item.js +45 -0
  333. package/dist/entities/shipment_item.test.js +6 -0
  334. package/dist/entities/shipment_item_fulfillment.js +46 -0
  335. package/dist/entities/shipment_item_fulfillment.test.js +6 -0
  336. package/dist/entities/shipment_log.js +75 -0
  337. package/dist/entities/shipment_method.js +104 -0
  338. package/dist/entities/shipment_method.test.js +6 -0
  339. package/dist/entities/shipment_method_variation.js +77 -0
  340. package/dist/entities/shipment_method_variation.test.js +6 -0
  341. package/dist/entities/short_url.js +73 -0
  342. package/dist/entities/short_url.test.js +6 -0
  343. package/dist/entities/subscription_plan.js +122 -0
  344. package/dist/entities/subscription_plan.test.js +6 -0
  345. package/dist/entities/supply_domain.js +62 -0
  346. package/dist/entities/supply_domain.test.js +6 -0
  347. package/dist/entities/support_conversation.js +106 -0
  348. package/dist/entities/support_conversation.test.js +6 -0
  349. package/dist/entities/support_message.js +66 -0
  350. package/dist/entities/support_message.test.js +6 -0
  351. package/dist/entities/system_role.js +49 -0
  352. package/dist/entities/system_role.test.js +6 -0
  353. package/dist/entities/theme.js +125 -0
  354. package/dist/entities/theme.test.js +35 -0
  355. package/dist/entities/theme_css_setting.js +57 -0
  356. package/dist/entities/theme_css_setting.test.js +6 -0
  357. package/dist/entities/user.js +508 -0
  358. package/dist/entities/user.test.js +134 -0
  359. package/dist/entities/user_company.js +62 -0
  360. package/dist/entities/user_company.test.js +6 -0
  361. package/dist/entities/variation.js +108 -0
  362. package/dist/entities/variation.test.js +6 -0
  363. package/dist/entities/variation_field.js +381 -0
  364. package/dist/entities/variation_field.test.js +69 -0
  365. package/dist/entities/variation_fields_option.js +153 -0
  366. package/dist/entities/variation_fields_option.test.js +43 -0
  367. package/dist/entities/variation_option.js +104 -0
  368. package/dist/entities/variation_option.test.js +6 -0
  369. package/dist/entities/variations_group.js +79 -0
  370. package/dist/entities/variations_group.test.js +6 -0
  371. package/dist/entity.js +950 -0
  372. package/dist/index.js +86 -0
  373. package/dist/merchi.js +296 -0
  374. package/dist/merchi.test.js +21 -0
  375. package/dist/pricing/discount.js +11 -0
  376. package/dist/pricing/discount.test.js +35 -0
  377. package/dist/pricing/estimate.js +335 -0
  378. package/dist/pricing/estimate.test.js +403 -0
  379. package/dist/pricing/golden.test.js +61 -0
  380. package/dist/pricing/index.js +7 -0
  381. package/dist/pricing/index.test.js +8 -0
  382. package/dist/pricing/inventory.js +238 -0
  383. package/dist/pricing/inventory.test.js +188 -0
  384. package/dist/pricing/matrix.js +245 -0
  385. package/dist/pricing/matrix.test.js +437 -0
  386. package/dist/pricing/round.js +29 -0
  387. package/dist/pricing/round.test.js +27 -0
  388. package/dist/pricing/types.js +1 -0
  389. package/dist/pricing/visibility.js +227 -0
  390. package/dist/pricing/visibility.test.js +210 -0
  391. package/dist/request.js +169 -0
  392. package/dist/request.test.js +70 -0
  393. package/dist/test_util.js +17 -0
  394. package/dist/toasts.js +14 -0
  395. package/dist/toasts.test.js +28 -0
  396. package/dist/util/float.js +34 -0
  397. package/dist/util/index.js +3 -0
  398. package/dist/util/query_string.js +13 -0
  399. package/dist/util/query_string.test.js +6 -0
  400. package/dist/util/validation.js +3 -0
  401. package/dist/util/validation.test.js +12 -0
  402. package/dist/uuid.js +8 -0
  403. package/dist/uuid.test.js +11 -0
  404. package/jest.config.js +23 -0
  405. package/merchi_sdk_ts-1.13.1.tgz +0 -0
  406. package/package.json +41 -0
  407. package/src/constants/auto_assign_production_on_actions.ts +6 -0
  408. package/src/constants/call_to_actions.test.ts +5 -0
  409. package/src/constants/call_to_actions.ts +14 -0
  410. package/src/constants/discount_types.test.ts +5 -0
  411. package/src/constants/discount_types.ts +5 -0
  412. package/src/constants/domain_types.test.ts +5 -0
  413. package/src/constants/domain_types.ts +12 -0
  414. package/src/constants/errors.ts +32 -0
  415. package/src/constants/event_types.test.ts +5 -0
  416. package/src/constants/event_types.ts +5 -0
  417. package/src/constants/field_types.test.ts +5 -0
  418. package/src/constants/field_types.ts +16 -0
  419. package/src/constants/index.ts +51 -0
  420. package/src/constants/input_types.test.ts +5 -0
  421. package/src/constants/input_types.ts +13 -0
  422. package/src/constants/inventory_status.test.ts +5 -0
  423. package/src/constants/inventory_statuses.ts +6 -0
  424. package/src/constants/invoice_types.test.ts +5 -0
  425. package/src/constants/invoice_types.ts +5 -0
  426. package/src/constants/item_types.test.ts +5 -0
  427. package/src/constants/item_types.ts +5 -0
  428. package/src/constants/job/drafting_status.test.ts +5 -0
  429. package/src/constants/job/drafting_status.ts +7 -0
  430. package/src/constants/job/payment_status.test.ts +5 -0
  431. package/src/constants/job/payment_status.ts +7 -0
  432. package/src/constants/job/production_status.test.ts +5 -0
  433. package/src/constants/job/production_status.ts +12 -0
  434. package/src/constants/job/shipment_status.test.ts +5 -0
  435. package/src/constants/job/shipment_status.ts +11 -0
  436. package/src/constants/job/supply_chain_request_status.test.ts +5 -0
  437. package/src/constants/job/supply_chain_request_status.ts +6 -0
  438. package/src/constants/job_priorities.test.ts +5 -0
  439. package/src/constants/job_priorities.ts +6 -0
  440. package/src/constants/job_status.test.ts +5 -0
  441. package/src/constants/job_status.ts +49 -0
  442. package/src/constants/job_types.test.ts +5 -0
  443. package/src/constants/job_types.ts +16 -0
  444. package/src/constants/notification_sections.test.ts +5 -0
  445. package/src/constants/notification_sections.ts +13 -0
  446. package/src/constants/notification_types.test.ts +5 -0
  447. package/src/constants/notification_types.ts +100 -0
  448. package/src/constants/notification_urgencies.test.ts +5 -0
  449. package/src/constants/notification_urgencies.ts +7 -0
  450. package/src/constants/payment_types.test.ts +5 -0
  451. package/src/constants/payment_types.ts +10 -0
  452. package/src/constants/platform.test.ts +15 -0
  453. package/src/constants/platform.ts +6 -0
  454. package/src/constants/product_types.test.ts +6 -0
  455. package/src/constants/product_types.ts +26 -0
  456. package/src/constants/rights.test.ts +5 -0
  457. package/src/constants/rights.ts +5 -0
  458. package/src/constants/robots_meta_directives.test.ts +5 -0
  459. package/src/constants/robots_meta_directives.ts +11 -0
  460. package/src/constants/roles.test.ts +5 -0
  461. package/src/constants/roles.ts +46 -0
  462. package/src/constants/shipment_companies.test.ts +5 -0
  463. package/src/constants/shipment_companies.ts +19 -0
  464. package/src/constants/shipment_services.test.ts +5 -0
  465. package/src/constants/shipment_services.ts +3 -0
  466. package/src/constants/system_roles.test.ts +5 -0
  467. package/src/constants/system_roles.ts +3 -0
  468. package/src/constants/theme_status.test.ts +5 -0
  469. package/src/constants/theme_status.ts +5 -0
  470. package/src/constants/user_types.test.ts +5 -0
  471. package/src/constants/user_types.ts +17 -0
  472. package/src/constants/white_label_accessibilities.test.ts +5 -0
  473. package/src/constants/white_label_accessibilities.ts +6 -0
  474. package/src/cookie.test.ts +11 -0
  475. package/src/cookie.ts +16 -0
  476. package/src/entities/address.test.ts +7 -0
  477. package/src/entities/address.ts +65 -0
  478. package/src/entities/agent_conversation.test.ts +7 -0
  479. package/src/entities/agent_conversation.ts +39 -0
  480. package/src/entities/agent_skill.test.ts +13 -0
  481. package/src/entities/agent_skill.ts +99 -0
  482. package/src/entities/agent_skill_approval.ts +26 -0
  483. package/src/entities/agent_skill_version.ts +64 -0
  484. package/src/entities/agent_token_analytics.ts +49 -0
  485. package/src/entities/agent_token_usage.test.ts +7 -0
  486. package/src/entities/agent_token_usage.ts +88 -0
  487. package/src/entities/assignment.test.ts +39 -0
  488. package/src/entities/assignment.ts +99 -0
  489. package/src/entities/automatic_payment_relationship.test.ts +8 -0
  490. package/src/entities/automatic_payment_relationship.ts +29 -0
  491. package/src/entities/bank.test.ts +7 -0
  492. package/src/entities/bank.ts +45 -0
  493. package/src/entities/cart.test.ts +57 -0
  494. package/src/entities/cart.ts +102 -0
  495. package/src/entities/cart_item.test.ts +33 -0
  496. package/src/entities/cart_item.ts +85 -0
  497. package/src/entities/cart_shipment_group.ts +21 -0
  498. package/src/entities/cart_shipment_quote.ts +33 -0
  499. package/src/entities/category.test.ts +57 -0
  500. package/src/entities/category.ts +59 -0
  501. package/src/entities/company.test.ts +7 -0
  502. package/src/entities/company.ts +397 -0
  503. package/src/entities/company_invitation.test.ts +7 -0
  504. package/src/entities/company_invitation.ts +33 -0
  505. package/src/entities/country_tax.test.ts +13 -0
  506. package/src/entities/country_tax.ts +46 -0
  507. package/src/entities/discount.test.ts +36 -0
  508. package/src/entities/discount.ts +50 -0
  509. package/src/entities/discount_group.test.ts +7 -0
  510. package/src/entities/discount_group.ts +43 -0
  511. package/src/entities/domain.test.ts +393 -0
  512. package/src/entities/domain.ts +1088 -0
  513. package/src/entities/domain_chat_settings.test.ts +16 -0
  514. package/src/entities/domain_chat_settings.ts +82 -0
  515. package/src/entities/domain_invitation.test.ts +7 -0
  516. package/src/entities/domain_invitation.ts +36 -0
  517. package/src/entities/domain_tag.test.ts +7 -0
  518. package/src/entities/domain_tag.ts +42 -0
  519. package/src/entities/draft.test.ts +29 -0
  520. package/src/entities/draft.ts +79 -0
  521. package/src/entities/draft_comment.test.ts +7 -0
  522. package/src/entities/draft_comment.ts +63 -0
  523. package/src/entities/draft_preview.test.ts +42 -0
  524. package/src/entities/draft_preview.ts +37 -0
  525. package/src/entities/draft_preview_layer.test.ts +7 -0
  526. package/src/entities/draft_preview_layer.ts +21 -0
  527. package/src/entities/draft_template.test.ts +14 -0
  528. package/src/entities/draft_template.ts +81 -0
  529. package/src/entities/email_address.test.ts +7 -0
  530. package/src/entities/email_address.ts +24 -0
  531. package/src/entities/email_counter.test.ts +7 -0
  532. package/src/entities/email_counter.ts +20 -0
  533. package/src/entities/enrolled_domain.test.ts +7 -0
  534. package/src/entities/enrolled_domain.ts +40 -0
  535. package/src/entities/exchange_rate.test.ts +7 -0
  536. package/src/entities/exchange_rate.ts +22 -0
  537. package/src/entities/file.test.ts +43 -0
  538. package/src/entities/file.ts +165 -0
  539. package/src/entities/internal_tag.test.ts +7 -0
  540. package/src/entities/internal_tag.ts +51 -0
  541. package/src/entities/inventory.test.ts +28 -0
  542. package/src/entities/inventory.ts +63 -0
  543. package/src/entities/inventory_group.test.ts +7 -0
  544. package/src/entities/inventory_group.ts +36 -0
  545. package/src/entities/inventory_unit_variation.test.ts +7 -0
  546. package/src/entities/inventory_unit_variation.ts +29 -0
  547. package/src/entities/invoice.test.ts +50 -0
  548. package/src/entities/invoice.ts +192 -0
  549. package/src/entities/item.test.ts +19 -0
  550. package/src/entities/item.ts +59 -0
  551. package/src/entities/job.test.ts +95 -0
  552. package/src/entities/job.ts +401 -0
  553. package/src/entities/job_comment.test.ts +7 -0
  554. package/src/entities/job_comment.ts +53 -0
  555. package/src/entities/job_note.ts +40 -0
  556. package/src/entities/job_operation_log.test.ts +7 -0
  557. package/src/entities/job_operation_log.ts +41 -0
  558. package/src/entities/matching_inventory.ts +29 -0
  559. package/src/entities/notification.test.ts +58 -0
  560. package/src/entities/notification.ts +106 -0
  561. package/src/entities/payment.test.ts +27 -0
  562. package/src/entities/payment.ts +77 -0
  563. package/src/entities/payment_device.test.ts +7 -0
  564. package/src/entities/payment_device.ts +30 -0
  565. package/src/entities/phone_number.test.ts +7 -0
  566. package/src/entities/phone_number.ts +36 -0
  567. package/src/entities/product.test.ts +776 -0
  568. package/src/entities/product.ts +486 -0
  569. package/src/entities/product_form.ts +51 -0
  570. package/src/entities/product_form_version.ts +51 -0
  571. package/src/entities/product_review.ts +221 -0
  572. package/src/entities/production_comment.test.ts +7 -0
  573. package/src/entities/production_comment.ts +53 -0
  574. package/src/entities/quote.test.ts +98 -0
  575. package/src/entities/quote.ts +140 -0
  576. package/src/entities/quote_item.test.ts +50 -0
  577. package/src/entities/quote_item.ts +71 -0
  578. package/src/entities/reminder.test.ts +16 -0
  579. package/src/entities/reminder.ts +74 -0
  580. package/src/entities/session.test.ts +7 -0
  581. package/src/entities/session.ts +28 -0
  582. package/src/entities/shipment.test.ts +24 -0
  583. package/src/entities/shipment.ts +196 -0
  584. package/src/entities/shipment_item.test.ts +7 -0
  585. package/src/entities/shipment_item.ts +15 -0
  586. package/src/entities/shipment_item_fulfillment.test.ts +8 -0
  587. package/src/entities/shipment_item_fulfillment.ts +15 -0
  588. package/src/entities/shipment_log.ts +37 -0
  589. package/src/entities/shipment_method.test.ts +7 -0
  590. package/src/entities/shipment_method.ts +61 -0
  591. package/src/entities/shipment_method_variation.test.ts +7 -0
  592. package/src/entities/shipment_method_variation.ts +39 -0
  593. package/src/entities/short_url.test.ts +7 -0
  594. package/src/entities/short_url.ts +36 -0
  595. package/src/entities/subscription_plan.test.ts +7 -0
  596. package/src/entities/subscription_plan.ts +73 -0
  597. package/src/entities/supply_domain.test.ts +7 -0
  598. package/src/entities/supply_domain.ts +27 -0
  599. package/src/entities/support_conversation.test.ts +7 -0
  600. package/src/entities/support_conversation.ts +65 -0
  601. package/src/entities/support_message.test.ts +7 -0
  602. package/src/entities/support_message.ts +30 -0
  603. package/src/entities/system_role.test.ts +7 -0
  604. package/src/entities/system_role.ts +17 -0
  605. package/src/entities/theme.test.ts +38 -0
  606. package/src/entities/theme.ts +82 -0
  607. package/src/entities/theme_css_setting.test.ts +7 -0
  608. package/src/entities/theme_css_setting.ts +23 -0
  609. package/src/entities/user.test.ts +154 -0
  610. package/src/entities/user.ts +428 -0
  611. package/src/entities/user_company.test.ts +7 -0
  612. package/src/entities/user_company.ts +27 -0
  613. package/src/entities/variation.test.ts +7 -0
  614. package/src/entities/variation.ts +64 -0
  615. package/src/entities/variation_field.test.ts +76 -0
  616. package/src/entities/variation_field.ts +268 -0
  617. package/src/entities/variation_fields_option.test.ts +46 -0
  618. package/src/entities/variation_fields_option.ts +107 -0
  619. package/src/entities/variation_option.test.ts +7 -0
  620. package/src/entities/variation_option.ts +58 -0
  621. package/src/entities/variations_group.test.ts +7 -0
  622. package/src/entities/variations_group.ts +41 -0
  623. package/src/entity.ts +1098 -0
  624. package/src/index.ts +189 -0
  625. package/src/merchi.test.ts +25 -0
  626. package/src/merchi.ts +445 -0
  627. package/src/pricing/__fixtures__/conditional_field_hidden.json +112 -0
  628. package/src/pricing/__fixtures__/conditional_field_visible.json +113 -0
  629. package/src/pricing/__fixtures__/group_product.json +130 -0
  630. package/src/pricing/__fixtures__/non_selectable_number_field.json +108 -0
  631. package/src/pricing/__fixtures__/quantity_break_discount.json +104 -0
  632. package/src/pricing/__fixtures__/simple_selectable_field.json +110 -0
  633. package/src/pricing/discount.test.ts +47 -0
  634. package/src/pricing/discount.ts +17 -0
  635. package/src/pricing/estimate.test.ts +486 -0
  636. package/src/pricing/estimate.ts +310 -0
  637. package/src/pricing/golden.test.ts +51 -0
  638. package/src/pricing/index.test.ts +9 -0
  639. package/src/pricing/index.ts +7 -0
  640. package/src/pricing/inventory.test.ts +197 -0
  641. package/src/pricing/inventory.ts +89 -0
  642. package/src/pricing/matrix.test.ts +553 -0
  643. package/src/pricing/matrix.ts +217 -0
  644. package/src/pricing/round.test.ts +31 -0
  645. package/src/pricing/round.ts +27 -0
  646. package/src/pricing/types.ts +140 -0
  647. package/src/pricing/visibility.test.ts +223 -0
  648. package/src/pricing/visibility.ts +111 -0
  649. package/src/request.test.ts +79 -0
  650. package/src/request.ts +196 -0
  651. package/src/test_util.ts +18 -0
  652. package/src/toasts.test.ts +38 -0
  653. package/src/toasts.ts +34 -0
  654. package/src/util/float.ts +12 -0
  655. package/src/util/index.ts +7 -0
  656. package/src/util/query_string.test.ts +7 -0
  657. package/src/util/query_string.ts +13 -0
  658. package/src/util/validation.test.ts +16 -0
  659. package/src/util/validation.ts +2 -0
  660. package/src/uuid.test.ts +15 -0
  661. package/src/uuid.ts +8 -0
  662. package/tsconfig.json +29 -0
package/src/entity.ts ADDED
@@ -0,0 +1,1098 @@
1
+ import 'reflect-metadata';
2
+ // eslint-disable-next-line no-unused-vars
3
+ import { RequestOptions } from './request.js';
4
+ // eslint-disable-next-line no-unused-vars
5
+ import { Merchi } from './merchi.js';
6
+ // eslint-disable-next-line no-unused-vars
7
+ import { NotificationType } from './constants/notification_types.js';
8
+ // eslint-disable-next-line no-unused-vars
9
+ import { NotificationSection } from './constants/notification_sections.js';
10
+ // eslint-disable-next-line no-unused-vars
11
+ import { Role } from './constants/roles.js';
12
+ // eslint-disable-next-line no-unused-vars
13
+ import { DomainType } from './constants/domain_types.js';
14
+ // eslint-disable-next-line no-unused-vars
15
+ import { ProductType } from './constants/product_types.js';
16
+ import { generateUUID } from './uuid.js';
17
+
18
+ function toUnixTimestamp(date: Date) {
19
+ return parseInt(String(date.getTime() / 1000)).toFixed(0);
20
+ }
21
+
22
+ interface Counter {
23
+ value: number;
24
+ }
25
+
26
+ interface PropertyOptions {
27
+ embeddedByDefault?: boolean;
28
+ jsonName?: string;
29
+ type?: any;
30
+ arrayType?: string;
31
+ }
32
+
33
+ export interface EmbedDescriptor {
34
+ [property: string]: {} | EmbedDescriptor;
35
+ }
36
+
37
+ export interface NestedIncludeArchivedDescriptor {
38
+ [property: string]: {} | boolean | NestedIncludeArchivedDescriptor;
39
+ }
40
+
41
+ export type IncludeArchivedDescriptor = boolean | NestedIncludeArchivedDescriptor;
42
+
43
+ interface FromJsonOptions {
44
+ arrayValueStrict?: boolean;
45
+ makeDirty?: boolean;
46
+ }
47
+
48
+ interface CreateOptions {
49
+ withRights?: boolean;
50
+ embed?: EmbedDescriptor;
51
+ }
52
+
53
+ interface SaveOptions {
54
+ withRights?: boolean;
55
+ embed?: EmbedDescriptor;
56
+ }
57
+
58
+ interface DeleteOptions {
59
+ withRights?: boolean;
60
+ }
61
+
62
+ interface GetOptions {
63
+ embed?: EmbedDescriptor;
64
+ includeArchived?: IncludeArchivedDescriptor;
65
+ withRights?: boolean;
66
+ }
67
+
68
+ export enum SerialiseMethod {
69
+ // eslint-disable-next-line no-unused-vars
70
+ TO_DICT = 'to_dict',
71
+ // eslint-disable-next-line no-unused-vars
72
+ ONLY_ID = 'only_id',
73
+ }
74
+
75
+ interface ListOptions {
76
+ as?: string;
77
+ asRole?: Role;
78
+ businessDomainsOnly?: boolean;
79
+ categoryId?: number;
80
+ clientId?: number;
81
+ clientCompanyId?: number;
82
+ categories?: string[];
83
+ clientOnly?: boolean;
84
+ companyCustomerId?: number;
85
+ companyId?: number;
86
+ companySupplierId?: number;
87
+ conversationId?: number;
88
+ dateFrom?: Date;
89
+ dateTo?: Date;
90
+ domainRoles?: Role[];
91
+ domainTypes?: DomainType[];
92
+ doesNotHaveAdminDomain?: boolean;
93
+ embed?: EmbedDescriptor;
94
+ entityTypes?: number[];
95
+ exclude?: number[];
96
+ excludeDomains?: number[];
97
+ excludeJobs?: number[];
98
+ googleMerchantCenterExported?: boolean;
99
+ groupBuyForJobId?: number;
100
+ groupBuyOnly?: boolean;
101
+ inbound?: boolean;
102
+ includeOnly?: number[];
103
+ inDomain?: number;
104
+ inDomainName?: string;
105
+ inDomainRoles?: number[];
106
+ isJobManager?: boolean;
107
+ isMaster?: boolean;
108
+ isOrder?: boolean;
109
+ isPrivate?: boolean;
110
+ jobNotifiable?: number;
111
+ descendants?: number;
112
+ limit?: number;
113
+ managedDomainsOnly?: boolean;
114
+ managedOnly?: boolean;
115
+ managerId?: number;
116
+ masterProduct?: number;
117
+ memberOnly?: boolean;
118
+ merchiOnly?: boolean;
119
+ notificationSender?: number;
120
+ notificationJob?: number;
121
+ notificationRecipient?: number;
122
+ notificationType?: NotificationType;
123
+ offset?: number;
124
+ orClientId?: number;
125
+ orClientCompanyId?: number | number[];
126
+ order?: string;
127
+ originalOf?: number;
128
+ platformCategoryId?: number;
129
+ priceRange?: number[];
130
+ productTypes?: ProductType[];
131
+ publicOnly?: boolean;
132
+ isDraft?: boolean;
133
+ q?: string;
134
+ receiverId?: number;
135
+ userAsReceiver?: number;
136
+ relatedAssignment?: number;
137
+ relatedDraft?: number;
138
+ relatedJob?: number;
139
+ relatedJobs?: number[];
140
+ relatedShipment?: number;
141
+ relatedProduct?: number;
142
+ relatedUser?: number;
143
+ savedByUser?: number;
144
+ section?: NotificationSection;
145
+ senderCompanyId?: number;
146
+ senderRole?: Role;
147
+ serialiseMethod?: SerialiseMethod;
148
+ shopifyOnly?: boolean;
149
+ sort?: string;
150
+ state?: string;
151
+ supplierResellOnly?: boolean;
152
+ tab?: string;
153
+ tags?: number[];
154
+ trackingNumber?: string;
155
+ tagsInternal?: number[];
156
+ excludeTags?: number[];
157
+ excludeTagsInternal?: number[];
158
+ tagNames?: string[];
159
+ teamOnly?: boolean;
160
+ turnaroundTimeDays?: number;
161
+ withRights?: boolean;
162
+ // Opt out of the unbounded ``SELECT count(*)`` the API runs to
163
+ // populate ``available``. When ``skipCount`` is true the response's
164
+ // ``available`` is ``null``. Use this whenever the caller only
165
+ // renders the visible page of rows (search modals, autocomplete,
166
+ // navbar dropdowns, infinite scroll, ...) and never reads
167
+ // ``metadata.available``. Paginated tables that show "Showing X
168
+ // of Y" or compute a page count must NOT set this.
169
+ skipCount?: boolean;
170
+ }
171
+
172
+ export interface ListMetadata {
173
+ canCreate?: boolean;
174
+ // ``null`` when the caller passed ``skipCount: true``: the API
175
+ // intentionally skipped the ``COUNT(*)`` query. JSON consumers
176
+ // can detect the absence rather than mistaking it for zero.
177
+ available: number | null;
178
+ count: number;
179
+ limit: number;
180
+ offset: number;
181
+ }
182
+
183
+ export interface ListResponse<T> {
184
+ items: T[];
185
+ metadata: ListMetadata;
186
+ }
187
+
188
+ interface SerialiseOptions {
189
+ existing?: FormData;
190
+ excludeOld?: boolean;
191
+ _prefix?: string;
192
+ }
193
+
194
+ interface PropertyInfo {
195
+ property: string; // this is the json name, e.g. 'id'
196
+ attribute: string; // this is the js attribute name, e.g. '_id'
197
+ type: Function; // e.g. Number
198
+ arrayType?: Entity; // if type is an array, arrayType is the element type
199
+ dirty: boolean; // true if out of date with server
200
+ currentValue?: any; // type is actually `this.type | undefined`
201
+ updatingOrder: boolean;
202
+ embeddedByDefault: boolean;
203
+ }
204
+
205
+ export class Entity {
206
+ private static jsonNameKey = Symbol('jsonName');
207
+ private static arrayTypeKey = Symbol('arrayType');
208
+ private static extraOptionsKey = Symbol('extraOptions');
209
+ private static propertiesSetKey = Symbol('propertiesSet');
210
+
211
+ protected static resourceName: string;
212
+ protected static singularName: string;
213
+ protected static pluralName: string;
214
+ protected static primaryKey = 'id';
215
+
216
+ // these will be set by the parent Merchi object
217
+ public merchi!: Merchi;
218
+ public static merchi: Merchi;
219
+
220
+ public isDirty = false;
221
+ // maps json names like 'id' to information about that property
222
+ public propertiesMap: Map<string, PropertyInfo>;
223
+ public readonly backObjects: Set<Entity> = new Set();
224
+
225
+ public key = generateUUID();
226
+
227
+ protected isSingleEntityProperty(info: PropertyInfo) {
228
+ // the choice of 'Product' below is unimportant -- all Entities should
229
+ // have the same prototype but i don't know how to get instanceof
230
+ // working, so i just compare prototypes directly.
231
+ return (
232
+ info.type.prototype === this.merchi.Product.prototype ||
233
+ info.type.prototype instanceof Entity
234
+ );
235
+ }
236
+
237
+ protected static property(options?: PropertyOptions) {
238
+ return function (target: Entity, propertyKey: string) {
239
+ if (!options) {
240
+ options = {};
241
+ }
242
+ const arrayType = options.arrayType;
243
+ const jsonName = options.jsonName || propertyKey;
244
+ let properties = Reflect.getMetadata(Entity.propertiesSetKey,
245
+ target.constructor);
246
+ properties = properties || new Set();
247
+ properties.add(propertyKey);
248
+ Reflect.defineMetadata(Entity.propertiesSetKey, properties,
249
+ target.constructor);
250
+ Reflect.defineMetadata(Entity.jsonNameKey, jsonName, target,
251
+ propertyKey);
252
+ Reflect.defineMetadata(Entity.arrayTypeKey, arrayType, target,
253
+ propertyKey);
254
+ Reflect.defineMetadata(Entity.extraOptionsKey, options, target,
255
+ propertyKey);
256
+ };
257
+ }
258
+
259
+ protected makePropertiesMap() {
260
+ const map = new Map();
261
+ const self = this;
262
+ const properties = Reflect.getMetadata(Entity.propertiesSetKey,
263
+ this.constructor);
264
+ properties.forEach(function(attributeName: any) {
265
+ const jsonName = Reflect.getMetadata(Entity.jsonNameKey, self,
266
+ attributeName);
267
+ const propertyType = Reflect.getMetadata('design:type', self,
268
+ attributeName);
269
+ // the array type is needed because 'design:type' breaks down
270
+ // with recursive classes, and also, does not contain the type
271
+ // of an arrays elements, which we need
272
+ const arrayType = Reflect.getMetadata(Entity.arrayTypeKey, self,
273
+ attributeName);
274
+ const realArrayType = self.getEntityClass(arrayType);
275
+ const options: PropertyOptions =
276
+ Reflect.getMetadata(Entity.extraOptionsKey, self, attributeName);
277
+ if (arrayType !== undefined) {
278
+ /* istanbul ignore next */
279
+ if (propertyType !== Object && propertyType !== Array) {
280
+ /* istanbul ignore next */
281
+ throw new Error('array type can only be given for arrays');
282
+ }
283
+ }
284
+ let type;
285
+ if (options.type === undefined) {
286
+ type = propertyType;
287
+ } else {
288
+ type = options.type;
289
+ if (typeof type === 'string') {
290
+ type = self.getEntityClass(type);
291
+ }
292
+ if (type.prototype instanceof Entity) {
293
+ type = self.merchi.setupClass(type);
294
+ }
295
+ }
296
+ const normallyEmbeddedByDefault = !(realArrayType ||
297
+ type.prototype instanceof Entity);
298
+ const embeddedByDefault = options.embeddedByDefault !== undefined ?
299
+ options.embeddedByDefault : normallyEmbeddedByDefault;
300
+ /* istanbul ignore next */
301
+ if (type === Object) {
302
+ /* istanbul ignore next */
303
+ const resource = (self.constructor as typeof Entity).resourceName;
304
+ const err = 'Bad attribute type ' +
305
+ `${resource}.${attributeName}: ${type}`;
306
+ throw new Error(err);
307
+ }
308
+ const propertyInfo: PropertyInfo = {property: jsonName,
309
+ attribute: attributeName,
310
+ type: type,
311
+ arrayType: realArrayType,
312
+ embeddedByDefault: embeddedByDefault,
313
+ dirty: true,
314
+ updatingOrder: false};
315
+ map.set(jsonName, propertyInfo);
316
+ });
317
+ return map;
318
+ }
319
+
320
+ public constructor(merchi?: Merchi) {
321
+ /* istanbul ignore next */
322
+ if (merchi !== undefined) {
323
+ this.merchi = merchi;
324
+ }
325
+ this.propertiesMap = this.makePropertiesMap();
326
+ this.setupProperties();
327
+ }
328
+
329
+ public getPrimaryKeyValue = () => {
330
+ const name: string = (this.constructor as typeof Entity).primaryKey;
331
+ const info = this.propertiesMap.get(name);
332
+ /* istanbul ignore next */
333
+ if (info !== undefined) {
334
+ return info.currentValue;
335
+ }
336
+ /* istanbul ignore next */
337
+ return undefined;
338
+ };
339
+
340
+ private setupProperties = () => {
341
+ const properties: any = {};
342
+ const makeSetSingle = (info: PropertyInfo) => {
343
+ const get = () => {
344
+ return info.currentValue;
345
+ };
346
+ const set = (newValue?: Entity) => {
347
+ info.currentValue = newValue;
348
+ this.addBackObject(newValue);
349
+ this.markDirty(info.property, newValue);
350
+ };
351
+ return { get: get,
352
+ set: set};
353
+ };
354
+ const makeSetScalar = (info: PropertyInfo) => {
355
+ const get = () => {
356
+ return info.currentValue;
357
+ };
358
+ const set = (newValue: any) => {
359
+ info.currentValue = newValue;
360
+ this.markDirty(info.property, newValue);
361
+ };
362
+ return { get: get,
363
+ set: set};
364
+ };
365
+ const makeSetArray = (info: PropertyInfo) => {
366
+ const get = () => {
367
+ return info.currentValue;
368
+ };
369
+ const set = (newValue?: Entity[]) => {
370
+ info.currentValue = newValue;
371
+ this.addBackObjectList(newValue);
372
+ this.markDirty(info.property, newValue);
373
+ };
374
+ return { get: get,
375
+ set: set};
376
+ };
377
+ for (const info of this.propertiesMap.values()) {
378
+ if (info.type.prototype instanceof Entity) {
379
+ properties[info.attribute] = makeSetSingle(info);
380
+ } else if (info.arrayType) {
381
+ properties[info.attribute] = makeSetArray(info);
382
+ } else {
383
+ properties[info.attribute] = makeSetScalar(info);
384
+ }
385
+ }
386
+ Object.defineProperties(this, properties);
387
+ };
388
+
389
+ public static get<T extends typeof Entity>(this: T, key: number | string,
390
+ options?: GetOptions):
391
+ Promise<InstanceType<T>>{
392
+ const resource = `/${this.resourceName}/${String(key)}/`;
393
+ const fetchOptions: RequestOptions = {};
394
+ fetchOptions.query = [];
395
+ if (options && options.embed) {
396
+ fetchOptions.query.push(['embed', JSON.stringify(options.embed)]);
397
+ }
398
+ if (options && options.includeArchived) {
399
+ fetchOptions.query.push(['include_archived',
400
+ JSON.stringify(options.includeArchived)]);
401
+ }
402
+ if (!(options && options.withRights)) {
403
+ fetchOptions.query.push(['skip_rights', 'y']);
404
+ }
405
+ return this.merchi.authenticatedFetch(resource, fetchOptions).
406
+ then((data: any) => {
407
+ const result: InstanceType<T> = (new this()) as InstanceType<T>;
408
+ result.fromJson(data[this.singularName]);
409
+ return result;
410
+ });
411
+ }
412
+
413
+ public static list<T extends typeof Entity>(this: T, options?: ListOptions):
414
+ Promise<ListResponse<InstanceType<T>>> {
415
+ const resource = `/${this.resourceName}/`;
416
+ const fetchOptions: RequestOptions = {};
417
+ fetchOptions.query = [];
418
+ if (options) {
419
+ if (options.embed) {
420
+ fetchOptions.query.push(['embed', JSON.stringify(options.embed)]);
421
+ }
422
+ if (options.offset !== undefined) {
423
+ fetchOptions.query.push(['offset', options.offset.toString()]);
424
+ }
425
+ if (options.descendants !== undefined) {
426
+ fetchOptions.query.push(['descendants', options.descendants.toString()]);
427
+ }
428
+ if (options.limit !== undefined) {
429
+ fetchOptions.query.push(['limit', options.limit.toString()]);
430
+ }
431
+ if (options.skipCount !== undefined) {
432
+ // Snake-case is the canonical form on the server; the API
433
+ // also accepts ``skipCount`` via its camelCase alias, but
434
+ // matching the wire shape of the other ``*_only`` /
435
+ // ``*_filter`` flags keeps logs and traffic captures
436
+ // consistent.
437
+ fetchOptions.query.push(
438
+ ['skip_count', options.skipCount.toString()]
439
+ );
440
+ }
441
+ if (options.q !== undefined) {
442
+ fetchOptions.query.push(['q', options.q]);
443
+ }
444
+ if (options.sort !== undefined) {
445
+ fetchOptions.query.push(['sort', options.sort]);
446
+ }
447
+ if (options.order !== undefined) {
448
+ fetchOptions.query.push(['order', options.order]);
449
+ }
450
+ if (options.serialiseMethod !== undefined) {
451
+ fetchOptions.query.push(['serialise_method', options.serialiseMethod]);
452
+ }
453
+ if (options.tab !== undefined) {
454
+ fetchOptions.query.push(['tab', options.tab]);
455
+ }
456
+ if (options.trackingNumber !== undefined) {
457
+ fetchOptions.query.push(['tracking_number', options.trackingNumber]);
458
+ }
459
+ if (options.as !== undefined) {
460
+ fetchOptions.query.push(['as', options.as]);
461
+ }
462
+ if (options.state !== undefined) {
463
+ fetchOptions.query.push(['state', options.state]);
464
+ }
465
+ if (options.categoryId !== undefined) {
466
+ fetchOptions.query.push(['category_id', options.categoryId.toString()]);
467
+ }
468
+ if (options.platformCategoryId !== undefined) {
469
+ fetchOptions.query.push(['platform_category_id', options.platformCategoryId.toString()]);
470
+ }
471
+ if (options.inDomain !== undefined) {
472
+ fetchOptions.query.push(['in_domain', options.inDomain.toString()]);
473
+ }
474
+ if (options.inDomainName !== undefined) {
475
+ fetchOptions.query.push(['in_domain_name', options.inDomainName.toString()]);
476
+ }
477
+ if (options.inDomainRoles !== undefined) {
478
+ fetchOptions.query.push(['in_domain_roles',
479
+ JSON.stringify(options.inDomainRoles)]);
480
+ }
481
+ if (options.isPrivate !== undefined) {
482
+ fetchOptions.query.push(['is_private', options.isPrivate.toString()]);
483
+ }
484
+ if (options.asRole !== undefined) {
485
+ fetchOptions.query.push(['as_role', options.asRole.toString()]);
486
+ }
487
+ if (options.isJobManager !== undefined) {
488
+ fetchOptions.query.push(
489
+ ['is_job_manager', options.isJobManager.toString()]);
490
+ }
491
+ if (options.groupBuyOnly !== undefined) {
492
+ fetchOptions.query.push(
493
+ ['group_buy_only', options.groupBuyOnly.toString()]);
494
+ }
495
+ if (options.publicOnly !== undefined) {
496
+ fetchOptions.query.push(['public_only', options.publicOnly.toString()]);
497
+ }
498
+ if (options.isDraft !== undefined) {
499
+ fetchOptions.query.push(['is_draft', options.isDraft.toString()]);
500
+ }
501
+ if (options.managedOnly !== undefined) {
502
+ fetchOptions.query.push(['managed_only',
503
+ options.managedOnly.toString()]);
504
+ }
505
+ if (options.doesNotHaveAdminDomain !== undefined) {
506
+ fetchOptions.query.push(['does_not_have_admin_domain',
507
+ options.doesNotHaveAdminDomain.toString()]);
508
+ }
509
+ if (options.categories !== undefined) {
510
+ fetchOptions.query.push(['categories', options.categories.toString()]);
511
+ }
512
+ if (options.clientOnly !== undefined) {
513
+ fetchOptions.query.push(['client_only',
514
+ options.clientOnly.toString()]);
515
+ }
516
+ if (options.teamOnly !== undefined) {
517
+ fetchOptions.query.push(['team_only',
518
+ options.teamOnly.toString()]);
519
+ }
520
+ if (options.memberOnly !== undefined) {
521
+ fetchOptions.query.push(['member_only', options.memberOnly.toString()]);
522
+ }
523
+ if (options.merchiOnly !== undefined) {
524
+ fetchOptions.query.push(['merchi_only', options.merchiOnly.toString()]);
525
+ }
526
+ if (options.supplierResellOnly !== undefined) {
527
+ fetchOptions.query.push(
528
+ ['supplier_resell_only', options.supplierResellOnly.toString()]
529
+ );
530
+ }
531
+ if (options.shopifyOnly !== undefined) {
532
+ fetchOptions.query.push(['shopify_only', options.shopifyOnly.toString()]);
533
+ }
534
+ if (options.inbound !== undefined) {
535
+ fetchOptions.query.push(['inbound', options.inbound.toString()]);
536
+ }
537
+ if (options.isMaster !== undefined) {
538
+ fetchOptions.query.push(['is_master', options.isMaster.toString()]);
539
+ }
540
+ if (options.domainRoles !== undefined) {
541
+ fetchOptions.query.push(['domain_roles',
542
+ options.domainRoles.join(',')]);
543
+ }
544
+ if (options.domainTypes !== undefined) {
545
+ fetchOptions.query.push(['domain_types',
546
+ options.domainTypes.join(',')]);
547
+ }
548
+ if (options.entityTypes !== undefined) {
549
+ fetchOptions.query.push(['entity_types',
550
+ options.entityTypes.join(',')]);
551
+ }
552
+ if (options.priceRange !== undefined) {
553
+ fetchOptions.query.push(['price_range',
554
+ options.priceRange.join(',')]);
555
+ }
556
+ if (options.productTypes !== undefined) {
557
+ fetchOptions.query.push(['product_types',
558
+ options.productTypes.join(',')]);
559
+ }
560
+ if (options.managedDomainsOnly !== undefined) {
561
+ fetchOptions.query.push(['managed_domains_only',
562
+ options.managedDomainsOnly.toString()]);
563
+ }
564
+ if (options.businessDomainsOnly !== undefined) {
565
+ fetchOptions.query.push(['business_domains_only',
566
+ options.businessDomainsOnly.toString()]);
567
+ }
568
+ if (options.dateFrom !== undefined) {
569
+ fetchOptions.query.push(['date_from',
570
+ toUnixTimestamp(options.dateFrom)]);
571
+ }
572
+ if (options.dateTo !== undefined) {
573
+ fetchOptions.query.push(['date_to', toUnixTimestamp(options.dateTo)]);
574
+ }
575
+ if (options.originalOf !== undefined) {
576
+ fetchOptions.query.push(['original_of',
577
+ options.originalOf.toString()]);
578
+ }
579
+ if (options.relatedAssignment !== undefined) {
580
+ fetchOptions.query.push(['related_assignment',
581
+ options.relatedAssignment.toString()]);
582
+ }
583
+ if (options.relatedDraft !== undefined) {
584
+ fetchOptions.query.push(['related_draft',
585
+ options.relatedDraft.toString()]);
586
+ }
587
+ if (options.relatedJob !== undefined) {
588
+ fetchOptions.query.push(['related_job', options.relatedJob.toString()]);
589
+ }
590
+ if (options.relatedJobs !== undefined) {
591
+ fetchOptions.query.push(['related_jobs', options.relatedJobs.toString()]);
592
+ }
593
+ if (options.relatedShipment !== undefined) {
594
+ fetchOptions.query.push(['related_shipment',
595
+ options.relatedShipment.toString()]);
596
+ }
597
+ if (options.relatedProduct !== undefined) {
598
+ fetchOptions.query.push(['related_product',
599
+ options.relatedProduct.toString()]);
600
+ }
601
+ if (options.jobNotifiable !== undefined) {
602
+ fetchOptions.query.push(['job_notifiable',
603
+ options.jobNotifiable.toString()]);
604
+ }
605
+ if (options.notificationType !== undefined) {
606
+ fetchOptions.query.push(['notification_type',
607
+ options.notificationType.toString()]);
608
+ }
609
+ if (options.notificationRecipient !== undefined) {
610
+ fetchOptions.query.push(['notification_recipient',
611
+ options.notificationRecipient.toString()]);
612
+ }
613
+ if (options.notificationSender !== undefined) {
614
+ fetchOptions.query.push(['notification_sender',
615
+ options.notificationSender.toString()]);
616
+ }
617
+ if (options.notificationJob !== undefined) {
618
+ fetchOptions.query.push(['notification_job',
619
+ options.notificationJob.toString()]);
620
+ }
621
+ if (options.relatedUser !== undefined) {
622
+ fetchOptions.query.push(['related_user',
623
+ options.relatedUser.toString()]);
624
+ }
625
+ if (options.clientId !== undefined) {
626
+ fetchOptions.query.push(['client_id', options.clientId.toString()]);
627
+ }
628
+ if (options.managerId !== undefined) {
629
+ fetchOptions.query.push(['manager_id', options.managerId.toString()]);
630
+ }
631
+ if (options.masterProduct !== undefined) {
632
+ fetchOptions.query.push(['master_product', options.masterProduct.toString()]);
633
+ }
634
+ if (options.clientCompanyId !== undefined) {
635
+ fetchOptions.query.push(['client_company_id',
636
+ options.clientCompanyId.toString()]);
637
+ }
638
+ if (options.savedByUser !== undefined) {
639
+ fetchOptions.query.push(['saved_by_user',
640
+ options.savedByUser.toString()]);
641
+ }
642
+ if (options.senderCompanyId !== undefined) {
643
+ fetchOptions.query.push(['sender_company_id',
644
+ options.senderCompanyId.toString()]);
645
+ }
646
+ if (options.receiverId !== undefined) {
647
+ fetchOptions.query.push(['receiver_id', options.receiverId.toString()]);
648
+ }
649
+ if (options.userAsReceiver !== undefined) {
650
+ fetchOptions.query.push(
651
+ ['user_as_receiver', options.userAsReceiver.toString()]
652
+ );
653
+ }
654
+ if (options.companyCustomerId !== undefined) {
655
+ fetchOptions.query.push(['company_customer_id', options.companyCustomerId.toString()]);
656
+ }
657
+ if (options.companyId !== undefined) {
658
+ fetchOptions.query.push(['company_id', options.companyId.toString()]);
659
+ }
660
+ if (options.companySupplierId !== undefined) {
661
+ fetchOptions.query.push(['company_supplier_id', options.companySupplierId.toString()]);
662
+ }
663
+ if (options.conversationId !== undefined) {
664
+ fetchOptions.query.push(['conversation_id',
665
+ options.conversationId.toString()]);
666
+ }
667
+ if (options.groupBuyForJobId !== undefined) {
668
+ fetchOptions.query.push(['group_buy_for_job_id',
669
+ options.groupBuyForJobId.toString()]);
670
+ }
671
+ if (options.googleMerchantCenterExported !== undefined) {
672
+ fetchOptions.query.push(['google_merchant_center_exported',
673
+ options.googleMerchantCenterExported.toString()]);
674
+ }
675
+ if (options.section !== undefined) {
676
+ fetchOptions.query.push(['section', options.section.toString()]);
677
+ }
678
+ if (options.senderRole !== undefined) {
679
+ fetchOptions.query.push(['sender_role', options.senderRole.toString()]);
680
+ }
681
+ if (options.isOrder) {
682
+ fetchOptions.query.push(['is_order', 'true']);
683
+ }
684
+ if (options.tags !== undefined) {
685
+ fetchOptions.query.push(['tags', options.tags.join(',')]);
686
+ }
687
+ if (options.excludeTags !== undefined) {
688
+ fetchOptions.query.push(['exclude_tags', options.excludeTags.join(',')]);
689
+ }
690
+ if (options.excludeTagsInternal !== undefined) {
691
+ fetchOptions.query.push(['exclude_tags_internal', options.excludeTagsInternal.join(',')]);
692
+ }
693
+ if (options.tagsInternal !== undefined) {
694
+ fetchOptions.query.push(['tags_internal', options.tagsInternal.join(',')]);
695
+ }
696
+ if (options.tagNames !== undefined) {
697
+ fetchOptions.query.push(['tag_names', options.tagNames.join(',')]);
698
+ }
699
+ if (options.turnaroundTimeDays !== undefined) {
700
+ fetchOptions.query.push(
701
+ ['turnaround_time_days', options.turnaroundTimeDays.toString()]
702
+ );
703
+ }
704
+ if (options.exclude !== undefined) {
705
+ fetchOptions.query.push(['exclude', options.exclude.join(',')]);
706
+ }
707
+ if (options.excludeDomains !== undefined) {
708
+ fetchOptions.query.push(
709
+ ['exclude_domains', options.excludeDomains.join(',')]);
710
+ }
711
+ if (options.excludeJobs !== undefined) {
712
+ fetchOptions.query.push(
713
+ ['exclude_jobs', options.excludeJobs.join(',')]);
714
+ }
715
+ if (options.includeOnly !== undefined) {
716
+ fetchOptions.query.push(['include_only', options.includeOnly.join(',')]);
717
+ }
718
+ if (options.orClientId !== undefined) {
719
+ fetchOptions.query.push(['or_client_id', options.orClientId.toString()]);
720
+ }
721
+ if (options.orClientCompanyId !== undefined) {
722
+ const companyIds = Array.isArray(options.orClientCompanyId)
723
+ ? options.orClientCompanyId.join(',')
724
+ : options.orClientCompanyId.toString();
725
+ fetchOptions.query.push(['or_client_company_id', companyIds]);
726
+ }
727
+ }
728
+ if (!(options && options.withRights)) {
729
+ fetchOptions.query.push(['skip_rights', 'y']);
730
+ }
731
+ return this.merchi.authenticatedFetch(resource, fetchOptions).then((data: any) => {
732
+ const metadata = {canCreate: data.canCreate,
733
+ available: data.available,
734
+ count: data.count,
735
+ limit: data.limit,
736
+ offset: data.offset};
737
+ const pluralName = this.pluralName;
738
+ const singularName = this.singularName;
739
+ const items: any[] = data[pluralName];
740
+ const entities = [];
741
+ for (const item of items) {
742
+ const entity: InstanceType<T> = (new this()) as InstanceType<T>;
743
+ entity.fromJson(item[singularName]);
744
+ entities.push(entity);
745
+ }
746
+ return {items: entities,
747
+ metadata: metadata};
748
+ });
749
+ }
750
+
751
+ public save = (options?: SaveOptions) => {
752
+ const primaryKey: number | string = this.getPrimaryKeyValue();
753
+ const constructor = this.constructor as typeof Entity;
754
+ const resourceName: string = constructor.resourceName;
755
+ const singularName: string = constructor.singularName;
756
+ const resource = `/${resourceName}/${String(primaryKey)}/`;
757
+ const data = this.toFormData();
758
+ const fetchOptions: RequestOptions = {method: 'PATCH',
759
+ body: data};
760
+ fetchOptions.query = [];
761
+ if (options && options.embed) {
762
+ fetchOptions.query.push(['embed', JSON.stringify(options.embed)]);
763
+ }
764
+ if (!(options && options.withRights)) {
765
+ fetchOptions.query.push(['skip_rights', 'y']);
766
+ }
767
+ return this.merchi.authenticatedFetch(resource, fetchOptions).then((data: any) => {
768
+ this.fromJson(data[singularName]);
769
+ this.cleanDirty();
770
+ return this;
771
+ });
772
+ };
773
+
774
+ public createFactory = (
775
+ {resourceName = (this.constructor as typeof Entity).resourceName}
776
+ ) => (options?: CreateOptions) => {
777
+ const resource = `/${resourceName}/`;
778
+ const data = this.toFormData();
779
+ const singularName = (this.constructor as typeof Entity).singularName;
780
+ const fetchOptions: RequestOptions = {method: 'POST',
781
+ body: data};
782
+
783
+ fetchOptions.query = [];
784
+ if (options && options.embed) {
785
+ fetchOptions.query.push(['embed', JSON.stringify(options.embed)]);
786
+ }
787
+ if (!(options && options.withRights)) {
788
+ fetchOptions.query.push(['skip_rights', 'y']);
789
+ }
790
+ return this.merchi.authenticatedFetch(resource, fetchOptions).
791
+ then((data: any) => {
792
+ this.fromJson(data[singularName]);
793
+ this.cleanDirty();
794
+ return this;});
795
+ };
796
+
797
+ public create = this.createFactory({});
798
+
799
+ private getEntityClass = (name: string) => {
800
+ if (name === undefined) {
801
+ return undefined;
802
+ }
803
+ return (this.merchi as any)[name];
804
+ };
805
+
806
+ public cleanDirty = () => {
807
+ // remove all dirty records of this entity, makes it untouched
808
+ // entity
809
+ this.isDirty = false;
810
+ for (const entry of this.propertiesMap.entries()) {
811
+ const propertyInfo = entry[1];
812
+ propertyInfo.dirty = false;
813
+ if (propertyInfo.currentValue !== undefined) {
814
+ if (propertyInfo.arrayType) {
815
+ propertyInfo.currentValue.map((v: any) => v.cleanDirty());
816
+ } else if (this.isSingleEntityProperty(propertyInfo)) {
817
+ if (propertyInfo.currentValue) {
818
+ propertyInfo.currentValue.cleanDirty();
819
+ }
820
+ }
821
+ }
822
+ }
823
+ };
824
+
825
+
826
+ public fromJson = (json: any, options?: FromJsonOptions) => {
827
+ options = options || {};
828
+ const { makeDirty = false, arrayValueStrict = true } = options;
829
+ options = { makeDirty, arrayValueStrict };
830
+ for (const key in json) {
831
+ const value: any = (json as any)[key];
832
+ if (value === undefined) continue;
833
+ const propertyInfo = this.propertiesMap.get(key);
834
+ if (propertyInfo !== undefined) {
835
+ propertyInfo.dirty = makeDirty;
836
+ if (propertyInfo.arrayType) {
837
+ // ignore array value if it is not an array and we did expect it
838
+ if (!arrayValueStrict && !Array.isArray(value)) continue;
839
+ const newValue: any = value.map((item: any, index: number) => {
840
+ const currentValue: any = propertyInfo.currentValue;
841
+ // if property already have an array of entities as relationship,
842
+ // try to merge with json one by one, this behavior may need to be
843
+ // configurable in the future.
844
+ if (currentValue && currentValue[index]) {
845
+ return currentValue[index].fromJson(item, options);
846
+ }
847
+ const nested = new (propertyInfo.arrayType as any)(this.merchi);
848
+ return nested.fromJson(item, options);
849
+ });
850
+ propertyInfo.currentValue = newValue;
851
+ } else if (this.isSingleEntityProperty(propertyInfo)) {
852
+ // if property already have a entity as relationship, try to merge
853
+ // with json first, this behavior may need to be configurable in the
854
+ // future.
855
+ if (propertyInfo.currentValue) {
856
+ propertyInfo.currentValue.fromJson(value, options);
857
+ } else {
858
+ const nested = new (propertyInfo.type as any)(this.merchi);
859
+ propertyInfo.currentValue = nested.fromJson(value, options);
860
+ }
861
+ } else {
862
+ propertyInfo.currentValue = value;
863
+ if (propertyInfo.type === Date && !!value) {
864
+ propertyInfo.currentValue = new Date(value * 1000);
865
+ }
866
+ }
867
+ }
868
+ }
869
+ return this;
870
+ };
871
+
872
+ public toJson = () => {
873
+ const json: any = {};
874
+ for (const entry of this.propertiesMap.entries()) {
875
+ const propertyName = entry[0];
876
+ const propertyInfo = entry[1];
877
+ if (propertyInfo.currentValue !== undefined) {
878
+ if (propertyInfo.arrayType) {
879
+ const array = propertyInfo.currentValue.map((v: any) => v.toJson());
880
+ json[propertyName] = array;
881
+ } else if (this.isSingleEntityProperty(propertyInfo)) {
882
+ if (propertyInfo.currentValue === null) {
883
+ json[propertyName] = null;
884
+ } else {
885
+ json[propertyName] = propertyInfo.currentValue.toJson();
886
+ }
887
+ } else {
888
+ const value = propertyInfo.currentValue;
889
+ json[propertyName] = value;
890
+ if (propertyInfo.type === Date && !!value) {
891
+ json[propertyName] = value.getTime() / 1000;
892
+ }
893
+ }
894
+ }
895
+ }
896
+ return json;
897
+ };
898
+
899
+ protected forEachProperty = (fn: (i: PropertyInfo) => void) => {
900
+ for (const info of this.propertiesMap.values()) {
901
+ fn(info);
902
+ }
903
+ };
904
+
905
+ public toFormData = (options?: SerialiseOptions,
906
+ fileIndex?: Counter): FormData => {
907
+ if (options === undefined) {
908
+ options = {};
909
+ }
910
+ const result = options.existing || new FormData();
911
+ const prefix = options._prefix || '';
912
+ const excludeOld = options.excludeOld === undefined ?
913
+ true : options.excludeOld;
914
+ if (fileIndex === undefined) {
915
+ fileIndex = {value: 0};
916
+ }
917
+ const appendData = (name: string, value: any) => {
918
+ /* istanbul ignore next */
919
+ if (name === undefined || value === undefined) {
920
+ /* istanbul ignore next */
921
+ return;
922
+ }
923
+ if (prefix) {
924
+ name = prefix + '-' + name;
925
+ }
926
+ result.set(name, value);
927
+ };
928
+ if ((this as any).fileData !== undefined) {
929
+ result.set(String(fileIndex.value), (this as any).fileData);
930
+ appendData('fileDataIndex', fileIndex.value);
931
+ fileIndex.value++;
932
+ }
933
+ const processArrayProperty = (info: PropertyInfo, value: Entity[]) => {
934
+ const remoteCount = value.length;
935
+ const initialLength = Array.from((result as any).entries()).length;
936
+ if (remoteCount === 0 && info.dirty) {
937
+ appendData(info.property + '-count', 0);
938
+ return;
939
+ }
940
+
941
+ let isDirty = false;
942
+ for (const item of value) {
943
+ if (item.isDirty) {
944
+ isDirty = true;
945
+ break;
946
+ }
947
+ }
948
+ isDirty = isDirty || info.dirty;
949
+ if (!isDirty && excludeOld) {
950
+ return;
951
+ }
952
+ for (let i = 0; i < remoteCount; ++i) {
953
+ let innerPrefix = info.property + '-' + i;
954
+ if (prefix) {
955
+ innerPrefix = prefix + '-' + innerPrefix;
956
+ }
957
+ value[i].toFormData(
958
+ {existing: result, _prefix: innerPrefix, excludeOld: excludeOld},
959
+ fileIndex);
960
+ }
961
+ const finalLength = Array.from((result as any).entries()).length;
962
+ if ((finalLength - initialLength) > 0) {
963
+ appendData(info.property + '-count', remoteCount);
964
+ if (info.updatingOrder) {
965
+ appendData(info.property + '-*updateOrder', 'true');
966
+ }
967
+ }
968
+ };
969
+ const processSingleEntityProperty = (info: PropertyInfo, value: Entity) => {
970
+ const primaryKey: string = (this.constructor as typeof Entity).primaryKey;
971
+ let innerPrefix = info.property + '-0';
972
+ if (prefix) {
973
+ innerPrefix = prefix + '-' + innerPrefix;
974
+ }
975
+ if (value === null) {
976
+ if (info.dirty || !excludeOld) {
977
+ appendData(info.property + '-0-' + primaryKey, '-1');
978
+ appendData(info.property + '-count', 1);
979
+ }
980
+ return;
981
+ }
982
+ if (!(info.dirty || (value.isDirty)) && excludeOld) {
983
+ return;
984
+ }
985
+ const initialLength = Array.from((result as any).entries()).length;
986
+ value.toFormData(
987
+ {existing: result, _prefix: innerPrefix, excludeOld: excludeOld},
988
+ fileIndex);
989
+ const finalLength = Array.from((result as any).entries()).length;
990
+ if ((finalLength - initialLength) > 0) {
991
+ appendData(info.property + '-count', 1);
992
+ }
993
+ };
994
+ const processScalarProperty = (info: PropertyInfo, value: any) => {
995
+ const primaryKey: string = (this.constructor as typeof Entity).primaryKey;
996
+ if (!excludeOld ||
997
+ info.dirty ||
998
+ (info.property === primaryKey && value)) {
999
+ if (info.type === Date && !!value) {
1000
+ value = value.getTime() / 1000;
1001
+ } else if (
1002
+ // JSON/JSONB columns (e.g. Domain.jobAgentPolicy) are plain objects.
1003
+ // FormData stringifies objects as "[object Object]" unless we encode.
1004
+ value !== null &&
1005
+ typeof value === 'object' &&
1006
+ !(typeof Blob !== 'undefined' && value instanceof Blob)
1007
+ ) {
1008
+ value = JSON.stringify(value);
1009
+ }
1010
+ appendData(info.property, value);
1011
+ }
1012
+ };
1013
+ const processProperty = (info: PropertyInfo) => {
1014
+ const value = (this as any)[info.attribute];
1015
+ if (value === undefined) {
1016
+ return;
1017
+ } else if (info.arrayType) {
1018
+ processArrayProperty(info, value);
1019
+ } else if (this.isSingleEntityProperty(info)) {
1020
+ processSingleEntityProperty(info, value);
1021
+ } else {
1022
+ processScalarProperty(info, value);
1023
+ }
1024
+ };
1025
+ this.forEachProperty(processProperty);
1026
+ return result;
1027
+ };
1028
+
1029
+ protected addBackObject = (remote?: Entity) => {
1030
+ if (remote !== undefined) {
1031
+ remote.backObjects.add(this);
1032
+ }
1033
+ };
1034
+
1035
+ protected addBackObjectList = (remotes?: Entity[]) => {
1036
+ if (remotes !== undefined) {
1037
+ remotes.forEach(this.addBackObject);
1038
+ }
1039
+ };
1040
+
1041
+ public updateOrder = (property: string) => {
1042
+ (this.propertiesMap.get(property) as PropertyInfo).updatingOrder = true;
1043
+ };
1044
+
1045
+ protected markDirty = (property: string, newValue: any) => {
1046
+ if (newValue === undefined) {
1047
+ return;
1048
+ }
1049
+ (this.propertiesMap.get(property) as PropertyInfo).dirty = true;
1050
+ const openSet: Entity[] = []; /* Queue (BFS) */
1051
+ const closedSet = new Set();
1052
+ openSet.push(this);
1053
+ while (openSet.length > 0) {
1054
+ const current = openSet.shift() as Entity;
1055
+ if (!closedSet.has(current)) {
1056
+ current.isDirty = true;
1057
+ closedSet.add(current);
1058
+ for (const child of current.backObjects) openSet.push(child);
1059
+ }
1060
+ }
1061
+ };
1062
+
1063
+ public delete = (options?: DeleteOptions) => {
1064
+ const primaryKey: number = this.getPrimaryKeyValue();
1065
+ const resourceName: string = (this.constructor as any).resourceName;
1066
+ const resource = `/${resourceName}/${String(primaryKey)}/`;
1067
+ const fetchOptions: RequestOptions = {method: 'DELETE'};
1068
+ fetchOptions.query = [];
1069
+ if (!(options && options.withRights)) {
1070
+ fetchOptions.query.push(['skip_rights', 'y']);
1071
+ }
1072
+ if (this.merchi.sessionToken) {
1073
+ fetchOptions.query.push(['session_token', this.merchi.sessionToken]);
1074
+ }
1075
+ return this.merchi.authenticatedFetch(resource, fetchOptions, true).then(
1076
+ () => {return this;});
1077
+ };
1078
+
1079
+ public recover = (options?: SaveOptions) => {
1080
+ const primaryKey: number | string = this.getPrimaryKeyValue();
1081
+ const constructor = this.constructor as typeof Entity;
1082
+ const resourceName: string = constructor.resourceName;
1083
+ const singularName: string = constructor.singularName;
1084
+ const resource = `/unarchive/${resourceName}/${String(primaryKey)}/`;
1085
+ const fetchOptions: RequestOptions = {method: 'POST'};
1086
+ fetchOptions.query = [];
1087
+ if (options && options.embed) {
1088
+ fetchOptions.query.push(['embed', JSON.stringify(options.embed)]);
1089
+ }
1090
+ if (!(options && options.withRights)) {
1091
+ fetchOptions.query.push(['skip_rights', 'y']);
1092
+ }
1093
+ return this.merchi.authenticatedFetch(resource, fetchOptions).then((data: any) => {
1094
+ this.fromJson(data[singularName]);
1095
+ return this;
1096
+ });
1097
+ };
1098
+ }