Skip to content

Commit

Permalink
Merge branch 'next-8818/admin-order-store-api' into 'master'
Browse files Browse the repository at this point in the history
NEXT-8818 - Migrate admin order to store-api

See merge request shopware/6/product/platform!2627
  • Loading branch information
tobiasberge committed Jul 9, 2020
2 parents 8160bb4 + 2f42d80 commit ffcca4f
Show file tree
Hide file tree
Showing 15 changed files with 332 additions and 24 deletions.
5 changes: 4 additions & 1 deletion UPGRADE-6.3.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,10 @@ Administration
* Replace the component 'sw-settings-user-create' with 'sw-users-permissions-user-create'
* Replace the component 'sw-settings-user-list' with 'sw-users-permissions-user-listing'
* When using `sw-custom-field-list` make sure you have set the `page`, `limit` and `total` prop
* Deprecated api services
* `cartSalesChannelService`: use `cartStoreApiService`
* `checkOutSalesChannelService`: use `checkoutStoreService`
* `salesChannelContextService`: use `storeContextService`
Storefront
--------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const lineItemConstants = Object.freeze({
* Uses the _proxy endpoint of the admin api to connect to the sales-channel-api endpoint cart
* @class
* @extends ApiService
* @deprecated tag:v6.4.0 - Use CartStoreApiService
*/
class CartSalesChannelService extends ApiService {
constructor(httpClient, loginService, apiEndpoint = 'cart') {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
import { deepCopyObject } from 'src/core/service/utils/object.utils';
import utils from 'src/core/service/util.service';
import ApiService from '../api.service';

const lineItemConstants = Object.freeze({
types: Object.freeze({
PRODUCT: 'product',
CREDIT: 'credit',
CUSTOM: 'custom',
PROMOTION: 'promotion'
}),

priceTypes: Object.freeze({
ABSOLUTE: 'absolute',
QUANTITY: 'quantity'
})
});

/**
* Gateway for the API end point "cart"
* Uses the _proxy endpoint of the admin api to connect to the store-api endpoint cart
* @class
* @extends ApiService
*/
class CartStoreService extends ApiService {
constructor(httpClient, loginService, apiEndpoint = 'cart') {
super(httpClient, loginService, apiEndpoint);
this.name = 'cartStoreService';
}

getLineItemTypes() {
return lineItemConstants.types;
}

getLineItemPriceTypes() {
return lineItemConstants.priceTypes;
}

mapLineItemTypeToPriceType(itemType) {
const lineItemTypes = this.getLineItemTypes();
const priceTypes = this.getLineItemPriceTypes();

const mapTypes = {
[lineItemTypes.PRODUCT]: priceTypes.QUANTITY,
[lineItemTypes.CUSTOM]: priceTypes.QUANTITY,
[lineItemTypes.CREDIT]: priceTypes.ABSOLUTE
};

return mapTypes[itemType];
}

createCart(salesChannelId, additionalParams = {}, additionalHeaders = {}) {
const route = `_proxy/store-api/${salesChannelId}/v1/checkout/cart`;
const headers = this.getBasicHeaders(additionalHeaders);

return this.httpClient.get(route, { additionalParams, headers });
}

getCart(salesChannelId, contextToken, additionalParams = {}, additionalHeaders = {}) {
const route = `_proxy/store-api/${salesChannelId}/v1/checkout/cart`;
const headers = {
...this.getBasicHeaders(additionalHeaders),
'sw-context-token': contextToken
};

return this.httpClient.get(route, { additionalParams, headers });
}

cancelCart(salesChannelId, contextToken, additionalParams = {}, additionalHeaders = {}) {
const route = `_proxy/store-api/${salesChannelId}/v1/checkout/cart`;
const headers = {
...this.getBasicHeaders(additionalHeaders),
'sw-context-token': contextToken
};

return this.httpClient.delete(route, { additionalParams, headers });
}

removeLineItems(
salesChannelId,
contextToken,
lineItemKeys,
additionalParams = {},
additionalHeaders = {}
) {
const route = `_proxy/store-api/${salesChannelId}/v1/checkout/cart/line-item`;
const headers = {
...this.getBasicHeaders(additionalHeaders),
'sw-context-token': contextToken
};

return this.httpClient.delete(route, { additionalParams, headers, data: { ids: lineItemKeys } });
}

getRouteForItem(id, salesChannelId) {
return `_proxy/store-api/${salesChannelId}/v1/checkout/cart/line-item`;
}

getPayloadForItem(item, salesChannelId, isNewProductItem, id) {
const dummyPrice = deepCopyObject(item.priceDefinition);
dummyPrice.taxRules = item.priceDefinition.taxRules;
dummyPrice.quantity = item.quantity;
dummyPrice.type = this.mapLineItemTypeToPriceType(item.type);

return {
items: [
{
id: id,
referencedId: id,
label: item.label,
quantity: item.quantity,
type: item.type,
description: item.description,
priceDefinition: dummyPrice,
stackable: true,
removable: true,
salesChannelId
}
]
};
}

saveLineItem(
salesChannelId,
contextToken,
item,
additionalParams = {},
additionalHeaders = {}
) {
const isNewProductItem = item._isNew && item.type === this.getLineItemTypes().PRODUCT;
const id = item.identifier || item.id || utils.createId();
const route = this.getRouteForItem(id, salesChannelId, isNewProductItem);
const headers = {
...this.getBasicHeaders(additionalHeaders),
'sw-context-token': contextToken
};

const payload = this.getPayloadForItem(item, salesChannelId, isNewProductItem, id);

if (item._isNew) {
return this.httpClient.post(route, payload, { additionalParams, headers });
}

return this.httpClient.patch(route, payload, { additionalParams, headers });
}

addPromotionCode(
salesChannelId,
contextToken,
code,
additionalParams = {},
additionalHeaders = {}
) {
const route = `_proxy/store-api/${salesChannelId}/v1/checkout/cart/line-item`;
const headers = {
...this.getBasicHeaders(additionalHeaders),
'sw-context-token': contextToken
};

const payload = {
items: [
{
type: 'promotion',
referencedId: code
}
]
};

return this.httpClient.post(route, payload, { additionalParams, headers });
}

modifyShippingCosts(salesChannelId, contextToken, shippingCosts, additionalHeaders, additionalParams = {},) {
const route = '_proxy/modify-shipping-costs';
const headers = {
...this.getBasicHeaders(additionalHeaders),
'sw-context-token': contextToken
};

return this.httpClient.patch(route, { salesChannelId, shippingCosts }, { additionalParams, headers });
}


disableAutomaticPromotions(contextToken, additionalParams = {}, additionalHeaders = {}) {
const route = '_proxy/disable-automatic-promotions';
const headers = {
...this.getBasicHeaders(additionalHeaders),
'sw-context-token': contextToken
};

return this.httpClient.patch(route, {}, { additionalParams, headers });
}

enableAutomaticPromotions(contextToken, additionalParams = {}, additionalHeaders = {}) {
const route = '_proxy/enable-automatic-promotions';
const headers = {
...this.getBasicHeaders(additionalHeaders),
'sw-context-token': contextToken
};

return this.httpClient.patch(route, {}, { additionalParams, headers });
}
}

export default CartStoreService;
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import ApiService from '../api.service';
* Uses the _proxy endpoint of the admin api to connect to the sales-channel-api endpoint cart
* @class
* @extends ApiService
* @deprecated tag:v6.4.0 - Use CheckoutStoreService
*/
class CheckOutSalesChannelService extends ApiService {
constructor(httpClient, loginService, apiEndpoint = 'checkout') {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import ApiService from '../api.service';

/**
* Gateway for the API end point "order"
* Uses the _proxy endpoint of the admin api to connect to the store-api endpoint cart
* @class
* @extends ApiService
*/
class CheckoutStoreService extends ApiService {
constructor(httpClient, loginService, apiEndpoint = 'checkout') {
super(httpClient, loginService, apiEndpoint);
this.name = 'checkoutStoreService';
}

checkout(salesChannelId, contextToken, additionalParams = {}, additionalHeaders = {}) {
const route = `_proxy/store-api/${salesChannelId}/v1/checkout/order`;
const headers = {
...this.getBasicHeaders(additionalHeaders),
'sw-context-token': contextToken
};
return this.httpClient
.post(route, {}, { additionalParams, headers });
}
}

export default CheckoutStoreService;
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import ApiService from '../api.service';
* Uses the _proxy endpoint of the admin api to connect to the sales-channel-api endpoint cart
* @class
* @extends ApiService
* @deprecated tag:v6.4.0 - Use storeContextService
*/
class SalesChannelContextService extends ApiService {
constructor(httpClient, loginService, apiEndpoint = 'sales-channel-context') {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import ApiService from '../api.service';

/**
* Gateway for the API end point "sales-channel-context"
* Uses the _proxy endpoint of the admin api to connect to the store-api endpoint cart
* @class
* @extends ApiService
*/
class StoreContextService extends ApiService {
constructor(httpClient, loginService, apiEndpoint = 'sales-channel-context') {
super(httpClient, loginService, apiEndpoint);
this.name = 'contextStoreService';
}

updateCustomerContext(
customerId,
salesChannelId,
contextToken,
additionalParams = {},
additionalHeaders = {}
) {
const route = '_proxy/switch-customer';
const headers = {
...this.getBasicHeaders(additionalHeaders),
'sw-context-token': contextToken
};

return this.httpClient
.patch(
route,
{ customerId: customerId, salesChannelId: salesChannelId },
{ additionalParams, headers }
);
}

updateContext(
context,
salesChannelId,
contextToken,
additionalParams = {},
additionalHeaders = {}
) {
const route = `_proxy/store-api/${salesChannelId}/v1/context`;
const headers = {
...this.getBasicHeaders(additionalHeaders),
'sw-context-token': contextToken
};

return this.httpClient
.patch(
route,
context,
{ additionalParams, headers }
);
}

getContext(salesChannelId, source, additionalParams = {}, additionalHeaders = {}) {
const route = `_proxy/store-api/${salesChannelId}/v1/${source}`;
const headers = this.getBasicHeaders(additionalHeaders);

return this.httpClient.post(route, {}, { additionalParams, headers });
}
}

export default StoreContextService;
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ Component.register('sw-order-create-promotion-modal', {

disableAutomaticPromotions() {
this.isLoading = true;
Service('cartSalesChannelService').disableAutomaticPromotions(this.cart.token).then(() => {
Service('cartStoreService').disableAutomaticPromotions(this.cart.token).then(() => {
this.isLoading = false;
this.$emit('save');
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ Component.register('sw-order-line-items-grid-sales-channel', {
},

lineItemTypes() {
return Service('cartSalesChannelService').getLineItemTypes();
return Service('cartStoreService').getLineItemTypes();
},

isCartTokenAvailable() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ Component.register('sw-order-line-items-grid', {
},

lineItemTypes() {
return Service('cartSalesChannelService').getLineItemTypes();
return Service('cartStoreService').getLineItemTypes();
},

getLineItemColumns() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ Component.register('sw-order-product-select', {
},

lineItemTypes() {
return Service('cartSalesChannelService').getLineItemTypes();
return Service('cartStoreService').getLineItemTypes();
},

lineItemPriceTypes() {
return Service('cartSalesChannelService').getLineItemPriceTypes();
return Service('cartStoreService').getLineItemPriceTypes();
},

isShownProductSelect() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ Component.register('sw-order-create', {
})
.then((response) => {
this.isSaveSuccessful = true;
this.orderId = get(response, 'data.data.id');
this.orderId = get(response, 'data.id');
})
.catch(() => this.showError())
.finally(() => {
Expand Down
Loading

0 comments on commit ffcca4f

Please sign in to comment.