cart.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. import { appApi } from 'src/boot/axios.js'
  2. import { Cart } from 'src/models/Cart.js'
  3. import { Order } from 'src/models/Order.js'
  4. import { GatewayList } from 'src/models/Gateway.js'
  5. import APIRepository from '../classes/APIRepository.js'
  6. export default class CartAPI extends APIRepository {
  7. constructor() {
  8. super('cart', appApi, '/orderproduct', new Cart())
  9. this.seller = 1 // 1: Alaa - 2: Soala
  10. this.APIAdresses = {
  11. addToCart: '/orderproduct',
  12. gateways: '/gateways',
  13. discountSubmit: '/order/submitCoupon',
  14. discountRemove: '/order/RemoveCoupon',
  15. reviewCart: '/checkout/review',
  16. getPaymentRedirectEncryptedLink: (device, paymentMethod) => '/getPaymentRedirectEncryptedLink?seller=' + this.seller + '&device=' + device + '&paymentMethod=' + paymentMethod,
  17. // getPaymentRedirectEncryptedLink: '/getPaymentRedirectEncryptedLink?seller=' + this.seller + '&device=web',
  18. removeFromCart: (id) => '/orderproduct/' + id,
  19. removeFromCartByProductId: (id) => 'remove-order-product/' + id,
  20. orderWithTransaction: (orderId) => '/orderWithTransaction/' + orderId
  21. }
  22. this.CacheList = {
  23. addToCart: this.name + this.APIAdresses.addToCart,
  24. gateways: this.name + this.APIAdresses.gateways,
  25. discountSubmit: this.name + this.APIAdresses.discountSubmit,
  26. discountRemove: this.name + this.APIAdresses.discountRemove,
  27. getPaymentRedirectEncryptedLink: (device, paymentMethod) => this.name + this.APIAdresses.getPaymentRedirectEncryptedLink(device, paymentMethod),
  28. reviewCart: this.name + this.APIAdresses.reviewCart,
  29. removeFromCart: id => this.name + this.APIAdresses.removeFromCart(id),
  30. removeFromCartByProductId: id => this.name + this.APIAdresses.removeFromCartByProductId(id),
  31. orderWithTransaction: orderId => this.name + this.APIAdresses.orderWithTransaction(orderId)
  32. }
  33. }
  34. addToCart(data = {}, cache) {
  35. const payload = {
  36. product_id: data.product_id, // Number or String
  37. products: data.products, // Number or String (List ofProduct's ID)
  38. attribute: data.attribute, // Number or String
  39. seller: this.seller
  40. }
  41. if (!payload.products || (Array.isArray(payload.products) && payload.products.length === 0)) {
  42. delete payload.products
  43. }
  44. if (!payload.attribute || (Array.isArray(payload.attribute) && payload.attribute.length === 0)) {
  45. delete payload.attribute
  46. }
  47. return this.sendRequest({
  48. apiMethod: 'post',
  49. api: this.api,
  50. request: this.APIAdresses.addToCart,
  51. cacheKey: this.CacheList.addToCart,
  52. ...(cache && { cache }),
  53. resolveCallback: (response) => {
  54. return response
  55. },
  56. rejectCallback: (error) => {
  57. return error
  58. },
  59. data: payload
  60. })
  61. }
  62. discountSubmit(data = {}) {
  63. return this.sendRequest({
  64. apiMethod: 'get',
  65. api: this.api,
  66. request: this.APIAdresses.discountSubmit,
  67. cacheKey: this.CacheList.discountSubmit,
  68. ...(data.cache && { cache: data.cache }),
  69. resolveCallback: (response) => {
  70. return response
  71. },
  72. rejectCallback: (error) => {
  73. return error
  74. },
  75. params: data.params
  76. })
  77. }
  78. discountRemove(data = {}) {
  79. return this.sendRequest({
  80. apiMethod: 'get',
  81. api: this.api,
  82. request: this.APIAdresses.discountRemove,
  83. cacheKey: this.CacheList.discountRemove,
  84. ...(!!data.cache && { cache: data.cache }),
  85. resolveCallback: (response) => {
  86. return response
  87. },
  88. rejectCallback: (error) => {
  89. return error
  90. }
  91. })
  92. }
  93. reviewCart(cartItems = [], cache = { TTL: 1000 }) {
  94. const queryParams = {}
  95. queryParams.seller = this.seller
  96. const setCartItemParam = function (cartItemIndex, paramsKey, value) {
  97. queryParams['cartItems' + '[' + cartItemIndex + ']' + paramsKey] = value
  98. }
  99. const setProductsParams = function (cartItemIndex, products) {
  100. if (!Array.isArray(products)) {
  101. return
  102. }
  103. const cartItemProductsLength = products.length
  104. for (let productItemIndex = 0; productItemIndex < cartItemProductsLength; productItemIndex++) {
  105. const productItem = products[productItemIndex]
  106. setCartItemParam(cartItemIndex, '[products]' + '[' + productItemIndex + ']', productItem)
  107. }
  108. }
  109. const setAttributesParams = function (cartItemIndex, attributes) {
  110. if (!Array.isArray(attributes)) {
  111. return
  112. }
  113. const cartItemAttributesLength = attributes.length
  114. for (let attributeItemIndex = 0; attributeItemIndex < cartItemAttributesLength; attributeItemIndex++) {
  115. const attributeItem = attributes[attributeItemIndex]
  116. setCartItemParam(cartItemIndex, '[products]' + '[' + attributeItemIndex + ']', attributeItem)
  117. }
  118. }
  119. const cartItemLength = cartItems.length
  120. let cartItemIndex = 0
  121. for (let i = 0; i < cartItemLength; i++) {
  122. const cartItem = cartItems[i]
  123. if (!cartItem.product_id) {
  124. continue
  125. }
  126. setCartItemParam(cartItemIndex, '[product_id]', cartItem.product_id)
  127. setProductsParams(cartItemIndex, cartItem.products)
  128. setAttributesParams(cartItemIndex, cartItem.attribute)
  129. cartItemIndex++
  130. }
  131. return this.sendRequest({
  132. apiMethod: 'get',
  133. api: this.api,
  134. request: this.APIAdresses.reviewCart,
  135. cacheKey: this.CacheList.reviewCart,
  136. ...(cache !== undefined && { cache }),
  137. resolveCallback: (response) => {
  138. return new Cart(response.data.data)
  139. },
  140. rejectCallback: (error) => {
  141. return error
  142. },
  143. data: queryParams
  144. })
  145. }
  146. getGateways(cache = { TTL: 1000 }) {
  147. return this.sendRequest({
  148. apiMethod: 'get',
  149. api: this.api,
  150. request: this.APIAdresses.gateways,
  151. cacheKey: this.CacheList.gateways,
  152. ...(cache !== undefined && { cache }),
  153. resolveCallback: (response) => {
  154. return new GatewayList(response.data.data)
  155. },
  156. rejectCallback: (error) => {
  157. return error
  158. }
  159. })
  160. }
  161. getPaymentRedirectEncryptedLink(data = { device: 'web', paymentMethod: null }, cache = { TTL: 1000 }) {
  162. return this.sendRequest({
  163. apiMethod: 'get',
  164. api: this.api,
  165. request: this.APIAdresses.getPaymentRedirectEncryptedLink(data.device, data.paymentMethod),
  166. cacheKey: this.CacheList.getPaymentRedirectEncryptedLink(data.device, data.paymentMethod),
  167. ...(cache !== undefined && { cache }),
  168. resolveCallback: (response) => {
  169. return response.data.data.url
  170. },
  171. rejectCallback: (error) => {
  172. return error
  173. }
  174. })
  175. }
  176. removeFromCart(orderProductId) {
  177. return this.sendRequest({
  178. apiMethod: 'delete',
  179. api: this.api,
  180. request: this.APIAdresses.removeFromCart(orderProductId),
  181. cacheKey: this.CacheList.removeFromCart(orderProductId),
  182. resolveCallback: (response) => {
  183. return new Cart(response.data.data)
  184. },
  185. rejectCallback: (error) => {
  186. return error
  187. }
  188. })
  189. }
  190. removeFromCartByProductId(productId) {
  191. return this.sendRequest({
  192. apiMethod: 'delete',
  193. api: this.api,
  194. request: this.APIAdresses.removeFromCartByProductId(productId),
  195. cacheKey: this.CacheList.removeFromCartByProductId(productId),
  196. resolveCallback: (response) => {
  197. return new Cart(response.data.data)
  198. },
  199. rejectCallback: (error) => {
  200. return error
  201. }
  202. })
  203. }
  204. getorderWithTransaction(orderId, cache = { TTL: 1000 }) {
  205. return this.sendRequest({
  206. apiMethod: 'get',
  207. api: this.api,
  208. request: this.APIAdresses.orderWithTransaction(orderId),
  209. cacheKey: this.CacheList.orderWithTransaction(orderId),
  210. ...(cache && { cache }),
  211. resolveCallback: (response) => {
  212. return new Order(response.data.data)
  213. },
  214. rejectCallback: (error) => {
  215. return error
  216. }
  217. })
  218. }
  219. }