@daffodil/customer-order 0.0.0-PLACEHOLDER
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.
- package/.eslintrc.js +52 -0
- package/README.md +9 -0
- package/driver/magento/ng-package.json +8 -0
- package/driver/magento/package.json +3 -0
- package/driver/magento/src/driver.module.ts +30 -0
- package/driver/magento/src/errors/public_api.ts +1 -0
- package/driver/magento/src/errors/transform.ts +6 -0
- package/driver/magento/src/index.ts +1 -0
- package/driver/magento/src/injection-tokens/fragments/order.token.spec.ts +42 -0
- package/driver/magento/src/injection-tokens/fragments/order.token.ts +42 -0
- package/driver/magento/src/injection-tokens/public_api.ts +9 -0
- package/driver/magento/src/injection-tokens/transforms/extra.token.spec.ts +34 -0
- package/driver/magento/src/injection-tokens/transforms/extra.token.ts +42 -0
- package/driver/magento/src/injection-tokens/transforms/token.spec.ts +50 -0
- package/driver/magento/src/injection-tokens/transforms/token.ts +29 -0
- package/driver/magento/src/interfaces/order-extra-transform.type.ts +6 -0
- package/driver/magento/src/interfaces/order-transform.type.ts +6 -0
- package/driver/magento/src/interfaces/public_api.ts +2 -0
- package/driver/magento/src/models/public_api.ts +2 -0
- package/driver/magento/src/models/requests/customer-orders-filter-input.type.ts +6 -0
- package/driver/magento/src/models/requests/public_api.ts +1 -0
- package/driver/magento/src/models/responses/customer-orders.type.ts +9 -0
- package/driver/magento/src/models/responses/get-customer-order.type.ts +10 -0
- package/driver/magento/src/models/responses/get-customer-orders.type.ts +8 -0
- package/driver/magento/src/models/responses/order.type.ts +27 -0
- package/driver/magento/src/models/responses/public_api.ts +5 -0
- package/driver/magento/src/order.service.spec.ts +189 -0
- package/driver/magento/src/order.service.ts +94 -0
- package/driver/magento/src/public_api.ts +11 -0
- package/driver/magento/src/queries/fragments/customer-order.ts +53 -0
- package/driver/magento/src/queries/fragments/public_api.ts +1 -0
- package/driver/magento/src/queries/get-customer-order.ts +27 -0
- package/driver/magento/src/queries/get-customer-orders.ts +33 -0
- package/driver/magento/src/queries/public_api.ts +4 -0
- package/driver/magento/src/transforms/public_api.ts +1 -0
- package/driver/magento/src/transforms/responses/order-collection.ts +43 -0
- package/driver/magento/src/transforms/responses/order.spec.ts +31 -0
- package/driver/magento/src/transforms/responses/order.ts +291 -0
- package/driver/magento/src/transforms/responses/public_api.ts +2 -0
- package/driver/magento/src/validators/get-order.spec.ts +48 -0
- package/driver/magento/src/validators/get-order.ts +13 -0
- package/driver/magento/src/validators/get-orders.spec.ts +46 -0
- package/driver/magento/src/validators/get-orders.ts +13 -0
- package/driver/magento/src/validators/public_api.ts +1 -0
- package/driver/magento/testing/ng-package.json +8 -0
- package/driver/magento/testing/package.json +3 -0
- package/driver/magento/testing/src/factories/order.factory.spec.ts +47 -0
- package/driver/magento/testing/src/factories/order.factory.ts +72 -0
- package/driver/magento/testing/src/factories/orders.factory.spec.ts +34 -0
- package/driver/magento/testing/src/factories/orders.factory.ts +43 -0
- package/driver/magento/testing/src/factories/public_api.ts +2 -0
- package/driver/magento/testing/src/index.ts +1 -0
- package/driver/magento/testing/src/public_api.ts +1 -0
- package/driver/ng-package.json +8 -0
- package/driver/package.json +3 -0
- package/driver/src/errors/address-not-found.ts +17 -0
- package/driver/src/errors/invalid-api-response.ts +17 -0
- package/driver/src/errors/map.ts +16 -0
- package/driver/src/errors/public_api.ts +9 -0
- package/driver/src/index.ts +1 -0
- package/driver/src/public_api.ts +1 -0
- package/karma.conf.js +11 -0
- package/ng-package.json +7 -0
- package/ng-package.prod.json +7 -0
- package/package.json +48 -0
- package/src/index.ts +1 -0
- package/src/injection-tokens/error-matcher.token.ts +12 -0
- package/src/injection-tokens/public_api.ts +1 -0
- package/src/public_api.ts +1 -0
- package/test.ts +23 -0
- package/tsconfig.json +23 -0
- package/tsconfig.lib.json +28 -0
- package/tsconfig.lib.prod.json +9 -0
- package/tsconfig.spec.json +17 -0
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Inject,
|
|
3
|
+
Injectable,
|
|
4
|
+
} from '@angular/core';
|
|
5
|
+
import { Apollo } from 'apollo-angular';
|
|
6
|
+
import { DocumentNode } from 'graphql';
|
|
7
|
+
import {
|
|
8
|
+
Observable,
|
|
9
|
+
throwError,
|
|
10
|
+
} from 'rxjs';
|
|
11
|
+
import {
|
|
12
|
+
map,
|
|
13
|
+
catchError,
|
|
14
|
+
} from 'rxjs/operators';
|
|
15
|
+
|
|
16
|
+
import { DaffAuthStorageService } from '@daffodil/auth';
|
|
17
|
+
import { DaffCollectionRequest } from '@daffodil/core';
|
|
18
|
+
import {
|
|
19
|
+
DaffOrder,
|
|
20
|
+
DaffOrderCollection,
|
|
21
|
+
} from '@daffodil/order';
|
|
22
|
+
import { DaffOrderServiceInterface } from '@daffodil/order/driver';
|
|
23
|
+
import { DaffOrderMagentoService } from '@daffodil/order/driver/magento/2.4.1';
|
|
24
|
+
|
|
25
|
+
import { transformMagentoReviewsError } from './errors/transform';
|
|
26
|
+
import {
|
|
27
|
+
DAFF_CUSTOMER_ORDER_MAGENTO_EXTRA_ORDER_FRAGMENTS,
|
|
28
|
+
DAFF_CUSTOMER_ORDER_MAGENTO_ORDER_TRANSFORM,
|
|
29
|
+
} from './injection-tokens/public_api';
|
|
30
|
+
import { DaffMagentoCustomerOrderTransform } from './interfaces/public_api';
|
|
31
|
+
import {
|
|
32
|
+
MagentoGetCustomerOrderResponse,
|
|
33
|
+
MagentoGetCustomerOrdersResponse,
|
|
34
|
+
} from './models/public_api';
|
|
35
|
+
import {
|
|
36
|
+
getCustomerOrder,
|
|
37
|
+
getCustomerOrders,
|
|
38
|
+
} from './queries/public_api';
|
|
39
|
+
import {
|
|
40
|
+
MagentoCustomerOrderCollectionTransformer,
|
|
41
|
+
daffMagentoCustomerOrderTransformOrder,
|
|
42
|
+
} from './transforms/public_api';
|
|
43
|
+
import { validateGetCustomerOrderResponse } from './validators/get-order';
|
|
44
|
+
import { validateGetCustomerOrdersResponse } from './validators/public_api';
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* A service for making Magento GraphQL queries for customers.
|
|
48
|
+
*
|
|
49
|
+
* @inheritdoc
|
|
50
|
+
*/
|
|
51
|
+
@Injectable({
|
|
52
|
+
providedIn: 'root',
|
|
53
|
+
})
|
|
54
|
+
export class DaffCustomerOrderMagentoService implements DaffOrderServiceInterface {
|
|
55
|
+
constructor(
|
|
56
|
+
private apollo: Apollo,
|
|
57
|
+
private authStorage: DaffAuthStorageService,
|
|
58
|
+
private guestDriver: DaffOrderMagentoService,
|
|
59
|
+
@Inject(DAFF_CUSTOMER_ORDER_MAGENTO_EXTRA_ORDER_FRAGMENTS) private extraFragments: DocumentNode[],
|
|
60
|
+
private transformer: MagentoCustomerOrderCollectionTransformer,
|
|
61
|
+
@Inject(DAFF_CUSTOMER_ORDER_MAGENTO_ORDER_TRANSFORM) private orderTransform: DaffMagentoCustomerOrderTransform,
|
|
62
|
+
) {}
|
|
63
|
+
|
|
64
|
+
list(cartId?: string, request: DaffCollectionRequest = {}): Observable<DaffOrderCollection> {
|
|
65
|
+
return this.authStorage.getAuthToken()
|
|
66
|
+
? this.apollo.query<MagentoGetCustomerOrdersResponse>({
|
|
67
|
+
query: getCustomerOrders(this.extraFragments),
|
|
68
|
+
variables: {
|
|
69
|
+
currentPage: request.currentPage,
|
|
70
|
+
pageSize: request.pageSize,
|
|
71
|
+
},
|
|
72
|
+
}).pipe(
|
|
73
|
+
map(validateGetCustomerOrdersResponse),
|
|
74
|
+
map(response => this.transformer.transform(response.data)),
|
|
75
|
+
catchError(err => throwError(() => transformMagentoReviewsError(err))),
|
|
76
|
+
)
|
|
77
|
+
: this.guestDriver.list(cartId);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
get(orderId: string, cartId?: string): Observable<DaffOrder> {
|
|
81
|
+
return this.authStorage.getAuthToken()
|
|
82
|
+
? this.apollo.query<MagentoGetCustomerOrderResponse>({
|
|
83
|
+
query: getCustomerOrder(this.extraFragments),
|
|
84
|
+
variables: {
|
|
85
|
+
id: orderId,
|
|
86
|
+
},
|
|
87
|
+
}).pipe(
|
|
88
|
+
map(validateGetCustomerOrderResponse),
|
|
89
|
+
map(result => this.orderTransform(result.data.customer.orders.items[0], result.data.customer.email)),
|
|
90
|
+
catchError(err => throwError(() => transformMagentoReviewsError(err))),
|
|
91
|
+
)
|
|
92
|
+
: this.guestDriver.get(orderId, cartId);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export * from './transforms/public_api';
|
|
2
|
+
export * from './models/public_api';
|
|
3
|
+
export * from './queries/public_api';
|
|
4
|
+
export * from './errors/public_api';
|
|
5
|
+
export * from './validators/public_api';
|
|
6
|
+
export * from './injection-tokens/public_api';
|
|
7
|
+
export * from './interfaces/public_api';
|
|
8
|
+
|
|
9
|
+
export { DaffCustomerOrderMagentoService } from './order.service';
|
|
10
|
+
|
|
11
|
+
export { DaffCustomerOrderMagentoDriverModule } from './driver.module';
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { gql } from 'apollo-angular';
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
orderTotalFragment,
|
|
5
|
+
orderCreditFragment,
|
|
6
|
+
orderAddressFragment,
|
|
7
|
+
orderInvoiceFragment,
|
|
8
|
+
orderItemFragment,
|
|
9
|
+
orderPaymentFragment,
|
|
10
|
+
orderShipmentFragment,
|
|
11
|
+
} from '@daffodil/order/driver/magento/2.4.1';
|
|
12
|
+
|
|
13
|
+
export const magentoCustomerOrderFragment = gql`
|
|
14
|
+
fragment magentoCustomerOrder on CustomerOrder {
|
|
15
|
+
id
|
|
16
|
+
order_date
|
|
17
|
+
status
|
|
18
|
+
carrier
|
|
19
|
+
number
|
|
20
|
+
shipping_method
|
|
21
|
+
items {
|
|
22
|
+
...orderItem
|
|
23
|
+
}
|
|
24
|
+
billing_address {
|
|
25
|
+
...orderAddress
|
|
26
|
+
}
|
|
27
|
+
shipping_address {
|
|
28
|
+
...orderAddress
|
|
29
|
+
}
|
|
30
|
+
shipments {
|
|
31
|
+
...orderShipment
|
|
32
|
+
}
|
|
33
|
+
payment_methods {
|
|
34
|
+
...orderPayment
|
|
35
|
+
}
|
|
36
|
+
invoices {
|
|
37
|
+
...orderInvoice
|
|
38
|
+
}
|
|
39
|
+
credit_memos {
|
|
40
|
+
...orderCredit
|
|
41
|
+
}
|
|
42
|
+
total {
|
|
43
|
+
...orderTotal
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
${orderItemFragment}
|
|
47
|
+
${orderShipmentFragment}
|
|
48
|
+
${orderPaymentFragment}
|
|
49
|
+
${orderInvoiceFragment}
|
|
50
|
+
${orderCreditFragment}
|
|
51
|
+
${orderAddressFragment}
|
|
52
|
+
${orderTotalFragment}
|
|
53
|
+
`;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { magentoCustomerOrderFragment } from './customer-order';
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { gql } from 'apollo-angular';
|
|
2
|
+
import { DocumentNode } from 'graphql';
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
daffBuildFragmentDefinition,
|
|
6
|
+
daffBuildFragmentNameSpread,
|
|
7
|
+
} from '@daffodil/core/graphql';
|
|
8
|
+
|
|
9
|
+
import { magentoCustomerOrderFragment } from './fragments/public_api';
|
|
10
|
+
|
|
11
|
+
export const MAGENTO_GET_CUSTOMER_ORDER_QUERY_NAME = 'MagentoGetCustomerOrder';
|
|
12
|
+
|
|
13
|
+
export const getCustomerOrder = (extraProductFragments: DocumentNode[] = []) => gql`
|
|
14
|
+
query ${MAGENTO_GET_CUSTOMER_ORDER_QUERY_NAME}($id: String!) {
|
|
15
|
+
customer {
|
|
16
|
+
email
|
|
17
|
+
orders(filter: {number: {eq: $id}}) {
|
|
18
|
+
items {
|
|
19
|
+
...magentoCustomerOrder
|
|
20
|
+
${daffBuildFragmentNameSpread(...extraProductFragments)}
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
${magentoCustomerOrderFragment}
|
|
26
|
+
${daffBuildFragmentDefinition(...extraProductFragments)}
|
|
27
|
+
`;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { gql } from 'apollo-angular';
|
|
2
|
+
import { DocumentNode } from 'graphql';
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
daffBuildFragmentNameSpread,
|
|
6
|
+
daffBuildFragmentDefinition,
|
|
7
|
+
} from '@daffodil/core/graphql';
|
|
8
|
+
import { magentoSearchResultPageInfoFragment } from '@daffodil/driver/magento';
|
|
9
|
+
|
|
10
|
+
import { magentoCustomerOrderFragment } from './fragments/public_api';
|
|
11
|
+
|
|
12
|
+
export const MAGENTO_GET_CUSTOMER_ORDERS_QUERY_NAME = 'MagentoGetCustomerOrders';
|
|
13
|
+
|
|
14
|
+
export const getCustomerOrders = (extraProductFragments: DocumentNode[] = []) => gql`
|
|
15
|
+
query ${MAGENTO_GET_CUSTOMER_ORDERS_QUERY_NAME}($currentPage: Int, $pageSize: Int) {
|
|
16
|
+
customer {
|
|
17
|
+
email
|
|
18
|
+
orders(currentPage: $currentPage, pageSize: $pageSize) {
|
|
19
|
+
items {
|
|
20
|
+
...magentoCustomerOrder
|
|
21
|
+
${daffBuildFragmentNameSpread(...extraProductFragments)}
|
|
22
|
+
}
|
|
23
|
+
page_info {
|
|
24
|
+
...magentoSearchResultPageInfo
|
|
25
|
+
}
|
|
26
|
+
total_count
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
${magentoCustomerOrderFragment}
|
|
31
|
+
${magentoSearchResultPageInfoFragment}
|
|
32
|
+
${daffBuildFragmentDefinition(...extraProductFragments)}
|
|
33
|
+
`;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './responses/public_api';
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Inject,
|
|
3
|
+
Injectable,
|
|
4
|
+
} from '@angular/core';
|
|
5
|
+
|
|
6
|
+
import {
|
|
7
|
+
daffIdentifiableArrayToDict,
|
|
8
|
+
DaffSortDirectionEnum,
|
|
9
|
+
} from '@daffodil/core';
|
|
10
|
+
import { magentoPageInfoTransform } from '@daffodil/driver/magento';
|
|
11
|
+
import { DaffOrderCollection } from '@daffodil/order';
|
|
12
|
+
|
|
13
|
+
import { DAFF_CUSTOMER_ORDER_MAGENTO_ORDER_TRANSFORM } from '../../injection-tokens/public_api';
|
|
14
|
+
import { DaffMagentoCustomerOrderTransform } from '../../interfaces/public_api';
|
|
15
|
+
import { MagentoGetCustomerOrdersResponse } from '../../models/public_api';
|
|
16
|
+
|
|
17
|
+
@Injectable({
|
|
18
|
+
providedIn: 'any',
|
|
19
|
+
})
|
|
20
|
+
export class MagentoCustomerOrderCollectionTransformer {
|
|
21
|
+
constructor(
|
|
22
|
+
@Inject(DAFF_CUSTOMER_ORDER_MAGENTO_ORDER_TRANSFORM) private orderTransform: DaffMagentoCustomerOrderTransform,
|
|
23
|
+
) {}
|
|
24
|
+
|
|
25
|
+
transform(response: MagentoGetCustomerOrdersResponse): DaffOrderCollection {
|
|
26
|
+
const orders = response.customer.orders.items.map((order) => this.orderTransform(order, response.customer.email));
|
|
27
|
+
return {
|
|
28
|
+
data: daffIdentifiableArrayToDict(orders),
|
|
29
|
+
metadata: {
|
|
30
|
+
...magentoPageInfoTransform(response.customer.orders.page_info),
|
|
31
|
+
count: response.customer.orders.total_count,
|
|
32
|
+
filters: {},
|
|
33
|
+
ids: orders.map(order => order.id),
|
|
34
|
+
sortOptions: {
|
|
35
|
+
default: null,
|
|
36
|
+
options: [],
|
|
37
|
+
},
|
|
38
|
+
appliedSortDirection: DaffSortDirectionEnum.Ascending,
|
|
39
|
+
appliedSortOption: null,
|
|
40
|
+
},
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { TestBed } from '@angular/core/testing';
|
|
2
|
+
|
|
3
|
+
import { MagentoCustomerOrder } from '@daffodil/customer-order/driver/magento';
|
|
4
|
+
import { MagentoCustomerOrderFactory } from '@daffodil/customer-order/driver/magento/testing';
|
|
5
|
+
import { DaffOrder } from '@daffodil/order';
|
|
6
|
+
|
|
7
|
+
import { daffMagentoCustomerOrderTransformOrder } from './order';
|
|
8
|
+
|
|
9
|
+
describe('@daffodil/customer-order/driver/magento | daffMagentoCustomerOrderTransformOrder', () => {
|
|
10
|
+
let orderFactory: MagentoCustomerOrderFactory;
|
|
11
|
+
|
|
12
|
+
let mockMagentoOrder: MagentoCustomerOrder;
|
|
13
|
+
|
|
14
|
+
beforeEach(() => {
|
|
15
|
+
orderFactory = TestBed.inject(MagentoCustomerOrderFactory);
|
|
16
|
+
|
|
17
|
+
mockMagentoOrder = orderFactory.create();
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
describe('daffMagentoCustomerOrderTransformOrder | transforming a magento order into a daff order', () => {
|
|
21
|
+
let transformedOrder: DaffOrder;
|
|
22
|
+
|
|
23
|
+
beforeEach(() => {
|
|
24
|
+
transformedOrder = daffMagentoCustomerOrderTransformOrder(mockMagentoOrder, 'email');
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it('should return an object with the correct values', () => {
|
|
28
|
+
expect(transformedOrder.id).toEqual(mockMagentoOrder.number);
|
|
29
|
+
});
|
|
30
|
+
});
|
|
31
|
+
});
|
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
import {
|
|
2
|
+
daffAdd,
|
|
3
|
+
daffSubtract,
|
|
4
|
+
} from '@daffodil/core';
|
|
5
|
+
import { MagentoDiscount } from '@daffodil/driver/magento';
|
|
6
|
+
import {
|
|
7
|
+
DaffOrder,
|
|
8
|
+
DaffOrderTotal,
|
|
9
|
+
DaffOrderItem,
|
|
10
|
+
DaffOrderAddress,
|
|
11
|
+
DaffOrderShipment,
|
|
12
|
+
DaffOrderShipmentItem,
|
|
13
|
+
DaffOrderShipmentTracking,
|
|
14
|
+
DaffOrderPayment,
|
|
15
|
+
DaffOrderInvoice,
|
|
16
|
+
DaffOrderTotalTypeEnum,
|
|
17
|
+
DaffCompositeOrderItemOption,
|
|
18
|
+
DaffOrderItemType,
|
|
19
|
+
DaffConfigurableOrderItemAttribute,
|
|
20
|
+
DaffOrderCoupon,
|
|
21
|
+
} from '@daffodil/order';
|
|
22
|
+
import {
|
|
23
|
+
MagentoOrderTotal,
|
|
24
|
+
MagentoOrderItemOption,
|
|
25
|
+
MagentoOrderBundleItemSelectedOption,
|
|
26
|
+
MagentoOrderItem,
|
|
27
|
+
MagentoOrderItemType,
|
|
28
|
+
MagentoOrderBundleItem,
|
|
29
|
+
MagentoOrderAddress,
|
|
30
|
+
MagentoOrderShipmentItem,
|
|
31
|
+
MagentoOrderShipmentTracking,
|
|
32
|
+
MagentoOrderShipment,
|
|
33
|
+
MagentoOrderPayment,
|
|
34
|
+
MagentoOrderInvoiceItem,
|
|
35
|
+
MagentoOrderInvoice,
|
|
36
|
+
MagentoOrderCreditItem,
|
|
37
|
+
MagentoOrderCredit,
|
|
38
|
+
} from '@daffodil/order/driver/magento/2.4.1';
|
|
39
|
+
|
|
40
|
+
import { MagentoCustomerOrder } from '../../models/public_api';
|
|
41
|
+
|
|
42
|
+
export function daffMagentoCustomerOrderTransformTotals(totals: MagentoOrderTotal): DaffOrderTotal[] {
|
|
43
|
+
return [
|
|
44
|
+
{
|
|
45
|
+
label: 'Subtotal',
|
|
46
|
+
type: DaffOrderTotalTypeEnum.Subtotal,
|
|
47
|
+
value: totals.subtotal.value,
|
|
48
|
+
sort_order: 0,
|
|
49
|
+
},
|
|
50
|
+
{
|
|
51
|
+
label: 'Discount',
|
|
52
|
+
type: DaffOrderTotalTypeEnum.Discount,
|
|
53
|
+
value: totals.discounts.reduce((acc, discount) => daffAdd(acc, discount.amount.value), 0),
|
|
54
|
+
sort_order: 10,
|
|
55
|
+
},
|
|
56
|
+
{
|
|
57
|
+
label: 'Shipping',
|
|
58
|
+
type: DaffOrderTotalTypeEnum.Shipping,
|
|
59
|
+
value: totals.total_shipping.value,
|
|
60
|
+
sort_order: 20,
|
|
61
|
+
},
|
|
62
|
+
{
|
|
63
|
+
label: 'Tax',
|
|
64
|
+
type: DaffOrderTotalTypeEnum.Tax,
|
|
65
|
+
value: totals.total_tax.value,
|
|
66
|
+
sort_order: 30,
|
|
67
|
+
},
|
|
68
|
+
{
|
|
69
|
+
label: 'Grand Total',
|
|
70
|
+
type: DaffOrderTotalTypeEnum.GrandTotal,
|
|
71
|
+
value: totals.grand_total.value,
|
|
72
|
+
sort_order: 40,
|
|
73
|
+
},
|
|
74
|
+
];
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export function daffMagentoCustomerOrderTransformCouponDiscount(discount: MagentoDiscount): DaffOrderCoupon {
|
|
78
|
+
return {
|
|
79
|
+
code: discount.label,
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export function daffMagentoCustomerOrderTransformConfigurableOption(option: MagentoOrderItemOption): DaffConfigurableOrderItemAttribute {
|
|
84
|
+
return {
|
|
85
|
+
attribute_label: option.label,
|
|
86
|
+
value_label: option.value,
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export function daffMagentoCustomerOrderTransformBundleOption(option: MagentoOrderBundleItemSelectedOption): DaffCompositeOrderItemOption {
|
|
91
|
+
return {
|
|
92
|
+
option_label: option.label,
|
|
93
|
+
value_label: option.values && option.values[0] && option.values[0].product_name,
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export function daffMagentoCustomerOrderTransformAdditionalItemFields(item: MagentoOrderItem) {
|
|
98
|
+
switch (item.product_type) {
|
|
99
|
+
case MagentoOrderItemType.Bundle:
|
|
100
|
+
return {
|
|
101
|
+
type: DaffOrderItemType.Composite,
|
|
102
|
+
options: (<MagentoOrderBundleItem>item).bundle_options.map(daffMagentoCustomerOrderTransformBundleOption),
|
|
103
|
+
};
|
|
104
|
+
case MagentoOrderItemType.Configurable:
|
|
105
|
+
return {
|
|
106
|
+
type: DaffOrderItemType.Configurable,
|
|
107
|
+
attributes: item.selected_options.map(daffMagentoCustomerOrderTransformConfigurableOption),
|
|
108
|
+
};
|
|
109
|
+
case MagentoOrderItemType.Simple:
|
|
110
|
+
return {
|
|
111
|
+
type: DaffOrderItemType.Simple,
|
|
112
|
+
};
|
|
113
|
+
default:
|
|
114
|
+
return {};
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export function daffMagentoCustomerOrderTransformItem(item: MagentoOrderItem, order: MagentoCustomerOrder, qty: number): DaffOrderItem {
|
|
119
|
+
const discount = item.discounts.reduce((acc, d) => daffAdd(acc, d.amount.value), 0);
|
|
120
|
+
const rowTotal = qty * item.product_sale_price.value;
|
|
121
|
+
const rowTotalWithDiscount = qty * daffSubtract(item.product_sale_price.value, discount);
|
|
122
|
+
|
|
123
|
+
return {
|
|
124
|
+
type: DaffOrderItemType.Simple,
|
|
125
|
+
id: null,
|
|
126
|
+
item_id: null,
|
|
127
|
+
qty_ordered: item.quantity_ordered,
|
|
128
|
+
qty_canceled: item.quantity_canceled,
|
|
129
|
+
qty_fulfilled: item.quantity_shipped,
|
|
130
|
+
qty,
|
|
131
|
+
image: {
|
|
132
|
+
url: item.product_url_key,
|
|
133
|
+
id: null,
|
|
134
|
+
label: null,
|
|
135
|
+
},
|
|
136
|
+
order_id: order.id,
|
|
137
|
+
created_at: null,
|
|
138
|
+
updated_at: null,
|
|
139
|
+
product_id: null,
|
|
140
|
+
parent_item_id: null,
|
|
141
|
+
sku: item.product_sku,
|
|
142
|
+
name: item.product_name,
|
|
143
|
+
weight: null,
|
|
144
|
+
price: item.product_sale_price.value,
|
|
145
|
+
discount_percent: Math.floor(discount / item.product_sale_price.value * 100),
|
|
146
|
+
discount_amount: discount,
|
|
147
|
+
tax_percent: null,
|
|
148
|
+
tax_amount: null,
|
|
149
|
+
row_total: rowTotal,
|
|
150
|
+
row_total_with_discount: rowTotalWithDiscount,
|
|
151
|
+
row_weight: null,
|
|
152
|
+
tax_before_discount: null,
|
|
153
|
+
...daffMagentoCustomerOrderTransformAdditionalItemFields(item),
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
export function daffMagentoCustomerOrderTransformAddress(address: MagentoOrderAddress, order: MagentoCustomerOrder): DaffOrderAddress {
|
|
158
|
+
return {
|
|
159
|
+
order_id: order.id,
|
|
160
|
+
prefix: address.prefix,
|
|
161
|
+
suffix: address.suffix,
|
|
162
|
+
firstname: address.firstname,
|
|
163
|
+
middlename: address.middlename,
|
|
164
|
+
lastname: address.lastname,
|
|
165
|
+
telephone: address.telephone,
|
|
166
|
+
street: address.street[0],
|
|
167
|
+
street2: address.street[1],
|
|
168
|
+
city: address.city,
|
|
169
|
+
region: String(address.region_id),
|
|
170
|
+
region_code: address.region_code,
|
|
171
|
+
country: address.country_code,
|
|
172
|
+
postcode: address.postcode,
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
export function daffMagentoCustomerOrderTransformShipmentItem(shipmentItem: MagentoOrderShipmentItem, order: MagentoCustomerOrder): DaffOrderShipmentItem {
|
|
177
|
+
return {
|
|
178
|
+
item: daffMagentoCustomerOrderTransformItem(shipmentItem.order_item, order, shipmentItem.quantity_shipped),
|
|
179
|
+
qty: shipmentItem.quantity_shipped,
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
export function daffMagentoCustomerOrderTransformShipmentTracking(tracking: MagentoOrderShipmentTracking): DaffOrderShipmentTracking {
|
|
184
|
+
return {
|
|
185
|
+
tracking_number: tracking.number,
|
|
186
|
+
tracking_url: null,
|
|
187
|
+
carrier: tracking.carrier,
|
|
188
|
+
title: tracking.title,
|
|
189
|
+
carrier_logo: null,
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
export function daffMagentoCustomerOrderTransformShipment(shipment: MagentoOrderShipment, order: MagentoCustomerOrder): DaffOrderShipment {
|
|
194
|
+
return {
|
|
195
|
+
carrier: order.carrier,
|
|
196
|
+
carrier_title: null,
|
|
197
|
+
code: null,
|
|
198
|
+
method: order.shipping_method,
|
|
199
|
+
method_description: null,
|
|
200
|
+
tracking: shipment.tracking.map(daffMagentoCustomerOrderTransformShipmentTracking),
|
|
201
|
+
items: shipment.items.map(item => daffMagentoCustomerOrderTransformShipmentItem(item, order)),
|
|
202
|
+
};
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
export function daffMagentoCustomerOrderTransformPayment(payment: MagentoOrderPayment, order: MagentoCustomerOrder): DaffOrderPayment {
|
|
206
|
+
const findAdditionalData = key => {
|
|
207
|
+
const index = payment.additional_data.findIndex(({ name }) => name === key);
|
|
208
|
+
|
|
209
|
+
return index > -1 ? payment.additional_data[index].value : null;
|
|
210
|
+
};
|
|
211
|
+
return {
|
|
212
|
+
id: null,
|
|
213
|
+
payment_id: null,
|
|
214
|
+
order_id: order.id,
|
|
215
|
+
created_at: null,
|
|
216
|
+
updated_at: null,
|
|
217
|
+
method: payment.type,
|
|
218
|
+
cc_type: findAdditionalData('cc_type'),
|
|
219
|
+
cc_last4: findAdditionalData('cc_last4'),
|
|
220
|
+
cc_owner: findAdditionalData('cc_owner'),
|
|
221
|
+
cc_exp_month: findAdditionalData('cc_exp_month'),
|
|
222
|
+
cc_exp_year: findAdditionalData('cc_exp_year'),
|
|
223
|
+
};
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
export function daffMagentoCustomerOrderTransformInvoiceItem(invoiceItem: MagentoOrderInvoiceItem, order: MagentoCustomerOrder): DaffOrderShipmentItem {
|
|
227
|
+
return {
|
|
228
|
+
item: daffMagentoCustomerOrderTransformItem(invoiceItem.order_item, order, invoiceItem.quantity_invoiced),
|
|
229
|
+
qty: invoiceItem.quantity_invoiced,
|
|
230
|
+
};
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
export function daffMagentoCustomerOrderTransformInvoice(invoice: MagentoOrderInvoice, order: MagentoCustomerOrder, payment: MagentoOrderPayment): DaffOrderInvoice {
|
|
234
|
+
return {
|
|
235
|
+
totals: daffMagentoCustomerOrderTransformTotals(invoice.total),
|
|
236
|
+
billing_address: daffMagentoCustomerOrderTransformAddress(order.billing_address, order),
|
|
237
|
+
shipping_address: daffMagentoCustomerOrderTransformAddress(order.shipping_address, order),
|
|
238
|
+
payment: daffMagentoCustomerOrderTransformPayment(payment, order),
|
|
239
|
+
items: invoice.items.map(item => daffMagentoCustomerOrderTransformInvoiceItem(item, order)),
|
|
240
|
+
shipping_method: null,
|
|
241
|
+
};
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
export function daffMagentoCustomerOrderTransformCreditItem(creditItem: MagentoOrderCreditItem, order: MagentoCustomerOrder): DaffOrderShipmentItem {
|
|
245
|
+
return {
|
|
246
|
+
item: daffMagentoCustomerOrderTransformItem(creditItem.order_item, order, creditItem.quantity_refunded),
|
|
247
|
+
qty: creditItem.quantity_refunded,
|
|
248
|
+
};
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
export function daffMagentoCustomerOrderTransformCredit(credit: MagentoOrderCredit, order: MagentoCustomerOrder): DaffOrderInvoice {
|
|
252
|
+
return {
|
|
253
|
+
totals: daffMagentoCustomerOrderTransformTotals(credit.total),
|
|
254
|
+
billing_address: daffMagentoCustomerOrderTransformAddress(order.billing_address, order),
|
|
255
|
+
shipping_address: daffMagentoCustomerOrderTransformAddress(order.shipping_address, order),
|
|
256
|
+
payment: daffMagentoCustomerOrderTransformPayment(order.payment_methods[0], order),
|
|
257
|
+
items: credit.items.map(item => daffMagentoCustomerOrderTransformCreditItem(item, order)),
|
|
258
|
+
shipping_method: null,
|
|
259
|
+
};
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
/**
|
|
263
|
+
* Transforms the MagentoCustomerOrder from the magento order query into a DaffOrder.
|
|
264
|
+
*/
|
|
265
|
+
export function daffMagentoCustomerOrderTransformOrder(order: MagentoCustomerOrder, email: string): DaffOrder {
|
|
266
|
+
return {
|
|
267
|
+
extra_attributes: order,
|
|
268
|
+
|
|
269
|
+
id: order.number,
|
|
270
|
+
email,
|
|
271
|
+
customer_id: null,
|
|
272
|
+
updated_at: null,
|
|
273
|
+
created_at: order.order_date,
|
|
274
|
+
status: order.status,
|
|
275
|
+
|
|
276
|
+
totals: daffMagentoCustomerOrderTransformTotals(order.total),
|
|
277
|
+
applied_codes: order.total.discounts.map(daffMagentoCustomerOrderTransformCouponDiscount),
|
|
278
|
+
items: order.items.map(item => daffMagentoCustomerOrderTransformItem(item, order, item.quantity_ordered)),
|
|
279
|
+
billing_addresses: [
|
|
280
|
+
daffMagentoCustomerOrderTransformAddress(order.billing_address, order),
|
|
281
|
+
],
|
|
282
|
+
shipping_addresses: [
|
|
283
|
+
daffMagentoCustomerOrderTransformAddress(order.shipping_address, order),
|
|
284
|
+
],
|
|
285
|
+
shipments: order.shipments.map(shipment => daffMagentoCustomerOrderTransformShipment(shipment, order)),
|
|
286
|
+
payment: daffMagentoCustomerOrderTransformPayment(order.payment_methods[0], order),
|
|
287
|
+
// TODO: find out if the index is the correct payment for invoice
|
|
288
|
+
invoices: order.invoices.map((invoice, index) => daffMagentoCustomerOrderTransformInvoice(invoice, order, order.payment_methods[index])),
|
|
289
|
+
credits: order.credit_memos.map(credit => daffMagentoCustomerOrderTransformCredit(credit, order)),
|
|
290
|
+
};
|
|
291
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { TestBed } from '@angular/core/testing';
|
|
2
|
+
import { ApolloQueryResult } from '@apollo/client/core';
|
|
3
|
+
|
|
4
|
+
import { DaffCustomerInvalidAPIResponseError } from '@daffodil/customer-order/driver';
|
|
5
|
+
import { MagentoGetCustomerOrderResponse } from '@daffodil/customer-order/driver/magento';
|
|
6
|
+
import { MagentoCustomerOrderFactory } from '@daffodil/customer-order/driver/magento/testing';
|
|
7
|
+
|
|
8
|
+
import { validateGetCustomerOrderResponse as validator } from './get-order';
|
|
9
|
+
|
|
10
|
+
describe('@daffodil/customer-order/driver/magento | validateGetCustomerOrderResponse', () => {
|
|
11
|
+
let response: ApolloQueryResult<MagentoGetCustomerOrderResponse>;
|
|
12
|
+
let orderFactory: MagentoCustomerOrderFactory;
|
|
13
|
+
|
|
14
|
+
beforeEach(() => {
|
|
15
|
+
orderFactory = TestBed.inject(MagentoCustomerOrderFactory);
|
|
16
|
+
|
|
17
|
+
response = {
|
|
18
|
+
data: {
|
|
19
|
+
customer: {
|
|
20
|
+
email: 'email',
|
|
21
|
+
orders: {
|
|
22
|
+
items: [orderFactory.create()],
|
|
23
|
+
},
|
|
24
|
+
},
|
|
25
|
+
},
|
|
26
|
+
loading: null,
|
|
27
|
+
networkStatus: null,
|
|
28
|
+
};
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
describe('and when the response has a customer', () => {
|
|
32
|
+
it('should return the response and not throw an error', () => {
|
|
33
|
+
const result = validator(response);
|
|
34
|
+
|
|
35
|
+
expect(result).toEqual(response);
|
|
36
|
+
});
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
describe('and when the response does not have customer', () => {
|
|
40
|
+
beforeEach(() => {
|
|
41
|
+
response.data.customer = null;
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it('should throw a DaffCustomerInvalidAPIResponseError', () => {
|
|
45
|
+
expect(() => validator(response)).toThrow(jasmine.any(DaffCustomerInvalidAPIResponseError));
|
|
46
|
+
});
|
|
47
|
+
});
|
|
48
|
+
});
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { ApolloQueryResult } from '@apollo/client/core';
|
|
2
|
+
|
|
3
|
+
import { DaffCustomerInvalidAPIResponseError } from '@daffodil/customer-order/driver';
|
|
4
|
+
|
|
5
|
+
import { MagentoGetCustomerOrderResponse } from '../models/public_api';
|
|
6
|
+
|
|
7
|
+
export const validateGetCustomerOrderResponse = (response: ApolloQueryResult<MagentoGetCustomerOrderResponse>) => {
|
|
8
|
+
if (response.data.customer?.orders.items[0]) {
|
|
9
|
+
return response;
|
|
10
|
+
} else {
|
|
11
|
+
throw new DaffCustomerInvalidAPIResponseError('Get customer order response does not contain a customer order.');
|
|
12
|
+
}
|
|
13
|
+
};
|