Skip to content

Commit 7d11b65

Browse files
committed
[Components] Mews
1 parent 5ce46c3 commit 7d11b65

File tree

18 files changed

+757
-12
lines changed

18 files changed

+757
-12
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import app from "../../mews.app.mjs";
2+
3+
export default {
4+
name: "Cancel Reservation",
5+
description: "Cancel a reservation in Mews.",
6+
key: "mews-cancel-reservation",
7+
version: "0.0.1",
8+
type: "action",
9+
props: {
10+
app,
11+
reservationIds: {
12+
type: "string[]",
13+
label: "Reservation IDs",
14+
description: "IDs of reservations to cancel",
15+
},
16+
},
17+
async run({ $ }) {
18+
const {
19+
app: mews, reservationIds,
20+
} = this;
21+
const data = {
22+
ReservationIds: reservationIds,
23+
};
24+
const response = await mews.reservationsCancel({
25+
data,
26+
$,
27+
});
28+
$.export("summary", `Cancelled ${reservationIds.length} reservation(s)`);
29+
return response;
30+
},
31+
};
32+
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import app from "../../mews.app.mjs";
2+
3+
export default {
4+
name: "Create Reservation",
5+
description: "Create a reservation in Mews.",
6+
key: "mews-create-reservation",
7+
version: "0.0.1",
8+
type: "action",
9+
props: {
10+
app,
11+
reservations: {
12+
type: "string[]",
13+
label: "Reservations (JSON)",
14+
description: "Array of reservation objects as JSON strings, each per Mews API schema.",
15+
},
16+
},
17+
async run({ $ }) {
18+
const { app: mews } = this;
19+
const reservations = (this.reservations || []).map((r) => {
20+
try { return JSON.parse(r); } catch { return r; }
21+
});
22+
const data = {
23+
Reservations: reservations,
24+
};
25+
const response = await mews.reservationsCreate({
26+
data,
27+
$,
28+
});
29+
$.export("summary", `Created ${reservations.length} reservation(s)`);
30+
return response;
31+
},
32+
};
33+
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import app from "../../mews.app.mjs";
2+
import utils from "../../common/uilts.mjs";
3+
4+
export default {
5+
name: "Fetch Customers",
6+
description: "Retrieve customers using Mews Connector API. [See the documentation](https://mews-systems.gitbook.io/connector-api/operations/customers#get-all-customers)",
7+
key: "mews-fetch-customers",
8+
version: "0.0.1",
9+
type: "action",
10+
props: {
11+
app,
12+
additionalFields: {
13+
propDefinition: [
14+
app,
15+
"additionalFields",
16+
],
17+
},
18+
},
19+
async run({ $ }) {
20+
const {
21+
app,
22+
additionalFields,
23+
} = this;
24+
25+
const items = await app.paginate({
26+
requester: app.customersGetAll,
27+
requesterArgs: {
28+
$,
29+
data: utils.parseJson(additionalFields),
30+
},
31+
resultKey: "Customers",
32+
});
33+
$.export("summary", `Successfully fetched ${items.length} customers`);
34+
return items;
35+
},
36+
};
37+
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import app from "../../mews.app.mjs";
2+
import utils from "../../common/uilts.mjs";
3+
4+
export default {
5+
name: "Fetch Order Items",
6+
description: "Retrieve order items using Mews Connector API. [See the documentation](https://mews-systems.gitbook.io/connector-api/operations/orderitems#get-all-order-items)",
7+
key: "mews-fetch-order-items",
8+
version: "0.0.1",
9+
type: "action",
10+
props: {
11+
app,
12+
additionalFields: {
13+
propDefinition: [
14+
app,
15+
"additionalFields",
16+
],
17+
},
18+
},
19+
async run({ $ }) {
20+
const {
21+
app,
22+
additionalFields,
23+
} = this;
24+
25+
const items = await app.paginate({
26+
requester: app.orderItemsGetAll,
27+
requesterArgs: {
28+
$,
29+
data: utils.parseJson(additionalFields),
30+
},
31+
resultKey: "OrderItems",
32+
});
33+
$.export("summary", `Successfully fetched ${items.length} order items`);
34+
return items;
35+
},
36+
};
37+
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import app from "../../mews.app.mjs";
2+
import utils from "../../common/uilts.mjs";
3+
4+
export default {
5+
name: "Fetch Products",
6+
description: "Retrieve products using Mews Connector API. [See the documentation](https://mews-systems.gitbook.io/connector-api/operations/products#get-all-products)",
7+
key: "mews-fetch-products",
8+
version: "0.0.1",
9+
type: "action",
10+
props: {
11+
app,
12+
additionalFields: {
13+
propDefinition: [
14+
app,
15+
"additionalFields",
16+
],
17+
},
18+
},
19+
async run({ $ }) {
20+
const {
21+
app,
22+
additionalFields,
23+
} = this;
24+
25+
const items = await app.paginate({
26+
requester: app.productsGetAll,
27+
requesterArgs: {
28+
$,
29+
data: utils.parseJson(additionalFields),
30+
},
31+
resultKey: "Products",
32+
});
33+
$.export("summary", `Successfully fetched ${items.length} products`);
34+
return items;
35+
},
36+
};
37+
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import app from "../../mews.app.mjs";
2+
import utils from "../../common/uilts.mjs";
3+
4+
export default {
5+
name: "Fetch Reservations",
6+
description: "Retrieve reservations using Mews Connector API. [See the documentation](https://mews-systems.gitbook.io/connector-api/operations/reservations#get-all-reservations-ver-2023-06-06)",
7+
key: "mews-fetch-reservations",
8+
version: "0.0.1",
9+
type: "action",
10+
props: {
11+
app,
12+
additionalFields: {
13+
propDefinition: [
14+
app,
15+
"additionalFields",
16+
],
17+
},
18+
},
19+
async run({ $ }) {
20+
const {
21+
app,
22+
additionalFields,
23+
} = this;
24+
25+
const items = await app.paginate({
26+
requester: app.reservationsGetAll,
27+
requesterArgs: {
28+
$,
29+
data: utils.parseJson(additionalFields),
30+
},
31+
resultKey: "Reservations",
32+
});
33+
34+
$.export("summary", `Successfully fetched ${items.length} reservations`);
35+
return items;
36+
},
37+
};
38+
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import app from "../../mews.app.mjs";
2+
3+
export default {
4+
name: "Update Reservation",
5+
description: "Update an existing reservation in Mews.",
6+
key: "mews-update-reservation",
7+
version: "0.0.1",
8+
type: "action",
9+
props: {
10+
app,
11+
reservations: {
12+
type: "string[]",
13+
label: "Reservations (JSON)",
14+
description: "Array of reservation objects with Id and fields to update, as JSON strings.",
15+
},
16+
},
17+
async run({ $ }) {
18+
const { app: mews } = this;
19+
const reservations = (this.reservations || []).map((r) => {
20+
try { return JSON.parse(r); } catch { return r; }
21+
});
22+
const data = {
23+
Reservations: reservations,
24+
};
25+
const response = await mews.reservationsUpdate({
26+
data,
27+
$,
28+
});
29+
$.export("summary", `Updated ${reservations.length} reservation(s)`);
30+
return response;
31+
},
32+
};
33+

components/mews/common/uilts.mjs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
const parseJson = (input, maxDepth = 100) => {
2+
const seen = new WeakSet();
3+
const parse = (value) => {
4+
if (maxDepth <= 0) {
5+
return value;
6+
}
7+
if (typeof(value) === "string") {
8+
// Only parse if the string looks like a JSON object or array
9+
const trimmed = value.trim();
10+
if (
11+
(trimmed.startsWith("{") && trimmed.endsWith("}")) ||
12+
(trimmed.startsWith("[") && trimmed.endsWith("]"))
13+
) {
14+
try {
15+
return parseJson(JSON.parse(value), maxDepth - 1);
16+
} catch (e) {
17+
return value;
18+
}
19+
}
20+
return value;
21+
} else if (typeof(value) === "object" && value !== null && !Array.isArray(value)) {
22+
if (seen.has(value)) {
23+
return value;
24+
}
25+
seen.add(value);
26+
return Object.entries(value)
27+
.reduce((acc, [
28+
key,
29+
val,
30+
]) => Object.assign(acc, {
31+
[key]: parse(val),
32+
}), {});
33+
} else if (Array.isArray(value)) {
34+
return value.map((item) => parse(item));
35+
}
36+
return value;
37+
};
38+
39+
return parse(input);
40+
};
41+
42+
export default {
43+
parseJson,
44+
};

0 commit comments

Comments
 (0)