@pinelab/vendure-plugin-sendcloud 1.2.0 → 1.2.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.
package/CHANGELOG.md CHANGED
@@ -1,3 +1,11 @@
1
+ # 1.2.2 (2024-06-13)
2
+
3
+ - Added better logging for failing incoming webhooks
4
+
5
+ # 1.2.1 (2023-01-16)
6
+
7
+ - Correctly transition order to Delivered after it has been Shipped
8
+
1
9
  # 1.2.0 (2023-11-16)
2
10
 
3
11
  - Fulfill on order placement, to prevent stock levels going out of sync
package/README.md CHANGED
@@ -1,8 +1,6 @@
1
1
  # Vendure SendCloud plugin
2
2
 
3
- ![Vendure version](https://img.shields.io/badge/dynamic/json.svg?url=https%3A%2F%2Fraw.githubusercontent.com%2FPinelab-studio%2Fpinelab-vendure-plugins%2Fmain%2Fpackage.json&query=$.devDependencies[%27@vendure/core%27]&colorB=blue&label=Built%20on%20Vendure)
4
-
5
- ### Visit [pinelab-plugins.com](https://pinelab-plugins.com/plugin/vendure-plugin-sendcloud) for more documentation and examples.
3
+ ### [Official documentation here](https://pinelab-plugins.com/plugin/vendure-plugin-sendcloud)
6
4
 
7
5
  This plugin syncs orders to the SendCloud fulfillment platform.
8
6
 
@@ -8,12 +8,14 @@ export declare function fulfillAll(ctx: RequestContext, orderService: OrderServi
8
8
  /**
9
9
  * Fulfills all items to shipped using transitionFulfillmentToState
10
10
  */
11
- export declare function transitionToShipped(orderService: OrderService, ctx: RequestContext, order: Order, handler: ConfigurableOperationInput): Promise<Fulfillment>;
11
+ export declare function transitionToShipped(orderService: OrderService, ctx: RequestContext, order: Order, handler: ConfigurableOperationInput): Promise<Fulfillment | FulfillmentStateTransitionError>;
12
12
  /**
13
13
  * Fulfills all items to shipped, then to delivered using transitionFulfillmentToState
14
14
  */
15
- export declare function transitionToDelivered(orderService: OrderService, ctx: RequestContext, order: Order, handler: ConfigurableOperationInput): Promise<Fulfillment>;
15
+ export declare function transitionToDelivered(orderService: OrderService, ctx: RequestContext, order: Order, handler: ConfigurableOperationInput): Promise<(Fulfillment | FulfillmentStateTransitionError)[]>;
16
16
  /**
17
17
  * Throws the error result if the transition failed
18
+ * Ignores transition errors where from and to state are the same,
19
+ * because that still results in the situation we want
18
20
  */
19
21
  export declare function throwIfTransitionFailed(result: FulfillmentStateTransitionError | Fulfillment | AddFulfillmentToOrderResult): void;
@@ -36,14 +36,28 @@ exports.transitionToShipped = transitionToShipped;
36
36
  * Fulfills all items to shipped, then to delivered using transitionFulfillmentToState
37
37
  */
38
38
  async function transitionToDelivered(orderService, ctx, order, handler) {
39
- const fulfillment = await transitionToShipped(orderService, ctx, order, handler);
40
- const result = await orderService.transitionFulfillmentToState(ctx, fulfillment.id, 'Delivered');
41
- throwIfTransitionFailed(result);
42
- return result;
39
+ const shippedResult = await transitionToShipped(orderService, ctx, order, handler);
40
+ let fulfillments = [];
41
+ if (shippedResult.errorCode) {
42
+ fulfillments = await orderService.getOrderFulfillments(ctx, order);
43
+ }
44
+ else {
45
+ // if not an error, shippedResult is the only fulfillment
46
+ fulfillments.push(shippedResult);
47
+ }
48
+ const results = [];
49
+ for (const fulfillment of fulfillments) {
50
+ const result = await orderService.transitionFulfillmentToState(ctx, fulfillment.id, 'Delivered');
51
+ throwIfTransitionFailed(result);
52
+ results.push(result);
53
+ }
54
+ return results;
43
55
  }
44
56
  exports.transitionToDelivered = transitionToDelivered;
45
57
  /**
46
58
  * Throws the error result if the transition failed
59
+ * Ignores transition errors where from and to state are the same,
60
+ * because that still results in the situation we want
47
61
  */
48
62
  function throwIfTransitionFailed(result) {
49
63
  const stateError = result;
@@ -19,12 +19,20 @@ const sendcloud_client_1 = require("./sendcloud.client");
19
19
  const core_1 = require("@vendure/core");
20
20
  const constants_1 = require("./constants");
21
21
  const sendcloud_types_1 = require("./types/sendcloud.types");
22
+ const util_1 = require("util");
22
23
  let SendcloudController = class SendcloudController {
23
24
  constructor(sendcloudService) {
24
25
  this.sendcloudService = sendcloudService;
25
26
  }
26
27
  async webhook(req, signature, channelToken) {
27
- const body = JSON.parse(req.body.toString());
28
+ let body;
29
+ try {
30
+ body = JSON.parse(req.body.toString());
31
+ }
32
+ catch (e) {
33
+ core_1.Logger.error(`Error parsing incoming webhook body: ${e?.message ?? e}`, constants_1.loggerCtx, (0, util_1.inspect)(req.body));
34
+ return;
35
+ }
28
36
  const rawBody = req.rawBody;
29
37
  const ctx = await this.sendcloudService.createContext(channelToken);
30
38
  const { client } = await this.sendcloudService.getClient(ctx);
@@ -110,7 +110,7 @@ let SendcloudService = class SendcloudService {
110
110
  }
111
111
  if (sendcloudStatus.orderState === 'Shipped') {
112
112
  await this.shipAll(ctx, order);
113
- return core_2.Logger.info(`Successfully update order ${orderCode} to ${sendcloudStatus.orderState}`, constants_1.loggerCtx);
113
+ return core_2.Logger.info(`Successfully updated order ${orderCode} to Shipped`, constants_1.loggerCtx);
114
114
  }
115
115
  order = await this.connection
116
116
  .getRepository(ctx, core_2.Order)
@@ -120,7 +120,7 @@ let SendcloudService = class SendcloudService {
120
120
  code: sendcloud_handler_1.sendcloudHandler.code,
121
121
  arguments: [],
122
122
  });
123
- return core_2.Logger.info(`Successfully update order ${orderCode} to ${sendcloudStatus.orderState}`, constants_1.loggerCtx);
123
+ return core_2.Logger.info(`Successfully updated order ${orderCode} to Delivered`, constants_1.loggerCtx);
124
124
  }
125
125
  // Fall through, means unhandled state
126
126
  core_2.Logger.info(`Not handling state ${sendcloudStatus.orderState}`, constants_1.loggerCtx);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pinelab/vendure-plugin-sendcloud",
3
- "version": "1.2.0",
3
+ "version": "1.2.2",
4
4
  "description": "Vendure plugin for syncing orders with SendCloud",
5
5
  "author": "Martijn van de Brug <martijn@pinelab.studio>",
6
6
  "homepage": "https://pinelab-plugins.com/",
@@ -20,7 +20,8 @@
20
20
  "scripts": {
21
21
  "build": "rimraf dist && tsc && copyfiles -u 1 'src/ui/**/*' dist/vendure-plugin-sendcloud/src/",
22
22
  "test": "vitest run",
23
- "start": "yarn ts-node test/dev-server.ts"
23
+ "start": "yarn ts-node test/dev-server.ts",
24
+ "lint": "echo 'No linting configured'"
24
25
  },
25
- "gitHead": "51491de3e0babe4f579d880f935501c2b4d6f7a1"
26
+ "gitHead": "7531c167693e3a991ef910c6d398658547d313bb"
26
27
  }